@huggingface/inference 3.13.0 → 3.13.2
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 +119 -124
- package/dist/index.cjs +100 -88
- package/dist/index.js +100 -88
- package/dist/src/lib/getDefaultTask.d.ts.map +1 -1
- package/dist/src/lib/getProviderHelper.d.ts +31 -31
- package/dist/src/lib/getProviderHelper.d.ts.map +1 -1
- package/dist/src/providers/fal-ai.d.ts +3 -17
- package/dist/src/providers/fal-ai.d.ts.map +1 -1
- package/dist/src/providers/hf-inference.d.ts +5 -1
- package/dist/src/providers/hf-inference.d.ts.map +1 -1
- package/dist/src/providers/providerHelper.d.ts +5 -1
- package/dist/src/providers/providerHelper.d.ts.map +1 -1
- package/dist/src/snippets/getInferenceSnippets.d.ts.map +1 -1
- package/dist/src/snippets/templates.exported.d.ts.map +1 -1
- package/dist/src/tasks/audio/automaticSpeechRecognition.d.ts.map +1 -1
- package/dist/src/tasks/cv/imageToImage.d.ts.map +1 -1
- package/dist/src/vendor/fetch-event-source/parse.d.ts.map +1 -1
- package/dist/test/test-files.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/lib/getDefaultTask.ts +2 -1
- package/src/lib/getProviderHelper.ts +40 -36
- package/src/providers/fal-ai.ts +26 -1
- package/src/providers/hf-inference.ts +31 -2
- package/src/providers/providerHelper.ts +5 -1
- package/src/snippets/getInferenceSnippets.ts +13 -4
- package/src/snippets/templates.exported.ts +3 -1
- package/src/tasks/audio/automaticSpeechRecognition.ts +2 -32
- package/src/tasks/cv/imageToImage.ts +3 -18
package/dist/index.cjs
CHANGED
|
@@ -235,6 +235,43 @@ var BaseTextGenerationTask = class extends TaskProviderHelper {
|
|
|
235
235
|
}
|
|
236
236
|
};
|
|
237
237
|
|
|
238
|
+
// src/utils/base64FromBytes.ts
|
|
239
|
+
function base64FromBytes(arr) {
|
|
240
|
+
if (globalThis.Buffer) {
|
|
241
|
+
return globalThis.Buffer.from(arr).toString("base64");
|
|
242
|
+
} else {
|
|
243
|
+
const bin = [];
|
|
244
|
+
arr.forEach((byte) => {
|
|
245
|
+
bin.push(String.fromCharCode(byte));
|
|
246
|
+
});
|
|
247
|
+
return globalThis.btoa(bin.join(""));
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// src/utils/pick.ts
|
|
252
|
+
function pick(o, props) {
|
|
253
|
+
return Object.assign(
|
|
254
|
+
{},
|
|
255
|
+
...props.map((prop) => {
|
|
256
|
+
if (o[prop] !== void 0) {
|
|
257
|
+
return { [prop]: o[prop] };
|
|
258
|
+
}
|
|
259
|
+
})
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// src/utils/typedInclude.ts
|
|
264
|
+
function typedInclude(arr, v) {
|
|
265
|
+
return arr.includes(v);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// src/utils/omit.ts
|
|
269
|
+
function omit(o, props) {
|
|
270
|
+
const propsArr = Array.isArray(props) ? props : [props];
|
|
271
|
+
const letsKeep = Object.keys(o).filter((prop) => !typedInclude(propsArr, prop));
|
|
272
|
+
return pick(o, letsKeep);
|
|
273
|
+
}
|
|
274
|
+
|
|
238
275
|
// src/providers/hf-inference.ts
|
|
239
276
|
var EQUIVALENT_SENTENCE_TRANSFORMERS_TASKS = ["feature-extraction", "sentence-similarity"];
|
|
240
277
|
var HFInferenceTask = class extends TaskProviderHelper {
|
|
@@ -342,6 +379,12 @@ var HFInferenceAutomaticSpeechRecognitionTask = class extends HFInferenceTask {
|
|
|
342
379
|
async getResponse(response) {
|
|
343
380
|
return response;
|
|
344
381
|
}
|
|
382
|
+
async preparePayloadAsync(args) {
|
|
383
|
+
return "data" in args ? args : {
|
|
384
|
+
...omit(args, "inputs"),
|
|
385
|
+
data: args.inputs
|
|
386
|
+
};
|
|
387
|
+
}
|
|
345
388
|
};
|
|
346
389
|
var HFInferenceAudioToAudioTask = class extends HFInferenceTask {
|
|
347
390
|
async getResponse(response) {
|
|
@@ -410,6 +453,22 @@ var HFInferenceImageToTextTask = class extends HFInferenceTask {
|
|
|
410
453
|
}
|
|
411
454
|
};
|
|
412
455
|
var HFInferenceImageToImageTask = class extends HFInferenceTask {
|
|
456
|
+
async preparePayloadAsync(args) {
|
|
457
|
+
if (!args.parameters) {
|
|
458
|
+
return {
|
|
459
|
+
...args,
|
|
460
|
+
model: args.model,
|
|
461
|
+
data: args.inputs
|
|
462
|
+
};
|
|
463
|
+
} else {
|
|
464
|
+
return {
|
|
465
|
+
...args,
|
|
466
|
+
inputs: base64FromBytes(
|
|
467
|
+
new Uint8Array(args.inputs instanceof ArrayBuffer ? args.inputs : await args.inputs.arrayBuffer())
|
|
468
|
+
)
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
}
|
|
413
472
|
async getResponse(response) {
|
|
414
473
|
if (response instanceof Blob) {
|
|
415
474
|
return response;
|
|
@@ -566,11 +625,6 @@ var HFInferenceTextToAudioTask = class extends HFInferenceTask {
|
|
|
566
625
|
}
|
|
567
626
|
};
|
|
568
627
|
|
|
569
|
-
// src/utils/typedInclude.ts
|
|
570
|
-
function typedInclude(arr, v) {
|
|
571
|
-
return arr.includes(v);
|
|
572
|
-
}
|
|
573
|
-
|
|
574
628
|
// src/lib/getInferenceProviderMapping.ts
|
|
575
629
|
var inferenceProviderMappingCache = /* @__PURE__ */ new Map();
|
|
576
630
|
async function fetchInferenceProviderMappingForModel(modelId, accessToken, options) {
|
|
@@ -656,25 +710,6 @@ function delay(ms) {
|
|
|
656
710
|
});
|
|
657
711
|
}
|
|
658
712
|
|
|
659
|
-
// src/utils/pick.ts
|
|
660
|
-
function pick(o, props) {
|
|
661
|
-
return Object.assign(
|
|
662
|
-
{},
|
|
663
|
-
...props.map((prop) => {
|
|
664
|
-
if (o[prop] !== void 0) {
|
|
665
|
-
return { [prop]: o[prop] };
|
|
666
|
-
}
|
|
667
|
-
})
|
|
668
|
-
);
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
// src/utils/omit.ts
|
|
672
|
-
function omit(o, props) {
|
|
673
|
-
const propsArr = Array.isArray(props) ? props : [props];
|
|
674
|
-
const letsKeep = Object.keys(o).filter((prop) => !typedInclude(propsArr, prop));
|
|
675
|
-
return pick(o, letsKeep);
|
|
676
|
-
}
|
|
677
|
-
|
|
678
713
|
// src/providers/black-forest-labs.ts
|
|
679
714
|
var BLACK_FOREST_LABS_AI_API_BASE_URL = "https://api.us1.bfl.ai";
|
|
680
715
|
var BlackForestLabsTextToImageTask = class extends TaskProviderHelper {
|
|
@@ -881,6 +916,27 @@ var FalAIAutomaticSpeechRecognitionTask = class extends FalAITask {
|
|
|
881
916
|
}
|
|
882
917
|
return { text: res.text };
|
|
883
918
|
}
|
|
919
|
+
async preparePayloadAsync(args) {
|
|
920
|
+
const blob = "data" in args && args.data instanceof Blob ? args.data : "inputs" in args ? args.inputs : void 0;
|
|
921
|
+
const contentType = blob?.type;
|
|
922
|
+
if (!contentType) {
|
|
923
|
+
throw new Error(
|
|
924
|
+
`Unable to determine the input's content-type. Make sure your are passing a Blob when using provider fal-ai.`
|
|
925
|
+
);
|
|
926
|
+
}
|
|
927
|
+
if (!FAL_AI_SUPPORTED_BLOB_TYPES.includes(contentType)) {
|
|
928
|
+
throw new Error(
|
|
929
|
+
`Provider fal-ai does not support blob type ${contentType} - supported content types are: ${FAL_AI_SUPPORTED_BLOB_TYPES.join(
|
|
930
|
+
", "
|
|
931
|
+
)}`
|
|
932
|
+
);
|
|
933
|
+
}
|
|
934
|
+
const base64audio = base64FromBytes(new Uint8Array(await blob.arrayBuffer()));
|
|
935
|
+
return {
|
|
936
|
+
..."data" in args ? omit(args, "data") : omit(args, "inputs"),
|
|
937
|
+
audio_url: `data:${contentType};base64,${base64audio}`
|
|
938
|
+
};
|
|
939
|
+
}
|
|
884
940
|
};
|
|
885
941
|
var FalAITextToSpeechTask = class extends FalAITask {
|
|
886
942
|
preparePayload(params) {
|
|
@@ -1467,10 +1523,8 @@ var PROVIDERS = {
|
|
|
1467
1523
|
}
|
|
1468
1524
|
};
|
|
1469
1525
|
function getProviderHelper(provider, task) {
|
|
1470
|
-
if (provider === "hf-inference") {
|
|
1471
|
-
|
|
1472
|
-
return new HFInferenceTask();
|
|
1473
|
-
}
|
|
1526
|
+
if (provider === "hf-inference" && !task || provider === "auto") {
|
|
1527
|
+
return new HFInferenceTask();
|
|
1474
1528
|
}
|
|
1475
1529
|
if (!task) {
|
|
1476
1530
|
throw new Error("you need to provide a task name when using an external provider, e.g. 'text-to-image'");
|
|
@@ -1489,7 +1543,7 @@ function getProviderHelper(provider, task) {
|
|
|
1489
1543
|
|
|
1490
1544
|
// package.json
|
|
1491
1545
|
var name = "@huggingface/inference";
|
|
1492
|
-
var version = "3.13.
|
|
1546
|
+
var version = "3.13.2";
|
|
1493
1547
|
|
|
1494
1548
|
// src/lib/makeRequestOptions.ts
|
|
1495
1549
|
var tasks = null;
|
|
@@ -1890,24 +1944,11 @@ async function audioToAudio(args, options) {
|
|
|
1890
1944
|
return providerHelper.getResponse(res);
|
|
1891
1945
|
}
|
|
1892
1946
|
|
|
1893
|
-
// src/utils/base64FromBytes.ts
|
|
1894
|
-
function base64FromBytes(arr) {
|
|
1895
|
-
if (globalThis.Buffer) {
|
|
1896
|
-
return globalThis.Buffer.from(arr).toString("base64");
|
|
1897
|
-
} else {
|
|
1898
|
-
const bin = [];
|
|
1899
|
-
arr.forEach((byte) => {
|
|
1900
|
-
bin.push(String.fromCharCode(byte));
|
|
1901
|
-
});
|
|
1902
|
-
return globalThis.btoa(bin.join(""));
|
|
1903
|
-
}
|
|
1904
|
-
}
|
|
1905
|
-
|
|
1906
1947
|
// src/tasks/audio/automaticSpeechRecognition.ts
|
|
1907
1948
|
async function automaticSpeechRecognition(args, options) {
|
|
1908
1949
|
const provider = await resolveProvider(args.provider, args.model, args.endpointUrl);
|
|
1909
1950
|
const providerHelper = getProviderHelper(provider, "automatic-speech-recognition");
|
|
1910
|
-
const payload = await
|
|
1951
|
+
const payload = await providerHelper.preparePayloadAsync(args);
|
|
1911
1952
|
const { data: res } = await innerRequest(payload, providerHelper, {
|
|
1912
1953
|
...options,
|
|
1913
1954
|
task: "automatic-speech-recognition"
|
|
@@ -1918,31 +1959,6 @@ async function automaticSpeechRecognition(args, options) {
|
|
|
1918
1959
|
}
|
|
1919
1960
|
return providerHelper.getResponse(res);
|
|
1920
1961
|
}
|
|
1921
|
-
async function buildPayload(args) {
|
|
1922
|
-
if (args.provider === "fal-ai") {
|
|
1923
|
-
const blob = "data" in args && args.data instanceof Blob ? args.data : "inputs" in args ? args.inputs : void 0;
|
|
1924
|
-
const contentType = blob?.type;
|
|
1925
|
-
if (!contentType) {
|
|
1926
|
-
throw new Error(
|
|
1927
|
-
`Unable to determine the input's content-type. Make sure your are passing a Blob when using provider fal-ai.`
|
|
1928
|
-
);
|
|
1929
|
-
}
|
|
1930
|
-
if (!FAL_AI_SUPPORTED_BLOB_TYPES.includes(contentType)) {
|
|
1931
|
-
throw new Error(
|
|
1932
|
-
`Provider fal-ai does not support blob type ${contentType} - supported content types are: ${FAL_AI_SUPPORTED_BLOB_TYPES.join(
|
|
1933
|
-
", "
|
|
1934
|
-
)}`
|
|
1935
|
-
);
|
|
1936
|
-
}
|
|
1937
|
-
const base64audio = base64FromBytes(new Uint8Array(await blob.arrayBuffer()));
|
|
1938
|
-
return {
|
|
1939
|
-
..."data" in args ? omit(args, "data") : omit(args, "inputs"),
|
|
1940
|
-
audio_url: `data:${contentType};base64,${base64audio}`
|
|
1941
|
-
};
|
|
1942
|
-
} else {
|
|
1943
|
-
return preparePayload(args);
|
|
1944
|
-
}
|
|
1945
|
-
}
|
|
1946
1962
|
|
|
1947
1963
|
// src/tasks/audio/textToSpeech.ts
|
|
1948
1964
|
async function textToSpeech(args, options) {
|
|
@@ -1988,22 +2004,8 @@ async function imageSegmentation(args, options) {
|
|
|
1988
2004
|
async function imageToImage(args, options) {
|
|
1989
2005
|
const provider = await resolveProvider(args.provider, args.model, args.endpointUrl);
|
|
1990
2006
|
const providerHelper = getProviderHelper(provider, "image-to-image");
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
reqArgs = {
|
|
1994
|
-
accessToken: args.accessToken,
|
|
1995
|
-
model: args.model,
|
|
1996
|
-
data: args.inputs
|
|
1997
|
-
};
|
|
1998
|
-
} else {
|
|
1999
|
-
reqArgs = {
|
|
2000
|
-
...args,
|
|
2001
|
-
inputs: base64FromBytes(
|
|
2002
|
-
new Uint8Array(args.inputs instanceof ArrayBuffer ? args.inputs : await args.inputs.arrayBuffer())
|
|
2003
|
-
)
|
|
2004
|
-
};
|
|
2005
|
-
}
|
|
2006
|
-
const { data: res } = await innerRequest(reqArgs, providerHelper, {
|
|
2007
|
+
const payload = await providerHelper.preparePayloadAsync(args);
|
|
2008
|
+
const { data: res } = await innerRequest(payload, providerHelper, {
|
|
2007
2009
|
...options,
|
|
2008
2010
|
task: "image-to-image"
|
|
2009
2011
|
});
|
|
@@ -2468,7 +2470,7 @@ const video = await client.textToVideo({
|
|
|
2468
2470
|
"textToImage": '{% if provider == "fal-ai" %}\nimport fal_client\n\n{% if providerInputs.asObj.loras is defined and providerInputs.asObj.loras != none %}\nresult = fal_client.subscribe(\n "{{ providerModelId }}",\n arguments={\n "prompt": {{ inputs.asObj.inputs }},\n "loras":{{ providerInputs.asObj.loras | tojson }},\n },\n)\n{% else %}\nresult = fal_client.subscribe(\n "{{ providerModelId }}",\n arguments={\n "prompt": {{ inputs.asObj.inputs }},\n },\n)\n{% endif %} \nprint(result)\n{% endif %} '
|
|
2469
2471
|
},
|
|
2470
2472
|
"huggingface_hub": {
|
|
2471
|
-
"basic": 'result = client.{{ methodName }}(\n
|
|
2473
|
+
"basic": 'result = client.{{ methodName }}(\n {{ inputs.asObj.inputs }},\n model="{{ model.id }}",\n)',
|
|
2472
2474
|
"basicAudio": 'output = client.{{ methodName }}({{ inputs.asObj.inputs }}, model="{{ model.id }}")',
|
|
2473
2475
|
"basicImage": 'output = client.{{ methodName }}({{ inputs.asObj.inputs }}, model="{{ model.id }}")',
|
|
2474
2476
|
"conversational": 'completion = client.chat.completions.create(\n model="{{ model.id }}",\n{{ inputs.asPythonString }}\n)\n\nprint(completion.choices[0].message) ',
|
|
@@ -2476,6 +2478,8 @@ const video = await client.textToVideo({
|
|
|
2476
2478
|
"documentQuestionAnswering": 'output = client.document_question_answering(\n "{{ inputs.asObj.image }}",\n question="{{ inputs.asObj.question }}",\n model="{{ model.id }}",\n) ',
|
|
2477
2479
|
"imageToImage": '# output is a PIL.Image object\nimage = client.image_to_image(\n "{{ inputs.asObj.inputs }}",\n prompt="{{ inputs.asObj.parameters.prompt }}",\n model="{{ model.id }}",\n) ',
|
|
2478
2480
|
"importInferenceClient": 'from huggingface_hub import InferenceClient\n\nclient = InferenceClient(\n provider="{{ provider }}",\n api_key="{{ accessToken }}",\n{% if billTo %}\n bill_to="{{ billTo }}",\n{% endif %}\n)',
|
|
2481
|
+
"questionAnswering": 'answer = client.question_answering(\n question="{{ inputs.asObj.question }}",\n context="{{ inputs.asObj.context }}",\n model="{{ model.id }}",\n) ',
|
|
2482
|
+
"tableQuestionAnswering": 'answer = client.question_answering(\n query="{{ inputs.asObj.query }}",\n table={{ inputs.asObj.table }},\n model="{{ model.id }}",\n) ',
|
|
2479
2483
|
"textToImage": '# output is a PIL.Image object\nimage = client.text_to_image(\n {{ inputs.asObj.inputs }},\n model="{{ model.id }}",\n) ',
|
|
2480
2484
|
"textToSpeech": '# audio is returned as bytes\naudio = client.text_to_speech(\n {{ inputs.asObj.inputs }},\n model="{{ model.id }}",\n) \n',
|
|
2481
2485
|
"textToVideo": 'video = client.text_to_video(\n {{ inputs.asObj.inputs }},\n model="{{ model.id }}",\n) '
|
|
@@ -2617,7 +2621,7 @@ var snippetGenerator = (templateName, inputPreparationFn) => {
|
|
|
2617
2621
|
}
|
|
2618
2622
|
let providerHelper;
|
|
2619
2623
|
try {
|
|
2620
|
-
providerHelper = getProviderHelper(provider
|
|
2624
|
+
providerHelper = getProviderHelper(provider, task);
|
|
2621
2625
|
} catch (e) {
|
|
2622
2626
|
console.error(`Failed to get provider helper for ${provider} (${task})`, e);
|
|
2623
2627
|
return [];
|
|
@@ -2729,6 +2733,14 @@ var prepareConversationalInput = (model, opts) => {
|
|
|
2729
2733
|
...opts?.top_p ? { top_p: opts?.top_p } : void 0
|
|
2730
2734
|
};
|
|
2731
2735
|
};
|
|
2736
|
+
var prepareQuestionAnsweringInput = (model) => {
|
|
2737
|
+
const data = JSON.parse((0, import_tasks.getModelInputSnippet)(model));
|
|
2738
|
+
return { question: data.question, context: data.context };
|
|
2739
|
+
};
|
|
2740
|
+
var prepareTableQuestionAnsweringInput = (model) => {
|
|
2741
|
+
const data = JSON.parse((0, import_tasks.getModelInputSnippet)(model));
|
|
2742
|
+
return { query: data.query, table: JSON.stringify(data.table) };
|
|
2743
|
+
};
|
|
2732
2744
|
var snippets = {
|
|
2733
2745
|
"audio-classification": snippetGenerator("basicAudio"),
|
|
2734
2746
|
"audio-to-audio": snippetGenerator("basicAudio"),
|
|
@@ -2742,12 +2754,12 @@ var snippets = {
|
|
|
2742
2754
|
"image-to-image": snippetGenerator("imageToImage", prepareImageToImageInput),
|
|
2743
2755
|
"image-to-text": snippetGenerator("basicImage"),
|
|
2744
2756
|
"object-detection": snippetGenerator("basicImage"),
|
|
2745
|
-
"question-answering": snippetGenerator("
|
|
2757
|
+
"question-answering": snippetGenerator("questionAnswering", prepareQuestionAnsweringInput),
|
|
2746
2758
|
"sentence-similarity": snippetGenerator("basic"),
|
|
2747
2759
|
summarization: snippetGenerator("basic"),
|
|
2748
2760
|
"tabular-classification": snippetGenerator("tabular"),
|
|
2749
2761
|
"tabular-regression": snippetGenerator("tabular"),
|
|
2750
|
-
"table-question-answering": snippetGenerator("
|
|
2762
|
+
"table-question-answering": snippetGenerator("tableQuestionAnswering", prepareTableQuestionAnsweringInput),
|
|
2751
2763
|
"text-classification": snippetGenerator("basic"),
|
|
2752
2764
|
"text-generation": snippetGenerator("basic"),
|
|
2753
2765
|
"text-to-audio": snippetGenerator("textToAudio"),
|
package/dist/index.js
CHANGED
|
@@ -177,6 +177,43 @@ var BaseTextGenerationTask = class extends TaskProviderHelper {
|
|
|
177
177
|
}
|
|
178
178
|
};
|
|
179
179
|
|
|
180
|
+
// src/utils/base64FromBytes.ts
|
|
181
|
+
function base64FromBytes(arr) {
|
|
182
|
+
if (globalThis.Buffer) {
|
|
183
|
+
return globalThis.Buffer.from(arr).toString("base64");
|
|
184
|
+
} else {
|
|
185
|
+
const bin = [];
|
|
186
|
+
arr.forEach((byte) => {
|
|
187
|
+
bin.push(String.fromCharCode(byte));
|
|
188
|
+
});
|
|
189
|
+
return globalThis.btoa(bin.join(""));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// src/utils/pick.ts
|
|
194
|
+
function pick(o, props) {
|
|
195
|
+
return Object.assign(
|
|
196
|
+
{},
|
|
197
|
+
...props.map((prop) => {
|
|
198
|
+
if (o[prop] !== void 0) {
|
|
199
|
+
return { [prop]: o[prop] };
|
|
200
|
+
}
|
|
201
|
+
})
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// src/utils/typedInclude.ts
|
|
206
|
+
function typedInclude(arr, v) {
|
|
207
|
+
return arr.includes(v);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// src/utils/omit.ts
|
|
211
|
+
function omit(o, props) {
|
|
212
|
+
const propsArr = Array.isArray(props) ? props : [props];
|
|
213
|
+
const letsKeep = Object.keys(o).filter((prop) => !typedInclude(propsArr, prop));
|
|
214
|
+
return pick(o, letsKeep);
|
|
215
|
+
}
|
|
216
|
+
|
|
180
217
|
// src/providers/hf-inference.ts
|
|
181
218
|
var EQUIVALENT_SENTENCE_TRANSFORMERS_TASKS = ["feature-extraction", "sentence-similarity"];
|
|
182
219
|
var HFInferenceTask = class extends TaskProviderHelper {
|
|
@@ -284,6 +321,12 @@ var HFInferenceAutomaticSpeechRecognitionTask = class extends HFInferenceTask {
|
|
|
284
321
|
async getResponse(response) {
|
|
285
322
|
return response;
|
|
286
323
|
}
|
|
324
|
+
async preparePayloadAsync(args) {
|
|
325
|
+
return "data" in args ? args : {
|
|
326
|
+
...omit(args, "inputs"),
|
|
327
|
+
data: args.inputs
|
|
328
|
+
};
|
|
329
|
+
}
|
|
287
330
|
};
|
|
288
331
|
var HFInferenceAudioToAudioTask = class extends HFInferenceTask {
|
|
289
332
|
async getResponse(response) {
|
|
@@ -352,6 +395,22 @@ var HFInferenceImageToTextTask = class extends HFInferenceTask {
|
|
|
352
395
|
}
|
|
353
396
|
};
|
|
354
397
|
var HFInferenceImageToImageTask = class extends HFInferenceTask {
|
|
398
|
+
async preparePayloadAsync(args) {
|
|
399
|
+
if (!args.parameters) {
|
|
400
|
+
return {
|
|
401
|
+
...args,
|
|
402
|
+
model: args.model,
|
|
403
|
+
data: args.inputs
|
|
404
|
+
};
|
|
405
|
+
} else {
|
|
406
|
+
return {
|
|
407
|
+
...args,
|
|
408
|
+
inputs: base64FromBytes(
|
|
409
|
+
new Uint8Array(args.inputs instanceof ArrayBuffer ? args.inputs : await args.inputs.arrayBuffer())
|
|
410
|
+
)
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
}
|
|
355
414
|
async getResponse(response) {
|
|
356
415
|
if (response instanceof Blob) {
|
|
357
416
|
return response;
|
|
@@ -508,11 +567,6 @@ var HFInferenceTextToAudioTask = class extends HFInferenceTask {
|
|
|
508
567
|
}
|
|
509
568
|
};
|
|
510
569
|
|
|
511
|
-
// src/utils/typedInclude.ts
|
|
512
|
-
function typedInclude(arr, v) {
|
|
513
|
-
return arr.includes(v);
|
|
514
|
-
}
|
|
515
|
-
|
|
516
570
|
// src/lib/getInferenceProviderMapping.ts
|
|
517
571
|
var inferenceProviderMappingCache = /* @__PURE__ */ new Map();
|
|
518
572
|
async function fetchInferenceProviderMappingForModel(modelId, accessToken, options) {
|
|
@@ -598,25 +652,6 @@ function delay(ms) {
|
|
|
598
652
|
});
|
|
599
653
|
}
|
|
600
654
|
|
|
601
|
-
// src/utils/pick.ts
|
|
602
|
-
function pick(o, props) {
|
|
603
|
-
return Object.assign(
|
|
604
|
-
{},
|
|
605
|
-
...props.map((prop) => {
|
|
606
|
-
if (o[prop] !== void 0) {
|
|
607
|
-
return { [prop]: o[prop] };
|
|
608
|
-
}
|
|
609
|
-
})
|
|
610
|
-
);
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
// src/utils/omit.ts
|
|
614
|
-
function omit(o, props) {
|
|
615
|
-
const propsArr = Array.isArray(props) ? props : [props];
|
|
616
|
-
const letsKeep = Object.keys(o).filter((prop) => !typedInclude(propsArr, prop));
|
|
617
|
-
return pick(o, letsKeep);
|
|
618
|
-
}
|
|
619
|
-
|
|
620
655
|
// src/providers/black-forest-labs.ts
|
|
621
656
|
var BLACK_FOREST_LABS_AI_API_BASE_URL = "https://api.us1.bfl.ai";
|
|
622
657
|
var BlackForestLabsTextToImageTask = class extends TaskProviderHelper {
|
|
@@ -823,6 +858,27 @@ var FalAIAutomaticSpeechRecognitionTask = class extends FalAITask {
|
|
|
823
858
|
}
|
|
824
859
|
return { text: res.text };
|
|
825
860
|
}
|
|
861
|
+
async preparePayloadAsync(args) {
|
|
862
|
+
const blob = "data" in args && args.data instanceof Blob ? args.data : "inputs" in args ? args.inputs : void 0;
|
|
863
|
+
const contentType = blob?.type;
|
|
864
|
+
if (!contentType) {
|
|
865
|
+
throw new Error(
|
|
866
|
+
`Unable to determine the input's content-type. Make sure your are passing a Blob when using provider fal-ai.`
|
|
867
|
+
);
|
|
868
|
+
}
|
|
869
|
+
if (!FAL_AI_SUPPORTED_BLOB_TYPES.includes(contentType)) {
|
|
870
|
+
throw new Error(
|
|
871
|
+
`Provider fal-ai does not support blob type ${contentType} - supported content types are: ${FAL_AI_SUPPORTED_BLOB_TYPES.join(
|
|
872
|
+
", "
|
|
873
|
+
)}`
|
|
874
|
+
);
|
|
875
|
+
}
|
|
876
|
+
const base64audio = base64FromBytes(new Uint8Array(await blob.arrayBuffer()));
|
|
877
|
+
return {
|
|
878
|
+
..."data" in args ? omit(args, "data") : omit(args, "inputs"),
|
|
879
|
+
audio_url: `data:${contentType};base64,${base64audio}`
|
|
880
|
+
};
|
|
881
|
+
}
|
|
826
882
|
};
|
|
827
883
|
var FalAITextToSpeechTask = class extends FalAITask {
|
|
828
884
|
preparePayload(params) {
|
|
@@ -1409,10 +1465,8 @@ var PROVIDERS = {
|
|
|
1409
1465
|
}
|
|
1410
1466
|
};
|
|
1411
1467
|
function getProviderHelper(provider, task) {
|
|
1412
|
-
if (provider === "hf-inference") {
|
|
1413
|
-
|
|
1414
|
-
return new HFInferenceTask();
|
|
1415
|
-
}
|
|
1468
|
+
if (provider === "hf-inference" && !task || provider === "auto") {
|
|
1469
|
+
return new HFInferenceTask();
|
|
1416
1470
|
}
|
|
1417
1471
|
if (!task) {
|
|
1418
1472
|
throw new Error("you need to provide a task name when using an external provider, e.g. 'text-to-image'");
|
|
@@ -1431,7 +1485,7 @@ function getProviderHelper(provider, task) {
|
|
|
1431
1485
|
|
|
1432
1486
|
// package.json
|
|
1433
1487
|
var name = "@huggingface/inference";
|
|
1434
|
-
var version = "3.13.
|
|
1488
|
+
var version = "3.13.2";
|
|
1435
1489
|
|
|
1436
1490
|
// src/lib/makeRequestOptions.ts
|
|
1437
1491
|
var tasks = null;
|
|
@@ -1832,24 +1886,11 @@ async function audioToAudio(args, options) {
|
|
|
1832
1886
|
return providerHelper.getResponse(res);
|
|
1833
1887
|
}
|
|
1834
1888
|
|
|
1835
|
-
// src/utils/base64FromBytes.ts
|
|
1836
|
-
function base64FromBytes(arr) {
|
|
1837
|
-
if (globalThis.Buffer) {
|
|
1838
|
-
return globalThis.Buffer.from(arr).toString("base64");
|
|
1839
|
-
} else {
|
|
1840
|
-
const bin = [];
|
|
1841
|
-
arr.forEach((byte) => {
|
|
1842
|
-
bin.push(String.fromCharCode(byte));
|
|
1843
|
-
});
|
|
1844
|
-
return globalThis.btoa(bin.join(""));
|
|
1845
|
-
}
|
|
1846
|
-
}
|
|
1847
|
-
|
|
1848
1889
|
// src/tasks/audio/automaticSpeechRecognition.ts
|
|
1849
1890
|
async function automaticSpeechRecognition(args, options) {
|
|
1850
1891
|
const provider = await resolveProvider(args.provider, args.model, args.endpointUrl);
|
|
1851
1892
|
const providerHelper = getProviderHelper(provider, "automatic-speech-recognition");
|
|
1852
|
-
const payload = await
|
|
1893
|
+
const payload = await providerHelper.preparePayloadAsync(args);
|
|
1853
1894
|
const { data: res } = await innerRequest(payload, providerHelper, {
|
|
1854
1895
|
...options,
|
|
1855
1896
|
task: "automatic-speech-recognition"
|
|
@@ -1860,31 +1901,6 @@ async function automaticSpeechRecognition(args, options) {
|
|
|
1860
1901
|
}
|
|
1861
1902
|
return providerHelper.getResponse(res);
|
|
1862
1903
|
}
|
|
1863
|
-
async function buildPayload(args) {
|
|
1864
|
-
if (args.provider === "fal-ai") {
|
|
1865
|
-
const blob = "data" in args && args.data instanceof Blob ? args.data : "inputs" in args ? args.inputs : void 0;
|
|
1866
|
-
const contentType = blob?.type;
|
|
1867
|
-
if (!contentType) {
|
|
1868
|
-
throw new Error(
|
|
1869
|
-
`Unable to determine the input's content-type. Make sure your are passing a Blob when using provider fal-ai.`
|
|
1870
|
-
);
|
|
1871
|
-
}
|
|
1872
|
-
if (!FAL_AI_SUPPORTED_BLOB_TYPES.includes(contentType)) {
|
|
1873
|
-
throw new Error(
|
|
1874
|
-
`Provider fal-ai does not support blob type ${contentType} - supported content types are: ${FAL_AI_SUPPORTED_BLOB_TYPES.join(
|
|
1875
|
-
", "
|
|
1876
|
-
)}`
|
|
1877
|
-
);
|
|
1878
|
-
}
|
|
1879
|
-
const base64audio = base64FromBytes(new Uint8Array(await blob.arrayBuffer()));
|
|
1880
|
-
return {
|
|
1881
|
-
..."data" in args ? omit(args, "data") : omit(args, "inputs"),
|
|
1882
|
-
audio_url: `data:${contentType};base64,${base64audio}`
|
|
1883
|
-
};
|
|
1884
|
-
} else {
|
|
1885
|
-
return preparePayload(args);
|
|
1886
|
-
}
|
|
1887
|
-
}
|
|
1888
1904
|
|
|
1889
1905
|
// src/tasks/audio/textToSpeech.ts
|
|
1890
1906
|
async function textToSpeech(args, options) {
|
|
@@ -1930,22 +1946,8 @@ async function imageSegmentation(args, options) {
|
|
|
1930
1946
|
async function imageToImage(args, options) {
|
|
1931
1947
|
const provider = await resolveProvider(args.provider, args.model, args.endpointUrl);
|
|
1932
1948
|
const providerHelper = getProviderHelper(provider, "image-to-image");
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
reqArgs = {
|
|
1936
|
-
accessToken: args.accessToken,
|
|
1937
|
-
model: args.model,
|
|
1938
|
-
data: args.inputs
|
|
1939
|
-
};
|
|
1940
|
-
} else {
|
|
1941
|
-
reqArgs = {
|
|
1942
|
-
...args,
|
|
1943
|
-
inputs: base64FromBytes(
|
|
1944
|
-
new Uint8Array(args.inputs instanceof ArrayBuffer ? args.inputs : await args.inputs.arrayBuffer())
|
|
1945
|
-
)
|
|
1946
|
-
};
|
|
1947
|
-
}
|
|
1948
|
-
const { data: res } = await innerRequest(reqArgs, providerHelper, {
|
|
1949
|
+
const payload = await providerHelper.preparePayloadAsync(args);
|
|
1950
|
+
const { data: res } = await innerRequest(payload, providerHelper, {
|
|
1949
1951
|
...options,
|
|
1950
1952
|
task: "image-to-image"
|
|
1951
1953
|
});
|
|
@@ -2413,7 +2415,7 @@ const video = await client.textToVideo({
|
|
|
2413
2415
|
"textToImage": '{% if provider == "fal-ai" %}\nimport fal_client\n\n{% if providerInputs.asObj.loras is defined and providerInputs.asObj.loras != none %}\nresult = fal_client.subscribe(\n "{{ providerModelId }}",\n arguments={\n "prompt": {{ inputs.asObj.inputs }},\n "loras":{{ providerInputs.asObj.loras | tojson }},\n },\n)\n{% else %}\nresult = fal_client.subscribe(\n "{{ providerModelId }}",\n arguments={\n "prompt": {{ inputs.asObj.inputs }},\n },\n)\n{% endif %} \nprint(result)\n{% endif %} '
|
|
2414
2416
|
},
|
|
2415
2417
|
"huggingface_hub": {
|
|
2416
|
-
"basic": 'result = client.{{ methodName }}(\n
|
|
2418
|
+
"basic": 'result = client.{{ methodName }}(\n {{ inputs.asObj.inputs }},\n model="{{ model.id }}",\n)',
|
|
2417
2419
|
"basicAudio": 'output = client.{{ methodName }}({{ inputs.asObj.inputs }}, model="{{ model.id }}")',
|
|
2418
2420
|
"basicImage": 'output = client.{{ methodName }}({{ inputs.asObj.inputs }}, model="{{ model.id }}")',
|
|
2419
2421
|
"conversational": 'completion = client.chat.completions.create(\n model="{{ model.id }}",\n{{ inputs.asPythonString }}\n)\n\nprint(completion.choices[0].message) ',
|
|
@@ -2421,6 +2423,8 @@ const video = await client.textToVideo({
|
|
|
2421
2423
|
"documentQuestionAnswering": 'output = client.document_question_answering(\n "{{ inputs.asObj.image }}",\n question="{{ inputs.asObj.question }}",\n model="{{ model.id }}",\n) ',
|
|
2422
2424
|
"imageToImage": '# output is a PIL.Image object\nimage = client.image_to_image(\n "{{ inputs.asObj.inputs }}",\n prompt="{{ inputs.asObj.parameters.prompt }}",\n model="{{ model.id }}",\n) ',
|
|
2423
2425
|
"importInferenceClient": 'from huggingface_hub import InferenceClient\n\nclient = InferenceClient(\n provider="{{ provider }}",\n api_key="{{ accessToken }}",\n{% if billTo %}\n bill_to="{{ billTo }}",\n{% endif %}\n)',
|
|
2426
|
+
"questionAnswering": 'answer = client.question_answering(\n question="{{ inputs.asObj.question }}",\n context="{{ inputs.asObj.context }}",\n model="{{ model.id }}",\n) ',
|
|
2427
|
+
"tableQuestionAnswering": 'answer = client.question_answering(\n query="{{ inputs.asObj.query }}",\n table={{ inputs.asObj.table }},\n model="{{ model.id }}",\n) ',
|
|
2424
2428
|
"textToImage": '# output is a PIL.Image object\nimage = client.text_to_image(\n {{ inputs.asObj.inputs }},\n model="{{ model.id }}",\n) ',
|
|
2425
2429
|
"textToSpeech": '# audio is returned as bytes\naudio = client.text_to_speech(\n {{ inputs.asObj.inputs }},\n model="{{ model.id }}",\n) \n',
|
|
2426
2430
|
"textToVideo": 'video = client.text_to_video(\n {{ inputs.asObj.inputs }},\n model="{{ model.id }}",\n) '
|
|
@@ -2562,7 +2566,7 @@ var snippetGenerator = (templateName, inputPreparationFn) => {
|
|
|
2562
2566
|
}
|
|
2563
2567
|
let providerHelper;
|
|
2564
2568
|
try {
|
|
2565
|
-
providerHelper = getProviderHelper(provider
|
|
2569
|
+
providerHelper = getProviderHelper(provider, task);
|
|
2566
2570
|
} catch (e) {
|
|
2567
2571
|
console.error(`Failed to get provider helper for ${provider} (${task})`, e);
|
|
2568
2572
|
return [];
|
|
@@ -2674,6 +2678,14 @@ var prepareConversationalInput = (model, opts) => {
|
|
|
2674
2678
|
...opts?.top_p ? { top_p: opts?.top_p } : void 0
|
|
2675
2679
|
};
|
|
2676
2680
|
};
|
|
2681
|
+
var prepareQuestionAnsweringInput = (model) => {
|
|
2682
|
+
const data = JSON.parse(getModelInputSnippet(model));
|
|
2683
|
+
return { question: data.question, context: data.context };
|
|
2684
|
+
};
|
|
2685
|
+
var prepareTableQuestionAnsweringInput = (model) => {
|
|
2686
|
+
const data = JSON.parse(getModelInputSnippet(model));
|
|
2687
|
+
return { query: data.query, table: JSON.stringify(data.table) };
|
|
2688
|
+
};
|
|
2677
2689
|
var snippets = {
|
|
2678
2690
|
"audio-classification": snippetGenerator("basicAudio"),
|
|
2679
2691
|
"audio-to-audio": snippetGenerator("basicAudio"),
|
|
@@ -2687,12 +2699,12 @@ var snippets = {
|
|
|
2687
2699
|
"image-to-image": snippetGenerator("imageToImage", prepareImageToImageInput),
|
|
2688
2700
|
"image-to-text": snippetGenerator("basicImage"),
|
|
2689
2701
|
"object-detection": snippetGenerator("basicImage"),
|
|
2690
|
-
"question-answering": snippetGenerator("
|
|
2702
|
+
"question-answering": snippetGenerator("questionAnswering", prepareQuestionAnsweringInput),
|
|
2691
2703
|
"sentence-similarity": snippetGenerator("basic"),
|
|
2692
2704
|
summarization: snippetGenerator("basic"),
|
|
2693
2705
|
"tabular-classification": snippetGenerator("tabular"),
|
|
2694
2706
|
"tabular-regression": snippetGenerator("tabular"),
|
|
2695
|
-
"table-question-answering": snippetGenerator("
|
|
2707
|
+
"table-question-answering": snippetGenerator("tableQuestionAnswering", prepareTableQuestionAnsweringInput),
|
|
2696
2708
|
"text-classification": snippetGenerator("basic"),
|
|
2697
2709
|
"text-generation": snippetGenerator("basic"),
|
|
2698
2710
|
"text-to-audio": snippetGenerator("textToAudio"),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getDefaultTask.d.ts","sourceRoot":"","sources":["../../../src/lib/getDefaultTask.ts"],"names":[],"mappings":"AAYA,MAAM,WAAW,kBAAkB;IAClC,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;CACrB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CACnC,KAAK,EAAE,MAAM,EACb,WAAW,EAAE,MAAM,GAAG,SAAS,EAC/B,OAAO,CAAC,EAAE,kBAAkB,GAC1B,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"getDefaultTask.d.ts","sourceRoot":"","sources":["../../../src/lib/getDefaultTask.ts"],"names":[],"mappings":"AAYA,MAAM,WAAW,kBAAkB;IAClC,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;CACrB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CACnC,KAAK,EAAE,MAAM,EACb,WAAW,EAAE,MAAM,GAAG,SAAS,EAC/B,OAAO,CAAC,EAAE,kBAAkB,GAC1B,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAmCxB"}
|