Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

chore: delete unused old download functions #2133

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
231 changes: 0 additions & 231 deletions engine/services/model_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,54 +197,6 @@ void ModelService::ForceIndexingModelList() {
}
}

cpp::result<std::string, std::string> 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::string>{});

std::vector<std::string> 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<std::string> 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<config::ModelConfig> ModelService::GetDownloadedModel(
const std::string& modelId) const {

Expand Down Expand Up @@ -402,85 +354,6 @@ ModelService::EstimateModel(const std::string& model_handle,
}
}

cpp::result<std::string, std::string> 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);
}
Expand Down Expand Up @@ -632,110 +505,6 @@ ModelService::DownloadModelFromCortexsoAsync(
return download_service_->AddTask(task, on_finished);
}

cpp::result<std::string, std::string> 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<std::string, std::string>
ModelService::DownloadHuggingFaceGgufModel(
const std::string& author, const std::string& modelName,
std::optional<std::string> 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<std::string> 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<void, std::string> ModelService::DeleteModel(
const std::string& model_handle) {
namespace fs = std::filesystem;
Expand Down
21 changes: 1 addition & 20 deletions engine/services/model_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ class ModelService {
cpp::result<std::string, std::string> AbortDownloadModel(
const std::string& task_id);

cpp::result<std::string, std::string> DownloadModelFromCortexso(
const std::string& name, const std::string& branch = "main");

cpp::result<DownloadTask, std::string> DownloadModelFromCortexsoAsync(
const std::string& name, const std::string& branch = "main",
std::optional<std::string> temp_model_id = std::nullopt);
Expand All @@ -70,8 +67,6 @@ class ModelService {
cpp::result<ModelPullInfo, std::string> GetModelPullInfo(
const std::string& model_handle);

cpp::result<std::string, std::string> HandleUrl(const std::string& url);

cpp::result<DownloadTask, std::string> HandleDownloadUrlAsync(
const std::string& url, std::optional<std::string> temp_model_id,
std::optional<std::string> temp_name);
Expand All @@ -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<std::string, std::string> DownloadHuggingFaceGgufModel(
const std::string& author, const std::string& modelName,
std::optional<std::string> 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<std::string, std::string> HandleCortexsoModel(
const std::string& modelName);

cpp::result<std::optional<std::string>, 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<DatabaseService> db_service_;
Expand Down
Loading