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