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

Commit 752c2c5

Browse files
fix: add filter by tag for repository list (#2021)
Co-authored-by: sangjanai <sang@jan.ai>
1 parent 845d3de commit 752c2c5

File tree

4 files changed

+46
-29
lines changed

4 files changed

+46
-29
lines changed

engine/controllers/models.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -859,10 +859,11 @@ void Models::GetModelSource(
859859
void Models::GetRepositoryList(
860860
const HttpRequestPtr& req,
861861
std::function<void(const HttpResponsePtr&)>&& callback,
862-
std::optional<std::string> author) {
862+
std::optional<std::string> author, std::optional<std::string> tag) {
863863
if (!author.has_value())
864864
author = "cortexso";
865-
auto res = model_src_svc_->GetRepositoryList(author.value());
865+
auto res =
866+
model_src_svc_->GetRepositoryList(author.value(), tag.value_or(""));
866867
if (res.has_error()) {
867868
Json::Value ret;
868869
ret["message"] = res.error();

engine/controllers/models.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ class Models : public drogon::HttpController<Models, false> {
4444
ADD_METHOD_TO(Models::DeleteModelSource, "/v1/models/sources", Delete);
4545
ADD_METHOD_TO(Models::GetModelSources, "/v1/models/sources", Get);
4646
ADD_METHOD_TO(Models::GetModelSource, "/v1/models/sources/{src}", Get);
47-
ADD_METHOD_TO(Models::GetRepositoryList, "/v1/models/hub?author={author}",
48-
Get);
47+
ADD_METHOD_TO(Models::GetRepositoryList,
48+
"/v1/models/hub?author={author}&tag={tag}", Get);
4949
METHOD_LIST_END
5050

5151
explicit Models(std::shared_ptr<DatabaseService> db_service,
@@ -115,7 +115,8 @@ class Models : public drogon::HttpController<Models, false> {
115115

116116
void GetRepositoryList(const HttpRequestPtr& req,
117117
std::function<void(const HttpResponsePtr&)>&& callback,
118-
std::optional<std::string> author);
118+
std::optional<std::string> author,
119+
std::optional<std::string> tag);
119120

120121
private:
121122
std::shared_ptr<DatabaseService> db_service_;

engine/services/model_source_service.cc

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,6 @@
1414
namespace hu = huggingface_utils;
1515

1616
namespace {
17-
struct ModelInfo {
18-
std::string id;
19-
int likes;
20-
int trending_score;
21-
bool is_private;
22-
int downloads;
23-
std::vector<std::string> tags;
24-
std::string created_at;
25-
std::string model_id;
26-
};
27-
2817
std::vector<ModelInfo> ParseJsonString(const std::string& json_str) {
2918
std::vector<ModelInfo> models;
3019

@@ -201,20 +190,37 @@ cpp::result<ModelSource, std::string> ModelSourceService::GetModelSource(
201190
}
202191

203192
cpp::result<std::vector<std::string>, std::string>
204-
ModelSourceService::GetRepositoryList(std::string_view author) {
193+
ModelSourceService::GetRepositoryList(std::string_view author,
194+
std::string_view tag_filter) {
205195
std::string as(author);
196+
auto get_repo_list = [this, &as, &tag_filter] {
197+
std::vector<std::string> repo_list;
198+
auto const& mis = cortexso_repos_.at(as);
199+
for (auto const& mi : mis) {
200+
if (!tag_filter.empty()) {
201+
if (std::count(mi.tags.begin(), mi.tags.end(), tag_filter)) {
202+
repo_list.push_back(mi.id);
203+
}
204+
} else {
205+
repo_list.push_back(mi.id);
206+
}
207+
}
208+
return repo_list;
209+
};
206210
if (cortexso_repos_.find(as) != cortexso_repos_.end() &&
207-
!cortexso_repos_.at(as).empty())
208-
return cortexso_repos_.at(as);
211+
!cortexso_repos_.at(as).empty()) {
212+
return get_repo_list();
213+
}
214+
209215
const auto begin = std::chrono::high_resolution_clock::now();
210216
auto res =
211217
curl_utils::SimpleGet("https://huggingface.co/api/models?author=" + as);
212218
if (res.has_value()) {
213219
auto repos = ParseJsonString(res.value());
214220
for (auto& r : repos) {
215-
cortexso_repos_[as].push_back(r.id);
221+
cortexso_repos_[as].push_back(r);
216222
}
217-
return cortexso_repos_.at(as);
223+
return get_repo_list();
218224
} else {
219225
return cpp::fail(res.error());
220226
}

engine/services/model_source_service.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ struct ModelSource {
3737
};
3838
};
3939

40+
struct ModelInfo {
41+
std::string id;
42+
int likes;
43+
int trending_score;
44+
bool is_private;
45+
int downloads;
46+
std::vector<std::string> tags;
47+
std::string created_at;
48+
std::string model_id;
49+
};
50+
4051
class ModelSourceService {
4152
public:
4253
explicit ModelSourceService(std::shared_ptr<DatabaseService> db_service);
@@ -54,7 +65,7 @@ class ModelSourceService {
5465
cpp::result<ModelSource, std::string> GetModelSource(const std::string& src);
5566

5667
cpp::result<std::vector<std::string>, std::string> GetRepositoryList(
57-
std::string_view author);
68+
std::string_view author, std::string_view tag_filter);
5869

5970
private:
6071
cpp::result<bool, std::string> AddHfOrg(const std::string& model_source,
@@ -75,12 +86,10 @@ class ModelSourceService {
7586
const std::string& model_source, const std::string& author,
7687
const std::string& model_name);
7788

78-
cpp::result<std::string, std::string>
79-
AddCortexsoRepoBranch(const std::string& model_source,
80-
const std::string& author,
81-
const std::string& model_name,
82-
const std::string& branch, const std::string& metadata,
83-
const std::string& desc);
89+
cpp::result<std::string, std::string> AddCortexsoRepoBranch(
90+
const std::string& model_source, const std::string& author,
91+
const std::string& model_name, const std::string& branch,
92+
const std::string& metadata, const std::string& desc);
8493

8594
void SyncModelSource();
8695

@@ -89,5 +98,5 @@ class ModelSourceService {
8998
std::thread sync_db_thread_;
9099
std::atomic<bool> running_;
91100

92-
std::unordered_map<std::string, std::vector<std::string>> cortexso_repos_;
101+
std::unordered_map<std::string, std::vector<ModelInfo>> cortexso_repos_;
93102
};

0 commit comments

Comments
 (0)