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

Commit 0f33259

Browse files
committed
fix: add get models by source
1 parent 099fe58 commit 0f33259

File tree

8 files changed

+91
-20
lines changed

8 files changed

+91
-20
lines changed

engine/controllers/models.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,4 +821,23 @@ void Models::GetModelSources(
821821
resp->setStatusCode(k200OK);
822822
callback(resp);
823823
}
824+
}
825+
826+
void Models::GetModelSource(
827+
const HttpRequestPtr& req,
828+
std::function<void(const HttpResponsePtr&)>&& callback,
829+
const std::string& src) {
830+
auto res = model_src_svc_->GetModelSource(src);
831+
if (res.has_error()) {
832+
Json::Value ret;
833+
ret["message"] = res.error();
834+
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
835+
resp->setStatusCode(k400BadRequest);
836+
callback(resp);
837+
} else {
838+
auto& info = res.value();
839+
auto resp = cortex_utils::CreateCortexHttpJsonResponse(info.ToJson());
840+
resp->setStatusCode(k200OK);
841+
callback(resp);
842+
}
824843
}

engine/controllers/models.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Models : public drogon::HttpController<Models, false> {
4343
ADD_METHOD_TO(Models::AddModelSource, "/v1/models/sources", Post);
4444
ADD_METHOD_TO(Models::DeleteModelSource, "/v1/models/sources", Delete);
4545
ADD_METHOD_TO(Models::GetModelSources, "/v1/models/sources", Get);
46+
ADD_METHOD_TO(Models::GetModelSource, "/v1/models?source={src}", Get);
4647
METHOD_LIST_END
4748

4849
explicit Models(std::shared_ptr<DatabaseService> db_service,
@@ -106,6 +107,10 @@ class Models : public drogon::HttpController<Models, false> {
106107
void GetModelSources(const HttpRequestPtr& req,
107108
std::function<void(const HttpResponsePtr&)>&& callback);
108109

110+
void GetModelSource(const HttpRequestPtr& req,
111+
std::function<void(const HttpResponsePtr&)>&& callback,
112+
const std::string& src);
113+
109114
private:
110115
std::shared_ptr<DatabaseService> db_service_;
111116
std::shared_ptr<ModelService> model_service_;

engine/database/models.cc

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,18 +287,32 @@ cpp::result<std::vector<std::string>, std::string> Models::GetModelSources()
287287
}
288288
}
289289

290-
cpp::result<std::vector<std::string>, std::string> Models::GetModels(
290+
cpp::result<std::vector<ModelEntry>, std::string> Models::GetModels(
291291
const std::string& model_src) const {
292292
try {
293-
std::vector<std::string> ids;
293+
std::vector<ModelEntry> res;
294294
SQLite::Statement query(db_,
295-
"SELECT model_id FROM models WHERE model_source = "
295+
"SELECT model_id, author_repo_id, branch_name, "
296+
"path_to_model_yaml, model_alias, model_format, "
297+
"model_source, status, engine, metadata FROM "
298+
"models WHERE model_source = "
296299
"? AND status = \"downloadable\"");
297300
query.bind(1, model_src);
298301
while (query.executeStep()) {
299-
ids.push_back(query.getColumn(0).getString());
302+
ModelEntry entry;
303+
entry.model = query.getColumn(0).getString();
304+
entry.author_repo_id = query.getColumn(1).getString();
305+
entry.branch_name = query.getColumn(2).getString();
306+
entry.path_to_model_yaml = query.getColumn(3).getString();
307+
entry.model_alias = query.getColumn(4).getString();
308+
entry.model_format = query.getColumn(5).getString();
309+
entry.model_source = query.getColumn(6).getString();
310+
entry.status = StringToStatus(query.getColumn(7).getString());
311+
entry.engine = query.getColumn(8).getString();
312+
entry.metadata = query.getColumn(9).getString();
313+
res.push_back(entry);
300314
}
301-
return ids;
315+
return res;
302316
} catch (const std::exception& e) {
303317
return cpp::fail(e.what());
304318
}

engine/database/models.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Models {
5757
const std::string& identifier) const;
5858
bool HasModel(const std::string& identifier) const;
5959
cpp::result<std::vector<std::string>, std::string> GetModelSources() const;
60-
cpp::result<std::vector<std::string>, std::string> GetModels(
60+
cpp::result<std::vector<ModelEntry>, std::string> GetModels(
6161
const std::string& model_src) const;
6262
cpp::result<std::vector<ModelEntry>, std::string> GetDownloadableModels() const;
6363
};

engine/services/database_service.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ DatabaseService::GetModelSources() const {
123123
return cortex::db::Models().GetModelSources();
124124
}
125125

126-
cpp::result<std::vector<std::string>, std::string> DatabaseService::GetModels(
126+
cpp::result<std::vector<ModelEntry>, std::string> DatabaseService::GetModels(
127127
const std::string& model_src) const {
128128
return cortex::db::Models().GetModels(model_src);
129129
}

engine/services/database_service.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class DatabaseService {
6161
const std::string& identifier) const;
6262
bool HasModel(const std::string& identifier) const;
6363
cpp::result<std::vector<std::string>, std::string> GetModelSources() const;
64-
cpp::result<std::vector<std::string>, std::string> GetModels(
64+
cpp::result<std::vector<ModelEntry>, std::string> GetModels(
6565
const std::string& model_src) const;
6666
cpp::result<std::vector<ModelEntry>, std::string> GetDownloadableModels()
6767
const;

engine/services/model_source_service.cc

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ ModelSourceService::GetModelSources() {
154154
std::unordered_map<std::string, ModelSource> ms;
155155
for (auto const& m : models) {
156156
auto meta_json = json_helper::ParseJsonString(m.metadata);
157-
ms[m.model_source].models.push_back({m.model, meta_json["size"].asUInt64()});
157+
ms[m.model_source].models.push_back(
158+
{m.model, meta_json["size"].asUInt64()});
158159
meta_json.removeMember("size");
159160
if (ms[m.model_source].metadata.empty()) {
160161
ms[m.model_source].metadata = json_helper::DumpJsonString(meta_json);
@@ -165,6 +166,28 @@ ModelSourceService::GetModelSources() {
165166
return ms;
166167
}
167168

169+
cpp::result<ModelSource, std::string> ModelSourceService::GetModelSource(
170+
const std::string& src) {
171+
auto res = db_service_->GetModels(src);
172+
if (res.has_error()) {
173+
return cpp::fail(res.error());
174+
}
175+
176+
auto& models = res.value();
177+
ModelSource ms;
178+
for (auto const& m : models) {
179+
auto meta_json = json_helper::ParseJsonString(m.metadata);
180+
ms.models.push_back({m.model, meta_json["size"].asUInt64()});
181+
meta_json.removeMember("size");
182+
if (ms.metadata.empty()) {
183+
ms.metadata = json_helper::DumpJsonString(meta_json);
184+
}
185+
ms.id = m.model_source;
186+
LOG_INFO << m.model;
187+
}
188+
return ms;
189+
}
190+
168191
cpp::result<bool, std::string> ModelSourceService::AddHfOrg(
169192
const std::string& model_source, const std::string& author) {
170193
auto res = curl_utils::SimpleGet("https://huggingface.co/api/models?author=" +
@@ -196,8 +219,8 @@ cpp::result<bool, std::string> ModelSourceService::AddHfRepo(
196219
const std::string& model_name) {
197220
// Get models from db
198221

199-
auto model_list_before =
200-
db_service_->GetModels(model_source).value_or(std::vector<std::string>{});
222+
auto model_list_before = db_service_->GetModels(model_source)
223+
.value_or(std::vector<cortex::db::ModelEntry>{});
201224
std::unordered_set<std::string> updated_model_list;
202225
auto add_res = AddRepoSiblings(model_source, author, model_name);
203226
if (add_res.has_error()) {
@@ -206,8 +229,8 @@ cpp::result<bool, std::string> ModelSourceService::AddHfRepo(
206229
updated_model_list = add_res.value();
207230
}
208231
for (auto const& mid : model_list_before) {
209-
if (updated_model_list.find(mid) == updated_model_list.end()) {
210-
if (auto del_res = db_service_->DeleteModelEntry(mid);
232+
if (updated_model_list.find(mid.model) == updated_model_list.end()) {
233+
if (auto del_res = db_service_->DeleteModelEntry(mid.model);
211234
del_res.has_error()) {
212235
CTL_INF(del_res.error());
213236
}
@@ -331,16 +354,22 @@ cpp::result<bool, std::string> ModelSourceService::AddCortexsoRepo(
331354
if (repo_info.has_error()) {
332355
return cpp::fail(repo_info.error());
333356
}
357+
358+
auto readme = hu::GetReadMe(author, model_name);
359+
std::string desc;
360+
if (!readme.has_error()) {
361+
desc = readme.value();
362+
}
334363
// Get models from db
335364

336-
auto model_list_before =
337-
db_service_->GetModels(model_source).value_or(std::vector<std::string>{});
365+
auto model_list_before = db_service_->GetModels(model_source)
366+
.value_or(std::vector<cortex::db::ModelEntry>{});
338367
std::unordered_set<std::string> updated_model_list;
339368

340369
for (auto const& [branch, _] : branches.value()) {
341370
CTL_INF(branch);
342371
auto add_res = AddCortexsoRepoBranch(model_source, author, model_name,
343-
branch, repo_info->metadata)
372+
branch, repo_info->metadata, desc)
344373
.value_or(std::unordered_set<std::string>{});
345374
for (auto const& a : add_res) {
346375
updated_model_list.insert(a);
@@ -349,8 +378,8 @@ cpp::result<bool, std::string> ModelSourceService::AddCortexsoRepo(
349378

350379
// Clean up
351380
for (auto const& mid : model_list_before) {
352-
if (updated_model_list.find(mid) == updated_model_list.end()) {
353-
if (auto del_res = db_service_->DeleteModelEntry(mid);
381+
if (updated_model_list.find(mid.model) == updated_model_list.end()) {
382+
if (auto del_res = db_service_->DeleteModelEntry(mid.model);
354383
del_res.has_error()) {
355384
CTL_INF(del_res.error());
356385
}
@@ -364,7 +393,8 @@ ModelSourceService::AddCortexsoRepoBranch(const std::string& model_source,
364393
const std::string& author,
365394
const std::string& model_name,
366395
const std::string& branch,
367-
const std::string& metadata) {
396+
const std::string& metadata,
397+
const std::string& desc) {
368398
std::unordered_set<std::string> res;
369399

370400
url_parser::Url url = {
@@ -393,6 +423,7 @@ ModelSourceService::AddCortexsoRepoBranch(const std::string& model_source,
393423
} else {
394424
auto meta_json = json_helper::ParseJsonString(metadata);
395425
meta_json["size"] = model_size;
426+
meta_json["description"] = desc;
396427
std::string model_id = model_name + ":" + branch;
397428
cortex::db::ModelEntry e = {
398429
.model = model_id,

engine/services/model_source_service.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class ModelSourceService {
4848
cpp::result<std::unordered_map<std::string, ModelSource>, std::string>
4949
GetModelSources();
5050

51+
cpp::result<ModelSource, std::string> GetModelSource(const std::string& src);
52+
5153
private:
5254
cpp::result<bool, std::string> AddHfOrg(const std::string& model_source,
5355
const std::string& author);
@@ -71,7 +73,7 @@ class ModelSourceService {
7173
AddCortexsoRepoBranch(const std::string& model_source,
7274
const std::string& author,
7375
const std::string& model_name,
74-
const std::string& branch, const std::string& metadata);
76+
const std::string& branch, const std::string& metadata, const std::string& desc);
7577

7678
void SyncModelSource();
7779

0 commit comments

Comments
 (0)