@@ -144,9 +144,25 @@ cpp::result<bool, std::string> ModelSourceService::RemoveModelSource(
144144 return true ;
145145}
146146
147- cpp::result<std::vector <std::string>, std::string>
147+ cpp::result<std::unordered_map <std::string, ModelSource >, std::string>
148148ModelSourceService::GetModelSources () {
149- return db_service_->GetModelSources ();
149+ auto res = db_service_->GetDownloadableModels ();
150+ if (res.has_error ()) {
151+ return cpp::fail (res.error ());
152+ }
153+ auto & models = res.value ();
154+ std::unordered_map<std::string, ModelSource> ms;
155+ for (auto const & m : models) {
156+ auto meta_json = json_helper::ParseJsonString (m.metadata );
157+ ms[m.model_source ].models .push_back ({m.model , meta_json[" size" ].asUInt64 ()});
158+ meta_json.removeMember (" size" );
159+ if (ms[m.model_source ].metadata .empty ()) {
160+ ms[m.model_source ].metadata = json_helper::DumpJsonString (meta_json);
161+ }
162+ ms[m.model_source ].id = m.model_source ;
163+ LOG_INFO << m.model ;
164+ }
165+ return ms;
150166}
151167
152168cpp::result<bool , std::string> ModelSourceService::AddHfOrg (
@@ -155,32 +171,17 @@ cpp::result<bool, std::string> ModelSourceService::AddHfOrg(
155171 author);
156172 if (res.has_value ()) {
157173 auto models = ParseJsonString (res.value ());
158- // Get models from db
159-
160- auto model_list_before = db_service_->GetModels (model_source)
161- .value_or (std::vector<std::string>{});
162- std::unordered_set<std::string> updated_model_list;
163174 // Add new models
164175 for (auto const & m : models) {
165176 CTL_DBG (m.id );
177+
166178 auto author_model = string_utils::SplitBy (m.id , " /" );
167179 if (author_model.size () == 2 ) {
168180 auto const & author = author_model[0 ];
169181 auto const & model_name = author_model[1 ];
170- auto add_res = AddRepoSiblings (model_source, author, model_name)
171- .value_or (std::unordered_set<std::string>{});
172- for (auto const & a : add_res) {
173- updated_model_list.insert (a);
174- }
175- }
176- }
177-
178- // Clean up
179- for (auto const & mid : model_list_before) {
180- if (updated_model_list.find (mid) == updated_model_list.end ()) {
181- if (auto del_res = db_service_->DeleteModelEntry (mid);
182- del_res.has_error ()) {
183- CTL_INF (del_res.error ());
182+ auto r = AddHfRepo (model_source + " /" + model_name, author, model_name);
183+ if (r.has_error ()) {
184+ CTL_WRN (r.error ());
184185 }
185186 }
186187 }
@@ -231,8 +232,32 @@ ModelSourceService::AddRepoSiblings(const std::string& model_source,
231232 " supported." );
232233 }
233234
235+ auto siblings_fs = hu::GetSiblingsFileSize (author, model_name);
236+
237+ if (siblings_fs.has_error ()) {
238+ return cpp::fail (" Could not get siblings file size: " + author + " /" +
239+ model_name);
240+ }
241+
242+ auto readme = hu::GetReadMe (author, model_name);
243+ std::string desc;
244+ if (!readme.has_error ()) {
245+ desc = readme.value ();
246+ }
247+
248+ auto meta_json = json_helper::ParseJsonString (repo_info->metadata );
249+ for (auto & m : meta_json[" siblings" ]) {
250+ m[" size" ] = siblings_fs.value ()
251+ .file_sizes .at (m[" rfilename" ].asString ())
252+ .size_in_bytes ;
253+ }
254+ meta_json[" description" ] = desc;
255+ LOG_DEBUG << meta_json.toStyledString ();
256+
234257 for (const auto & sibling : repo_info->siblings ) {
235258 if (string_utils::EndsWith (sibling.rfilename , " .gguf" )) {
259+ meta_json[" size" ] =
260+ siblings_fs.value ().file_sizes .at (sibling.rfilename ).size_in_bytes ;
236261 std::string model_id =
237262 author + " :" + model_name + " :" + sibling.rfilename ;
238263 cortex::db::ModelEntry e = {
@@ -245,7 +270,7 @@ ModelSourceService::AddRepoSiblings(const std::string& model_source,
245270 .model_source = model_source,
246271 .status = cortex::db::ModelStatus::Downloadable,
247272 .engine = " llama-cpp" ,
248- .metadata = repo_info-> metadata };
273+ .metadata = json_helper::DumpJsonString (meta_json) };
249274 if (!db_service_->HasModel (model_id)) {
250275 if (auto add_res = db_service_->AddModelEntry (e); add_res.has_error ()) {
251276 CTL_INF (add_res.error ());
@@ -273,46 +298,16 @@ cpp::result<bool, std::string> ModelSourceService::AddCortexsoOrg(
273298 " https://huggingface.co/api/models?author=cortexso" );
274299 if (res.has_value ()) {
275300 auto models = ParseJsonString (res.value ());
276- // Get models from db
277-
278- auto model_list_before = db_service_->GetModels (model_source)
279- .value_or (std::vector<std::string>{});
280- std::unordered_set<std::string> updated_model_list;
281301 for (auto const & m : models) {
282302 CTL_INF (m.id );
283303 auto author_model = string_utils::SplitBy (m.id , " /" );
284304 if (author_model.size () == 2 ) {
285305 auto const & author = author_model[0 ];
286306 auto const & model_name = author_model[1 ];
287- auto branches = huggingface_utils::GetModelRepositoryBranches (
288- " cortexso" , model_name);
289- if (branches.has_error ()) {
290- CTL_INF (branches.error ());
291- continue ;
292- }
293-
294- auto repo_info = hu::GetHuggingFaceModelRepoInfo (author, model_name);
295- if (repo_info.has_error ()) {
296- CTL_INF (repo_info.error ());
297- continue ;
298- }
299- for (auto const & [branch, _] : branches.value ()) {
300- CTL_INF (branch);
301- auto add_res = AddCortexsoRepoBranch (model_source, author, model_name,
302- branch, repo_info->metadata )
303- .value_or (std::unordered_set<std::string>{});
304- for (auto const & a : add_res) {
305- updated_model_list.insert (a);
306- }
307- }
308- }
309- }
310- // Clean up
311- for (auto const & mid : model_list_before) {
312- if (updated_model_list.find (mid) == updated_model_list.end ()) {
313- if (auto del_res = db_service_->DeleteModelEntry (mid);
314- del_res.has_error ()) {
315- CTL_INF (del_res.error ());
307+ auto r = AddCortexsoRepo (model_source + " /" + model_name, author,
308+ model_name);
309+ if (r.has_error ()) {
310+ CTL_WRN (r.error ());
316311 }
317312 }
318313 }
@@ -384,27 +379,32 @@ ModelSourceService::AddCortexsoRepoBranch(const std::string& model_source,
384379 }
385380
386381 bool has_gguf = false ;
382+ uint64_t model_size = 0 ;
387383 for (const auto & value : result.value ()) {
388384 auto path = value[" path" ].asString ();
389385 if (path.find (" .gguf" ) != std::string::npos) {
390386 has_gguf = true ;
387+ model_size = value[" size" ].asUInt64 ();
391388 }
392389 }
393390 if (!has_gguf) {
394391 CTL_INF (" Only support gguf file format! - branch: " << branch);
395392 return {};
396393 } else {
394+ auto meta_json = json_helper::ParseJsonString (metadata);
395+ meta_json[" size" ] = model_size;
397396 std::string model_id = model_name + " :" + branch;
398- cortex::db::ModelEntry e = {.model = model_id,
399- .author_repo_id = author,
400- .branch_name = branch,
401- .path_to_model_yaml = " " ,
402- .model_alias = " " ,
403- .model_format = " cortexso" ,
404- .model_source = model_source,
405- .status = cortex::db::ModelStatus::Downloadable,
406- .engine = " llama-cpp" ,
407- .metadata = metadata};
397+ cortex::db::ModelEntry e = {
398+ .model = model_id,
399+ .author_repo_id = author,
400+ .branch_name = branch,
401+ .path_to_model_yaml = " " ,
402+ .model_alias = " " ,
403+ .model_format = " cortexso" ,
404+ .model_source = model_source,
405+ .status = cortex::db::ModelStatus::Downloadable,
406+ .engine = " llama-cpp" ,
407+ .metadata = json_helper::DumpJsonString (meta_json)};
408408 if (!db_service_->HasModel (model_id)) {
409409 CTL_INF (" Adding model to db: " << model_name << " :" << branch);
410410 if (auto res = db_service_->AddModelEntry (e);
0 commit comments