diff --git a/common/arg.cpp b/common/arg.cpp index 67b492e11..51f226e96 100644 --- a/common/arg.cpp +++ b/common/arg.cpp @@ -4,6 +4,7 @@ #include "chat.h" #include "common.h" #include "download.h" +#include "http.h" #include "json-schema-to-grammar.h" #include "json.h" #include "llama.h" @@ -399,7 +400,26 @@ bool common_models_handler_is_preset_repo(const common_models_handler & handler) return !handler.plan.preset.url.empty(); } +static common_download_opts download_opts_for_url(const common_download_opts & opts, const std::string & url) { + common_download_opts result = opts; + try { + const auto hf = common_http_parse_url(common_get_model_endpoint()); + const auto parts = common_http_parse_url(url); + + if (parts.scheme == hf.scheme && + parts.host == hf.host && + parts.port == hf.port && + string_starts_with(result.bearer_token, "hf_")) { + return result; + } + } catch (const std::exception &) { + } + result.bearer_token.clear(); + return result; +} + static std::vector build_url_tasks(const common_params_model & model, common_download_opts opts) { + opts = download_opts_for_url(opts, model.url); auto parts = common_download_get_all_parts(model.url); std::vector tasks; @@ -488,7 +508,7 @@ void common_models_handler_apply(common_models_handler & handler, common_params common_download_task task; task.url = params.mmproj.url; task.local_path = params.mmproj.path; - task.opts = opts; + task.opts = download_opts_for_url(opts, task.url); tasks.push_back(task); } bool had_spec_url = false; @@ -496,7 +516,7 @@ void common_models_handler_apply(common_models_handler & handler, common_params common_download_task task; task.url = params.speculative.draft.mparams.url; task.local_path = params.speculative.draft.mparams.path; - task.opts = opts; + task.opts = download_opts_for_url(opts, task.url); tasks.push_back(task); had_spec_url = true; }