@huggingface/inference 3.6.1 → 3.7.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 +0 -25
- package/dist/index.cjs +256 -154
- package/dist/index.js +256 -154
- package/dist/src/config.d.ts +1 -0
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/lib/makeRequestOptions.d.ts.map +1 -1
- package/dist/src/providers/black-forest-labs.d.ts.map +1 -1
- package/dist/src/providers/cerebras.d.ts.map +1 -1
- package/dist/src/providers/cohere.d.ts.map +1 -1
- package/dist/src/providers/fal-ai.d.ts +6 -16
- package/dist/src/providers/fal-ai.d.ts.map +1 -1
- package/dist/src/providers/fireworks-ai.d.ts.map +1 -1
- package/dist/src/providers/hf-inference.d.ts.map +1 -1
- package/dist/src/providers/hyperbolic.d.ts.map +1 -1
- package/dist/src/providers/nebius.d.ts.map +1 -1
- package/dist/src/providers/novita.d.ts.map +1 -1
- package/dist/src/providers/openai.d.ts.map +1 -1
- package/dist/src/providers/replicate.d.ts.map +1 -1
- package/dist/src/providers/sambanova.d.ts.map +1 -1
- package/dist/src/providers/together.d.ts.map +1 -1
- package/dist/src/tasks/audio/automaticSpeechRecognition.d.ts.map +1 -1
- package/dist/src/tasks/custom/request.d.ts +1 -0
- package/dist/src/tasks/custom/request.d.ts.map +1 -1
- package/dist/src/tasks/custom/streamingRequest.d.ts +1 -0
- package/dist/src/tasks/custom/streamingRequest.d.ts.map +1 -1
- package/dist/src/tasks/cv/imageToText.d.ts.map +1 -1
- package/dist/src/tasks/cv/objectDetection.d.ts +1 -1
- package/dist/src/tasks/cv/objectDetection.d.ts.map +1 -1
- package/dist/src/tasks/cv/textToVideo.d.ts +1 -1
- package/dist/src/tasks/cv/textToVideo.d.ts.map +1 -1
- package/dist/src/tasks/cv/zeroShotImageClassification.d.ts +1 -1
- package/dist/src/tasks/cv/zeroShotImageClassification.d.ts.map +1 -1
- package/dist/src/tasks/multimodal/documentQuestionAnswering.d.ts +1 -1
- package/dist/src/tasks/multimodal/documentQuestionAnswering.d.ts.map +1 -1
- package/dist/src/tasks/multimodal/visualQuestionAnswering.d.ts.map +1 -1
- package/dist/src/tasks/nlp/chatCompletion.d.ts +1 -1
- package/dist/src/tasks/nlp/chatCompletion.d.ts.map +1 -1
- package/dist/src/tasks/nlp/chatCompletionStream.d.ts +1 -1
- package/dist/src/tasks/nlp/chatCompletionStream.d.ts.map +1 -1
- package/dist/src/tasks/nlp/questionAnswering.d.ts.map +1 -1
- package/dist/src/tasks/nlp/sentenceSimilarity.d.ts.map +1 -1
- package/dist/src/tasks/nlp/textClassification.d.ts.map +1 -1
- package/dist/src/tasks/nlp/tokenClassification.d.ts.map +1 -1
- package/dist/src/tasks/nlp/zeroShotClassification.d.ts.map +1 -1
- package/dist/src/types.d.ts +11 -2
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/utils/request.d.ts +27 -0
- package/dist/src/utils/request.d.ts.map +1 -0
- package/dist/test/InferenceClient.spec.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/config.ts +1 -0
- package/src/lib/makeRequestOptions.ts +8 -3
- package/src/providers/black-forest-labs.ts +6 -2
- package/src/providers/cerebras.ts +6 -2
- package/src/providers/cohere.ts +6 -2
- package/src/providers/fal-ai.ts +85 -3
- package/src/providers/fireworks-ai.ts +6 -2
- package/src/providers/hf-inference.ts +6 -2
- package/src/providers/hyperbolic.ts +6 -2
- package/src/providers/nebius.ts +6 -2
- package/src/providers/novita.ts +5 -2
- package/src/providers/openai.ts +6 -2
- package/src/providers/replicate.ts +6 -2
- package/src/providers/sambanova.ts +6 -2
- package/src/providers/together.ts +6 -2
- package/src/snippets/templates.exported.ts +1 -1
- package/src/tasks/audio/audioClassification.ts +2 -2
- package/src/tasks/audio/audioToAudio.ts +2 -2
- package/src/tasks/audio/automaticSpeechRecognition.ts +3 -3
- package/src/tasks/audio/textToSpeech.ts +2 -2
- package/src/tasks/custom/request.ts +7 -32
- package/src/tasks/custom/streamingRequest.ts +5 -85
- package/src/tasks/cv/imageClassification.ts +2 -2
- package/src/tasks/cv/imageSegmentation.ts +2 -2
- package/src/tasks/cv/imageToImage.ts +2 -2
- package/src/tasks/cv/imageToText.ts +7 -9
- package/src/tasks/cv/objectDetection.ts +4 -4
- package/src/tasks/cv/textToImage.ts +3 -3
- package/src/tasks/cv/textToVideo.ts +23 -36
- package/src/tasks/cv/zeroShotImageClassification.ts +4 -5
- package/src/tasks/multimodal/documentQuestionAnswering.ts +13 -13
- package/src/tasks/multimodal/visualQuestionAnswering.ts +4 -2
- package/src/tasks/nlp/chatCompletion.ts +3 -4
- package/src/tasks/nlp/chatCompletionStream.ts +3 -3
- package/src/tasks/nlp/featureExtraction.ts +2 -2
- package/src/tasks/nlp/fillMask.ts +2 -2
- package/src/tasks/nlp/questionAnswering.ts +3 -2
- package/src/tasks/nlp/sentenceSimilarity.ts +2 -11
- package/src/tasks/nlp/summarization.ts +2 -2
- package/src/tasks/nlp/tableQuestionAnswering.ts +2 -2
- package/src/tasks/nlp/textClassification.ts +8 -9
- package/src/tasks/nlp/textGeneration.ts +16 -16
- package/src/tasks/nlp/textGenerationStream.ts +2 -2
- package/src/tasks/nlp/tokenClassification.ts +9 -10
- package/src/tasks/nlp/translation.ts +2 -2
- package/src/tasks/nlp/zeroShotClassification.ts +9 -10
- package/src/tasks/tabular/tabularClassification.ts +2 -2
- package/src/tasks/tabular/tabularRegression.ts +2 -2
- package/src/types.ts +13 -2
- package/src/utils/request.ts +161 -0
package/dist/index.cjs
CHANGED
|
@@ -101,9 +101,13 @@ __export(tasks_exports, {
|
|
|
101
101
|
// src/config.ts
|
|
102
102
|
var HF_HUB_URL = "https://huggingface.co";
|
|
103
103
|
var HF_ROUTER_URL = "https://router.huggingface.co";
|
|
104
|
+
var HF_HEADER_X_BILL_TO = "X-HF-Bill-To";
|
|
104
105
|
|
|
105
106
|
// src/providers/black-forest-labs.ts
|
|
106
107
|
var BLACK_FOREST_LABS_AI_API_BASE_URL = "https://api.us1.bfl.ai";
|
|
108
|
+
var makeBaseUrl = () => {
|
|
109
|
+
return BLACK_FOREST_LABS_AI_API_BASE_URL;
|
|
110
|
+
};
|
|
107
111
|
var makeBody = (params) => {
|
|
108
112
|
return params.args;
|
|
109
113
|
};
|
|
@@ -118,7 +122,7 @@ var makeUrl = (params) => {
|
|
|
118
122
|
return `${params.baseUrl}/v1/${params.model}`;
|
|
119
123
|
};
|
|
120
124
|
var BLACK_FOREST_LABS_CONFIG = {
|
|
121
|
-
|
|
125
|
+
makeBaseUrl,
|
|
122
126
|
makeBody,
|
|
123
127
|
makeHeaders,
|
|
124
128
|
makeUrl
|
|
@@ -126,6 +130,9 @@ var BLACK_FOREST_LABS_CONFIG = {
|
|
|
126
130
|
|
|
127
131
|
// src/providers/cerebras.ts
|
|
128
132
|
var CEREBRAS_API_BASE_URL = "https://api.cerebras.ai";
|
|
133
|
+
var makeBaseUrl2 = () => {
|
|
134
|
+
return CEREBRAS_API_BASE_URL;
|
|
135
|
+
};
|
|
129
136
|
var makeBody2 = (params) => {
|
|
130
137
|
return {
|
|
131
138
|
...params.args,
|
|
@@ -139,7 +146,7 @@ var makeUrl2 = (params) => {
|
|
|
139
146
|
return `${params.baseUrl}/v1/chat/completions`;
|
|
140
147
|
};
|
|
141
148
|
var CEREBRAS_CONFIG = {
|
|
142
|
-
|
|
149
|
+
makeBaseUrl: makeBaseUrl2,
|
|
143
150
|
makeBody: makeBody2,
|
|
144
151
|
makeHeaders: makeHeaders2,
|
|
145
152
|
makeUrl: makeUrl2
|
|
@@ -147,6 +154,9 @@ var CEREBRAS_CONFIG = {
|
|
|
147
154
|
|
|
148
155
|
// src/providers/cohere.ts
|
|
149
156
|
var COHERE_API_BASE_URL = "https://api.cohere.com";
|
|
157
|
+
var makeBaseUrl3 = () => {
|
|
158
|
+
return COHERE_API_BASE_URL;
|
|
159
|
+
};
|
|
150
160
|
var makeBody3 = (params) => {
|
|
151
161
|
return {
|
|
152
162
|
...params.args,
|
|
@@ -160,14 +170,40 @@ var makeUrl3 = (params) => {
|
|
|
160
170
|
return `${params.baseUrl}/compatibility/v1/chat/completions`;
|
|
161
171
|
};
|
|
162
172
|
var COHERE_CONFIG = {
|
|
163
|
-
|
|
173
|
+
makeBaseUrl: makeBaseUrl3,
|
|
164
174
|
makeBody: makeBody3,
|
|
165
175
|
makeHeaders: makeHeaders3,
|
|
166
176
|
makeUrl: makeUrl3
|
|
167
177
|
};
|
|
168
178
|
|
|
179
|
+
// src/lib/InferenceOutputError.ts
|
|
180
|
+
var InferenceOutputError = class extends TypeError {
|
|
181
|
+
constructor(message) {
|
|
182
|
+
super(
|
|
183
|
+
`Invalid inference output: ${message}. Use the 'request' method with the same parameters to do a custom call with no type checking.`
|
|
184
|
+
);
|
|
185
|
+
this.name = "InferenceOutputError";
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
// src/lib/isUrl.ts
|
|
190
|
+
function isUrl(modelOrUrl) {
|
|
191
|
+
return /^http(s?):/.test(modelOrUrl) || modelOrUrl.startsWith("/");
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// src/utils/delay.ts
|
|
195
|
+
function delay(ms) {
|
|
196
|
+
return new Promise((resolve) => {
|
|
197
|
+
setTimeout(() => resolve(), ms);
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
169
201
|
// src/providers/fal-ai.ts
|
|
170
202
|
var FAL_AI_API_BASE_URL = "https://fal.run";
|
|
203
|
+
var FAL_AI_API_BASE_URL_QUEUE = "https://queue.fal.run";
|
|
204
|
+
var makeBaseUrl4 = (task) => {
|
|
205
|
+
return task === "text-to-video" ? FAL_AI_API_BASE_URL_QUEUE : FAL_AI_API_BASE_URL;
|
|
206
|
+
};
|
|
171
207
|
var makeBody4 = (params) => {
|
|
172
208
|
return params.args;
|
|
173
209
|
};
|
|
@@ -177,17 +213,64 @@ var makeHeaders4 = (params) => {
|
|
|
177
213
|
};
|
|
178
214
|
};
|
|
179
215
|
var makeUrl4 = (params) => {
|
|
180
|
-
|
|
216
|
+
const baseUrl = `${params.baseUrl}/${params.model}`;
|
|
217
|
+
if (params.authMethod !== "provider-key" && params.task === "text-to-video") {
|
|
218
|
+
return `${baseUrl}?_subdomain=queue`;
|
|
219
|
+
}
|
|
220
|
+
return baseUrl;
|
|
181
221
|
};
|
|
182
222
|
var FAL_AI_CONFIG = {
|
|
183
|
-
|
|
223
|
+
makeBaseUrl: makeBaseUrl4,
|
|
184
224
|
makeBody: makeBody4,
|
|
185
225
|
makeHeaders: makeHeaders4,
|
|
186
226
|
makeUrl: makeUrl4
|
|
187
227
|
};
|
|
228
|
+
async function pollFalResponse(res, url, headers) {
|
|
229
|
+
const requestId = res.request_id;
|
|
230
|
+
if (!requestId) {
|
|
231
|
+
throw new InferenceOutputError("No request ID found in the response");
|
|
232
|
+
}
|
|
233
|
+
let status = res.status;
|
|
234
|
+
const parsedUrl = new URL(url);
|
|
235
|
+
const baseUrl = `${parsedUrl.protocol}//${parsedUrl.host}${parsedUrl.host === "router.huggingface.co" ? "/fal-ai" : ""}`;
|
|
236
|
+
const modelId = new URL(res.response_url).pathname;
|
|
237
|
+
const queryParams = parsedUrl.search;
|
|
238
|
+
const statusUrl = `${baseUrl}${modelId}/status${queryParams}`;
|
|
239
|
+
const resultUrl = `${baseUrl}${modelId}${queryParams}`;
|
|
240
|
+
while (status !== "COMPLETED") {
|
|
241
|
+
await delay(500);
|
|
242
|
+
const statusResponse = await fetch(statusUrl, { headers });
|
|
243
|
+
if (!statusResponse.ok) {
|
|
244
|
+
throw new InferenceOutputError("Failed to fetch response status from fal-ai API");
|
|
245
|
+
}
|
|
246
|
+
try {
|
|
247
|
+
status = (await statusResponse.json()).status;
|
|
248
|
+
} catch (error) {
|
|
249
|
+
throw new InferenceOutputError("Failed to parse status response from fal-ai API");
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
const resultResponse = await fetch(resultUrl, { headers });
|
|
253
|
+
let result;
|
|
254
|
+
try {
|
|
255
|
+
result = await resultResponse.json();
|
|
256
|
+
} catch (error) {
|
|
257
|
+
throw new InferenceOutputError("Failed to parse result response from fal-ai API");
|
|
258
|
+
}
|
|
259
|
+
if (typeof result === "object" && !!result && "video" in result && typeof result.video === "object" && !!result.video && "url" in result.video && typeof result.video.url === "string" && isUrl(result.video.url)) {
|
|
260
|
+
const urlResponse = await fetch(result.video.url);
|
|
261
|
+
return await urlResponse.blob();
|
|
262
|
+
} else {
|
|
263
|
+
throw new InferenceOutputError(
|
|
264
|
+
"Expected { video: { url: string } } result format, got instead: " + JSON.stringify(result)
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
188
268
|
|
|
189
269
|
// src/providers/fireworks-ai.ts
|
|
190
270
|
var FIREWORKS_AI_API_BASE_URL = "https://api.fireworks.ai";
|
|
271
|
+
var makeBaseUrl5 = () => {
|
|
272
|
+
return FIREWORKS_AI_API_BASE_URL;
|
|
273
|
+
};
|
|
191
274
|
var makeBody5 = (params) => {
|
|
192
275
|
return {
|
|
193
276
|
...params.args,
|
|
@@ -204,13 +287,16 @@ var makeUrl5 = (params) => {
|
|
|
204
287
|
return `${params.baseUrl}/inference`;
|
|
205
288
|
};
|
|
206
289
|
var FIREWORKS_AI_CONFIG = {
|
|
207
|
-
|
|
290
|
+
makeBaseUrl: makeBaseUrl5,
|
|
208
291
|
makeBody: makeBody5,
|
|
209
292
|
makeHeaders: makeHeaders5,
|
|
210
293
|
makeUrl: makeUrl5
|
|
211
294
|
};
|
|
212
295
|
|
|
213
296
|
// src/providers/hf-inference.ts
|
|
297
|
+
var makeBaseUrl6 = () => {
|
|
298
|
+
return `${HF_ROUTER_URL}/hf-inference`;
|
|
299
|
+
};
|
|
214
300
|
var makeBody6 = (params) => {
|
|
215
301
|
return {
|
|
216
302
|
...params.args,
|
|
@@ -230,7 +316,7 @@ var makeUrl6 = (params) => {
|
|
|
230
316
|
return `${params.baseUrl}/models/${params.model}`;
|
|
231
317
|
};
|
|
232
318
|
var HF_INFERENCE_CONFIG = {
|
|
233
|
-
|
|
319
|
+
makeBaseUrl: makeBaseUrl6,
|
|
234
320
|
makeBody: makeBody6,
|
|
235
321
|
makeHeaders: makeHeaders6,
|
|
236
322
|
makeUrl: makeUrl6
|
|
@@ -238,6 +324,9 @@ var HF_INFERENCE_CONFIG = {
|
|
|
238
324
|
|
|
239
325
|
// src/providers/hyperbolic.ts
|
|
240
326
|
var HYPERBOLIC_API_BASE_URL = "https://api.hyperbolic.xyz";
|
|
327
|
+
var makeBaseUrl7 = () => {
|
|
328
|
+
return HYPERBOLIC_API_BASE_URL;
|
|
329
|
+
};
|
|
241
330
|
var makeBody7 = (params) => {
|
|
242
331
|
return {
|
|
243
332
|
...params.args,
|
|
@@ -254,7 +343,7 @@ var makeUrl7 = (params) => {
|
|
|
254
343
|
return `${params.baseUrl}/v1/chat/completions`;
|
|
255
344
|
};
|
|
256
345
|
var HYPERBOLIC_CONFIG = {
|
|
257
|
-
|
|
346
|
+
makeBaseUrl: makeBaseUrl7,
|
|
258
347
|
makeBody: makeBody7,
|
|
259
348
|
makeHeaders: makeHeaders7,
|
|
260
349
|
makeUrl: makeUrl7
|
|
@@ -262,6 +351,9 @@ var HYPERBOLIC_CONFIG = {
|
|
|
262
351
|
|
|
263
352
|
// src/providers/nebius.ts
|
|
264
353
|
var NEBIUS_API_BASE_URL = "https://api.studio.nebius.ai";
|
|
354
|
+
var makeBaseUrl8 = () => {
|
|
355
|
+
return NEBIUS_API_BASE_URL;
|
|
356
|
+
};
|
|
265
357
|
var makeBody8 = (params) => {
|
|
266
358
|
return {
|
|
267
359
|
...params.args,
|
|
@@ -284,7 +376,7 @@ var makeUrl8 = (params) => {
|
|
|
284
376
|
return params.baseUrl;
|
|
285
377
|
};
|
|
286
378
|
var NEBIUS_CONFIG = {
|
|
287
|
-
|
|
379
|
+
makeBaseUrl: makeBaseUrl8,
|
|
288
380
|
makeBody: makeBody8,
|
|
289
381
|
makeHeaders: makeHeaders8,
|
|
290
382
|
makeUrl: makeUrl8
|
|
@@ -292,6 +384,9 @@ var NEBIUS_CONFIG = {
|
|
|
292
384
|
|
|
293
385
|
// src/providers/novita.ts
|
|
294
386
|
var NOVITA_API_BASE_URL = "https://api.novita.ai";
|
|
387
|
+
var makeBaseUrl9 = () => {
|
|
388
|
+
return NOVITA_API_BASE_URL;
|
|
389
|
+
};
|
|
295
390
|
var makeBody9 = (params) => {
|
|
296
391
|
return {
|
|
297
392
|
...params.args,
|
|
@@ -312,7 +407,7 @@ var makeUrl9 = (params) => {
|
|
|
312
407
|
return params.baseUrl;
|
|
313
408
|
};
|
|
314
409
|
var NOVITA_CONFIG = {
|
|
315
|
-
|
|
410
|
+
makeBaseUrl: makeBaseUrl9,
|
|
316
411
|
makeBody: makeBody9,
|
|
317
412
|
makeHeaders: makeHeaders9,
|
|
318
413
|
makeUrl: makeUrl9
|
|
@@ -320,6 +415,9 @@ var NOVITA_CONFIG = {
|
|
|
320
415
|
|
|
321
416
|
// src/providers/replicate.ts
|
|
322
417
|
var REPLICATE_API_BASE_URL = "https://api.replicate.com";
|
|
418
|
+
var makeBaseUrl10 = () => {
|
|
419
|
+
return REPLICATE_API_BASE_URL;
|
|
420
|
+
};
|
|
323
421
|
var makeBody10 = (params) => {
|
|
324
422
|
return {
|
|
325
423
|
input: params.args,
|
|
@@ -336,7 +434,7 @@ var makeUrl10 = (params) => {
|
|
|
336
434
|
return `${params.baseUrl}/v1/models/${params.model}/predictions`;
|
|
337
435
|
};
|
|
338
436
|
var REPLICATE_CONFIG = {
|
|
339
|
-
|
|
437
|
+
makeBaseUrl: makeBaseUrl10,
|
|
340
438
|
makeBody: makeBody10,
|
|
341
439
|
makeHeaders: makeHeaders10,
|
|
342
440
|
makeUrl: makeUrl10
|
|
@@ -344,6 +442,9 @@ var REPLICATE_CONFIG = {
|
|
|
344
442
|
|
|
345
443
|
// src/providers/sambanova.ts
|
|
346
444
|
var SAMBANOVA_API_BASE_URL = "https://api.sambanova.ai";
|
|
445
|
+
var makeBaseUrl11 = () => {
|
|
446
|
+
return SAMBANOVA_API_BASE_URL;
|
|
447
|
+
};
|
|
347
448
|
var makeBody11 = (params) => {
|
|
348
449
|
return {
|
|
349
450
|
...params.args,
|
|
@@ -360,7 +461,7 @@ var makeUrl11 = (params) => {
|
|
|
360
461
|
return params.baseUrl;
|
|
361
462
|
};
|
|
362
463
|
var SAMBANOVA_CONFIG = {
|
|
363
|
-
|
|
464
|
+
makeBaseUrl: makeBaseUrl11,
|
|
364
465
|
makeBody: makeBody11,
|
|
365
466
|
makeHeaders: makeHeaders11,
|
|
366
467
|
makeUrl: makeUrl11
|
|
@@ -368,6 +469,9 @@ var SAMBANOVA_CONFIG = {
|
|
|
368
469
|
|
|
369
470
|
// src/providers/together.ts
|
|
370
471
|
var TOGETHER_API_BASE_URL = "https://api.together.xyz";
|
|
472
|
+
var makeBaseUrl12 = () => {
|
|
473
|
+
return TOGETHER_API_BASE_URL;
|
|
474
|
+
};
|
|
371
475
|
var makeBody12 = (params) => {
|
|
372
476
|
return {
|
|
373
477
|
...params.args,
|
|
@@ -390,7 +494,7 @@ var makeUrl12 = (params) => {
|
|
|
390
494
|
return params.baseUrl;
|
|
391
495
|
};
|
|
392
496
|
var TOGETHER_CONFIG = {
|
|
393
|
-
|
|
497
|
+
makeBaseUrl: makeBaseUrl12,
|
|
394
498
|
makeBody: makeBody12,
|
|
395
499
|
makeHeaders: makeHeaders12,
|
|
396
500
|
makeUrl: makeUrl12
|
|
@@ -398,6 +502,9 @@ var TOGETHER_CONFIG = {
|
|
|
398
502
|
|
|
399
503
|
// src/providers/openai.ts
|
|
400
504
|
var OPENAI_API_BASE_URL = "https://api.openai.com";
|
|
505
|
+
var makeBaseUrl13 = () => {
|
|
506
|
+
return OPENAI_API_BASE_URL;
|
|
507
|
+
};
|
|
401
508
|
var makeBody13 = (params) => {
|
|
402
509
|
if (!params.chatCompletion) {
|
|
403
510
|
throw new Error("OpenAI only supports chat completions.");
|
|
@@ -417,21 +524,16 @@ var makeUrl13 = (params) => {
|
|
|
417
524
|
return `${params.baseUrl}/v1/chat/completions`;
|
|
418
525
|
};
|
|
419
526
|
var OPENAI_CONFIG = {
|
|
420
|
-
|
|
527
|
+
makeBaseUrl: makeBaseUrl13,
|
|
421
528
|
makeBody: makeBody13,
|
|
422
529
|
makeHeaders: makeHeaders13,
|
|
423
530
|
makeUrl: makeUrl13,
|
|
424
531
|
clientSideRoutingOnly: true
|
|
425
532
|
};
|
|
426
533
|
|
|
427
|
-
// src/lib/isUrl.ts
|
|
428
|
-
function isUrl(modelOrUrl) {
|
|
429
|
-
return /^http(s?):/.test(modelOrUrl) || modelOrUrl.startsWith("/");
|
|
430
|
-
}
|
|
431
|
-
|
|
432
534
|
// package.json
|
|
433
535
|
var name = "@huggingface/inference";
|
|
434
|
-
var version = "3.
|
|
536
|
+
var version = "3.7.0";
|
|
435
537
|
|
|
436
538
|
// src/providers/consts.ts
|
|
437
539
|
var HARDCODED_MODEL_ID_MAPPING = {
|
|
@@ -553,7 +655,7 @@ function makeRequestOptionsFromResolvedModel(resolvedModel, args, options) {
|
|
|
553
655
|
const { accessToken, endpointUrl, provider: maybeProvider, model, ...remainingArgs } = args;
|
|
554
656
|
const provider = maybeProvider ?? "hf-inference";
|
|
555
657
|
const providerConfig = providerConfigs[provider];
|
|
556
|
-
const { includeCredentials, task, chatCompletion: chatCompletion2, signal } = options ?? {};
|
|
658
|
+
const { includeCredentials, task, chatCompletion: chatCompletion2, signal, billTo } = options ?? {};
|
|
557
659
|
const authMethod = (() => {
|
|
558
660
|
if (providerConfig.clientSideRoutingOnly) {
|
|
559
661
|
if (accessToken && accessToken.startsWith("hf_")) {
|
|
@@ -570,7 +672,8 @@ function makeRequestOptionsFromResolvedModel(resolvedModel, args, options) {
|
|
|
570
672
|
return "none";
|
|
571
673
|
})();
|
|
572
674
|
const url = endpointUrl ? chatCompletion2 ? endpointUrl + `/v1/chat/completions` : endpointUrl : providerConfig.makeUrl({
|
|
573
|
-
|
|
675
|
+
authMethod,
|
|
676
|
+
baseUrl: authMethod !== "provider-key" ? HF_HUB_INFERENCE_PROXY_TEMPLATE.replace("{{PROVIDER}}", provider) : providerConfig.makeBaseUrl(task),
|
|
574
677
|
model: resolvedModel,
|
|
575
678
|
chatCompletion: chatCompletion2,
|
|
576
679
|
task
|
|
@@ -580,6 +683,9 @@ function makeRequestOptionsFromResolvedModel(resolvedModel, args, options) {
|
|
|
580
683
|
accessToken,
|
|
581
684
|
authMethod
|
|
582
685
|
});
|
|
686
|
+
if (billTo) {
|
|
687
|
+
headers[HF_HEADER_X_BILL_TO] = billTo;
|
|
688
|
+
}
|
|
583
689
|
if (!binary) {
|
|
584
690
|
headers["Content-Type"] = "application/json";
|
|
585
691
|
}
|
|
@@ -633,37 +739,6 @@ function removeProviderPrefix(model, provider) {
|
|
|
633
739
|
return model.slice(provider.length + 1);
|
|
634
740
|
}
|
|
635
741
|
|
|
636
|
-
// src/tasks/custom/request.ts
|
|
637
|
-
async function request(args, options) {
|
|
638
|
-
const { url, info } = await makeRequestOptions(args, options);
|
|
639
|
-
const response = await (options?.fetch ?? fetch)(url, info);
|
|
640
|
-
if (options?.retry_on_error !== false && response.status === 503) {
|
|
641
|
-
return request(args, options);
|
|
642
|
-
}
|
|
643
|
-
if (!response.ok) {
|
|
644
|
-
const contentType = response.headers.get("Content-Type");
|
|
645
|
-
if (["application/json", "application/problem+json"].some((ct) => contentType?.startsWith(ct))) {
|
|
646
|
-
const output = await response.json();
|
|
647
|
-
if ([400, 422, 404, 500].includes(response.status) && options?.chatCompletion) {
|
|
648
|
-
throw new Error(
|
|
649
|
-
`Server ${args.model} does not seem to support chat completion. Error: ${JSON.stringify(output.error)}`
|
|
650
|
-
);
|
|
651
|
-
}
|
|
652
|
-
if (output.error || output.detail) {
|
|
653
|
-
throw new Error(JSON.stringify(output.error ?? output.detail));
|
|
654
|
-
} else {
|
|
655
|
-
throw new Error(output);
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
const message = contentType?.startsWith("text/plain;") ? await response.text() : void 0;
|
|
659
|
-
throw new Error(message ?? "An error occurred while fetching the blob");
|
|
660
|
-
}
|
|
661
|
-
if (response.headers.get("Content-Type")?.startsWith("application/json")) {
|
|
662
|
-
return await response.json();
|
|
663
|
-
}
|
|
664
|
-
return await response.blob();
|
|
665
|
-
}
|
|
666
|
-
|
|
667
742
|
// src/vendor/fetch-event-source/parse.ts
|
|
668
743
|
function getLines(onLine) {
|
|
669
744
|
let buffer;
|
|
@@ -763,12 +838,44 @@ function newMessage() {
|
|
|
763
838
|
};
|
|
764
839
|
}
|
|
765
840
|
|
|
766
|
-
// src/
|
|
767
|
-
async function
|
|
841
|
+
// src/utils/request.ts
|
|
842
|
+
async function innerRequest(args, options) {
|
|
843
|
+
const { url, info } = await makeRequestOptions(args, options);
|
|
844
|
+
const response = await (options?.fetch ?? fetch)(url, info);
|
|
845
|
+
const requestContext = { url, info };
|
|
846
|
+
if (options?.retry_on_error !== false && response.status === 503) {
|
|
847
|
+
return innerRequest(args, options);
|
|
848
|
+
}
|
|
849
|
+
if (!response.ok) {
|
|
850
|
+
const contentType = response.headers.get("Content-Type");
|
|
851
|
+
if (["application/json", "application/problem+json"].some((ct) => contentType?.startsWith(ct))) {
|
|
852
|
+
const output = await response.json();
|
|
853
|
+
if ([400, 422, 404, 500].includes(response.status) && options?.chatCompletion) {
|
|
854
|
+
throw new Error(
|
|
855
|
+
`Server ${args.model} does not seem to support chat completion. Error: ${JSON.stringify(output.error)}`
|
|
856
|
+
);
|
|
857
|
+
}
|
|
858
|
+
if (output.error || output.detail) {
|
|
859
|
+
throw new Error(JSON.stringify(output.error ?? output.detail));
|
|
860
|
+
} else {
|
|
861
|
+
throw new Error(output);
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
const message = contentType?.startsWith("text/plain;") ? await response.text() : void 0;
|
|
865
|
+
throw new Error(message ?? "An error occurred while fetching the blob");
|
|
866
|
+
}
|
|
867
|
+
if (response.headers.get("Content-Type")?.startsWith("application/json")) {
|
|
868
|
+
const data = await response.json();
|
|
869
|
+
return { data, requestContext };
|
|
870
|
+
}
|
|
871
|
+
const blob = await response.blob();
|
|
872
|
+
return { data: blob, requestContext };
|
|
873
|
+
}
|
|
874
|
+
async function* innerStreamingRequest(args, options) {
|
|
768
875
|
const { url, info } = await makeRequestOptions({ ...args, stream: true }, options);
|
|
769
876
|
const response = await (options?.fetch ?? fetch)(url, info);
|
|
770
877
|
if (options?.retry_on_error !== false && response.status === 503) {
|
|
771
|
-
return yield*
|
|
878
|
+
return yield* innerStreamingRequest(args, options);
|
|
772
879
|
}
|
|
773
880
|
if (!response.ok) {
|
|
774
881
|
if (response.headers.get("Content-Type")?.startsWith("application/json")) {
|
|
@@ -782,6 +889,9 @@ async function* streamingRequest(args, options) {
|
|
|
782
889
|
if (output.error && "message" in output.error && typeof output.error.message === "string") {
|
|
783
890
|
throw new Error(output.error.message);
|
|
784
891
|
}
|
|
892
|
+
if (typeof output.message === "string") {
|
|
893
|
+
throw new Error(output.message);
|
|
894
|
+
}
|
|
785
895
|
}
|
|
786
896
|
throw new Error(`Server response contains error: ${response.status}`);
|
|
787
897
|
}
|
|
@@ -834,15 +944,22 @@ async function* streamingRequest(args, options) {
|
|
|
834
944
|
}
|
|
835
945
|
}
|
|
836
946
|
|
|
837
|
-
// src/
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
947
|
+
// src/tasks/custom/request.ts
|
|
948
|
+
async function request(args, options) {
|
|
949
|
+
console.warn(
|
|
950
|
+
"The request method is deprecated and will be removed in a future version of huggingface.js. Use specific task functions instead."
|
|
951
|
+
);
|
|
952
|
+
const result = await innerRequest(args, options);
|
|
953
|
+
return result.data;
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
// src/tasks/custom/streamingRequest.ts
|
|
957
|
+
async function* streamingRequest(args, options) {
|
|
958
|
+
console.warn(
|
|
959
|
+
"The streamingRequest method is deprecated and will be removed in a future version of huggingface.js. Use specific task functions instead."
|
|
960
|
+
);
|
|
961
|
+
yield* innerStreamingRequest(args, options);
|
|
962
|
+
}
|
|
846
963
|
|
|
847
964
|
// src/utils/pick.ts
|
|
848
965
|
function pick(o, props) {
|
|
@@ -879,7 +996,7 @@ function preparePayload(args) {
|
|
|
879
996
|
// src/tasks/audio/audioClassification.ts
|
|
880
997
|
async function audioClassification(args, options) {
|
|
881
998
|
const payload = preparePayload(args);
|
|
882
|
-
const res = await
|
|
999
|
+
const { data: res } = await innerRequest(payload, {
|
|
883
1000
|
...options,
|
|
884
1001
|
task: "audio-classification"
|
|
885
1002
|
});
|
|
@@ -906,7 +1023,7 @@ function base64FromBytes(arr) {
|
|
|
906
1023
|
// src/tasks/audio/automaticSpeechRecognition.ts
|
|
907
1024
|
async function automaticSpeechRecognition(args, options) {
|
|
908
1025
|
const payload = await buildPayload(args);
|
|
909
|
-
const res = await
|
|
1026
|
+
const { data: res } = await innerRequest(payload, {
|
|
910
1027
|
...options,
|
|
911
1028
|
task: "automatic-speech-recognition"
|
|
912
1029
|
});
|
|
@@ -950,7 +1067,7 @@ async function textToSpeech(args, options) {
|
|
|
950
1067
|
...args.parameters,
|
|
951
1068
|
text: args.inputs
|
|
952
1069
|
} : args;
|
|
953
|
-
const res = await
|
|
1070
|
+
const { data: res } = await innerRequest(payload, {
|
|
954
1071
|
...options,
|
|
955
1072
|
task: "text-to-speech"
|
|
956
1073
|
});
|
|
@@ -976,7 +1093,7 @@ async function textToSpeech(args, options) {
|
|
|
976
1093
|
// src/tasks/audio/audioToAudio.ts
|
|
977
1094
|
async function audioToAudio(args, options) {
|
|
978
1095
|
const payload = preparePayload(args);
|
|
979
|
-
const res = await
|
|
1096
|
+
const { data: res } = await innerRequest(payload, {
|
|
980
1097
|
...options,
|
|
981
1098
|
task: "audio-to-audio"
|
|
982
1099
|
});
|
|
@@ -1002,7 +1119,7 @@ function preparePayload2(args) {
|
|
|
1002
1119
|
// src/tasks/cv/imageClassification.ts
|
|
1003
1120
|
async function imageClassification(args, options) {
|
|
1004
1121
|
const payload = preparePayload2(args);
|
|
1005
|
-
const res = await
|
|
1122
|
+
const { data: res } = await innerRequest(payload, {
|
|
1006
1123
|
...options,
|
|
1007
1124
|
task: "image-classification"
|
|
1008
1125
|
});
|
|
@@ -1016,7 +1133,7 @@ async function imageClassification(args, options) {
|
|
|
1016
1133
|
// src/tasks/cv/imageSegmentation.ts
|
|
1017
1134
|
async function imageSegmentation(args, options) {
|
|
1018
1135
|
const payload = preparePayload2(args);
|
|
1019
|
-
const res = await
|
|
1136
|
+
const { data: res } = await innerRequest(payload, {
|
|
1020
1137
|
...options,
|
|
1021
1138
|
task: "image-segmentation"
|
|
1022
1139
|
});
|
|
@@ -1030,20 +1147,20 @@ async function imageSegmentation(args, options) {
|
|
|
1030
1147
|
// src/tasks/cv/imageToText.ts
|
|
1031
1148
|
async function imageToText(args, options) {
|
|
1032
1149
|
const payload = preparePayload2(args);
|
|
1033
|
-
const res =
|
|
1150
|
+
const { data: res } = await innerRequest(payload, {
|
|
1034
1151
|
...options,
|
|
1035
1152
|
task: "image-to-text"
|
|
1036
|
-
})
|
|
1037
|
-
if (typeof res?.generated_text !== "string") {
|
|
1153
|
+
});
|
|
1154
|
+
if (typeof res?.[0]?.generated_text !== "string") {
|
|
1038
1155
|
throw new InferenceOutputError("Expected {generated_text: string}");
|
|
1039
1156
|
}
|
|
1040
|
-
return res;
|
|
1157
|
+
return res?.[0];
|
|
1041
1158
|
}
|
|
1042
1159
|
|
|
1043
1160
|
// src/tasks/cv/objectDetection.ts
|
|
1044
1161
|
async function objectDetection(args, options) {
|
|
1045
1162
|
const payload = preparePayload2(args);
|
|
1046
|
-
const res = await
|
|
1163
|
+
const { data: res } = await innerRequest(payload, {
|
|
1047
1164
|
...options,
|
|
1048
1165
|
task: "object-detection"
|
|
1049
1166
|
});
|
|
@@ -1058,13 +1175,6 @@ async function objectDetection(args, options) {
|
|
|
1058
1175
|
return res;
|
|
1059
1176
|
}
|
|
1060
1177
|
|
|
1061
|
-
// src/utils/delay.ts
|
|
1062
|
-
function delay(ms) {
|
|
1063
|
-
return new Promise((resolve) => {
|
|
1064
|
-
setTimeout(() => resolve(), ms);
|
|
1065
|
-
});
|
|
1066
|
-
}
|
|
1067
|
-
|
|
1068
1178
|
// src/tasks/cv/textToImage.ts
|
|
1069
1179
|
function getResponseFormatArg(provider) {
|
|
1070
1180
|
switch (provider) {
|
|
@@ -1087,7 +1197,7 @@ async function textToImage(args, options) {
|
|
|
1087
1197
|
...getResponseFormatArg(args.provider),
|
|
1088
1198
|
prompt: args.inputs
|
|
1089
1199
|
};
|
|
1090
|
-
const res = await
|
|
1200
|
+
const { data: res } = await innerRequest(payload, {
|
|
1091
1201
|
...options,
|
|
1092
1202
|
task: "text-to-image"
|
|
1093
1203
|
});
|
|
@@ -1176,7 +1286,7 @@ async function imageToImage(args, options) {
|
|
|
1176
1286
|
)
|
|
1177
1287
|
};
|
|
1178
1288
|
}
|
|
1179
|
-
const res = await
|
|
1289
|
+
const { data: res } = await innerRequest(reqArgs, {
|
|
1180
1290
|
...options,
|
|
1181
1291
|
task: "image-to-image"
|
|
1182
1292
|
});
|
|
@@ -1211,7 +1321,7 @@ async function preparePayload3(args) {
|
|
|
1211
1321
|
}
|
|
1212
1322
|
async function zeroShotImageClassification(args, options) {
|
|
1213
1323
|
const payload = await preparePayload3(args);
|
|
1214
|
-
const res = await
|
|
1324
|
+
const { data: res } = await innerRequest(payload, {
|
|
1215
1325
|
...options,
|
|
1216
1326
|
task: "zero-shot-image-classification"
|
|
1217
1327
|
});
|
|
@@ -1231,37 +1341,36 @@ async function textToVideo(args, options) {
|
|
|
1231
1341
|
);
|
|
1232
1342
|
}
|
|
1233
1343
|
const payload = args.provider === "fal-ai" || args.provider === "replicate" || args.provider === "novita" ? { ...omit(args, ["inputs", "parameters"]), ...args.parameters, prompt: args.inputs } : args;
|
|
1234
|
-
const
|
|
1344
|
+
const { data, requestContext } = await innerRequest(payload, {
|
|
1235
1345
|
...options,
|
|
1236
1346
|
task: "text-to-video"
|
|
1237
1347
|
});
|
|
1238
1348
|
if (args.provider === "fal-ai") {
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
return await urlResponse.blob();
|
|
1349
|
+
return await pollFalResponse(
|
|
1350
|
+
data,
|
|
1351
|
+
requestContext.url,
|
|
1352
|
+
requestContext.info.headers
|
|
1353
|
+
);
|
|
1245
1354
|
} else if (args.provider === "novita") {
|
|
1246
|
-
const isValidOutput = typeof
|
|
1355
|
+
const isValidOutput = typeof data === "object" && !!data && "video" in data && typeof data.video === "object" && !!data.video && "video_url" in data.video && typeof data.video.video_url === "string" && isUrl(data.video.video_url);
|
|
1247
1356
|
if (!isValidOutput) {
|
|
1248
1357
|
throw new InferenceOutputError("Expected { video: { video_url: string } }");
|
|
1249
1358
|
}
|
|
1250
|
-
const urlResponse = await fetch(
|
|
1359
|
+
const urlResponse = await fetch(data.video.video_url);
|
|
1251
1360
|
return await urlResponse.blob();
|
|
1252
1361
|
} else {
|
|
1253
|
-
const isValidOutput = typeof
|
|
1362
|
+
const isValidOutput = typeof data === "object" && !!data && "output" in data && typeof data.output === "string" && isUrl(data.output);
|
|
1254
1363
|
if (!isValidOutput) {
|
|
1255
1364
|
throw new InferenceOutputError("Expected { output: string }");
|
|
1256
1365
|
}
|
|
1257
|
-
const urlResponse = await fetch(
|
|
1366
|
+
const urlResponse = await fetch(data.output);
|
|
1258
1367
|
return await urlResponse.blob();
|
|
1259
1368
|
}
|
|
1260
1369
|
}
|
|
1261
1370
|
|
|
1262
1371
|
// src/tasks/nlp/featureExtraction.ts
|
|
1263
1372
|
async function featureExtraction(args, options) {
|
|
1264
|
-
const res = await
|
|
1373
|
+
const { data: res } = await innerRequest(args, {
|
|
1265
1374
|
...options,
|
|
1266
1375
|
task: "feature-extraction"
|
|
1267
1376
|
});
|
|
@@ -1284,7 +1393,7 @@ async function featureExtraction(args, options) {
|
|
|
1284
1393
|
|
|
1285
1394
|
// src/tasks/nlp/fillMask.ts
|
|
1286
1395
|
async function fillMask(args, options) {
|
|
1287
|
-
const res = await
|
|
1396
|
+
const { data: res } = await innerRequest(args, {
|
|
1288
1397
|
...options,
|
|
1289
1398
|
task: "fill-mask"
|
|
1290
1399
|
});
|
|
@@ -1301,7 +1410,7 @@ async function fillMask(args, options) {
|
|
|
1301
1410
|
|
|
1302
1411
|
// src/tasks/nlp/questionAnswering.ts
|
|
1303
1412
|
async function questionAnswering(args, options) {
|
|
1304
|
-
const res = await
|
|
1413
|
+
const { data: res } = await innerRequest(args, {
|
|
1305
1414
|
...options,
|
|
1306
1415
|
task: "question-answering"
|
|
1307
1416
|
});
|
|
@@ -1316,7 +1425,7 @@ async function questionAnswering(args, options) {
|
|
|
1316
1425
|
|
|
1317
1426
|
// src/tasks/nlp/sentenceSimilarity.ts
|
|
1318
1427
|
async function sentenceSimilarity(args, options) {
|
|
1319
|
-
const res = await
|
|
1428
|
+
const { data: res } = await innerRequest(args, {
|
|
1320
1429
|
...options,
|
|
1321
1430
|
task: "sentence-similarity"
|
|
1322
1431
|
});
|
|
@@ -1326,17 +1435,10 @@ async function sentenceSimilarity(args, options) {
|
|
|
1326
1435
|
}
|
|
1327
1436
|
return res;
|
|
1328
1437
|
}
|
|
1329
|
-
function prepareInput(args) {
|
|
1330
|
-
return {
|
|
1331
|
-
...omit(args, ["inputs", "parameters"]),
|
|
1332
|
-
inputs: { ...omit(args.inputs, "sourceSentence") },
|
|
1333
|
-
parameters: { source_sentence: args.inputs.sourceSentence, ...args.parameters }
|
|
1334
|
-
};
|
|
1335
|
-
}
|
|
1336
1438
|
|
|
1337
1439
|
// src/tasks/nlp/summarization.ts
|
|
1338
1440
|
async function summarization(args, options) {
|
|
1339
|
-
const res = await
|
|
1441
|
+
const { data: res } = await innerRequest(args, {
|
|
1340
1442
|
...options,
|
|
1341
1443
|
task: "summarization"
|
|
1342
1444
|
});
|
|
@@ -1349,7 +1451,7 @@ async function summarization(args, options) {
|
|
|
1349
1451
|
|
|
1350
1452
|
// src/tasks/nlp/tableQuestionAnswering.ts
|
|
1351
1453
|
async function tableQuestionAnswering(args, options) {
|
|
1352
|
-
const res = await
|
|
1454
|
+
const { data: res } = await innerRequest(args, {
|
|
1353
1455
|
...options,
|
|
1354
1456
|
task: "table-question-answering"
|
|
1355
1457
|
});
|
|
@@ -1369,15 +1471,16 @@ function validate(elem) {
|
|
|
1369
1471
|
|
|
1370
1472
|
// src/tasks/nlp/textClassification.ts
|
|
1371
1473
|
async function textClassification(args, options) {
|
|
1372
|
-
const res =
|
|
1474
|
+
const { data: res } = await innerRequest(args, {
|
|
1373
1475
|
...options,
|
|
1374
1476
|
task: "text-classification"
|
|
1375
|
-
})
|
|
1376
|
-
const
|
|
1477
|
+
});
|
|
1478
|
+
const output = res?.[0];
|
|
1479
|
+
const isValidOutput = Array.isArray(output) && output.every((x) => typeof x?.label === "string" && typeof x.score === "number");
|
|
1377
1480
|
if (!isValidOutput) {
|
|
1378
1481
|
throw new InferenceOutputError("Expected Array<{label: string, score: number}>");
|
|
1379
1482
|
}
|
|
1380
|
-
return
|
|
1483
|
+
return output;
|
|
1381
1484
|
}
|
|
1382
1485
|
|
|
1383
1486
|
// src/utils/toArray.ts
|
|
@@ -1392,7 +1495,7 @@ function toArray(obj) {
|
|
|
1392
1495
|
async function textGeneration(args, options) {
|
|
1393
1496
|
if (args.provider === "together") {
|
|
1394
1497
|
args.prompt = args.inputs;
|
|
1395
|
-
const raw = await
|
|
1498
|
+
const { data: raw } = await innerRequest(args, {
|
|
1396
1499
|
...options,
|
|
1397
1500
|
task: "text-generation"
|
|
1398
1501
|
});
|
|
@@ -1413,10 +1516,10 @@ async function textGeneration(args, options) {
|
|
|
1413
1516
|
} : void 0,
|
|
1414
1517
|
...omit(args, ["inputs", "parameters"])
|
|
1415
1518
|
};
|
|
1416
|
-
const raw = await
|
|
1519
|
+
const raw = (await innerRequest(payload, {
|
|
1417
1520
|
...options,
|
|
1418
1521
|
task: "text-generation"
|
|
1419
|
-
});
|
|
1522
|
+
})).data;
|
|
1420
1523
|
const isValidOutput = typeof raw === "object" && "choices" in raw && Array.isArray(raw?.choices) && typeof raw?.model === "string";
|
|
1421
1524
|
if (!isValidOutput) {
|
|
1422
1525
|
throw new InferenceOutputError("Expected ChatCompletionOutput");
|
|
@@ -1426,23 +1529,22 @@ async function textGeneration(args, options) {
|
|
|
1426
1529
|
generated_text: completion.message.content
|
|
1427
1530
|
};
|
|
1428
1531
|
} else {
|
|
1429
|
-
const res =
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
);
|
|
1435
|
-
const isValidOutput = Array.isArray(res) && res.every((x) => "generated_text" in x && typeof x?.generated_text === "string");
|
|
1532
|
+
const { data: res } = await innerRequest(args, {
|
|
1533
|
+
...options,
|
|
1534
|
+
task: "text-generation"
|
|
1535
|
+
});
|
|
1536
|
+
const output = toArray(res);
|
|
1537
|
+
const isValidOutput = Array.isArray(output) && output.every((x) => "generated_text" in x && typeof x?.generated_text === "string");
|
|
1436
1538
|
if (!isValidOutput) {
|
|
1437
1539
|
throw new InferenceOutputError("Expected Array<{generated_text: string}>");
|
|
1438
1540
|
}
|
|
1439
|
-
return
|
|
1541
|
+
return output?.[0];
|
|
1440
1542
|
}
|
|
1441
1543
|
}
|
|
1442
1544
|
|
|
1443
1545
|
// src/tasks/nlp/textGenerationStream.ts
|
|
1444
1546
|
async function* textGenerationStream(args, options) {
|
|
1445
|
-
yield*
|
|
1547
|
+
yield* innerStreamingRequest(args, {
|
|
1446
1548
|
...options,
|
|
1447
1549
|
task: "text-generation"
|
|
1448
1550
|
});
|
|
@@ -1450,13 +1552,12 @@ async function* textGenerationStream(args, options) {
|
|
|
1450
1552
|
|
|
1451
1553
|
// src/tasks/nlp/tokenClassification.ts
|
|
1452
1554
|
async function tokenClassification(args, options) {
|
|
1453
|
-
const res =
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
)
|
|
1459
|
-
const isValidOutput = Array.isArray(res) && res.every(
|
|
1555
|
+
const { data: res } = await innerRequest(args, {
|
|
1556
|
+
...options,
|
|
1557
|
+
task: "token-classification"
|
|
1558
|
+
});
|
|
1559
|
+
const output = toArray(res);
|
|
1560
|
+
const isValidOutput = Array.isArray(output) && output.every(
|
|
1460
1561
|
(x) => typeof x.end === "number" && typeof x.entity_group === "string" && typeof x.score === "number" && typeof x.start === "number" && typeof x.word === "string"
|
|
1461
1562
|
);
|
|
1462
1563
|
if (!isValidOutput) {
|
|
@@ -1464,12 +1565,12 @@ async function tokenClassification(args, options) {
|
|
|
1464
1565
|
"Expected Array<{end: number, entity_group: string, score: number, start: number, word: string}>"
|
|
1465
1566
|
);
|
|
1466
1567
|
}
|
|
1467
|
-
return
|
|
1568
|
+
return output;
|
|
1468
1569
|
}
|
|
1469
1570
|
|
|
1470
1571
|
// src/tasks/nlp/translation.ts
|
|
1471
1572
|
async function translation(args, options) {
|
|
1472
|
-
const res = await
|
|
1573
|
+
const { data: res } = await innerRequest(args, {
|
|
1473
1574
|
...options,
|
|
1474
1575
|
task: "translation"
|
|
1475
1576
|
});
|
|
@@ -1482,24 +1583,23 @@ async function translation(args, options) {
|
|
|
1482
1583
|
|
|
1483
1584
|
// src/tasks/nlp/zeroShotClassification.ts
|
|
1484
1585
|
async function zeroShotClassification(args, options) {
|
|
1485
|
-
const res =
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
)
|
|
1491
|
-
const isValidOutput = Array.isArray(res) && res.every(
|
|
1586
|
+
const { data: res } = await innerRequest(args, {
|
|
1587
|
+
...options,
|
|
1588
|
+
task: "zero-shot-classification"
|
|
1589
|
+
});
|
|
1590
|
+
const output = toArray(res);
|
|
1591
|
+
const isValidOutput = Array.isArray(output) && output.every(
|
|
1492
1592
|
(x) => Array.isArray(x.labels) && x.labels.every((_label) => typeof _label === "string") && Array.isArray(x.scores) && x.scores.every((_score) => typeof _score === "number") && typeof x.sequence === "string"
|
|
1493
1593
|
);
|
|
1494
1594
|
if (!isValidOutput) {
|
|
1495
1595
|
throw new InferenceOutputError("Expected Array<{labels: string[], scores: number[], sequence: string}>");
|
|
1496
1596
|
}
|
|
1497
|
-
return
|
|
1597
|
+
return output;
|
|
1498
1598
|
}
|
|
1499
1599
|
|
|
1500
1600
|
// src/tasks/nlp/chatCompletion.ts
|
|
1501
1601
|
async function chatCompletion(args, options) {
|
|
1502
|
-
const res = await
|
|
1602
|
+
const { data: res } = await innerRequest(args, {
|
|
1503
1603
|
...options,
|
|
1504
1604
|
task: "text-generation",
|
|
1505
1605
|
chatCompletion: true
|
|
@@ -1514,7 +1614,7 @@ async function chatCompletion(args, options) {
|
|
|
1514
1614
|
|
|
1515
1615
|
// src/tasks/nlp/chatCompletionStream.ts
|
|
1516
1616
|
async function* chatCompletionStream(args, options) {
|
|
1517
|
-
yield*
|
|
1617
|
+
yield* innerStreamingRequest(args, {
|
|
1518
1618
|
...options,
|
|
1519
1619
|
task: "text-generation",
|
|
1520
1620
|
chatCompletion: true
|
|
@@ -1531,19 +1631,21 @@ async function documentQuestionAnswering(args, options) {
|
|
|
1531
1631
|
image: base64FromBytes(new Uint8Array(await args.inputs.image.arrayBuffer()))
|
|
1532
1632
|
}
|
|
1533
1633
|
};
|
|
1534
|
-
const res =
|
|
1535
|
-
|
|
1634
|
+
const { data: res } = await innerRequest(
|
|
1635
|
+
reqArgs,
|
|
1636
|
+
{
|
|
1536
1637
|
...options,
|
|
1537
1638
|
task: "document-question-answering"
|
|
1538
|
-
}
|
|
1639
|
+
}
|
|
1539
1640
|
);
|
|
1540
|
-
const
|
|
1641
|
+
const output = toArray(res);
|
|
1642
|
+
const isValidOutput = Array.isArray(output) && output.every(
|
|
1541
1643
|
(elem) => typeof elem === "object" && !!elem && typeof elem?.answer === "string" && (typeof elem.end === "number" || typeof elem.end === "undefined") && (typeof elem.score === "number" || typeof elem.score === "undefined") && (typeof elem.start === "number" || typeof elem.start === "undefined")
|
|
1542
1644
|
);
|
|
1543
1645
|
if (!isValidOutput) {
|
|
1544
1646
|
throw new InferenceOutputError("Expected Array<{answer: string, end?: number, score?: number, start?: number}>");
|
|
1545
1647
|
}
|
|
1546
|
-
return
|
|
1648
|
+
return output[0];
|
|
1547
1649
|
}
|
|
1548
1650
|
|
|
1549
1651
|
// src/tasks/multimodal/visualQuestionAnswering.ts
|
|
@@ -1556,7 +1658,7 @@ async function visualQuestionAnswering(args, options) {
|
|
|
1556
1658
|
image: base64FromBytes(new Uint8Array(await args.inputs.image.arrayBuffer()))
|
|
1557
1659
|
}
|
|
1558
1660
|
};
|
|
1559
|
-
const res = await
|
|
1661
|
+
const { data: res } = await innerRequest(reqArgs, {
|
|
1560
1662
|
...options,
|
|
1561
1663
|
task: "visual-question-answering"
|
|
1562
1664
|
});
|
|
@@ -1571,7 +1673,7 @@ async function visualQuestionAnswering(args, options) {
|
|
|
1571
1673
|
|
|
1572
1674
|
// src/tasks/tabular/tabularRegression.ts
|
|
1573
1675
|
async function tabularRegression(args, options) {
|
|
1574
|
-
const res = await
|
|
1676
|
+
const { data: res } = await innerRequest(args, {
|
|
1575
1677
|
...options,
|
|
1576
1678
|
task: "tabular-regression"
|
|
1577
1679
|
});
|
|
@@ -1584,7 +1686,7 @@ async function tabularRegression(args, options) {
|
|
|
1584
1686
|
|
|
1585
1687
|
// src/tasks/tabular/tabularClassification.ts
|
|
1586
1688
|
async function tabularClassification(args, options) {
|
|
1587
|
-
const res = await
|
|
1689
|
+
const { data: res } = await innerRequest(args, {
|
|
1588
1690
|
...options,
|
|
1589
1691
|
task: "tabular-classification"
|
|
1590
1692
|
});
|
|
@@ -1672,7 +1774,7 @@ var templates = {
|
|
|
1672
1774
|
"basicAudio": 'async function query(data) {\n const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "audio/flac"\n },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\n const result = await response.json();\n return result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});',
|
|
1673
1775
|
"basicImage": 'async function query(data) {\n const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "image/jpeg"\n },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\n const result = await response.json();\n return result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});',
|
|
1674
1776
|
"textToAudio": '{% if model.library_name == "transformers" %}\nasync function query(data) {\n const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "application/json",\n },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\n const result = await response.blob();\n return result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n // Returns a byte object of the Audio wavform. Use it directly!\n});\n{% else %}\nasync function query(data) {\n const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "application/json",\n },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\n const result = await response.json();\n return result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});\n{% endif %} ',
|
|
1675
|
-
"textToImage": 'async function query(data) {\n const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "application/json",\n },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\n const result = await response.blob();\n return result;\n}\n\nquery({
|
|
1777
|
+
"textToImage": 'async function query(data) {\n const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "application/json",\n },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\n const result = await response.blob();\n return result;\n}\n\n\nquery({ {{ providerInputs.asTsString }} }).then((response) => {\n // Use image\n});',
|
|
1676
1778
|
"zeroShotClassification": 'async function query(data) {\n const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "application/json",\n },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\n const result = await response.json();\n return result;\n}\n\nquery({\n inputs: {{ providerInputs.asObj.inputs }},\n parameters: { candidate_labels: ["refund", "legal", "faq"] }\n}).then((response) => {\n console.log(JSON.stringify(response));\n});'
|
|
1677
1779
|
},
|
|
1678
1780
|
"huggingface.js": {
|