@@ -197,54 +197,6 @@ void ModelService::ForceIndexingModelList() {
197
197
}
198
198
}
199
199
200
- cpp::result<std::string, std::string> ModelService::HandleCortexsoModel (
201
- const std::string& modelName) {
202
- auto branches =
203
- huggingface_utils::GetModelRepositoryBranches (" cortexso" , modelName);
204
- if (branches.has_error ()) {
205
- return cpp::fail (branches.error ());
206
- }
207
-
208
- auto default_model_branch = huggingface_utils::GetDefaultBranch (modelName);
209
-
210
- auto downloaded_model_ids = db_service_->FindRelatedModel (modelName).value_or (
211
- std::vector<std::string>{});
212
-
213
- std::vector<std::string> avai_download_opts{};
214
- for (const auto & branch : branches.value ()) {
215
- if (branch.second .name == " main" ) { // main branch only have metadata. skip
216
- continue ;
217
- }
218
- auto model_id = modelName + " :" + branch.second .name ;
219
- if (std::find (downloaded_model_ids.begin (), downloaded_model_ids.end (),
220
- model_id) !=
221
- downloaded_model_ids.end ()) { // if downloaded, we skip it
222
- continue ;
223
- }
224
- avai_download_opts.emplace_back (model_id);
225
- }
226
-
227
- if (avai_download_opts.empty ()) {
228
- // TODO: only with pull, we return
229
- return cpp::fail (" No variant available" );
230
- }
231
- std::optional<std::string> normalized_def_branch = std::nullopt;
232
- if (default_model_branch.has_value ()) {
233
- normalized_def_branch = modelName + " :" + default_model_branch.value ();
234
- }
235
- string_utils::SortStrings (downloaded_model_ids);
236
- string_utils::SortStrings (avai_download_opts);
237
- auto selection = cli_selection_utils::PrintModelSelection (
238
- downloaded_model_ids, avai_download_opts, normalized_def_branch);
239
- if (!selection.has_value ()) {
240
- return cpp::fail (" Invalid selection" );
241
- }
242
-
243
- CLI_LOG (" Selected: " << selection.value ());
244
- auto branch_name = selection.value ().substr (modelName.size () + 1 );
245
- return DownloadModelFromCortexso (modelName, branch_name);
246
- }
247
-
248
200
std::optional<config::ModelConfig> ModelService::GetDownloadedModel (
249
201
const std::string& modelId) const {
250
202
@@ -402,85 +354,6 @@ ModelService::EstimateModel(const std::string& model_handle,
402
354
}
403
355
}
404
356
405
- cpp::result<std::string, std::string> ModelService::HandleUrl (
406
- const std::string& url) {
407
- auto url_obj = url_parser::FromUrlString (url);
408
- if (url_obj.has_error ()) {
409
- return cpp::fail (" Invalid url: " + url);
410
- }
411
-
412
- if (url_obj->host == kHuggingFaceHost ) {
413
- if (url_obj->pathParams [2 ] == " blob" ) {
414
- url_obj->pathParams [2 ] = " resolve" ;
415
- }
416
- }
417
- auto author{url_obj->pathParams [0 ]};
418
- auto model_id{url_obj->pathParams [1 ]};
419
- auto file_name{url_obj->pathParams .back ()};
420
-
421
- if (author == " cortexso" ) {
422
- return DownloadModelFromCortexso (model_id);
423
- }
424
-
425
- if (url_obj->pathParams .size () < 5 ) {
426
- if (url_obj->pathParams .size () < 2 ) {
427
- return cpp::fail (" Invalid url: " + url);
428
- }
429
- return DownloadHuggingFaceGgufModel (author, model_id, std::nullopt);
430
- }
431
-
432
- std::string huggingFaceHost{kHuggingFaceHost };
433
- std::string unique_model_id{author + " :" + model_id + " :" + file_name};
434
-
435
- auto model_entry = db_service_->GetModelInfo (unique_model_id);
436
-
437
- if (model_entry.has_value ()) {
438
- CLI_LOG (" Model already downloaded: " << unique_model_id);
439
- return unique_model_id;
440
- }
441
-
442
- auto local_path{file_manager_utils::GetModelsContainerPath () /
443
- kHuggingFaceHost / author / model_id / file_name};
444
-
445
- try {
446
- std::filesystem::create_directories (local_path.parent_path ());
447
- } catch (const std::filesystem::filesystem_error&) {
448
- // if file exist, remove it
449
- std::filesystem::remove (local_path.parent_path ());
450
- std::filesystem::create_directories (local_path.parent_path ());
451
- }
452
-
453
- auto download_url = url_parser::FromUrl (url_obj.value ());
454
- // this assume that the model being downloaded is a single gguf file
455
- auto downloadTask{DownloadTask{.id = model_id,
456
- .type = DownloadType::Model,
457
- .items = {DownloadItem{
458
- .id = unique_model_id,
459
- .downloadUrl = download_url,
460
- .localPath = local_path,
461
- }}}};
462
-
463
- auto on_finished = [this , author](const DownloadTask& finishedTask) {
464
- // Sum downloadedBytes from all items
465
- uint64_t model_size = 0 ;
466
- for (const auto & item : finishedTask.items ) {
467
- model_size = model_size + item.bytes .value_or (0 );
468
- }
469
- auto gguf_download_item = finishedTask.items [0 ];
470
- ParseGguf (*db_service_, gguf_download_item, author, std::nullopt,
471
- model_size);
472
- };
473
-
474
- auto result = download_service_->AddDownloadTask (downloadTask, on_finished);
475
- if (result.has_error ()) {
476
- CTL_ERR (result.error ());
477
- return cpp::fail (result.error ());
478
- } else if (result && result.value ()) {
479
- CLI_LOG (" Model " << model_id << " downloaded successfully!" )
480
- }
481
- return unique_model_id;
482
- }
483
-
484
357
bool ModelService::HasModel (const std::string& id) const {
485
358
return db_service_->HasModel (id);
486
359
}
@@ -632,110 +505,6 @@ ModelService::DownloadModelFromCortexsoAsync(
632
505
return download_service_->AddTask (task, on_finished);
633
506
}
634
507
635
- cpp::result<std::string, std::string> ModelService::DownloadModelFromCortexso (
636
- const std::string& name, const std::string& branch) {
637
-
638
- auto download_task = GetDownloadTask (name, branch);
639
- if (download_task.has_error ()) {
640
- return cpp::fail (download_task.error ());
641
- }
642
-
643
- std::string model_id{name + " :" + branch};
644
- auto on_finished = [this , branch,
645
- model_id](const DownloadTask& finishedTask) {
646
- const DownloadItem* model_yml_item = nullptr ;
647
- auto need_parse_gguf = true ;
648
-
649
- for (const auto & item : finishedTask.items ) {
650
- if (item.localPath .filename ().string () == " model.yml" ) {
651
- model_yml_item = &item;
652
- }
653
- }
654
-
655
- if (model_yml_item == nullptr ) {
656
- CTL_WRN (" model.yml not found in the downloaded files for " + model_id);
657
- return ;
658
- }
659
- auto url_obj = url_parser::FromUrlString (model_yml_item->downloadUrl );
660
- CTL_INF (" Adding model to modellist with branch: " << branch);
661
- config::YamlHandler yaml_handler;
662
- yaml_handler.ModelConfigFromFile (model_yml_item->localPath .string ());
663
- auto mc = yaml_handler.GetModelConfig ();
664
- mc.model = model_id;
665
- yaml_handler.UpdateModelConfig (mc);
666
- yaml_handler.WriteYamlFile (model_yml_item->localPath .string ());
667
-
668
- auto rel =
669
- file_manager_utils::ToRelativeCortexDataPath (model_yml_item->localPath );
670
- CTL_INF (" path_to_model_yaml: " << rel.string ());
671
-
672
- if (!db_service_->HasModel (model_id)) {
673
- cortex::db::ModelEntry model_entry{
674
- .model = model_id,
675
- .author_repo_id = " cortexso" ,
676
- .branch_name = branch,
677
- .path_to_model_yaml = rel.string (),
678
- .model_alias = model_id,
679
- .status = cortex::db::ModelStatus::Downloaded};
680
- auto result = db_service_->AddModelEntry (model_entry);
681
-
682
- if (result.has_error ()) {
683
- CTL_ERR (" Error adding model to modellist: " + result.error ());
684
- }
685
- } else {
686
- if (auto m = db_service_->GetModelInfo (model_id); m.has_value ()) {
687
- auto upd_m = m.value ();
688
- upd_m.status = cortex::db::ModelStatus::Downloaded;
689
- if (auto r = db_service_->UpdateModelEntry (model_id, upd_m);
690
- r.has_error ()) {
691
- CTL_ERR (r.error ());
692
- }
693
- }
694
- }
695
- };
696
-
697
- auto result =
698
- download_service_->AddDownloadTask (download_task.value (), on_finished);
699
- if (result.has_error ()) {
700
- return cpp::fail (result.error ());
701
- } else if (result && result.value ()) {
702
- CLI_LOG (" Model " << model_id << " downloaded successfully!" )
703
- return model_id;
704
- }
705
- return cpp::fail (" Failed to download model " + model_id);
706
- }
707
-
708
- cpp::result<std::string, std::string>
709
- ModelService::DownloadHuggingFaceGgufModel (
710
- const std::string& author, const std::string& modelName,
711
- std::optional<std::string> fileName) {
712
- auto repo_info =
713
- huggingface_utils::GetHuggingFaceModelRepoInfo (author, modelName);
714
-
715
- if (!repo_info.has_value ()) {
716
- return cpp::fail (" Model not found" );
717
- }
718
-
719
- if (!repo_info->gguf .has_value ()) {
720
- return cpp::fail (
721
- " Not a GGUF model. Currently, only GGUF single file is "
722
- " supported." );
723
- }
724
-
725
- std::vector<std::string> options{};
726
- for (const auto & sibling : repo_info->siblings ) {
727
- if (string_utils::EndsWith (sibling.rfilename , " .gguf" )) {
728
- options.push_back (sibling.rfilename );
729
- }
730
- }
731
- auto selection = cli_selection_utils::PrintSelection (options);
732
- std::cout << " Selected: " << selection.value () << std::endl;
733
-
734
- auto download_url = huggingface_utils::GetDownloadableUrl (author, modelName,
735
- selection.value ());
736
- return HandleUrl (download_url);
737
- }
738
-
739
508
cpp::result<void , std::string> ModelService::DeleteModel (
740
509
const std::string& model_handle) {
741
510
namespace fs = std::filesystem;
0 commit comments