diff --git a/common/common.cpp b/common/common.cpp index cdcb352b5..7a21081c1 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -191,6 +191,15 @@ int32_t cpu_get_num_math() { // CLI argument parsing // +static std::string get_cached_file(const std::string & path) { + std::string cache_directory = fs_get_cache_directory(); + const bool success = fs_create_directory_with_parents(cache_directory); + if (!success) { + throw std::runtime_error("failed to create cache directory: " + cache_directory); + } + return cache_directory + string_split(path, '/').back(); +} + void gpt_params_handle_model_default(gpt_params & params) { if (!params.hf_repo.empty()) { // short-hand to avoid specifying --hf-file -> default it to --model @@ -200,19 +209,13 @@ void gpt_params_handle_model_default(gpt_params & params) { } params.hf_file = params.model; } else if (params.model.empty()) { - std::string cache_directory = fs_get_cache_directory(); - const bool success = fs_create_directory_with_parents(cache_directory); - if (!success) { - throw std::runtime_error("failed to create cache directory: " + cache_directory); - } - params.model = cache_directory + string_split(params.hf_file, '/').back(); + params.model = get_cached_file(params.hf_file); } } else if (!params.model_url.empty()) { if (params.model.empty()) { auto f = string_split(params.model_url, '#').front(); f = string_split(f, '?').front(); - f = string_split(f, '/').back(); - params.model = "models/" + f; + params.model = get_cached_file(f); } } else if (params.model.empty()) { params.model = DEFAULT_MODEL_PATH;