@huggingface/inference 3.4.0 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/index.cjs +1081 -66
- package/dist/index.js +1092 -66
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/lib/makeRequestOptions.d.ts.map +1 -1
- package/dist/src/providers/cerebras.d.ts +19 -0
- package/dist/src/providers/cerebras.d.ts.map +1 -0
- package/dist/src/providers/consts.d.ts.map +1 -1
- package/dist/src/providers/openai.d.ts +6 -0
- package/dist/src/providers/openai.d.ts.map +1 -0
- package/dist/src/snippets/curl.d.ts +17 -0
- package/dist/src/snippets/curl.d.ts.map +1 -0
- package/dist/src/snippets/index.d.ts +5 -0
- package/dist/src/snippets/index.d.ts.map +1 -0
- package/dist/src/snippets/js.d.ts +21 -0
- package/dist/src/snippets/js.d.ts.map +1 -0
- package/dist/src/snippets/python.d.ts +23 -0
- package/dist/src/snippets/python.d.ts.map +1 -0
- package/dist/src/types.d.ts +4 -3
- package/dist/src/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +3 -0
- package/src/lib/makeRequestOptions.ts +40 -13
- package/src/providers/cerebras.ts +41 -0
- package/src/providers/consts.ts +2 -0
- package/src/providers/openai.ts +35 -0
- package/src/snippets/curl.ts +177 -0
- package/src/snippets/index.ts +5 -0
- package/src/snippets/js.ts +475 -0
- package/src/snippets/python.ts +487 -0
- package/src/types.ts +4 -2
package/dist/index.js
CHANGED
|
@@ -67,8 +67,8 @@ var BLACK_FOREST_LABS_CONFIG = {
|
|
|
67
67
|
makeUrl
|
|
68
68
|
};
|
|
69
69
|
|
|
70
|
-
// src/providers/
|
|
71
|
-
var
|
|
70
|
+
// src/providers/cerebras.ts
|
|
71
|
+
var CEREBRAS_API_BASE_URL = "https://api.cerebras.ai";
|
|
72
72
|
var makeBody2 = (params) => {
|
|
73
73
|
return {
|
|
74
74
|
...params.args,
|
|
@@ -79,47 +79,68 @@ var makeHeaders2 = (params) => {
|
|
|
79
79
|
return { Authorization: `Bearer ${params.accessToken}` };
|
|
80
80
|
};
|
|
81
81
|
var makeUrl2 = (params) => {
|
|
82
|
-
return `${params.baseUrl}/
|
|
82
|
+
return `${params.baseUrl}/v1/chat/completions`;
|
|
83
83
|
};
|
|
84
|
-
var
|
|
85
|
-
baseUrl:
|
|
84
|
+
var CEREBRAS_CONFIG = {
|
|
85
|
+
baseUrl: CEREBRAS_API_BASE_URL,
|
|
86
86
|
makeBody: makeBody2,
|
|
87
87
|
makeHeaders: makeHeaders2,
|
|
88
88
|
makeUrl: makeUrl2
|
|
89
89
|
};
|
|
90
90
|
|
|
91
|
+
// src/providers/cohere.ts
|
|
92
|
+
var COHERE_API_BASE_URL = "https://api.cohere.com";
|
|
93
|
+
var makeBody3 = (params) => {
|
|
94
|
+
return {
|
|
95
|
+
...params.args,
|
|
96
|
+
model: params.model
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
var makeHeaders3 = (params) => {
|
|
100
|
+
return { Authorization: `Bearer ${params.accessToken}` };
|
|
101
|
+
};
|
|
102
|
+
var makeUrl3 = (params) => {
|
|
103
|
+
return `${params.baseUrl}/compatibility/v1/chat/completions`;
|
|
104
|
+
};
|
|
105
|
+
var COHERE_CONFIG = {
|
|
106
|
+
baseUrl: COHERE_API_BASE_URL,
|
|
107
|
+
makeBody: makeBody3,
|
|
108
|
+
makeHeaders: makeHeaders3,
|
|
109
|
+
makeUrl: makeUrl3
|
|
110
|
+
};
|
|
111
|
+
|
|
91
112
|
// src/providers/fal-ai.ts
|
|
92
113
|
var FAL_AI_API_BASE_URL = "https://fal.run";
|
|
93
|
-
var
|
|
114
|
+
var makeBody4 = (params) => {
|
|
94
115
|
return params.args;
|
|
95
116
|
};
|
|
96
|
-
var
|
|
117
|
+
var makeHeaders4 = (params) => {
|
|
97
118
|
return {
|
|
98
119
|
Authorization: params.authMethod === "provider-key" ? `Key ${params.accessToken}` : `Bearer ${params.accessToken}`
|
|
99
120
|
};
|
|
100
121
|
};
|
|
101
|
-
var
|
|
122
|
+
var makeUrl4 = (params) => {
|
|
102
123
|
return `${params.baseUrl}/${params.model}`;
|
|
103
124
|
};
|
|
104
125
|
var FAL_AI_CONFIG = {
|
|
105
126
|
baseUrl: FAL_AI_API_BASE_URL,
|
|
106
|
-
makeBody:
|
|
107
|
-
makeHeaders:
|
|
108
|
-
makeUrl:
|
|
127
|
+
makeBody: makeBody4,
|
|
128
|
+
makeHeaders: makeHeaders4,
|
|
129
|
+
makeUrl: makeUrl4
|
|
109
130
|
};
|
|
110
131
|
|
|
111
132
|
// src/providers/fireworks-ai.ts
|
|
112
133
|
var FIREWORKS_AI_API_BASE_URL = "https://api.fireworks.ai/inference";
|
|
113
|
-
var
|
|
134
|
+
var makeBody5 = (params) => {
|
|
114
135
|
return {
|
|
115
136
|
...params.args,
|
|
116
137
|
...params.chatCompletion ? { model: params.model } : void 0
|
|
117
138
|
};
|
|
118
139
|
};
|
|
119
|
-
var
|
|
140
|
+
var makeHeaders5 = (params) => {
|
|
120
141
|
return { Authorization: `Bearer ${params.accessToken}` };
|
|
121
142
|
};
|
|
122
|
-
var
|
|
143
|
+
var makeUrl5 = (params) => {
|
|
123
144
|
if (params.task === "text-generation" && params.chatCompletion) {
|
|
124
145
|
return `${params.baseUrl}/v1/chat/completions`;
|
|
125
146
|
}
|
|
@@ -127,22 +148,22 @@ var makeUrl4 = (params) => {
|
|
|
127
148
|
};
|
|
128
149
|
var FIREWORKS_AI_CONFIG = {
|
|
129
150
|
baseUrl: FIREWORKS_AI_API_BASE_URL,
|
|
130
|
-
makeBody:
|
|
131
|
-
makeHeaders:
|
|
132
|
-
makeUrl:
|
|
151
|
+
makeBody: makeBody5,
|
|
152
|
+
makeHeaders: makeHeaders5,
|
|
153
|
+
makeUrl: makeUrl5
|
|
133
154
|
};
|
|
134
155
|
|
|
135
156
|
// src/providers/hf-inference.ts
|
|
136
|
-
var
|
|
157
|
+
var makeBody6 = (params) => {
|
|
137
158
|
return {
|
|
138
159
|
...params.args,
|
|
139
160
|
...params.chatCompletion ? { model: params.model } : void 0
|
|
140
161
|
};
|
|
141
162
|
};
|
|
142
|
-
var
|
|
163
|
+
var makeHeaders6 = (params) => {
|
|
143
164
|
return { Authorization: `Bearer ${params.accessToken}` };
|
|
144
165
|
};
|
|
145
|
-
var
|
|
166
|
+
var makeUrl6 = (params) => {
|
|
146
167
|
if (params.task && ["feature-extraction", "sentence-similarity"].includes(params.task)) {
|
|
147
168
|
return `${params.baseUrl}/pipeline/${params.task}/${params.model}`;
|
|
148
169
|
}
|
|
@@ -153,23 +174,23 @@ var makeUrl5 = (params) => {
|
|
|
153
174
|
};
|
|
154
175
|
var HF_INFERENCE_CONFIG = {
|
|
155
176
|
baseUrl: `${HF_ROUTER_URL}/hf-inference`,
|
|
156
|
-
makeBody:
|
|
157
|
-
makeHeaders:
|
|
158
|
-
makeUrl:
|
|
177
|
+
makeBody: makeBody6,
|
|
178
|
+
makeHeaders: makeHeaders6,
|
|
179
|
+
makeUrl: makeUrl6
|
|
159
180
|
};
|
|
160
181
|
|
|
161
182
|
// src/providers/hyperbolic.ts
|
|
162
183
|
var HYPERBOLIC_API_BASE_URL = "https://api.hyperbolic.xyz";
|
|
163
|
-
var
|
|
184
|
+
var makeBody7 = (params) => {
|
|
164
185
|
return {
|
|
165
186
|
...params.args,
|
|
166
187
|
...params.task === "text-to-image" ? { model_name: params.model } : { model: params.model }
|
|
167
188
|
};
|
|
168
189
|
};
|
|
169
|
-
var
|
|
190
|
+
var makeHeaders7 = (params) => {
|
|
170
191
|
return { Authorization: `Bearer ${params.accessToken}` };
|
|
171
192
|
};
|
|
172
|
-
var
|
|
193
|
+
var makeUrl7 = (params) => {
|
|
173
194
|
if (params.task === "text-to-image") {
|
|
174
195
|
return `${params.baseUrl}/v1/images/generations`;
|
|
175
196
|
}
|
|
@@ -177,23 +198,23 @@ var makeUrl6 = (params) => {
|
|
|
177
198
|
};
|
|
178
199
|
var HYPERBOLIC_CONFIG = {
|
|
179
200
|
baseUrl: HYPERBOLIC_API_BASE_URL,
|
|
180
|
-
makeBody:
|
|
181
|
-
makeHeaders:
|
|
182
|
-
makeUrl:
|
|
201
|
+
makeBody: makeBody7,
|
|
202
|
+
makeHeaders: makeHeaders7,
|
|
203
|
+
makeUrl: makeUrl7
|
|
183
204
|
};
|
|
184
205
|
|
|
185
206
|
// src/providers/nebius.ts
|
|
186
207
|
var NEBIUS_API_BASE_URL = "https://api.studio.nebius.ai";
|
|
187
|
-
var
|
|
208
|
+
var makeBody8 = (params) => {
|
|
188
209
|
return {
|
|
189
210
|
...params.args,
|
|
190
211
|
model: params.model
|
|
191
212
|
};
|
|
192
213
|
};
|
|
193
|
-
var
|
|
214
|
+
var makeHeaders8 = (params) => {
|
|
194
215
|
return { Authorization: `Bearer ${params.accessToken}` };
|
|
195
216
|
};
|
|
196
|
-
var
|
|
217
|
+
var makeUrl8 = (params) => {
|
|
197
218
|
if (params.task === "text-to-image") {
|
|
198
219
|
return `${params.baseUrl}/v1/images/generations`;
|
|
199
220
|
}
|
|
@@ -207,23 +228,23 @@ var makeUrl7 = (params) => {
|
|
|
207
228
|
};
|
|
208
229
|
var NEBIUS_CONFIG = {
|
|
209
230
|
baseUrl: NEBIUS_API_BASE_URL,
|
|
210
|
-
makeBody:
|
|
211
|
-
makeHeaders:
|
|
212
|
-
makeUrl:
|
|
231
|
+
makeBody: makeBody8,
|
|
232
|
+
makeHeaders: makeHeaders8,
|
|
233
|
+
makeUrl: makeUrl8
|
|
213
234
|
};
|
|
214
235
|
|
|
215
236
|
// src/providers/novita.ts
|
|
216
237
|
var NOVITA_API_BASE_URL = "https://api.novita.ai/v3/openai";
|
|
217
|
-
var
|
|
238
|
+
var makeBody9 = (params) => {
|
|
218
239
|
return {
|
|
219
240
|
...params.args,
|
|
220
241
|
...params.chatCompletion ? { model: params.model } : void 0
|
|
221
242
|
};
|
|
222
243
|
};
|
|
223
|
-
var
|
|
244
|
+
var makeHeaders9 = (params) => {
|
|
224
245
|
return { Authorization: `Bearer ${params.accessToken}` };
|
|
225
246
|
};
|
|
226
|
-
var
|
|
247
|
+
var makeUrl9 = (params) => {
|
|
227
248
|
if (params.task === "text-generation") {
|
|
228
249
|
if (params.chatCompletion) {
|
|
229
250
|
return `${params.baseUrl}/chat/completions`;
|
|
@@ -234,23 +255,23 @@ var makeUrl8 = (params) => {
|
|
|
234
255
|
};
|
|
235
256
|
var NOVITA_CONFIG = {
|
|
236
257
|
baseUrl: NOVITA_API_BASE_URL,
|
|
237
|
-
makeBody:
|
|
238
|
-
makeHeaders:
|
|
239
|
-
makeUrl:
|
|
258
|
+
makeBody: makeBody9,
|
|
259
|
+
makeHeaders: makeHeaders9,
|
|
260
|
+
makeUrl: makeUrl9
|
|
240
261
|
};
|
|
241
262
|
|
|
242
263
|
// src/providers/replicate.ts
|
|
243
264
|
var REPLICATE_API_BASE_URL = "https://api.replicate.com";
|
|
244
|
-
var
|
|
265
|
+
var makeBody10 = (params) => {
|
|
245
266
|
return {
|
|
246
267
|
input: params.args,
|
|
247
268
|
version: params.model.includes(":") ? params.model.split(":")[1] : void 0
|
|
248
269
|
};
|
|
249
270
|
};
|
|
250
|
-
var
|
|
271
|
+
var makeHeaders10 = (params) => {
|
|
251
272
|
return { Authorization: `Bearer ${params.accessToken}` };
|
|
252
273
|
};
|
|
253
|
-
var
|
|
274
|
+
var makeUrl10 = (params) => {
|
|
254
275
|
if (params.model.includes(":")) {
|
|
255
276
|
return `${params.baseUrl}/v1/predictions`;
|
|
256
277
|
}
|
|
@@ -258,23 +279,23 @@ var makeUrl9 = (params) => {
|
|
|
258
279
|
};
|
|
259
280
|
var REPLICATE_CONFIG = {
|
|
260
281
|
baseUrl: REPLICATE_API_BASE_URL,
|
|
261
|
-
makeBody:
|
|
262
|
-
makeHeaders:
|
|
263
|
-
makeUrl:
|
|
282
|
+
makeBody: makeBody10,
|
|
283
|
+
makeHeaders: makeHeaders10,
|
|
284
|
+
makeUrl: makeUrl10
|
|
264
285
|
};
|
|
265
286
|
|
|
266
287
|
// src/providers/sambanova.ts
|
|
267
288
|
var SAMBANOVA_API_BASE_URL = "https://api.sambanova.ai";
|
|
268
|
-
var
|
|
289
|
+
var makeBody11 = (params) => {
|
|
269
290
|
return {
|
|
270
291
|
...params.args,
|
|
271
292
|
...params.chatCompletion ? { model: params.model } : void 0
|
|
272
293
|
};
|
|
273
294
|
};
|
|
274
|
-
var
|
|
295
|
+
var makeHeaders11 = (params) => {
|
|
275
296
|
return { Authorization: `Bearer ${params.accessToken}` };
|
|
276
297
|
};
|
|
277
|
-
var
|
|
298
|
+
var makeUrl11 = (params) => {
|
|
278
299
|
if (params.task === "text-generation" && params.chatCompletion) {
|
|
279
300
|
return `${params.baseUrl}/v1/chat/completions`;
|
|
280
301
|
}
|
|
@@ -282,23 +303,23 @@ var makeUrl10 = (params) => {
|
|
|
282
303
|
};
|
|
283
304
|
var SAMBANOVA_CONFIG = {
|
|
284
305
|
baseUrl: SAMBANOVA_API_BASE_URL,
|
|
285
|
-
makeBody:
|
|
286
|
-
makeHeaders:
|
|
287
|
-
makeUrl:
|
|
306
|
+
makeBody: makeBody11,
|
|
307
|
+
makeHeaders: makeHeaders11,
|
|
308
|
+
makeUrl: makeUrl11
|
|
288
309
|
};
|
|
289
310
|
|
|
290
311
|
// src/providers/together.ts
|
|
291
312
|
var TOGETHER_API_BASE_URL = "https://api.together.xyz";
|
|
292
|
-
var
|
|
313
|
+
var makeBody12 = (params) => {
|
|
293
314
|
return {
|
|
294
315
|
...params.args,
|
|
295
316
|
model: params.model
|
|
296
317
|
};
|
|
297
318
|
};
|
|
298
|
-
var
|
|
319
|
+
var makeHeaders12 = (params) => {
|
|
299
320
|
return { Authorization: `Bearer ${params.accessToken}` };
|
|
300
321
|
};
|
|
301
|
-
var
|
|
322
|
+
var makeUrl12 = (params) => {
|
|
302
323
|
if (params.task === "text-to-image") {
|
|
303
324
|
return `${params.baseUrl}/v1/images/generations`;
|
|
304
325
|
}
|
|
@@ -312,9 +333,37 @@ var makeUrl11 = (params) => {
|
|
|
312
333
|
};
|
|
313
334
|
var TOGETHER_CONFIG = {
|
|
314
335
|
baseUrl: TOGETHER_API_BASE_URL,
|
|
315
|
-
makeBody:
|
|
316
|
-
makeHeaders:
|
|
317
|
-
makeUrl:
|
|
336
|
+
makeBody: makeBody12,
|
|
337
|
+
makeHeaders: makeHeaders12,
|
|
338
|
+
makeUrl: makeUrl12
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
// src/providers/openai.ts
|
|
342
|
+
var OPENAI_API_BASE_URL = "https://api.openai.com";
|
|
343
|
+
var makeBody13 = (params) => {
|
|
344
|
+
if (!params.chatCompletion) {
|
|
345
|
+
throw new Error("OpenAI only supports chat completions.");
|
|
346
|
+
}
|
|
347
|
+
return {
|
|
348
|
+
...params.args,
|
|
349
|
+
model: params.model
|
|
350
|
+
};
|
|
351
|
+
};
|
|
352
|
+
var makeHeaders13 = (params) => {
|
|
353
|
+
return { Authorization: `Bearer ${params.accessToken}` };
|
|
354
|
+
};
|
|
355
|
+
var makeUrl13 = (params) => {
|
|
356
|
+
if (!params.chatCompletion) {
|
|
357
|
+
throw new Error("OpenAI only supports chat completions.");
|
|
358
|
+
}
|
|
359
|
+
return `${params.baseUrl}/v1/chat/completions`;
|
|
360
|
+
};
|
|
361
|
+
var OPENAI_CONFIG = {
|
|
362
|
+
baseUrl: OPENAI_API_BASE_URL,
|
|
363
|
+
makeBody: makeBody13,
|
|
364
|
+
makeHeaders: makeHeaders13,
|
|
365
|
+
makeUrl: makeUrl13,
|
|
366
|
+
clientSideRoutingOnly: true
|
|
318
367
|
};
|
|
319
368
|
|
|
320
369
|
// src/lib/isUrl.ts
|
|
@@ -324,7 +373,7 @@ function isUrl(modelOrUrl) {
|
|
|
324
373
|
|
|
325
374
|
// package.json
|
|
326
375
|
var name = "@huggingface/inference";
|
|
327
|
-
var version = "3.
|
|
376
|
+
var version = "3.5.0";
|
|
328
377
|
|
|
329
378
|
// src/providers/consts.ts
|
|
330
379
|
var HARDCODED_MODEL_ID_MAPPING = {
|
|
@@ -335,6 +384,7 @@ var HARDCODED_MODEL_ID_MAPPING = {
|
|
|
335
384
|
* "Qwen/Qwen2.5-Coder-32B-Instruct": "Qwen2.5-Coder-32B-Instruct",
|
|
336
385
|
*/
|
|
337
386
|
"black-forest-labs": {},
|
|
387
|
+
cerebras: {},
|
|
338
388
|
cohere: {},
|
|
339
389
|
"fal-ai": {},
|
|
340
390
|
"fireworks-ai": {},
|
|
@@ -342,6 +392,7 @@ var HARDCODED_MODEL_ID_MAPPING = {
|
|
|
342
392
|
hyperbolic: {},
|
|
343
393
|
nebius: {},
|
|
344
394
|
novita: {},
|
|
395
|
+
openai: {},
|
|
345
396
|
replicate: {},
|
|
346
397
|
sambanova: {},
|
|
347
398
|
together: {}
|
|
@@ -396,11 +447,13 @@ var HF_HUB_INFERENCE_PROXY_TEMPLATE = `${HF_ROUTER_URL}/{{PROVIDER}}`;
|
|
|
396
447
|
var tasks = null;
|
|
397
448
|
var providerConfigs = {
|
|
398
449
|
"black-forest-labs": BLACK_FOREST_LABS_CONFIG,
|
|
450
|
+
cerebras: CEREBRAS_CONFIG,
|
|
399
451
|
cohere: COHERE_CONFIG,
|
|
400
452
|
"fal-ai": FAL_AI_CONFIG,
|
|
401
453
|
"fireworks-ai": FIREWORKS_AI_CONFIG,
|
|
402
454
|
"hf-inference": HF_INFERENCE_CONFIG,
|
|
403
455
|
hyperbolic: HYPERBOLIC_CONFIG,
|
|
456
|
+
openai: OPENAI_CONFIG,
|
|
404
457
|
nebius: NEBIUS_CONFIG,
|
|
405
458
|
novita: NOVITA_CONFIG,
|
|
406
459
|
replicate: REPLICATE_CONFIG,
|
|
@@ -424,13 +477,36 @@ async function makeRequestOptions(args, options) {
|
|
|
424
477
|
if (!providerConfig) {
|
|
425
478
|
throw new Error(`No provider config found for provider ${provider}`);
|
|
426
479
|
}
|
|
480
|
+
if (providerConfig.clientSideRoutingOnly && !maybeModel) {
|
|
481
|
+
throw new Error(`Provider ${provider} requires a model ID to be passed directly.`);
|
|
482
|
+
}
|
|
427
483
|
const hfModel = maybeModel ?? await loadDefaultModel(task);
|
|
428
|
-
const model =
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
484
|
+
const model = providerConfig.clientSideRoutingOnly ? (
|
|
485
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
486
|
+
removeProviderPrefix(maybeModel, provider)
|
|
487
|
+
) : (
|
|
488
|
+
// For closed-models API providers, one needs to pass the model ID directly (e.g. "gpt-3.5-turbo")
|
|
489
|
+
await getProviderModelId({ model: hfModel, provider }, args, {
|
|
490
|
+
task,
|
|
491
|
+
chatCompletion: chatCompletion2,
|
|
492
|
+
fetch: options?.fetch
|
|
493
|
+
})
|
|
494
|
+
);
|
|
495
|
+
const authMethod = (() => {
|
|
496
|
+
if (providerConfig.clientSideRoutingOnly) {
|
|
497
|
+
if (accessToken && accessToken.startsWith("hf_")) {
|
|
498
|
+
throw new Error(`Provider ${provider} is closed-source and does not support HF tokens.`);
|
|
499
|
+
}
|
|
500
|
+
return "provider-key";
|
|
501
|
+
}
|
|
502
|
+
if (accessToken) {
|
|
503
|
+
return accessToken.startsWith("hf_") ? "hf-token" : "provider-key";
|
|
504
|
+
}
|
|
505
|
+
if (includeCredentials === "include") {
|
|
506
|
+
return "credentials-include";
|
|
507
|
+
}
|
|
508
|
+
return "none";
|
|
509
|
+
})();
|
|
434
510
|
const url = endpointUrl ? chatCompletion2 ? endpointUrl + `/v1/chat/completions` : endpointUrl : providerConfig.makeUrl({
|
|
435
511
|
baseUrl: authMethod !== "provider-key" ? HF_HUB_INFERENCE_PROXY_TEMPLATE.replace("{{PROVIDER}}", provider) : providerConfig.baseUrl,
|
|
436
512
|
model,
|
|
@@ -488,6 +564,12 @@ async function loadTaskInfo() {
|
|
|
488
564
|
}
|
|
489
565
|
return await res.json();
|
|
490
566
|
}
|
|
567
|
+
function removeProviderPrefix(model, provider) {
|
|
568
|
+
if (!model.startsWith(`${provider}/`)) {
|
|
569
|
+
throw new Error(`Models from ${provider} must be prefixed by "${provider}/". Got "${model}".`);
|
|
570
|
+
}
|
|
571
|
+
return model.slice(provider.length + 1);
|
|
572
|
+
}
|
|
491
573
|
|
|
492
574
|
// src/tasks/custom/request.ts
|
|
493
575
|
async function request(args, options) {
|
|
@@ -1487,6 +1569,7 @@ var HfInferenceEndpoint = class {
|
|
|
1487
1569
|
// src/types.ts
|
|
1488
1570
|
var INFERENCE_PROVIDERS = [
|
|
1489
1571
|
"black-forest-labs",
|
|
1572
|
+
"cerebras",
|
|
1490
1573
|
"cohere",
|
|
1491
1574
|
"fal-ai",
|
|
1492
1575
|
"fireworks-ai",
|
|
@@ -1494,10 +1577,952 @@ var INFERENCE_PROVIDERS = [
|
|
|
1494
1577
|
"hyperbolic",
|
|
1495
1578
|
"nebius",
|
|
1496
1579
|
"novita",
|
|
1580
|
+
"openai",
|
|
1497
1581
|
"replicate",
|
|
1498
1582
|
"sambanova",
|
|
1499
1583
|
"together"
|
|
1500
1584
|
];
|
|
1585
|
+
|
|
1586
|
+
// src/snippets/index.ts
|
|
1587
|
+
var snippets_exports = {};
|
|
1588
|
+
__export(snippets_exports, {
|
|
1589
|
+
curl: () => curl_exports,
|
|
1590
|
+
js: () => js_exports,
|
|
1591
|
+
python: () => python_exports
|
|
1592
|
+
});
|
|
1593
|
+
|
|
1594
|
+
// src/snippets/curl.ts
|
|
1595
|
+
var curl_exports = {};
|
|
1596
|
+
__export(curl_exports, {
|
|
1597
|
+
curlSnippets: () => curlSnippets,
|
|
1598
|
+
getCurlInferenceSnippet: () => getCurlInferenceSnippet,
|
|
1599
|
+
snippetBasic: () => snippetBasic,
|
|
1600
|
+
snippetFile: () => snippetFile,
|
|
1601
|
+
snippetTextGeneration: () => snippetTextGeneration,
|
|
1602
|
+
snippetZeroShotClassification: () => snippetZeroShotClassification
|
|
1603
|
+
});
|
|
1604
|
+
import { HF_HUB_INFERENCE_PROXY_TEMPLATE as HF_HUB_INFERENCE_PROXY_TEMPLATE2 } from "@huggingface/tasks";
|
|
1605
|
+
import {
|
|
1606
|
+
getModelInputSnippet,
|
|
1607
|
+
stringifyGenerationConfig,
|
|
1608
|
+
stringifyMessages
|
|
1609
|
+
} from "@huggingface/tasks";
|
|
1610
|
+
var snippetBasic = (model, accessToken, provider) => {
|
|
1611
|
+
if (provider !== "hf-inference") {
|
|
1612
|
+
return [];
|
|
1613
|
+
}
|
|
1614
|
+
return [
|
|
1615
|
+
{
|
|
1616
|
+
client: "curl",
|
|
1617
|
+
content: `curl https://router.huggingface.co/hf-inference/models/${model.id} \\
|
|
1618
|
+
-X POST \\
|
|
1619
|
+
-d '{"inputs": ${getModelInputSnippet(model, true)}}' \\
|
|
1620
|
+
-H 'Content-Type: application/json' \\
|
|
1621
|
+
-H 'Authorization: Bearer ${accessToken || `{API_TOKEN}`}'`
|
|
1622
|
+
}
|
|
1623
|
+
];
|
|
1624
|
+
};
|
|
1625
|
+
var snippetTextGeneration = (model, accessToken, provider, providerModelId, opts) => {
|
|
1626
|
+
if (model.tags.includes("conversational")) {
|
|
1627
|
+
const baseUrl = provider === "hf-inference" ? `https://router.huggingface.co/hf-inference/models/${model.id}/v1/chat/completions` : HF_HUB_INFERENCE_PROXY_TEMPLATE2.replace("{{PROVIDER}}", provider) + "/v1/chat/completions";
|
|
1628
|
+
const modelId = providerModelId ?? model.id;
|
|
1629
|
+
const streaming = opts?.streaming ?? true;
|
|
1630
|
+
const exampleMessages = getModelInputSnippet(model);
|
|
1631
|
+
const messages = opts?.messages ?? exampleMessages;
|
|
1632
|
+
const config = {
|
|
1633
|
+
...opts?.temperature ? { temperature: opts.temperature } : void 0,
|
|
1634
|
+
max_tokens: opts?.max_tokens ?? 500,
|
|
1635
|
+
...opts?.top_p ? { top_p: opts.top_p } : void 0
|
|
1636
|
+
};
|
|
1637
|
+
return [
|
|
1638
|
+
{
|
|
1639
|
+
client: "curl",
|
|
1640
|
+
content: `curl '${baseUrl}' \\
|
|
1641
|
+
-H 'Authorization: Bearer ${accessToken || `{API_TOKEN}`}' \\
|
|
1642
|
+
-H 'Content-Type: application/json' \\
|
|
1643
|
+
--data '{
|
|
1644
|
+
"model": "${modelId}",
|
|
1645
|
+
"messages": ${stringifyMessages(messages, {
|
|
1646
|
+
indent: " ",
|
|
1647
|
+
attributeKeyQuotes: true,
|
|
1648
|
+
customContentEscaper: (str) => str.replace(/'/g, "'\\''")
|
|
1649
|
+
})},
|
|
1650
|
+
${stringifyGenerationConfig(config, {
|
|
1651
|
+
indent: "\n ",
|
|
1652
|
+
attributeKeyQuotes: true,
|
|
1653
|
+
attributeValueConnector: ": "
|
|
1654
|
+
})}
|
|
1655
|
+
"stream": ${!!streaming}
|
|
1656
|
+
}'`
|
|
1657
|
+
}
|
|
1658
|
+
];
|
|
1659
|
+
} else {
|
|
1660
|
+
return snippetBasic(model, accessToken, provider);
|
|
1661
|
+
}
|
|
1662
|
+
};
|
|
1663
|
+
var snippetZeroShotClassification = (model, accessToken, provider) => {
|
|
1664
|
+
if (provider !== "hf-inference") {
|
|
1665
|
+
return [];
|
|
1666
|
+
}
|
|
1667
|
+
return [
|
|
1668
|
+
{
|
|
1669
|
+
client: "curl",
|
|
1670
|
+
content: `curl https://router.huggingface.co/hf-inference/models/${model.id} \\
|
|
1671
|
+
-X POST \\
|
|
1672
|
+
-d '{"inputs": ${getModelInputSnippet(model, true)}, "parameters": {"candidate_labels": ["refund", "legal", "faq"]}}' \\
|
|
1673
|
+
-H 'Content-Type: application/json' \\
|
|
1674
|
+
-H 'Authorization: Bearer ${accessToken || `{API_TOKEN}`}'`
|
|
1675
|
+
}
|
|
1676
|
+
];
|
|
1677
|
+
};
|
|
1678
|
+
var snippetFile = (model, accessToken, provider) => {
|
|
1679
|
+
if (provider !== "hf-inference") {
|
|
1680
|
+
return [];
|
|
1681
|
+
}
|
|
1682
|
+
return [
|
|
1683
|
+
{
|
|
1684
|
+
client: "curl",
|
|
1685
|
+
content: `curl https://router.huggingface.co/hf-inference/models/${model.id} \\
|
|
1686
|
+
-X POST \\
|
|
1687
|
+
--data-binary '@${getModelInputSnippet(model, true, true)}' \\
|
|
1688
|
+
-H 'Authorization: Bearer ${accessToken || `{API_TOKEN}`}'`
|
|
1689
|
+
}
|
|
1690
|
+
];
|
|
1691
|
+
};
|
|
1692
|
+
var curlSnippets = {
|
|
1693
|
+
// Same order as in tasks/src/pipelines.ts
|
|
1694
|
+
"text-classification": snippetBasic,
|
|
1695
|
+
"token-classification": snippetBasic,
|
|
1696
|
+
"table-question-answering": snippetBasic,
|
|
1697
|
+
"question-answering": snippetBasic,
|
|
1698
|
+
"zero-shot-classification": snippetZeroShotClassification,
|
|
1699
|
+
translation: snippetBasic,
|
|
1700
|
+
summarization: snippetBasic,
|
|
1701
|
+
"feature-extraction": snippetBasic,
|
|
1702
|
+
"text-generation": snippetTextGeneration,
|
|
1703
|
+
"image-text-to-text": snippetTextGeneration,
|
|
1704
|
+
"text2text-generation": snippetBasic,
|
|
1705
|
+
"fill-mask": snippetBasic,
|
|
1706
|
+
"sentence-similarity": snippetBasic,
|
|
1707
|
+
"automatic-speech-recognition": snippetFile,
|
|
1708
|
+
"text-to-image": snippetBasic,
|
|
1709
|
+
"text-to-speech": snippetBasic,
|
|
1710
|
+
"text-to-audio": snippetBasic,
|
|
1711
|
+
"audio-to-audio": snippetFile,
|
|
1712
|
+
"audio-classification": snippetFile,
|
|
1713
|
+
"image-classification": snippetFile,
|
|
1714
|
+
"image-to-text": snippetFile,
|
|
1715
|
+
"object-detection": snippetFile,
|
|
1716
|
+
"image-segmentation": snippetFile
|
|
1717
|
+
};
|
|
1718
|
+
function getCurlInferenceSnippet(model, accessToken, provider, providerModelId, opts) {
|
|
1719
|
+
return model.pipeline_tag && model.pipeline_tag in curlSnippets ? curlSnippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId, opts) ?? [] : [];
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1722
|
+
// src/snippets/python.ts
|
|
1723
|
+
var python_exports = {};
|
|
1724
|
+
__export(python_exports, {
|
|
1725
|
+
getPythonInferenceSnippet: () => getPythonInferenceSnippet,
|
|
1726
|
+
pythonSnippets: () => pythonSnippets,
|
|
1727
|
+
snippetBasic: () => snippetBasic2,
|
|
1728
|
+
snippetConversational: () => snippetConversational,
|
|
1729
|
+
snippetDocumentQuestionAnswering: () => snippetDocumentQuestionAnswering,
|
|
1730
|
+
snippetFile: () => snippetFile2,
|
|
1731
|
+
snippetTabular: () => snippetTabular,
|
|
1732
|
+
snippetTextToAudio: () => snippetTextToAudio,
|
|
1733
|
+
snippetTextToImage: () => snippetTextToImage,
|
|
1734
|
+
snippetTextToVideo: () => snippetTextToVideo,
|
|
1735
|
+
snippetZeroShotClassification: () => snippetZeroShotClassification2,
|
|
1736
|
+
snippetZeroShotImageClassification: () => snippetZeroShotImageClassification
|
|
1737
|
+
});
|
|
1738
|
+
import { openAIbaseUrl } from "@huggingface/tasks";
|
|
1739
|
+
import {
|
|
1740
|
+
getModelInputSnippet as getModelInputSnippet2,
|
|
1741
|
+
stringifyGenerationConfig as stringifyGenerationConfig2,
|
|
1742
|
+
stringifyMessages as stringifyMessages2
|
|
1743
|
+
} from "@huggingface/tasks";
|
|
1744
|
+
var HFH_INFERENCE_CLIENT_METHODS = {
|
|
1745
|
+
"audio-classification": "audio_classification",
|
|
1746
|
+
"audio-to-audio": "audio_to_audio",
|
|
1747
|
+
"automatic-speech-recognition": "automatic_speech_recognition",
|
|
1748
|
+
"text-to-speech": "text_to_speech",
|
|
1749
|
+
"image-classification": "image_classification",
|
|
1750
|
+
"image-segmentation": "image_segmentation",
|
|
1751
|
+
"image-to-image": "image_to_image",
|
|
1752
|
+
"image-to-text": "image_to_text",
|
|
1753
|
+
"object-detection": "object_detection",
|
|
1754
|
+
"text-to-image": "text_to_image",
|
|
1755
|
+
"text-to-video": "text_to_video",
|
|
1756
|
+
"zero-shot-image-classification": "zero_shot_image_classification",
|
|
1757
|
+
"document-question-answering": "document_question_answering",
|
|
1758
|
+
"visual-question-answering": "visual_question_answering",
|
|
1759
|
+
"feature-extraction": "feature_extraction",
|
|
1760
|
+
"fill-mask": "fill_mask",
|
|
1761
|
+
"question-answering": "question_answering",
|
|
1762
|
+
"sentence-similarity": "sentence_similarity",
|
|
1763
|
+
summarization: "summarization",
|
|
1764
|
+
"table-question-answering": "table_question_answering",
|
|
1765
|
+
"text-classification": "text_classification",
|
|
1766
|
+
"text-generation": "text_generation",
|
|
1767
|
+
"token-classification": "token_classification",
|
|
1768
|
+
translation: "translation",
|
|
1769
|
+
"zero-shot-classification": "zero_shot_classification",
|
|
1770
|
+
"tabular-classification": "tabular_classification",
|
|
1771
|
+
"tabular-regression": "tabular_regression"
|
|
1772
|
+
};
|
|
1773
|
+
var snippetImportInferenceClient = (accessToken, provider) => `from huggingface_hub import InferenceClient
|
|
1774
|
+
|
|
1775
|
+
client = InferenceClient(
|
|
1776
|
+
provider="${provider}",
|
|
1777
|
+
api_key="${accessToken || "{API_TOKEN}"}"
|
|
1778
|
+
)`;
|
|
1779
|
+
var snippetConversational = (model, accessToken, provider, providerModelId, opts) => {
|
|
1780
|
+
const streaming = opts?.streaming ?? true;
|
|
1781
|
+
const exampleMessages = getModelInputSnippet2(model);
|
|
1782
|
+
const messages = opts?.messages ?? exampleMessages;
|
|
1783
|
+
const messagesStr = stringifyMessages2(messages, { attributeKeyQuotes: true });
|
|
1784
|
+
const config = {
|
|
1785
|
+
...opts?.temperature ? { temperature: opts.temperature } : void 0,
|
|
1786
|
+
max_tokens: opts?.max_tokens ?? 500,
|
|
1787
|
+
...opts?.top_p ? { top_p: opts.top_p } : void 0
|
|
1788
|
+
};
|
|
1789
|
+
const configStr = stringifyGenerationConfig2(config, {
|
|
1790
|
+
indent: "\n ",
|
|
1791
|
+
attributeValueConnector: "="
|
|
1792
|
+
});
|
|
1793
|
+
if (streaming) {
|
|
1794
|
+
return [
|
|
1795
|
+
{
|
|
1796
|
+
client: "huggingface_hub",
|
|
1797
|
+
content: `${snippetImportInferenceClient(accessToken, provider)}
|
|
1798
|
+
|
|
1799
|
+
messages = ${messagesStr}
|
|
1800
|
+
|
|
1801
|
+
stream = client.chat.completions.create(
|
|
1802
|
+
model="${model.id}",
|
|
1803
|
+
messages=messages,
|
|
1804
|
+
${configStr}
|
|
1805
|
+
stream=True
|
|
1806
|
+
)
|
|
1807
|
+
|
|
1808
|
+
for chunk in stream:
|
|
1809
|
+
print(chunk.choices[0].delta.content, end="")`
|
|
1810
|
+
},
|
|
1811
|
+
{
|
|
1812
|
+
client: "openai",
|
|
1813
|
+
content: `from openai import OpenAI
|
|
1814
|
+
|
|
1815
|
+
client = OpenAI(
|
|
1816
|
+
base_url="${openAIbaseUrl(provider)}",
|
|
1817
|
+
api_key="${accessToken || "{API_TOKEN}"}"
|
|
1818
|
+
)
|
|
1819
|
+
|
|
1820
|
+
messages = ${messagesStr}
|
|
1821
|
+
|
|
1822
|
+
stream = client.chat.completions.create(
|
|
1823
|
+
model="${providerModelId ?? model.id}",
|
|
1824
|
+
messages=messages,
|
|
1825
|
+
${configStr}
|
|
1826
|
+
stream=True
|
|
1827
|
+
)
|
|
1828
|
+
|
|
1829
|
+
for chunk in stream:
|
|
1830
|
+
print(chunk.choices[0].delta.content, end="")`
|
|
1831
|
+
}
|
|
1832
|
+
];
|
|
1833
|
+
} else {
|
|
1834
|
+
return [
|
|
1835
|
+
{
|
|
1836
|
+
client: "huggingface_hub",
|
|
1837
|
+
content: `${snippetImportInferenceClient(accessToken, provider)}
|
|
1838
|
+
|
|
1839
|
+
messages = ${messagesStr}
|
|
1840
|
+
|
|
1841
|
+
completion = client.chat.completions.create(
|
|
1842
|
+
model="${model.id}",
|
|
1843
|
+
messages=messages,
|
|
1844
|
+
${configStr}
|
|
1845
|
+
)
|
|
1846
|
+
|
|
1847
|
+
print(completion.choices[0].message)`
|
|
1848
|
+
},
|
|
1849
|
+
{
|
|
1850
|
+
client: "openai",
|
|
1851
|
+
content: `from openai import OpenAI
|
|
1852
|
+
|
|
1853
|
+
client = OpenAI(
|
|
1854
|
+
base_url="${openAIbaseUrl(provider)}",
|
|
1855
|
+
api_key="${accessToken || "{API_TOKEN}"}"
|
|
1856
|
+
)
|
|
1857
|
+
|
|
1858
|
+
messages = ${messagesStr}
|
|
1859
|
+
|
|
1860
|
+
completion = client.chat.completions.create(
|
|
1861
|
+
model="${providerModelId ?? model.id}",
|
|
1862
|
+
messages=messages,
|
|
1863
|
+
${configStr}
|
|
1864
|
+
)
|
|
1865
|
+
|
|
1866
|
+
print(completion.choices[0].message)`
|
|
1867
|
+
}
|
|
1868
|
+
];
|
|
1869
|
+
}
|
|
1870
|
+
};
|
|
1871
|
+
var snippetZeroShotClassification2 = (model) => {
|
|
1872
|
+
return [
|
|
1873
|
+
{
|
|
1874
|
+
client: "requests",
|
|
1875
|
+
content: `def query(payload):
|
|
1876
|
+
response = requests.post(API_URL, headers=headers, json=payload)
|
|
1877
|
+
return response.json()
|
|
1878
|
+
|
|
1879
|
+
output = query({
|
|
1880
|
+
"inputs": ${getModelInputSnippet2(model)},
|
|
1881
|
+
"parameters": {"candidate_labels": ["refund", "legal", "faq"]},
|
|
1882
|
+
})`
|
|
1883
|
+
}
|
|
1884
|
+
];
|
|
1885
|
+
};
|
|
1886
|
+
var snippetZeroShotImageClassification = (model) => {
|
|
1887
|
+
return [
|
|
1888
|
+
{
|
|
1889
|
+
client: "requests",
|
|
1890
|
+
content: `def query(data):
|
|
1891
|
+
with open(data["image_path"], "rb") as f:
|
|
1892
|
+
img = f.read()
|
|
1893
|
+
payload={
|
|
1894
|
+
"parameters": data["parameters"],
|
|
1895
|
+
"inputs": base64.b64encode(img).decode("utf-8")
|
|
1896
|
+
}
|
|
1897
|
+
response = requests.post(API_URL, headers=headers, json=payload)
|
|
1898
|
+
return response.json()
|
|
1899
|
+
|
|
1900
|
+
output = query({
|
|
1901
|
+
"image_path": ${getModelInputSnippet2(model)},
|
|
1902
|
+
"parameters": {"candidate_labels": ["cat", "dog", "llama"]},
|
|
1903
|
+
})`
|
|
1904
|
+
}
|
|
1905
|
+
];
|
|
1906
|
+
};
|
|
1907
|
+
var snippetBasic2 = (model, accessToken, provider) => {
|
|
1908
|
+
return [
|
|
1909
|
+
...model.pipeline_tag && model.pipeline_tag in HFH_INFERENCE_CLIENT_METHODS ? [
|
|
1910
|
+
{
|
|
1911
|
+
client: "huggingface_hub",
|
|
1912
|
+
content: `${snippetImportInferenceClient(accessToken, provider)}
|
|
1913
|
+
|
|
1914
|
+
result = client.${HFH_INFERENCE_CLIENT_METHODS[model.pipeline_tag]}(
|
|
1915
|
+
model="${model.id}",
|
|
1916
|
+
inputs=${getModelInputSnippet2(model)},
|
|
1917
|
+
provider="${provider}",
|
|
1918
|
+
)
|
|
1919
|
+
|
|
1920
|
+
print(result)
|
|
1921
|
+
`
|
|
1922
|
+
}
|
|
1923
|
+
] : [],
|
|
1924
|
+
{
|
|
1925
|
+
client: "requests",
|
|
1926
|
+
content: `def query(payload):
|
|
1927
|
+
response = requests.post(API_URL, headers=headers, json=payload)
|
|
1928
|
+
return response.json()
|
|
1929
|
+
|
|
1930
|
+
output = query({
|
|
1931
|
+
"inputs": ${getModelInputSnippet2(model)},
|
|
1932
|
+
})`
|
|
1933
|
+
}
|
|
1934
|
+
];
|
|
1935
|
+
};
|
|
1936
|
+
var snippetFile2 = (model) => {
|
|
1937
|
+
return [
|
|
1938
|
+
{
|
|
1939
|
+
client: "requests",
|
|
1940
|
+
content: `def query(filename):
|
|
1941
|
+
with open(filename, "rb") as f:
|
|
1942
|
+
data = f.read()
|
|
1943
|
+
response = requests.post(API_URL, headers=headers, data=data)
|
|
1944
|
+
return response.json()
|
|
1945
|
+
|
|
1946
|
+
output = query(${getModelInputSnippet2(model)})`
|
|
1947
|
+
}
|
|
1948
|
+
];
|
|
1949
|
+
};
|
|
1950
|
+
var snippetTextToImage = (model, accessToken, provider, providerModelId) => {
|
|
1951
|
+
return [
|
|
1952
|
+
{
|
|
1953
|
+
client: "huggingface_hub",
|
|
1954
|
+
content: `${snippetImportInferenceClient(accessToken, provider)}
|
|
1955
|
+
|
|
1956
|
+
# output is a PIL.Image object
|
|
1957
|
+
image = client.text_to_image(
|
|
1958
|
+
${getModelInputSnippet2(model)},
|
|
1959
|
+
model="${model.id}"
|
|
1960
|
+
)`
|
|
1961
|
+
},
|
|
1962
|
+
...provider === "fal-ai" ? [
|
|
1963
|
+
{
|
|
1964
|
+
client: "fal-client",
|
|
1965
|
+
content: `import fal_client
|
|
1966
|
+
|
|
1967
|
+
result = fal_client.subscribe(
|
|
1968
|
+
"${providerModelId ?? model.id}",
|
|
1969
|
+
arguments={
|
|
1970
|
+
"prompt": ${getModelInputSnippet2(model)},
|
|
1971
|
+
},
|
|
1972
|
+
)
|
|
1973
|
+
print(result)
|
|
1974
|
+
`
|
|
1975
|
+
}
|
|
1976
|
+
] : [],
|
|
1977
|
+
...provider === "hf-inference" ? [
|
|
1978
|
+
{
|
|
1979
|
+
client: "requests",
|
|
1980
|
+
content: `def query(payload):
|
|
1981
|
+
response = requests.post(API_URL, headers=headers, json=payload)
|
|
1982
|
+
return response.content
|
|
1983
|
+
|
|
1984
|
+
image_bytes = query({
|
|
1985
|
+
"inputs": ${getModelInputSnippet2(model)},
|
|
1986
|
+
})
|
|
1987
|
+
|
|
1988
|
+
# You can access the image with PIL.Image for example
|
|
1989
|
+
import io
|
|
1990
|
+
from PIL import Image
|
|
1991
|
+
image = Image.open(io.BytesIO(image_bytes))`
|
|
1992
|
+
}
|
|
1993
|
+
] : []
|
|
1994
|
+
];
|
|
1995
|
+
};
|
|
1996
|
+
var snippetTextToVideo = (model, accessToken, provider) => {
|
|
1997
|
+
return ["fal-ai", "replicate"].includes(provider) ? [
|
|
1998
|
+
{
|
|
1999
|
+
client: "huggingface_hub",
|
|
2000
|
+
content: `${snippetImportInferenceClient(accessToken, provider)}
|
|
2001
|
+
|
|
2002
|
+
video = client.text_to_video(
|
|
2003
|
+
${getModelInputSnippet2(model)},
|
|
2004
|
+
model="${model.id}"
|
|
2005
|
+
)`
|
|
2006
|
+
}
|
|
2007
|
+
] : [];
|
|
2008
|
+
};
|
|
2009
|
+
var snippetTabular = (model) => {
|
|
2010
|
+
return [
|
|
2011
|
+
{
|
|
2012
|
+
client: "requests",
|
|
2013
|
+
content: `def query(payload):
|
|
2014
|
+
response = requests.post(API_URL, headers=headers, json=payload)
|
|
2015
|
+
return response.content
|
|
2016
|
+
|
|
2017
|
+
response = query({
|
|
2018
|
+
"inputs": {"data": ${getModelInputSnippet2(model)}},
|
|
2019
|
+
})`
|
|
2020
|
+
}
|
|
2021
|
+
];
|
|
2022
|
+
};
|
|
2023
|
+
var snippetTextToAudio = (model) => {
|
|
2024
|
+
if (model.library_name === "transformers") {
|
|
2025
|
+
return [
|
|
2026
|
+
{
|
|
2027
|
+
client: "requests",
|
|
2028
|
+
content: `def query(payload):
|
|
2029
|
+
response = requests.post(API_URL, headers=headers, json=payload)
|
|
2030
|
+
return response.content
|
|
2031
|
+
|
|
2032
|
+
audio_bytes = query({
|
|
2033
|
+
"inputs": ${getModelInputSnippet2(model)},
|
|
2034
|
+
})
|
|
2035
|
+
# You can access the audio with IPython.display for example
|
|
2036
|
+
from IPython.display import Audio
|
|
2037
|
+
Audio(audio_bytes)`
|
|
2038
|
+
}
|
|
2039
|
+
];
|
|
2040
|
+
} else {
|
|
2041
|
+
return [
|
|
2042
|
+
{
|
|
2043
|
+
client: "requests",
|
|
2044
|
+
content: `def query(payload):
|
|
2045
|
+
response = requests.post(API_URL, headers=headers, json=payload)
|
|
2046
|
+
return response.json()
|
|
2047
|
+
|
|
2048
|
+
audio, sampling_rate = query({
|
|
2049
|
+
"inputs": ${getModelInputSnippet2(model)},
|
|
2050
|
+
})
|
|
2051
|
+
# You can access the audio with IPython.display for example
|
|
2052
|
+
from IPython.display import Audio
|
|
2053
|
+
Audio(audio, rate=sampling_rate)`
|
|
2054
|
+
}
|
|
2055
|
+
];
|
|
2056
|
+
}
|
|
2057
|
+
};
|
|
2058
|
+
var snippetDocumentQuestionAnswering = (model) => {
|
|
2059
|
+
return [
|
|
2060
|
+
{
|
|
2061
|
+
client: "requests",
|
|
2062
|
+
content: `def query(payload):
|
|
2063
|
+
with open(payload["image"], "rb") as f:
|
|
2064
|
+
img = f.read()
|
|
2065
|
+
payload["image"] = base64.b64encode(img).decode("utf-8")
|
|
2066
|
+
response = requests.post(API_URL, headers=headers, json=payload)
|
|
2067
|
+
return response.json()
|
|
2068
|
+
|
|
2069
|
+
output = query({
|
|
2070
|
+
"inputs": ${getModelInputSnippet2(model)},
|
|
2071
|
+
})`
|
|
2072
|
+
}
|
|
2073
|
+
];
|
|
2074
|
+
};
|
|
2075
|
+
var pythonSnippets = {
|
|
2076
|
+
// Same order as in tasks/src/pipelines.ts
|
|
2077
|
+
"text-classification": snippetBasic2,
|
|
2078
|
+
"token-classification": snippetBasic2,
|
|
2079
|
+
"table-question-answering": snippetBasic2,
|
|
2080
|
+
"question-answering": snippetBasic2,
|
|
2081
|
+
"zero-shot-classification": snippetZeroShotClassification2,
|
|
2082
|
+
translation: snippetBasic2,
|
|
2083
|
+
summarization: snippetBasic2,
|
|
2084
|
+
"feature-extraction": snippetBasic2,
|
|
2085
|
+
"text-generation": snippetBasic2,
|
|
2086
|
+
"text2text-generation": snippetBasic2,
|
|
2087
|
+
"image-text-to-text": snippetConversational,
|
|
2088
|
+
"fill-mask": snippetBasic2,
|
|
2089
|
+
"sentence-similarity": snippetBasic2,
|
|
2090
|
+
"automatic-speech-recognition": snippetFile2,
|
|
2091
|
+
"text-to-image": snippetTextToImage,
|
|
2092
|
+
"text-to-video": snippetTextToVideo,
|
|
2093
|
+
"text-to-speech": snippetTextToAudio,
|
|
2094
|
+
"text-to-audio": snippetTextToAudio,
|
|
2095
|
+
"audio-to-audio": snippetFile2,
|
|
2096
|
+
"audio-classification": snippetFile2,
|
|
2097
|
+
"image-classification": snippetFile2,
|
|
2098
|
+
"tabular-regression": snippetTabular,
|
|
2099
|
+
"tabular-classification": snippetTabular,
|
|
2100
|
+
"object-detection": snippetFile2,
|
|
2101
|
+
"image-segmentation": snippetFile2,
|
|
2102
|
+
"document-question-answering": snippetDocumentQuestionAnswering,
|
|
2103
|
+
"image-to-text": snippetFile2,
|
|
2104
|
+
"zero-shot-image-classification": snippetZeroShotImageClassification
|
|
2105
|
+
};
|
|
2106
|
+
function getPythonInferenceSnippet(model, accessToken, provider, providerModelId, opts) {
|
|
2107
|
+
if (model.tags.includes("conversational")) {
|
|
2108
|
+
return snippetConversational(model, accessToken, provider, providerModelId, opts);
|
|
2109
|
+
} else {
|
|
2110
|
+
const snippets = model.pipeline_tag && model.pipeline_tag in pythonSnippets ? pythonSnippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId) ?? [] : [];
|
|
2111
|
+
return snippets.map((snippet) => {
|
|
2112
|
+
return {
|
|
2113
|
+
...snippet,
|
|
2114
|
+
content: snippet.client === "requests" ? `import requests
|
|
2115
|
+
|
|
2116
|
+
API_URL = "${openAIbaseUrl(provider)}"
|
|
2117
|
+
headers = {"Authorization": ${accessToken ? `"Bearer ${accessToken}"` : `f"Bearer {API_TOKEN}"`}}
|
|
2118
|
+
|
|
2119
|
+
${snippet.content}` : snippet.content
|
|
2120
|
+
};
|
|
2121
|
+
});
|
|
2122
|
+
}
|
|
2123
|
+
}
|
|
2124
|
+
|
|
2125
|
+
// src/snippets/js.ts
|
|
2126
|
+
var js_exports = {};
|
|
2127
|
+
__export(js_exports, {
|
|
2128
|
+
getJsInferenceSnippet: () => getJsInferenceSnippet,
|
|
2129
|
+
jsSnippets: () => jsSnippets,
|
|
2130
|
+
snippetAutomaticSpeechRecognition: () => snippetAutomaticSpeechRecognition,
|
|
2131
|
+
snippetBasic: () => snippetBasic3,
|
|
2132
|
+
snippetFile: () => snippetFile3,
|
|
2133
|
+
snippetTextGeneration: () => snippetTextGeneration2,
|
|
2134
|
+
snippetTextToAudio: () => snippetTextToAudio2,
|
|
2135
|
+
snippetTextToImage: () => snippetTextToImage2,
|
|
2136
|
+
snippetTextToVideo: () => snippetTextToVideo2,
|
|
2137
|
+
snippetZeroShotClassification: () => snippetZeroShotClassification3
|
|
2138
|
+
});
|
|
2139
|
+
import { openAIbaseUrl as openAIbaseUrl2 } from "@huggingface/tasks";
|
|
2140
|
+
import {
|
|
2141
|
+
getModelInputSnippet as getModelInputSnippet3,
|
|
2142
|
+
stringifyGenerationConfig as stringifyGenerationConfig3,
|
|
2143
|
+
stringifyMessages as stringifyMessages3
|
|
2144
|
+
} from "@huggingface/tasks";
|
|
2145
|
+
var HFJS_METHODS = {
|
|
2146
|
+
"text-classification": "textClassification",
|
|
2147
|
+
"token-classification": "tokenClassification",
|
|
2148
|
+
"table-question-answering": "tableQuestionAnswering",
|
|
2149
|
+
"question-answering": "questionAnswering",
|
|
2150
|
+
translation: "translation",
|
|
2151
|
+
summarization: "summarization",
|
|
2152
|
+
"feature-extraction": "featureExtraction",
|
|
2153
|
+
"text-generation": "textGeneration",
|
|
2154
|
+
"text2text-generation": "textGeneration",
|
|
2155
|
+
"fill-mask": "fillMask",
|
|
2156
|
+
"sentence-similarity": "sentenceSimilarity"
|
|
2157
|
+
};
|
|
2158
|
+
var snippetBasic3 = (model, accessToken, provider) => {
|
|
2159
|
+
return [
|
|
2160
|
+
...model.pipeline_tag && model.pipeline_tag in HFJS_METHODS ? [
|
|
2161
|
+
{
|
|
2162
|
+
client: "huggingface.js",
|
|
2163
|
+
content: `import { HfInference } from "@huggingface/inference";
|
|
2164
|
+
|
|
2165
|
+
const client = new HfInference("${accessToken || `{API_TOKEN}`}");
|
|
2166
|
+
|
|
2167
|
+
const output = await client.${HFJS_METHODS[model.pipeline_tag]}({
|
|
2168
|
+
model: "${model.id}",
|
|
2169
|
+
inputs: ${getModelInputSnippet3(model)},
|
|
2170
|
+
provider: "${provider}",
|
|
2171
|
+
});
|
|
2172
|
+
|
|
2173
|
+
console.log(output);
|
|
2174
|
+
`
|
|
2175
|
+
}
|
|
2176
|
+
] : [],
|
|
2177
|
+
{
|
|
2178
|
+
client: "fetch",
|
|
2179
|
+
content: `async function query(data) {
|
|
2180
|
+
const response = await fetch(
|
|
2181
|
+
"https://router.huggingface.co/hf-inference/models/${model.id}",
|
|
2182
|
+
{
|
|
2183
|
+
headers: {
|
|
2184
|
+
Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
|
|
2185
|
+
"Content-Type": "application/json",
|
|
2186
|
+
},
|
|
2187
|
+
method: "POST",
|
|
2188
|
+
body: JSON.stringify(data),
|
|
2189
|
+
}
|
|
2190
|
+
);
|
|
2191
|
+
const result = await response.json();
|
|
2192
|
+
return result;
|
|
2193
|
+
}
|
|
2194
|
+
|
|
2195
|
+
query({"inputs": ${getModelInputSnippet3(model)}}).then((response) => {
|
|
2196
|
+
console.log(JSON.stringify(response));
|
|
2197
|
+
});`
|
|
2198
|
+
}
|
|
2199
|
+
];
|
|
2200
|
+
};
|
|
2201
|
+
var snippetTextGeneration2 = (model, accessToken, provider, providerModelId, opts) => {
|
|
2202
|
+
if (model.tags.includes("conversational")) {
|
|
2203
|
+
const streaming = opts?.streaming ?? true;
|
|
2204
|
+
const exampleMessages = getModelInputSnippet3(model);
|
|
2205
|
+
const messages = opts?.messages ?? exampleMessages;
|
|
2206
|
+
const messagesStr = stringifyMessages3(messages, { indent: " " });
|
|
2207
|
+
const config = {
|
|
2208
|
+
...opts?.temperature ? { temperature: opts.temperature } : void 0,
|
|
2209
|
+
max_tokens: opts?.max_tokens ?? 500,
|
|
2210
|
+
...opts?.top_p ? { top_p: opts.top_p } : void 0
|
|
2211
|
+
};
|
|
2212
|
+
const configStr = stringifyGenerationConfig3(config, {
|
|
2213
|
+
indent: "\n ",
|
|
2214
|
+
attributeValueConnector: ": "
|
|
2215
|
+
});
|
|
2216
|
+
if (streaming) {
|
|
2217
|
+
return [
|
|
2218
|
+
{
|
|
2219
|
+
client: "huggingface.js",
|
|
2220
|
+
content: `import { HfInference } from "@huggingface/inference";
|
|
2221
|
+
|
|
2222
|
+
const client = new HfInference("${accessToken || `{API_TOKEN}`}");
|
|
2223
|
+
|
|
2224
|
+
let out = "";
|
|
2225
|
+
|
|
2226
|
+
const stream = client.chatCompletionStream({
|
|
2227
|
+
model: "${model.id}",
|
|
2228
|
+
messages: ${messagesStr},
|
|
2229
|
+
provider: "${provider}",
|
|
2230
|
+
${configStr}
|
|
2231
|
+
});
|
|
2232
|
+
|
|
2233
|
+
for await (const chunk of stream) {
|
|
2234
|
+
if (chunk.choices && chunk.choices.length > 0) {
|
|
2235
|
+
const newContent = chunk.choices[0].delta.content;
|
|
2236
|
+
out += newContent;
|
|
2237
|
+
console.log(newContent);
|
|
2238
|
+
}
|
|
2239
|
+
}`
|
|
2240
|
+
},
|
|
2241
|
+
{
|
|
2242
|
+
client: "openai",
|
|
2243
|
+
content: `import { OpenAI } from "openai";
|
|
2244
|
+
|
|
2245
|
+
const client = new OpenAI({
|
|
2246
|
+
baseURL: "${openAIbaseUrl2(provider)}",
|
|
2247
|
+
apiKey: "${accessToken || `{API_TOKEN}`}"
|
|
2248
|
+
});
|
|
2249
|
+
|
|
2250
|
+
let out = "";
|
|
2251
|
+
|
|
2252
|
+
const stream = await client.chat.completions.create({
|
|
2253
|
+
model: "${providerModelId ?? model.id}",
|
|
2254
|
+
messages: ${messagesStr},
|
|
2255
|
+
${configStr}
|
|
2256
|
+
stream: true,
|
|
2257
|
+
});
|
|
2258
|
+
|
|
2259
|
+
for await (const chunk of stream) {
|
|
2260
|
+
if (chunk.choices && chunk.choices.length > 0) {
|
|
2261
|
+
const newContent = chunk.choices[0].delta.content;
|
|
2262
|
+
out += newContent;
|
|
2263
|
+
console.log(newContent);
|
|
2264
|
+
}
|
|
2265
|
+
}`
|
|
2266
|
+
}
|
|
2267
|
+
];
|
|
2268
|
+
} else {
|
|
2269
|
+
return [
|
|
2270
|
+
{
|
|
2271
|
+
client: "huggingface.js",
|
|
2272
|
+
content: `import { HfInference } from "@huggingface/inference";
|
|
2273
|
+
|
|
2274
|
+
const client = new HfInference("${accessToken || `{API_TOKEN}`}");
|
|
2275
|
+
|
|
2276
|
+
const chatCompletion = await client.chatCompletion({
|
|
2277
|
+
model: "${model.id}",
|
|
2278
|
+
messages: ${messagesStr},
|
|
2279
|
+
provider: "${provider}",
|
|
2280
|
+
${configStr}
|
|
2281
|
+
});
|
|
2282
|
+
|
|
2283
|
+
console.log(chatCompletion.choices[0].message);
|
|
2284
|
+
`
|
|
2285
|
+
},
|
|
2286
|
+
{
|
|
2287
|
+
client: "openai",
|
|
2288
|
+
content: `import { OpenAI } from "openai";
|
|
2289
|
+
|
|
2290
|
+
const client = new OpenAI({
|
|
2291
|
+
baseURL: "${openAIbaseUrl2(provider)}",
|
|
2292
|
+
apiKey: "${accessToken || `{API_TOKEN}`}"
|
|
2293
|
+
});
|
|
2294
|
+
|
|
2295
|
+
const chatCompletion = await client.chat.completions.create({
|
|
2296
|
+
model: "${providerModelId ?? model.id}",
|
|
2297
|
+
messages: ${messagesStr},
|
|
2298
|
+
${configStr}
|
|
2299
|
+
});
|
|
2300
|
+
|
|
2301
|
+
console.log(chatCompletion.choices[0].message);
|
|
2302
|
+
`
|
|
2303
|
+
}
|
|
2304
|
+
];
|
|
2305
|
+
}
|
|
2306
|
+
} else {
|
|
2307
|
+
return snippetBasic3(model, accessToken, provider);
|
|
2308
|
+
}
|
|
2309
|
+
};
|
|
2310
|
+
var snippetZeroShotClassification3 = (model, accessToken) => {
|
|
2311
|
+
return [
|
|
2312
|
+
{
|
|
2313
|
+
client: "fetch",
|
|
2314
|
+
content: `async function query(data) {
|
|
2315
|
+
const response = await fetch(
|
|
2316
|
+
"https://router.huggingface.co/hf-inference/models/${model.id}",
|
|
2317
|
+
{
|
|
2318
|
+
headers: {
|
|
2319
|
+
Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
|
|
2320
|
+
"Content-Type": "application/json",
|
|
2321
|
+
},
|
|
2322
|
+
method: "POST",
|
|
2323
|
+
body: JSON.stringify(data),
|
|
2324
|
+
}
|
|
2325
|
+
);
|
|
2326
|
+
const result = await response.json();
|
|
2327
|
+
return result;
|
|
2328
|
+
}
|
|
2329
|
+
|
|
2330
|
+
query({"inputs": ${getModelInputSnippet3(
|
|
2331
|
+
model
|
|
2332
|
+
)}, "parameters": {"candidate_labels": ["refund", "legal", "faq"]}}).then((response) => {
|
|
2333
|
+
console.log(JSON.stringify(response));
|
|
2334
|
+
});`
|
|
2335
|
+
}
|
|
2336
|
+
];
|
|
2337
|
+
};
|
|
2338
|
+
var snippetTextToImage2 = (model, accessToken, provider) => {
|
|
2339
|
+
return [
|
|
2340
|
+
{
|
|
2341
|
+
client: "huggingface.js",
|
|
2342
|
+
content: `import { HfInference } from "@huggingface/inference";
|
|
2343
|
+
|
|
2344
|
+
const client = new HfInference("${accessToken || `{API_TOKEN}`}");
|
|
2345
|
+
|
|
2346
|
+
const image = await client.textToImage({
|
|
2347
|
+
model: "${model.id}",
|
|
2348
|
+
inputs: ${getModelInputSnippet3(model)},
|
|
2349
|
+
parameters: { num_inference_steps: 5 },
|
|
2350
|
+
provider: "${provider}",
|
|
2351
|
+
});
|
|
2352
|
+
/// Use the generated image (it's a Blob)
|
|
2353
|
+
`
|
|
2354
|
+
},
|
|
2355
|
+
...provider === "hf-inference" ? [
|
|
2356
|
+
{
|
|
2357
|
+
client: "fetch",
|
|
2358
|
+
content: `async function query(data) {
|
|
2359
|
+
const response = await fetch(
|
|
2360
|
+
"https://router.huggingface.co/hf-inference/models/${model.id}",
|
|
2361
|
+
{
|
|
2362
|
+
headers: {
|
|
2363
|
+
Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
|
|
2364
|
+
"Content-Type": "application/json",
|
|
2365
|
+
},
|
|
2366
|
+
method: "POST",
|
|
2367
|
+
body: JSON.stringify(data),
|
|
2368
|
+
}
|
|
2369
|
+
);
|
|
2370
|
+
const result = await response.blob();
|
|
2371
|
+
return result;
|
|
2372
|
+
}
|
|
2373
|
+
query({"inputs": ${getModelInputSnippet3(model)}}).then((response) => {
|
|
2374
|
+
// Use image
|
|
2375
|
+
});`
|
|
2376
|
+
}
|
|
2377
|
+
] : []
|
|
2378
|
+
];
|
|
2379
|
+
};
|
|
2380
|
+
var snippetTextToVideo2 = (model, accessToken, provider) => {
|
|
2381
|
+
return ["fal-ai", "replicate"].includes(provider) ? [
|
|
2382
|
+
{
|
|
2383
|
+
client: "huggingface.js",
|
|
2384
|
+
content: `import { HfInference } from "@huggingface/inference";
|
|
2385
|
+
|
|
2386
|
+
const client = new HfInference("${accessToken || `{API_TOKEN}`}");
|
|
2387
|
+
|
|
2388
|
+
const video = await client.textToVideo({
|
|
2389
|
+
model: "${model.id}",
|
|
2390
|
+
provider: "${provider}",
|
|
2391
|
+
inputs: ${getModelInputSnippet3(model)},
|
|
2392
|
+
parameters: { num_inference_steps: 5 },
|
|
2393
|
+
});
|
|
2394
|
+
// Use the generated video (it's a Blob)
|
|
2395
|
+
`
|
|
2396
|
+
}
|
|
2397
|
+
] : [];
|
|
2398
|
+
};
|
|
2399
|
+
var snippetTextToAudio2 = (model, accessToken, provider) => {
|
|
2400
|
+
if (provider !== "hf-inference") {
|
|
2401
|
+
return [];
|
|
2402
|
+
}
|
|
2403
|
+
const commonSnippet = `async function query(data) {
|
|
2404
|
+
const response = await fetch(
|
|
2405
|
+
"https://router.huggingface.co/hf-inference/models/${model.id}",
|
|
2406
|
+
{
|
|
2407
|
+
headers: {
|
|
2408
|
+
Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
|
|
2409
|
+
"Content-Type": "application/json",
|
|
2410
|
+
},
|
|
2411
|
+
method: "POST",
|
|
2412
|
+
body: JSON.stringify(data),
|
|
2413
|
+
}
|
|
2414
|
+
);`;
|
|
2415
|
+
if (model.library_name === "transformers") {
|
|
2416
|
+
return [
|
|
2417
|
+
{
|
|
2418
|
+
client: "fetch",
|
|
2419
|
+
content: commonSnippet + `
|
|
2420
|
+
const result = await response.blob();
|
|
2421
|
+
return result;
|
|
2422
|
+
}
|
|
2423
|
+
query({"inputs": ${getModelInputSnippet3(model)}}).then((response) => {
|
|
2424
|
+
// Returns a byte object of the Audio wavform. Use it directly!
|
|
2425
|
+
});`
|
|
2426
|
+
}
|
|
2427
|
+
];
|
|
2428
|
+
} else {
|
|
2429
|
+
return [
|
|
2430
|
+
{
|
|
2431
|
+
client: "fetch",
|
|
2432
|
+
content: commonSnippet + `
|
|
2433
|
+
const result = await response.json();
|
|
2434
|
+
return result;
|
|
2435
|
+
}
|
|
2436
|
+
|
|
2437
|
+
query({"inputs": ${getModelInputSnippet3(model)}}).then((response) => {
|
|
2438
|
+
console.log(JSON.stringify(response));
|
|
2439
|
+
});`
|
|
2440
|
+
}
|
|
2441
|
+
];
|
|
2442
|
+
}
|
|
2443
|
+
};
|
|
2444
|
+
var snippetAutomaticSpeechRecognition = (model, accessToken, provider) => {
|
|
2445
|
+
return [
|
|
2446
|
+
{
|
|
2447
|
+
client: "huggingface.js",
|
|
2448
|
+
content: `import { HfInference } from "@huggingface/inference";
|
|
2449
|
+
|
|
2450
|
+
const client = new HfInference("${accessToken || `{API_TOKEN}`}");
|
|
2451
|
+
|
|
2452
|
+
const data = fs.readFileSync(${getModelInputSnippet3(model)});
|
|
2453
|
+
|
|
2454
|
+
const output = await client.automaticSpeechRecognition({
|
|
2455
|
+
data,
|
|
2456
|
+
model: "${model.id}",
|
|
2457
|
+
provider: "${provider}",
|
|
2458
|
+
});
|
|
2459
|
+
|
|
2460
|
+
console.log(output);
|
|
2461
|
+
`
|
|
2462
|
+
},
|
|
2463
|
+
...provider === "hf-inference" ? snippetFile3(model, accessToken, provider) : []
|
|
2464
|
+
];
|
|
2465
|
+
};
|
|
2466
|
+
var snippetFile3 = (model, accessToken, provider) => {
|
|
2467
|
+
if (provider !== "hf-inference") {
|
|
2468
|
+
return [];
|
|
2469
|
+
}
|
|
2470
|
+
return [
|
|
2471
|
+
{
|
|
2472
|
+
client: "fetch",
|
|
2473
|
+
content: `async function query(filename) {
|
|
2474
|
+
const data = fs.readFileSync(filename);
|
|
2475
|
+
const response = await fetch(
|
|
2476
|
+
"https://router.huggingface.co/hf-inference/models/${model.id}",
|
|
2477
|
+
{
|
|
2478
|
+
headers: {
|
|
2479
|
+
Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
|
|
2480
|
+
"Content-Type": "application/json",
|
|
2481
|
+
},
|
|
2482
|
+
method: "POST",
|
|
2483
|
+
body: data,
|
|
2484
|
+
}
|
|
2485
|
+
);
|
|
2486
|
+
const result = await response.json();
|
|
2487
|
+
return result;
|
|
2488
|
+
}
|
|
2489
|
+
|
|
2490
|
+
query(${getModelInputSnippet3(model)}).then((response) => {
|
|
2491
|
+
console.log(JSON.stringify(response));
|
|
2492
|
+
});`
|
|
2493
|
+
}
|
|
2494
|
+
];
|
|
2495
|
+
};
|
|
2496
|
+
var jsSnippets = {
|
|
2497
|
+
// Same order as in tasks/src/pipelines.ts
|
|
2498
|
+
"text-classification": snippetBasic3,
|
|
2499
|
+
"token-classification": snippetBasic3,
|
|
2500
|
+
"table-question-answering": snippetBasic3,
|
|
2501
|
+
"question-answering": snippetBasic3,
|
|
2502
|
+
"zero-shot-classification": snippetZeroShotClassification3,
|
|
2503
|
+
translation: snippetBasic3,
|
|
2504
|
+
summarization: snippetBasic3,
|
|
2505
|
+
"feature-extraction": snippetBasic3,
|
|
2506
|
+
"text-generation": snippetTextGeneration2,
|
|
2507
|
+
"image-text-to-text": snippetTextGeneration2,
|
|
2508
|
+
"text2text-generation": snippetBasic3,
|
|
2509
|
+
"fill-mask": snippetBasic3,
|
|
2510
|
+
"sentence-similarity": snippetBasic3,
|
|
2511
|
+
"automatic-speech-recognition": snippetAutomaticSpeechRecognition,
|
|
2512
|
+
"text-to-image": snippetTextToImage2,
|
|
2513
|
+
"text-to-video": snippetTextToVideo2,
|
|
2514
|
+
"text-to-speech": snippetTextToAudio2,
|
|
2515
|
+
"text-to-audio": snippetTextToAudio2,
|
|
2516
|
+
"audio-to-audio": snippetFile3,
|
|
2517
|
+
"audio-classification": snippetFile3,
|
|
2518
|
+
"image-classification": snippetFile3,
|
|
2519
|
+
"image-to-text": snippetFile3,
|
|
2520
|
+
"object-detection": snippetFile3,
|
|
2521
|
+
"image-segmentation": snippetFile3
|
|
2522
|
+
};
|
|
2523
|
+
function getJsInferenceSnippet(model, accessToken, provider, providerModelId, opts) {
|
|
2524
|
+
return model.pipeline_tag && model.pipeline_tag in jsSnippets ? jsSnippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId, opts) ?? [] : [];
|
|
2525
|
+
}
|
|
1501
2526
|
export {
|
|
1502
2527
|
HfInference,
|
|
1503
2528
|
HfInferenceEndpoint,
|
|
@@ -1519,6 +2544,7 @@ export {
|
|
|
1519
2544
|
questionAnswering,
|
|
1520
2545
|
request,
|
|
1521
2546
|
sentenceSimilarity,
|
|
2547
|
+
snippets_exports as snippets,
|
|
1522
2548
|
streamingRequest,
|
|
1523
2549
|
summarization,
|
|
1524
2550
|
tableQuestionAnswering,
|