Thomas G. Lopes commited on
Commit
078734b
·
1 Parent(s): 6b7aed6

fetch all models

Browse files
Files changed (1) hide show
  1. src/routes/api/models/+server.ts +56 -19
src/routes/api/models/+server.ts CHANGED
@@ -57,14 +57,14 @@ interface ApiQueryParams {
57
  pipeline_tag?: "text-generation" | "image-text-to-text";
58
  filter: string;
59
  inference_provider: string;
60
- limit: number;
 
61
  expand: string[];
62
  }
63
 
64
  const queryParams: ApiQueryParams = {
65
  filter: "conversational",
66
  inference_provider: "all",
67
- limit: 100,
68
  expand: ["inferenceProviderMapping", "config", "library_name", "pipeline_tag", "tags", "mask_token", "trendingScore"],
69
  };
70
 
@@ -75,7 +75,7 @@ function buildApiUrl(params: ApiQueryParams): string {
75
 
76
  // Add simple params
77
  Object.entries(params).forEach(([key, value]) => {
78
- if (!Array.isArray(value)) {
79
  url.searchParams.append(key, String(value));
80
  }
81
  });
@@ -88,6 +88,44 @@ function buildApiUrl(params: ApiQueryParams): string {
88
  return url.toString();
89
  }
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  export type ApiModelsResponse = {
92
  models: Model[];
93
  };
@@ -134,31 +172,30 @@ export const GET: RequestHandler = async ({ fetch }) => {
134
  let imgText2TextModels: Model[] = [];
135
 
136
  // Make the needed API calls in parallel
137
- const apiPromises: Promise<Response | void>[] = [];
138
  if (needTextGenFetch) {
139
- const url = buildApiUrl({ ...queryParams, pipeline_tag: "text-generation" });
140
  apiPromises.push(
141
- fetch(url, requestInit).then(async response => {
142
- if (!response.ok) {
143
- console.error(`Error fetching text-generation models`, response.status, response.statusText);
 
 
 
144
  newFailedApiCalls.textGeneration = true;
145
- } else {
146
- textGenModels = await response.json();
147
- }
148
- }),
149
  );
150
  }
151
 
152
  if (needImgTextFetch) {
153
  apiPromises.push(
154
- fetch(buildApiUrl({ ...queryParams, pipeline_tag: "image-text-to-text" }), requestInit).then(async response => {
155
- if (!response.ok) {
156
- console.error(`Error fetching image-text-to-text models`, response.status, response.statusText);
 
 
 
157
  newFailedApiCalls.imageTextToText = true;
158
- } else {
159
- imgText2TextModels = await response.json();
160
- }
161
- }),
162
  );
163
  }
164
 
 
57
  pipeline_tag?: "text-generation" | "image-text-to-text";
58
  filter: string;
59
  inference_provider: string;
60
+ limit?: number;
61
+ skip?: number;
62
  expand: string[];
63
  }
64
 
65
  const queryParams: ApiQueryParams = {
66
  filter: "conversational",
67
  inference_provider: "all",
 
68
  expand: ["inferenceProviderMapping", "config", "library_name", "pipeline_tag", "tags", "mask_token", "trendingScore"],
69
  };
70
 
 
75
 
76
  // Add simple params
77
  Object.entries(params).forEach(([key, value]) => {
78
+ if (!Array.isArray(value) && value !== undefined) {
79
  url.searchParams.append(key, String(value));
80
  }
81
  });
 
88
  return url.toString();
89
  }
90
 
91
+ async function fetchAllModelsWithPagination(
92
+ pipeline_tag: "text-generation" | "image-text-to-text",
93
+ fetch: typeof globalThis.fetch,
94
+ ): Promise<Model[]> {
95
+ const allModels: Model[] = [];
96
+ let skip = 0;
97
+ const batchSize = 1000;
98
+
99
+ while (true) {
100
+ const url = buildApiUrl({
101
+ ...queryParams,
102
+ pipeline_tag,
103
+ limit: batchSize,
104
+ skip,
105
+ });
106
+
107
+ const response = await fetch(url, requestInit);
108
+
109
+ if (!response.ok) {
110
+ break;
111
+ }
112
+
113
+ const models: Model[] = await response.json();
114
+
115
+ if (models.length === 0) {
116
+ break; // No more models to fetch
117
+ }
118
+
119
+ allModels.push(...models);
120
+ skip += batchSize;
121
+
122
+ // Optional: Add a small delay to be respectful to the API
123
+ await new Promise(resolve => setTimeout(resolve, 100));
124
+ }
125
+
126
+ return allModels;
127
+ }
128
+
129
  export type ApiModelsResponse = {
130
  models: Model[];
131
  };
 
172
  let imgText2TextModels: Model[] = [];
173
 
174
  // Make the needed API calls in parallel
175
+ const apiPromises: Promise<void>[] = [];
176
  if (needTextGenFetch) {
 
177
  apiPromises.push(
178
+ fetchAllModelsWithPagination("text-generation", fetch)
179
+ .then(models => {
180
+ textGenModels = models;
181
+ })
182
+ .catch(error => {
183
+ console.error(`Error fetching text-generation models:`, error);
184
  newFailedApiCalls.textGeneration = true;
185
+ }),
 
 
 
186
  );
187
  }
188
 
189
  if (needImgTextFetch) {
190
  apiPromises.push(
191
+ fetchAllModelsWithPagination("image-text-to-text", fetch)
192
+ .then(models => {
193
+ imgText2TextModels = models;
194
+ })
195
+ .catch(error => {
196
+ console.error(`Error fetching image-text-to-text models:`, error);
197
  newFailedApiCalls.imageTextToText = true;
198
+ }),
 
 
 
199
  );
200
  }
201