From cbead31e7b87222556b549ddd3f860860b24da51 Mon Sep 17 00:00:00 2001 From: Thien Tran Date: Mon, 17 Mar 2025 16:08:35 +0800 Subject: [PATCH] delete unused old download functions --- engine/services/model_service.cc | 231 ------------------------------- engine/services/model_service.h | 21 +-- 2 files changed, 1 insertion(+), 251 deletions(-) diff --git a/engine/services/model_service.cc b/engine/services/model_service.cc index 3129362ce..035b0446f 100644 --- a/engine/services/model_service.cc +++ b/engine/services/model_service.cc @@ -197,54 +197,6 @@ void ModelService::ForceIndexingModelList() { } } -cpp::result ModelService::HandleCortexsoModel( - const std::string& modelName) { - auto branches = - huggingface_utils::GetModelRepositoryBranches("cortexso", modelName); - if (branches.has_error()) { - return cpp::fail(branches.error()); - } - - auto default_model_branch = huggingface_utils::GetDefaultBranch(modelName); - - auto downloaded_model_ids = db_service_->FindRelatedModel(modelName).value_or( - std::vector{}); - - std::vector avai_download_opts{}; - for (const auto& branch : branches.value()) { - if (branch.second.name == "main") { // main branch only have metadata. skip - continue; - } - auto model_id = modelName + ":" + branch.second.name; - if (std::find(downloaded_model_ids.begin(), downloaded_model_ids.end(), - model_id) != - downloaded_model_ids.end()) { // if downloaded, we skip it - continue; - } - avai_download_opts.emplace_back(model_id); - } - - if (avai_download_opts.empty()) { - // TODO: only with pull, we return - return cpp::fail("No variant available"); - } - std::optional normalized_def_branch = std::nullopt; - if (default_model_branch.has_value()) { - normalized_def_branch = modelName + ":" + default_model_branch.value(); - } - string_utils::SortStrings(downloaded_model_ids); - string_utils::SortStrings(avai_download_opts); - auto selection = cli_selection_utils::PrintModelSelection( - downloaded_model_ids, avai_download_opts, normalized_def_branch); - if (!selection.has_value()) { - return cpp::fail("Invalid selection"); - } - - CLI_LOG("Selected: " << selection.value()); - auto branch_name = selection.value().substr(modelName.size() + 1); - return DownloadModelFromCortexso(modelName, branch_name); -} - std::optional ModelService::GetDownloadedModel( const std::string& modelId) const { @@ -402,85 +354,6 @@ ModelService::EstimateModel(const std::string& model_handle, } } -cpp::result ModelService::HandleUrl( - const std::string& url) { - auto url_obj = url_parser::FromUrlString(url); - if (url_obj.has_error()) { - return cpp::fail("Invalid url: " + url); - } - - if (url_obj->host == kHuggingFaceHost) { - if (url_obj->pathParams[2] == "blob") { - url_obj->pathParams[2] = "resolve"; - } - } - auto author{url_obj->pathParams[0]}; - auto model_id{url_obj->pathParams[1]}; - auto file_name{url_obj->pathParams.back()}; - - if (author == "cortexso") { - return DownloadModelFromCortexso(model_id); - } - - if (url_obj->pathParams.size() < 5) { - if (url_obj->pathParams.size() < 2) { - return cpp::fail("Invalid url: " + url); - } - return DownloadHuggingFaceGgufModel(author, model_id, std::nullopt); - } - - std::string huggingFaceHost{kHuggingFaceHost}; - std::string unique_model_id{author + ":" + model_id + ":" + file_name}; - - auto model_entry = db_service_->GetModelInfo(unique_model_id); - - if (model_entry.has_value()) { - CLI_LOG("Model already downloaded: " << unique_model_id); - return unique_model_id; - } - - auto local_path{file_manager_utils::GetModelsContainerPath() / - kHuggingFaceHost / author / model_id / file_name}; - - try { - std::filesystem::create_directories(local_path.parent_path()); - } catch (const std::filesystem::filesystem_error&) { - // if file exist, remove it - std::filesystem::remove(local_path.parent_path()); - std::filesystem::create_directories(local_path.parent_path()); - } - - auto download_url = url_parser::FromUrl(url_obj.value()); - // this assume that the model being downloaded is a single gguf file - auto downloadTask{DownloadTask{.id = model_id, - .type = DownloadType::Model, - .items = {DownloadItem{ - .id = unique_model_id, - .downloadUrl = download_url, - .localPath = local_path, - }}}}; - - auto on_finished = [this, author](const DownloadTask& finishedTask) { - // Sum downloadedBytes from all items - uint64_t model_size = 0; - for (const auto& item : finishedTask.items) { - model_size = model_size + item.bytes.value_or(0); - } - auto gguf_download_item = finishedTask.items[0]; - ParseGguf(*db_service_, gguf_download_item, author, std::nullopt, - model_size); - }; - - auto result = download_service_->AddDownloadTask(downloadTask, on_finished); - if (result.has_error()) { - CTL_ERR(result.error()); - return cpp::fail(result.error()); - } else if (result && result.value()) { - CLI_LOG("Model " << model_id << " downloaded successfully!") - } - return unique_model_id; -} - bool ModelService::HasModel(const std::string& id) const { return db_service_->HasModel(id); } @@ -632,110 +505,6 @@ ModelService::DownloadModelFromCortexsoAsync( return download_service_->AddTask(task, on_finished); } -cpp::result ModelService::DownloadModelFromCortexso( - const std::string& name, const std::string& branch) { - - auto download_task = GetDownloadTask(name, branch); - if (download_task.has_error()) { - return cpp::fail(download_task.error()); - } - - std::string model_id{name + ":" + branch}; - auto on_finished = [this, branch, - model_id](const DownloadTask& finishedTask) { - const DownloadItem* model_yml_item = nullptr; - auto need_parse_gguf = true; - - for (const auto& item : finishedTask.items) { - if (item.localPath.filename().string() == "model.yml") { - model_yml_item = &item; - } - } - - if (model_yml_item == nullptr) { - CTL_WRN("model.yml not found in the downloaded files for " + model_id); - return; - } - auto url_obj = url_parser::FromUrlString(model_yml_item->downloadUrl); - CTL_INF("Adding model to modellist with branch: " << branch); - config::YamlHandler yaml_handler; - yaml_handler.ModelConfigFromFile(model_yml_item->localPath.string()); - auto mc = yaml_handler.GetModelConfig(); - mc.model = model_id; - yaml_handler.UpdateModelConfig(mc); - yaml_handler.WriteYamlFile(model_yml_item->localPath.string()); - - auto rel = - file_manager_utils::ToRelativeCortexDataPath(model_yml_item->localPath); - CTL_INF("path_to_model_yaml: " << rel.string()); - - if (!db_service_->HasModel(model_id)) { - cortex::db::ModelEntry model_entry{ - .model = model_id, - .author_repo_id = "cortexso", - .branch_name = branch, - .path_to_model_yaml = rel.string(), - .model_alias = model_id, - .status = cortex::db::ModelStatus::Downloaded}; - auto result = db_service_->AddModelEntry(model_entry); - - if (result.has_error()) { - CTL_ERR("Error adding model to modellist: " + result.error()); - } - } else { - if (auto m = db_service_->GetModelInfo(model_id); m.has_value()) { - auto upd_m = m.value(); - upd_m.status = cortex::db::ModelStatus::Downloaded; - if (auto r = db_service_->UpdateModelEntry(model_id, upd_m); - r.has_error()) { - CTL_ERR(r.error()); - } - } - } - }; - - auto result = - download_service_->AddDownloadTask(download_task.value(), on_finished); - if (result.has_error()) { - return cpp::fail(result.error()); - } else if (result && result.value()) { - CLI_LOG("Model " << model_id << " downloaded successfully!") - return model_id; - } - return cpp::fail("Failed to download model " + model_id); -} - -cpp::result -ModelService::DownloadHuggingFaceGgufModel( - const std::string& author, const std::string& modelName, - std::optional fileName) { - auto repo_info = - huggingface_utils::GetHuggingFaceModelRepoInfo(author, modelName); - - if (!repo_info.has_value()) { - return cpp::fail("Model not found"); - } - - if (!repo_info->gguf.has_value()) { - return cpp::fail( - "Not a GGUF model. Currently, only GGUF single file is " - "supported."); - } - - std::vector options{}; - for (const auto& sibling : repo_info->siblings) { - if (string_utils::EndsWith(sibling.rfilename, ".gguf")) { - options.push_back(sibling.rfilename); - } - } - auto selection = cli_selection_utils::PrintSelection(options); - std::cout << "Selected: " << selection.value() << std::endl; - - auto download_url = huggingface_utils::GetDownloadableUrl(author, modelName, - selection.value()); - return HandleUrl(download_url); -} - cpp::result ModelService::DeleteModel( const std::string& model_handle) { namespace fs = std::filesystem; diff --git a/engine/services/model_service.h b/engine/services/model_service.h index 04c7f240a..beba91f8c 100644 --- a/engine/services/model_service.h +++ b/engine/services/model_service.h @@ -42,9 +42,6 @@ class ModelService { cpp::result AbortDownloadModel( const std::string& task_id); - cpp::result DownloadModelFromCortexso( - const std::string& name, const std::string& branch = "main"); - cpp::result DownloadModelFromCortexsoAsync( const std::string& name, const std::string& branch = "main", std::optional temp_model_id = std::nullopt); @@ -70,8 +67,6 @@ class ModelService { cpp::result GetModelPullInfo( const std::string& model_handle); - cpp::result HandleUrl(const std::string& url); - cpp::result HandleDownloadUrlAsync( const std::string& url, std::optional temp_model_id, std::optional temp_name); @@ -94,26 +89,12 @@ class ModelService { std::string GetEngineByModelId(const std::string& model_id) const; private: - /** - * Handle downloading model which have following pattern: author/model_name - */ - cpp::result DownloadHuggingFaceGgufModel( - const std::string& author, const std::string& modelName, - std::optional fileName); - - /** - * Handling cortexso models. Will look through cortexso's HF repository and - * listing all the branches, except main. Then print out the selection for user. - */ - cpp::result HandleCortexsoModel( - const std::string& modelName); - cpp::result, std::string> MayFallbackToCpu( const std::string& model_path, int ngl, int ctx_len, int n_batch = 2048, int n_ubatch = 2048, const std::string& kv_cache_type = "f16"); void ProcessBgrTasks(); - + int GetCpuThreads() const; std::shared_ptr db_service_;