11#include " model_service.h"
22#include < filesystem>
33#include < iostream>
4+ #include < optional>
45#include < ostream>
56#include " config/gguf_parser.h"
67#include " config/yaml_config.h"
@@ -58,8 +59,7 @@ cpp::result<DownloadTask, std::string> GetDownloadTask(
5859 httplib::Client cli (url.GetProtocolAndHost ());
5960 auto res = cli.Get (url.GetPathAndQuery ());
6061 if (res->status != httplib::StatusCode::OK_200) {
61- auto err = res.error ();
62- return cpp::fail (" HTTP error: " + httplib::to_string (err));
62+ return cpp::fail (" Model " + modelId + " not found" );
6363 }
6464 auto jsonResponse = json::parse (res->body );
6565
@@ -103,7 +103,6 @@ cpp::result<std::string, std::string> ModelService::DownloadModel(
103103 }
104104
105105 if (string_utils::StartsWith (input, " https://" )) {
106- // TODO: better name, for example handle url
107106 return HandleUrl (input, async);
108107 }
109108
@@ -195,13 +194,27 @@ cpp::result<std::string, std::string> ModelService::HandleUrl(
195194 auto file_name{url_obj.pathParams .back ()};
196195
197196 if (author == " cortexso" ) {
198- // TODO: try to get the branch
199197 return DownloadModelFromCortexso (model_id);
200198 }
201199
200+ if (url_obj.pathParams .size () < 5 ) {
201+ if (url_obj.pathParams .size () < 2 ) {
202+ return cpp::fail (" Invalid url: " + url);
203+ }
204+ return DownloadHuggingFaceGgufModel (author, model_id, std::nullopt , async);
205+ }
206+
202207 std::string huggingFaceHost{kHuggingFaceHost };
203- std::string unique_model_id{huggingFaceHost + " /" + author + " /" + model_id +
204- " /" + file_name};
208+ std::string unique_model_id{author + " :" + model_id + " :" + file_name};
209+
210+ cortex::db::Models modellist_handler;
211+ auto model_entry = modellist_handler.GetModelInfo (unique_model_id);
212+
213+ if (model_entry.has_value ()) {
214+ CLI_LOG (" Model already downloaded: " << unique_model_id);
215+ return cpp::fail (" Please delete the model before downloading again" );
216+ }
217+
205218 auto local_path{file_manager_utils::GetModelsContainerPath () /
206219 " huggingface.co" / author / model_id / file_name};
207220
@@ -240,7 +253,7 @@ cpp::result<std::string, std::string> ModelService::HandleUrl(
240253 } else {
241254 auto result = download_service_.AddDownloadTask (downloadTask, on_finished);
242255 if (result.has_error ()) {
243- // CTL_ERR(result.error());
256+ CTL_ERR (result.error ());
244257 return cpp::fail (result.error ());
245258 } else if (result && result.value ()) {
246259 CLI_LOG (" Model " << model_id << " downloaded successfully!" )
@@ -519,4 +532,4 @@ cpp::result<bool, std::string> ModelService::GetModelStatus(
519532 return cpp::fail (" Fail to get model status with ID '" + model_handle +
520533 " ': " + e.what ());
521534 }
522- }
535+ }
0 commit comments