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

Commit 099fe58

Browse files
committed
fix: add metadata
1 parent f090e0b commit 099fe58

File tree

8 files changed

+220
-71
lines changed

8 files changed

+220
-71
lines changed

engine/controllers/models.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -810,11 +810,11 @@ void Models::GetModelSources(
810810
resp->setStatusCode(k400BadRequest);
811811
callback(resp);
812812
} else {
813-
auto const& info = res.value();
813+
auto& info = res.value();
814814
Json::Value ret;
815815
Json::Value data(Json::arrayValue);
816-
for (auto const& i : info) {
817-
data.append(i);
816+
for (auto& i : info) {
817+
data.append(i.second.ToJson());
818818
}
819819
ret["data"] = data;
820820
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);

engine/database/models.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,34 @@ cpp::result<std::vector<std::string>, std::string> Models::GetModels(
304304
}
305305
}
306306

307+
cpp::result<std::vector<ModelEntry>, std::string>
308+
Models::GetDownloadableModels() const {
309+
try {
310+
std::vector<ModelEntry> res;
311+
SQLite::Statement query(
312+
db_,
313+
"SELECT model_id, author_repo_id, branch_name, "
314+
"path_to_model_yaml, model_alias, model_format, "
315+
"model_source, status, engine, metadata FROM models "
316+
"WHERE status = \"downloadable\"");
317+
while (query.executeStep()) {
318+
ModelEntry entry;
319+
entry.model = query.getColumn(0).getString();
320+
entry.author_repo_id = query.getColumn(1).getString();
321+
entry.branch_name = query.getColumn(2).getString();
322+
entry.path_to_model_yaml = query.getColumn(3).getString();
323+
entry.model_alias = query.getColumn(4).getString();
324+
entry.model_format = query.getColumn(5).getString();
325+
entry.model_source = query.getColumn(6).getString();
326+
entry.status = StringToStatus(query.getColumn(7).getString());
327+
entry.engine = query.getColumn(8).getString();
328+
entry.metadata = query.getColumn(9).getString();
329+
res.push_back(entry);
330+
}
331+
return res;
332+
} catch (const std::exception& e) {
333+
return cpp::fail(e.what());
334+
}
335+
}
336+
307337
} // namespace cortex::db

engine/database/models.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace cortex::db {
1010

1111
enum class ModelStatus { Remote, Downloaded, Downloadable };
1212

13-
1413
struct ModelEntry {
1514
std::string model;
1615
std::string author_repo_id;
@@ -60,6 +59,7 @@ class Models {
6059
cpp::result<std::vector<std::string>, std::string> GetModelSources() const;
6160
cpp::result<std::vector<std::string>, std::string> GetModels(
6261
const std::string& model_src) const;
62+
cpp::result<std::vector<ModelEntry>, std::string> GetDownloadableModels() const;
6363
};
6464

6565
} // namespace cortex::db

engine/services/database_service.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,9 @@ cpp::result<std::vector<std::string>, std::string> DatabaseService::GetModels(
127127
const std::string& model_src) const {
128128
return cortex::db::Models().GetModels(model_src);
129129
}
130+
131+
cpp::result<std::vector<ModelEntry>, std::string>
132+
DatabaseService::GetDownloadableModels() const {
133+
return cortex::db::Models().GetDownloadableModels();
134+
}
130135
// end models

engine/services/database_service.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class DatabaseService {
6363
cpp::result<std::vector<std::string>, std::string> GetModelSources() const;
6464
cpp::result<std::vector<std::string>, std::string> GetModels(
6565
const std::string& model_src) const;
66+
cpp::result<std::vector<ModelEntry>, std::string> GetDownloadableModels()
67+
const;
6668

6769
private:
6870
};

engine/services/model_source_service.cc

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -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>
148148
ModelSourceService::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

152168
cpp::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);

engine/services/model_source_service.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,35 @@
55
#include "services/database_service.h"
66
#include "utils/result.hpp"
77

8+
struct ModelSourceInfo {
9+
std::string id;
10+
uint64_t size;
11+
Json::Value ToJson() const {
12+
Json::Value root;
13+
root["id"] = id;
14+
root["size"] = size;
15+
return root;
16+
}
17+
};
18+
19+
struct ModelSource {
20+
std::string id;
21+
std::vector<ModelSourceInfo> models;
22+
std::string metadata;
23+
24+
Json::Value ToJson() {
25+
Json::Value root;
26+
root["id"] = id;
27+
Json::Value models_json;
28+
for (auto const& m : models) {
29+
models_json.append(m.ToJson());
30+
}
31+
root["models"] = models_json;
32+
root["metadata"] = metadata;
33+
return root;
34+
};
35+
};
36+
837
class ModelSourceService {
938
public:
1039
explicit ModelSourceService(std::shared_ptr<DatabaseService> db_service);
@@ -16,7 +45,8 @@ class ModelSourceService {
1645
cpp::result<bool, std::string> RemoveModelSource(
1746
const std::string& model_source);
1847

19-
cpp::result<std::vector<std::string>, std::string> GetModelSources();
48+
cpp::result<std::unordered_map<std::string, ModelSource>, std::string>
49+
GetModelSources();
2050

2151
private:
2252
cpp::result<bool, std::string> AddHfOrg(const std::string& model_source,

0 commit comments

Comments
 (0)