@@ -3909,9 +3909,57 @@ static bool llm_load_tensors(
3909
3909
return true ;
3910
3910
}
3911
3911
3912
+ // check if the URL is a HuggingFace model, and if so, try to download it
3913
+ static void hf_try_download_model (std::string & url) {
3914
+ bool is_url = false ;
3915
+
3916
+ if (url.size () > 22 ) {
3917
+ is_url = (url.compare (0 , 22 , " https://huggingface.co" ) == 0 );
3918
+ }
3919
+
3920
+ if (!is_url) {
3921
+ return ;
3922
+ }
3923
+
3924
+ // Examples:
3925
+ //
3926
+ // https://huggingface.co/TheBloke/Mixtral-8x7B-Instruct-v0.1-GGUF/resolve/main/mixtral-8x7b-instruct-v0.1.Q2_K.gguf
3927
+
3928
+ std::string basename;
3929
+ basename = url.substr (url.find_last_of (" /\\ " ) + 1 );
3930
+
3931
+ LLAMA_LOG_INFO (" %s: detected URL, attempting to download %s\n " , __func__, basename.c_str ());
3932
+
3933
+ {
3934
+ const std::string cmd = " wget -q --show-progress -c -O " + basename + " " + url;
3935
+ LLAMA_LOG_INFO (" %s: %s\n " , __func__, cmd.c_str ());
3936
+
3937
+ const int ret = system (cmd.c_str ());
3938
+ if (ret == 0 ) {
3939
+ url = basename;
3940
+ return ;
3941
+ }
3942
+ }
3943
+
3944
+ {
3945
+ const std::string cmd = " curl -C - -f -o " + basename + " -L " + url;
3946
+ LLAMA_LOG_INFO (" %s: %s\n " , __func__, cmd.c_str ());
3947
+
3948
+ const int ret = system (cmd.c_str ());
3949
+ if (ret == 0 ) {
3950
+ url = basename;
3951
+ return ;
3952
+ }
3953
+ }
3954
+
3955
+ LLAMA_LOG_WARN (" %s: failed to download\n " , __func__);
3956
+ }
3957
+
3912
3958
// Returns 0 on success, -1 on error, and -2 on cancellation via llama_progress_callback
3913
- static int llama_model_load (const std::string & fname, llama_model & model, const llama_model_params & params) {
3959
+ static int llama_model_load (std::string fname, llama_model & model, const llama_model_params & params) {
3914
3960
try {
3961
+ hf_try_download_model (fname);
3962
+
3915
3963
llama_model_loader ml (fname, params.use_mmap , params.kv_overrides );
3916
3964
3917
3965
model.hparams .vocab_only = params.vocab_only ;
0 commit comments