@huggingface/inference 3.7.0 → 3.8.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.
Files changed (141) hide show
  1. package/dist/index.cjs +1369 -941
  2. package/dist/index.js +1371 -943
  3. package/dist/src/lib/getInferenceProviderMapping.d.ts +21 -0
  4. package/dist/src/lib/getInferenceProviderMapping.d.ts.map +1 -0
  5. package/dist/src/lib/getProviderHelper.d.ts +37 -0
  6. package/dist/src/lib/getProviderHelper.d.ts.map +1 -0
  7. package/dist/src/lib/makeRequestOptions.d.ts +5 -5
  8. package/dist/src/lib/makeRequestOptions.d.ts.map +1 -1
  9. package/dist/src/providers/black-forest-labs.d.ts +14 -18
  10. package/dist/src/providers/black-forest-labs.d.ts.map +1 -1
  11. package/dist/src/providers/cerebras.d.ts +4 -2
  12. package/dist/src/providers/cerebras.d.ts.map +1 -1
  13. package/dist/src/providers/cohere.d.ts +5 -2
  14. package/dist/src/providers/cohere.d.ts.map +1 -1
  15. package/dist/src/providers/consts.d.ts +2 -3
  16. package/dist/src/providers/consts.d.ts.map +1 -1
  17. package/dist/src/providers/fal-ai.d.ts +50 -3
  18. package/dist/src/providers/fal-ai.d.ts.map +1 -1
  19. package/dist/src/providers/fireworks-ai.d.ts +5 -2
  20. package/dist/src/providers/fireworks-ai.d.ts.map +1 -1
  21. package/dist/src/providers/hf-inference.d.ts +126 -2
  22. package/dist/src/providers/hf-inference.d.ts.map +1 -1
  23. package/dist/src/providers/hyperbolic.d.ts +31 -2
  24. package/dist/src/providers/hyperbolic.d.ts.map +1 -1
  25. package/dist/src/providers/nebius.d.ts +20 -18
  26. package/dist/src/providers/nebius.d.ts.map +1 -1
  27. package/dist/src/providers/novita.d.ts +21 -18
  28. package/dist/src/providers/novita.d.ts.map +1 -1
  29. package/dist/src/providers/openai.d.ts +4 -2
  30. package/dist/src/providers/openai.d.ts.map +1 -1
  31. package/dist/src/providers/providerHelper.d.ts +182 -0
  32. package/dist/src/providers/providerHelper.d.ts.map +1 -0
  33. package/dist/src/providers/replicate.d.ts +23 -19
  34. package/dist/src/providers/replicate.d.ts.map +1 -1
  35. package/dist/src/providers/sambanova.d.ts +4 -2
  36. package/dist/src/providers/sambanova.d.ts.map +1 -1
  37. package/dist/src/providers/together.d.ts +32 -2
  38. package/dist/src/providers/together.d.ts.map +1 -1
  39. package/dist/src/snippets/getInferenceSnippets.d.ts +2 -1
  40. package/dist/src/snippets/getInferenceSnippets.d.ts.map +1 -1
  41. package/dist/src/tasks/audio/audioClassification.d.ts.map +1 -1
  42. package/dist/src/tasks/audio/automaticSpeechRecognition.d.ts.map +1 -1
  43. package/dist/src/tasks/audio/textToSpeech.d.ts.map +1 -1
  44. package/dist/src/tasks/audio/utils.d.ts +2 -1
  45. package/dist/src/tasks/audio/utils.d.ts.map +1 -1
  46. package/dist/src/tasks/custom/request.d.ts +0 -2
  47. package/dist/src/tasks/custom/request.d.ts.map +1 -1
  48. package/dist/src/tasks/custom/streamingRequest.d.ts +0 -2
  49. package/dist/src/tasks/custom/streamingRequest.d.ts.map +1 -1
  50. package/dist/src/tasks/cv/imageClassification.d.ts.map +1 -1
  51. package/dist/src/tasks/cv/imageSegmentation.d.ts.map +1 -1
  52. package/dist/src/tasks/cv/imageToImage.d.ts.map +1 -1
  53. package/dist/src/tasks/cv/imageToText.d.ts.map +1 -1
  54. package/dist/src/tasks/cv/objectDetection.d.ts.map +1 -1
  55. package/dist/src/tasks/cv/textToImage.d.ts.map +1 -1
  56. package/dist/src/tasks/cv/textToVideo.d.ts.map +1 -1
  57. package/dist/src/tasks/cv/zeroShotImageClassification.d.ts.map +1 -1
  58. package/dist/src/tasks/index.d.ts +6 -6
  59. package/dist/src/tasks/index.d.ts.map +1 -1
  60. package/dist/src/tasks/multimodal/documentQuestionAnswering.d.ts.map +1 -1
  61. package/dist/src/tasks/multimodal/visualQuestionAnswering.d.ts.map +1 -1
  62. package/dist/src/tasks/nlp/chatCompletion.d.ts.map +1 -1
  63. package/dist/src/tasks/nlp/chatCompletionStream.d.ts.map +1 -1
  64. package/dist/src/tasks/nlp/featureExtraction.d.ts.map +1 -1
  65. package/dist/src/tasks/nlp/fillMask.d.ts.map +1 -1
  66. package/dist/src/tasks/nlp/questionAnswering.d.ts.map +1 -1
  67. package/dist/src/tasks/nlp/sentenceSimilarity.d.ts.map +1 -1
  68. package/dist/src/tasks/nlp/summarization.d.ts.map +1 -1
  69. package/dist/src/tasks/nlp/tableQuestionAnswering.d.ts.map +1 -1
  70. package/dist/src/tasks/nlp/textClassification.d.ts.map +1 -1
  71. package/dist/src/tasks/nlp/textGeneration.d.ts.map +1 -1
  72. package/dist/src/tasks/nlp/textGenerationStream.d.ts.map +1 -1
  73. package/dist/src/tasks/nlp/tokenClassification.d.ts.map +1 -1
  74. package/dist/src/tasks/nlp/translation.d.ts.map +1 -1
  75. package/dist/src/tasks/nlp/zeroShotClassification.d.ts.map +1 -1
  76. package/dist/src/tasks/tabular/tabularClassification.d.ts.map +1 -1
  77. package/dist/src/tasks/tabular/tabularRegression.d.ts.map +1 -1
  78. package/dist/src/types.d.ts +5 -13
  79. package/dist/src/types.d.ts.map +1 -1
  80. package/dist/src/utils/request.d.ts +3 -2
  81. package/dist/src/utils/request.d.ts.map +1 -1
  82. package/package.json +3 -3
  83. package/src/lib/getInferenceProviderMapping.ts +96 -0
  84. package/src/lib/getProviderHelper.ts +270 -0
  85. package/src/lib/makeRequestOptions.ts +78 -97
  86. package/src/providers/black-forest-labs.ts +73 -22
  87. package/src/providers/cerebras.ts +6 -27
  88. package/src/providers/cohere.ts +9 -28
  89. package/src/providers/consts.ts +5 -2
  90. package/src/providers/fal-ai.ts +224 -77
  91. package/src/providers/fireworks-ai.ts +8 -29
  92. package/src/providers/hf-inference.ts +557 -34
  93. package/src/providers/hyperbolic.ts +107 -29
  94. package/src/providers/nebius.ts +65 -29
  95. package/src/providers/novita.ts +68 -32
  96. package/src/providers/openai.ts +6 -32
  97. package/src/providers/providerHelper.ts +354 -0
  98. package/src/providers/replicate.ts +124 -34
  99. package/src/providers/sambanova.ts +5 -30
  100. package/src/providers/together.ts +92 -28
  101. package/src/snippets/getInferenceSnippets.ts +39 -14
  102. package/src/snippets/templates.exported.ts +25 -25
  103. package/src/tasks/audio/audioClassification.ts +5 -8
  104. package/src/tasks/audio/audioToAudio.ts +4 -27
  105. package/src/tasks/audio/automaticSpeechRecognition.ts +5 -4
  106. package/src/tasks/audio/textToSpeech.ts +5 -29
  107. package/src/tasks/audio/utils.ts +2 -1
  108. package/src/tasks/custom/request.ts +3 -3
  109. package/src/tasks/custom/streamingRequest.ts +4 -3
  110. package/src/tasks/cv/imageClassification.ts +4 -8
  111. package/src/tasks/cv/imageSegmentation.ts +4 -9
  112. package/src/tasks/cv/imageToImage.ts +4 -7
  113. package/src/tasks/cv/imageToText.ts +4 -7
  114. package/src/tasks/cv/objectDetection.ts +4 -19
  115. package/src/tasks/cv/textToImage.ts +9 -137
  116. package/src/tasks/cv/textToVideo.ts +17 -64
  117. package/src/tasks/cv/zeroShotImageClassification.ts +4 -8
  118. package/src/tasks/index.ts +6 -6
  119. package/src/tasks/multimodal/documentQuestionAnswering.ts +4 -19
  120. package/src/tasks/multimodal/visualQuestionAnswering.ts +4 -12
  121. package/src/tasks/nlp/chatCompletion.ts +5 -20
  122. package/src/tasks/nlp/chatCompletionStream.ts +4 -3
  123. package/src/tasks/nlp/featureExtraction.ts +4 -19
  124. package/src/tasks/nlp/fillMask.ts +4 -17
  125. package/src/tasks/nlp/questionAnswering.ts +11 -26
  126. package/src/tasks/nlp/sentenceSimilarity.ts +4 -8
  127. package/src/tasks/nlp/summarization.ts +4 -7
  128. package/src/tasks/nlp/tableQuestionAnswering.ts +10 -30
  129. package/src/tasks/nlp/textClassification.ts +4 -9
  130. package/src/tasks/nlp/textGeneration.ts +11 -79
  131. package/src/tasks/nlp/textGenerationStream.ts +3 -1
  132. package/src/tasks/nlp/tokenClassification.ts +11 -23
  133. package/src/tasks/nlp/translation.ts +4 -7
  134. package/src/tasks/nlp/zeroShotClassification.ts +11 -21
  135. package/src/tasks/tabular/tabularClassification.ts +4 -7
  136. package/src/tasks/tabular/tabularRegression.ts +4 -7
  137. package/src/types.ts +5 -14
  138. package/src/utils/request.ts +7 -4
  139. package/dist/src/lib/getProviderModelId.d.ts +0 -10
  140. package/dist/src/lib/getProviderModelId.d.ts.map +0 -1
  141. package/src/lib/getProviderModelId.ts +0 -74
@@ -1,16 +1,18 @@
1
- import type { PipelineType, WidgetType } from "@huggingface/tasks/src/pipelines.js";
2
- import type { ChatCompletionInputMessage, GenerationParameters } from "@huggingface/tasks/src/tasks/index.js";
1
+ import { Template } from "@huggingface/jinja";
3
2
  import {
4
3
  type InferenceSnippet,
5
4
  type InferenceSnippetLanguage,
6
5
  type ModelDataMinimal,
7
- inferenceSnippetLanguages,
8
6
  getModelInputSnippet,
7
+ inferenceSnippetLanguages,
9
8
  } from "@huggingface/tasks";
10
- import type { InferenceProvider, InferenceTask, RequestArgs } from "../types";
11
- import { Template } from "@huggingface/jinja";
9
+ import type { PipelineType, WidgetType } from "@huggingface/tasks/src/pipelines.js";
10
+ import type { ChatCompletionInputMessage, GenerationParameters } from "@huggingface/tasks/src/tasks/index.js";
12
11
  import { makeRequestOptionsFromResolvedModel } from "../lib/makeRequestOptions";
12
+ import type { InferenceProvider, InferenceTask, RequestArgs } from "../types";
13
13
  import { templates } from "./templates.exported";
14
+ import type { InferenceProviderModelMapping } from "../lib/getInferenceProviderMapping";
15
+ import { getProviderHelper } from "../lib/getProviderHelper";
14
16
 
15
17
  const PYTHON_CLIENTS = ["huggingface_hub", "fal_client", "requests", "openai"] as const;
16
18
  const JS_CLIENTS = ["fetch", "huggingface.js", "openai"] as const;
@@ -35,6 +37,7 @@ interface TemplateParams {
35
37
  model?: ModelDataMinimal;
36
38
  provider?: InferenceProvider;
37
39
  providerModelId?: string;
40
+ billTo?: string;
38
41
  methodName?: string; // specific to snippetBasic
39
42
  importBase64?: boolean; // specific to snippetImportRequests
40
43
  importJson?: boolean; // specific to snippetImportRequests
@@ -116,10 +119,13 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
116
119
  model: ModelDataMinimal,
117
120
  accessToken: string,
118
121
  provider: InferenceProvider,
119
- providerModelId?: string,
122
+ inferenceProviderMapping?: InferenceProviderModelMapping,
123
+ billTo?: string,
120
124
  opts?: Record<string, unknown>
121
125
  ): InferenceSnippet[] => {
126
+ const providerModelId = inferenceProviderMapping?.providerId ?? model.id;
122
127
  /// Hacky: hard-code conversational templates here
128
+ let task = model.pipeline_tag as InferenceTask;
123
129
  if (
124
130
  model.pipeline_tag &&
125
131
  ["text-generation", "image-text-to-text"].includes(model.pipeline_tag) &&
@@ -127,14 +133,30 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
127
133
  ) {
128
134
  templateName = opts?.streaming ? "conversationalStream" : "conversational";
129
135
  inputPreparationFn = prepareConversationalInput;
136
+ task = "conversational";
137
+ }
138
+ let providerHelper: ReturnType<typeof getProviderHelper>;
139
+ try {
140
+ providerHelper = getProviderHelper(provider, task);
141
+ } catch (e) {
142
+ console.error(`Failed to get provider helper for ${provider} (${task})`, e);
143
+ return [];
130
144
  }
131
-
132
145
  /// Prepare inputs + make request
133
146
  const inputs = inputPreparationFn ? inputPreparationFn(model, opts) : { inputs: getModelInputSnippet(model) };
134
147
  const request = makeRequestOptionsFromResolvedModel(
135
- providerModelId ?? model.id,
136
- { accessToken: accessToken, provider: provider, ...inputs } as RequestArgs,
137
- { chatCompletion: templateName.includes("conversational"), task: model.pipeline_tag as InferenceTask }
148
+ providerModelId,
149
+ providerHelper,
150
+ {
151
+ accessToken,
152
+ provider,
153
+ ...inputs,
154
+ } as RequestArgs,
155
+ inferenceProviderMapping,
156
+ {
157
+ task,
158
+ billTo,
159
+ }
138
160
  );
139
161
 
140
162
  /// Parse request.info.body if not a binary.
@@ -172,6 +194,7 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
172
194
  model,
173
195
  provider,
174
196
  providerModelId: providerModelId ?? model.id,
197
+ billTo,
175
198
  };
176
199
 
177
200
  /// Iterate over clients => check if a snippet exists => generate
@@ -247,7 +270,7 @@ const prepareConversationalInput = (
247
270
  return {
248
271
  messages: opts?.messages ?? getModelInputSnippet(model),
249
272
  ...(opts?.temperature ? { temperature: opts?.temperature } : undefined),
250
- max_tokens: opts?.max_tokens ?? 500,
273
+ max_tokens: opts?.max_tokens ?? 512,
251
274
  ...(opts?.top_p ? { top_p: opts?.top_p } : undefined),
252
275
  };
253
276
  };
@@ -259,7 +282,8 @@ const snippets: Partial<
259
282
  model: ModelDataMinimal,
260
283
  accessToken: string,
261
284
  provider: InferenceProvider,
262
- providerModelId?: string,
285
+ inferenceProviderMapping?: InferenceProviderModelMapping,
286
+ billTo?: string,
263
287
  opts?: Record<string, unknown>
264
288
  ) => InferenceSnippet[]
265
289
  >
@@ -299,11 +323,12 @@ export function getInferenceSnippets(
299
323
  model: ModelDataMinimal,
300
324
  accessToken: string,
301
325
  provider: InferenceProvider,
302
- providerModelId?: string,
326
+ inferenceProviderMapping?: InferenceProviderModelMapping,
327
+ billTo?: string,
303
328
  opts?: Record<string, unknown>
304
329
  ): InferenceSnippet[] {
305
330
  return model.pipeline_tag && model.pipeline_tag in snippets
306
- ? snippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId, opts) ?? []
331
+ ? snippets[model.pipeline_tag]?.(model, accessToken, provider, inferenceProviderMapping, billTo, opts) ?? []
307
332
  : [];
308
333
  }
309
334
 
@@ -2,25 +2,25 @@
2
2
  export const templates: Record<string, Record<string, Record<string, string>>> = {
3
3
  "js": {
4
4
  "fetch": {
5
- "basic": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
6
- "basicAudio": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"audio/flac\"\n\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
7
- "basicImage": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"image/jpeg\"\n\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
8
- "textToAudio": "{% if model.library_name == \"transformers\" %}\nasync function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst 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\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\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 %} ",
9
- "textToImage": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.blob();\n\treturn result;\n}\n\n\nquery({ {{ providerInputs.asTsString }} }).then((response) => {\n // Use image\n});",
10
- "zeroShotClassification": "async function query(data) {\n const response = await fetch(\n\t\t\"{{ fullUrl }}\",\n {\n headers: {\n\t\t\t\tAuthorization: \"{{ 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});"
5
+ "basic": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
6
+ "basicAudio": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"audio/flac\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
7
+ "basicImage": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"image/jpeg\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
8
+ "textToAudio": "{% if model.library_name == \"transformers\" %}\nasync function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst 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\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\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 %} ",
9
+ "textToImage": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.blob();\n\treturn result;\n}\n\n\nquery({ {{ providerInputs.asTsString }} }).then((response) => {\n // Use image\n});",
10
+ "zeroShotClassification": "async function query(data) {\n const response = await fetch(\n\t\t\"{{ fullUrl }}\",\n {\n headers: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n \"Content-Type\": \"application/json\",\n{% if billTo %}\n \"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %} },\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});"
11
11
  },
12
12
  "huggingface.js": {
13
- "basic": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nconst output = await client.{{ methodName }}({\n\tmodel: \"{{ model.id }}\",\n\tinputs: {{ inputs.asObj.inputs }},\n\tprovider: \"{{ provider }}\",\n});\n\nconsole.log(output);",
14
- "basicAudio": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nconst data = fs.readFileSync({{inputs.asObj.inputs}});\n\nconst output = await client.{{ methodName }}({\n\tdata,\n\tmodel: \"{{ model.id }}\",\n\tprovider: \"{{ provider }}\",\n});\n\nconsole.log(output);",
15
- "basicImage": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nconst data = fs.readFileSync({{inputs.asObj.inputs}});\n\nconst output = await client.{{ methodName }}({\n\tdata,\n\tmodel: \"{{ model.id }}\",\n\tprovider: \"{{ provider }}\",\n});\n\nconsole.log(output);",
16
- "conversational": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nconst chatCompletion = await client.chatCompletion({\n provider: \"{{ provider }}\",\n model: \"{{ model.id }}\",\n{{ inputs.asTsString }}\n});\n\nconsole.log(chatCompletion.choices[0].message);",
17
- "conversationalStream": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nlet out = \"\";\n\nconst stream = await client.chatCompletionStream({\n provider: \"{{ provider }}\",\n model: \"{{ model.id }}\",\n{{ inputs.asTsString }}\n});\n\nfor await (const chunk of stream) {\n\tif (chunk.choices && chunk.choices.length > 0) {\n\t\tconst newContent = chunk.choices[0].delta.content;\n\t\tout += newContent;\n\t\tconsole.log(newContent);\n\t} \n}",
18
- "textToImage": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nconst image = await client.textToImage({\n provider: \"{{ provider }}\",\n model: \"{{ model.id }}\",\n\tinputs: {{ inputs.asObj.inputs }},\n\tparameters: { num_inference_steps: 5 },\n});\n/// Use the generated image (it's a Blob)",
19
- "textToVideo": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nconst image = await client.textToVideo({\n provider: \"{{ provider }}\",\n model: \"{{ model.id }}\",\n\tinputs: {{ inputs.asObj.inputs }},\n});\n// Use the generated video (it's a Blob)"
13
+ "basic": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nconst output = await client.{{ methodName }}({\n\tmodel: \"{{ model.id }}\",\n\tinputs: {{ inputs.asObj.inputs }},\n\tprovider: \"{{ provider }}\",\n}{% if billTo %}, {\n\tbillTo: \"{{ billTo }}\",\n}{% endif %});\n\nconsole.log(output);",
14
+ "basicAudio": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nconst data = fs.readFileSync({{inputs.asObj.inputs}});\n\nconst output = await client.{{ methodName }}({\n\tdata,\n\tmodel: \"{{ model.id }}\",\n\tprovider: \"{{ provider }}\",\n}{% if billTo %}, {\n\tbillTo: \"{{ billTo }}\",\n}{% endif %});\n\nconsole.log(output);",
15
+ "basicImage": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nconst data = fs.readFileSync({{inputs.asObj.inputs}});\n\nconst output = await client.{{ methodName }}({\n\tdata,\n\tmodel: \"{{ model.id }}\",\n\tprovider: \"{{ provider }}\",\n}{% if billTo %}, {\n\tbillTo: \"{{ billTo }}\",\n}{% endif %});\n\nconsole.log(output);",
16
+ "conversational": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nconst chatCompletion = await client.chatCompletion({\n provider: \"{{ provider }}\",\n model: \"{{ model.id }}\",\n{{ inputs.asTsString }}\n}{% if billTo %}, {\n billTo: \"{{ billTo }}\",\n}{% endif %});\n\nconsole.log(chatCompletion.choices[0].message);",
17
+ "conversationalStream": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nlet out = \"\";\n\nconst stream = await client.chatCompletionStream({\n provider: \"{{ provider }}\",\n model: \"{{ model.id }}\",\n{{ inputs.asTsString }}\n}{% if billTo %}, {\n billTo: \"{{ billTo }}\",\n}{% endif %});\n\nfor await (const chunk of stream) {\n\tif (chunk.choices && chunk.choices.length > 0) {\n\t\tconst newContent = chunk.choices[0].delta.content;\n\t\tout += newContent;\n\t\tconsole.log(newContent);\n\t} \n}",
18
+ "textToImage": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nconst image = await client.textToImage({\n provider: \"{{ provider }}\",\n model: \"{{ model.id }}\",\n\tinputs: {{ inputs.asObj.inputs }},\n\tparameters: { num_inference_steps: 5 },\n}{% if billTo %}, {\n billTo: \"{{ billTo }}\",\n}{% endif %});\n/// Use the generated image (it's a Blob)",
19
+ "textToVideo": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nconst image = await client.textToVideo({\n provider: \"{{ provider }}\",\n model: \"{{ model.id }}\",\n\tinputs: {{ inputs.asObj.inputs }},\n}{% if billTo %}, {\n billTo: \"{{ billTo }}\",\n}{% endif %});\n// Use the generated video (it's a Blob)"
20
20
  },
21
21
  "openai": {
22
- "conversational": "import { OpenAI } from \"openai\";\n\nconst client = new OpenAI({\n\tbaseURL: \"{{ baseUrl }}\",\n\tapiKey: \"{{ accessToken }}\",\n});\n\nconst chatCompletion = await client.chat.completions.create({\n\tmodel: \"{{ providerModelId }}\",\n{{ inputs.asTsString }}\n});\n\nconsole.log(chatCompletion.choices[0].message);",
23
- "conversationalStream": "import { OpenAI } from \"openai\";\n\nconst client = new OpenAI({\n\tbaseURL: \"{{ baseUrl }}\",\n\tapiKey: \"{{ accessToken }}\",\n});\n\nlet out = \"\";\n\nconst stream = await client.chat.completions.create({\n provider: \"{{ provider }}\",\n model: \"{{ model.id }}\",\n{{ inputs.asTsString }}\n});\n\nfor await (const chunk of stream) {\n\tif (chunk.choices && chunk.choices.length > 0) {\n\t\tconst newContent = chunk.choices[0].delta.content;\n\t\tout += newContent;\n\t\tconsole.log(newContent);\n\t} \n}"
22
+ "conversational": "import { OpenAI } from \"openai\";\n\nconst client = new OpenAI({\n\tbaseURL: \"{{ baseUrl }}\",\n\tapiKey: \"{{ accessToken }}\",\n{% if billTo %}\n\tdefaultHeaders: {\n\t\t\"X-HF-Bill-To\": \"{{ billTo }}\" \n\t}\n{% endif %}\n});\n\nconst chatCompletion = await client.chat.completions.create({\n\tmodel: \"{{ providerModelId }}\",\n{{ inputs.asTsString }}\n});\n\nconsole.log(chatCompletion.choices[0].message);",
23
+ "conversationalStream": "import { OpenAI } from \"openai\";\n\nconst client = new OpenAI({\n\tbaseURL: \"{{ baseUrl }}\",\n\tapiKey: \"{{ accessToken }}\",\n{% if billTo %}\n defaultHeaders: {\n\t\t\"X-HF-Bill-To\": \"{{ billTo }}\" \n\t}\n{% endif %}\n});\n\nconst stream = await client.chat.completions.create({\n model: \"{{ providerModelId }}\",\n{{ inputs.asTsString }}\n stream: true,\n});\n\nfor await (const chunk of stream) {\n process.stdout.write(chunk.choices[0]?.delta?.content || \"\");\n}"
24
24
  }
25
25
  },
26
26
  "python": {
@@ -35,13 +35,13 @@ export const templates: Record<string, Record<string, Record<string, string>>> =
35
35
  "conversationalStream": "stream = client.chat.completions.create(\n model=\"{{ model.id }}\",\n{{ inputs.asPythonString }}\n stream=True,\n)\n\nfor chunk in stream:\n print(chunk.choices[0].delta.content, end=\"\") ",
36
36
  "documentQuestionAnswering": "output = client.document_question_answering(\n \"{{ inputs.asObj.image }}\",\n question=\"{{ inputs.asObj.question }}\",\n model=\"{{ model.id }}\",\n) ",
37
37
  "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) ",
38
- "importInferenceClient": "from huggingface_hub import InferenceClient\n\nclient = InferenceClient(\n provider=\"{{ provider }}\",\n api_key=\"{{ accessToken }}\",\n)",
38
+ "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)",
39
39
  "textToImage": "# output is a PIL.Image object\nimage = client.text_to_image(\n {{ inputs.asObj.inputs }},\n model=\"{{ model.id }}\",\n) ",
40
40
  "textToVideo": "video = client.text_to_video(\n {{ inputs.asObj.inputs }},\n model=\"{{ model.id }}\",\n) "
41
41
  },
42
42
  "openai": {
43
- "conversational": "from openai import OpenAI\n\nclient = OpenAI(\n base_url=\"{{ baseUrl }}\",\n api_key=\"{{ accessToken }}\"\n)\n\ncompletion = client.chat.completions.create(\n model=\"{{ providerModelId }}\",\n{{ inputs.asPythonString }}\n)\n\nprint(completion.choices[0].message) ",
44
- "conversationalStream": "from openai import OpenAI\n\nclient = OpenAI(\n base_url=\"{{ baseUrl }}\",\n api_key=\"{{ accessToken }}\"\n)\n\nstream = client.chat.completions.create(\n model=\"{{ providerModelId }}\",\n{{ inputs.asPythonString }}\n stream=True,\n)\n\nfor chunk in stream:\n print(chunk.choices[0].delta.content, end=\"\")"
43
+ "conversational": "from openai import OpenAI\n\nclient = OpenAI(\n base_url=\"{{ baseUrl }}\",\n api_key=\"{{ accessToken }}\",\n{% if billTo %}\n default_headers={\n \"X-HF-Bill-To\": \"{{ billTo }}\"\n }\n{% endif %}\n)\n\ncompletion = client.chat.completions.create(\n model=\"{{ providerModelId }}\",\n{{ inputs.asPythonString }}\n)\n\nprint(completion.choices[0].message) ",
44
+ "conversationalStream": "from openai import OpenAI\n\nclient = OpenAI(\n base_url=\"{{ baseUrl }}\",\n api_key=\"{{ accessToken }}\",\n{% if billTo %}\n default_headers={\n \"X-HF-Bill-To\": \"{{ billTo }}\"\n }\n{% endif %}\n)\n\nstream = client.chat.completions.create(\n model=\"{{ providerModelId }}\",\n{{ inputs.asPythonString }}\n stream=True,\n)\n\nfor chunk in stream:\n print(chunk.choices[0].delta.content, end=\"\")"
45
45
  },
46
46
  "requests": {
47
47
  "basic": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\noutput = query({\n \"inputs\": {{ providerInputs.asObj.inputs }},\n}) ",
@@ -51,7 +51,7 @@ export const templates: Record<string, Record<string, Record<string, string>>> =
51
51
  "conversationalStream": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload, stream=True)\n for line in response.iter_lines():\n if not line.startswith(b\"data:\"):\n continue\n if line.strip() == b\"data: [DONE]\":\n return\n yield json.loads(line.decode(\"utf-8\").lstrip(\"data:\").rstrip(\"/n\"))\n\nchunks = query({\n{{ providerInputs.asJsonString }},\n \"stream\": True,\n})\n\nfor chunk in chunks:\n print(chunk[\"choices\"][0][\"delta\"][\"content\"], end=\"\")",
52
52
  "documentQuestionAnswering": "def query(payload):\n with open(payload[\"image\"], \"rb\") as f:\n img = f.read()\n payload[\"image\"] = base64.b64encode(img).decode(\"utf-8\")\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\noutput = query({\n \"inputs\": {\n \"image\": \"{{ inputs.asObj.image }}\",\n \"question\": \"{{ inputs.asObj.question }}\",\n },\n}) ",
53
53
  "imageToImage": "def query(payload):\n with open(payload[\"inputs\"], \"rb\") as f:\n img = f.read()\n payload[\"inputs\"] = base64.b64encode(img).decode(\"utf-8\")\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.content\n\nimage_bytes = query({\n{{ providerInputs.asJsonString }}\n})\n\n# You can access the image with PIL.Image for example\nimport io\nfrom PIL import Image\nimage = Image.open(io.BytesIO(image_bytes)) ",
54
- "importRequests": "{% if importBase64 %}\nimport base64\n{% endif %}\n{% if importJson %}\nimport json\n{% endif %}\nimport requests\n\nAPI_URL = \"{{ fullUrl }}\"\nheaders = {\"Authorization\": \"{{ authorizationHeader }}\"}",
54
+ "importRequests": "{% if importBase64 %}\nimport base64\n{% endif %}\n{% if importJson %}\nimport json\n{% endif %}\nimport requests\n\nAPI_URL = \"{{ fullUrl }}\"\nheaders = {\n \"Authorization\": \"{{ authorizationHeader }}\",\n{% if billTo %}\n \"X-HF-Bill-To\": \"{{ billTo }}\"\n{% endif %}\n}",
55
55
  "tabular": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.content\n\nresponse = query({\n \"inputs\": {\n \"data\": {{ providerInputs.asObj.inputs }}\n },\n}) ",
56
56
  "textToAudio": "{% if model.library_name == \"transformers\" %}\ndef query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.content\n\naudio_bytes = query({\n \"inputs\": {{ providerInputs.asObj.inputs }},\n})\n# You can access the audio with IPython.display for example\nfrom IPython.display import Audio\nAudio(audio_bytes)\n{% else %}\ndef query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\naudio, sampling_rate = query({\n \"inputs\": {{ providerInputs.asObj.inputs }},\n})\n# You can access the audio with IPython.display for example\nfrom IPython.display import Audio\nAudio(audio, rate=sampling_rate)\n{% endif %} ",
57
57
  "textToImage": "{% if provider == \"hf-inference\" %}\ndef query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.content\n\nimage_bytes = query({\n \"inputs\": {{ providerInputs.asObj.inputs }},\n})\n\n# You can access the image with PIL.Image for example\nimport io\nfrom PIL import Image\nimage = Image.open(io.BytesIO(image_bytes))\n{% endif %}",
@@ -61,12 +61,12 @@ export const templates: Record<string, Record<string, Record<string, string>>> =
61
61
  },
62
62
  "sh": {
63
63
  "curl": {
64
- "basic": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n -d '{\n{{ providerInputs.asCurlString }}\n }'",
65
- "basicAudio": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: audio/flac' \\\n --data-binary @{{ providerInputs.asObj.inputs }}",
66
- "basicImage": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: image/jpeg' \\\n --data-binary @{{ providerInputs.asObj.inputs }}",
67
- "conversational": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n -d '{\n{{ providerInputs.asCurlString }},\n \"stream\": false\n }'",
68
- "conversationalStream": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n -d '{\n{{ providerInputs.asCurlString }},\n \"stream\": true\n }'",
69
- "zeroShotClassification": "curl {{ fullUrl }} \\\n -X POST \\\n -d '{\"inputs\": {{ providerInputs.asObj.inputs }}, \"parameters\": {\"candidate_labels\": [\"refund\", \"legal\", \"faq\"]}}' \\\n -H 'Content-Type: application/json' \\\n -H 'Authorization: {{ authorizationHeader }}'"
64
+ "basic": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{ providerInputs.asCurlString }}\n }'",
65
+ "basicAudio": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: audio/flac' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n --data-binary @{{ providerInputs.asObj.inputs }}",
66
+ "basicImage": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: image/jpeg' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n --data-binary @{{ providerInputs.asObj.inputs }}",
67
+ "conversational": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{ providerInputs.asCurlString }},\n \"stream\": false\n }'",
68
+ "conversationalStream": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{ providerInputs.asCurlString }},\n \"stream\": true\n }'",
69
+ "zeroShotClassification": "curl {{ fullUrl }} \\\n -X POST \\\n -d '{\"inputs\": {{ providerInputs.asObj.inputs }}, \"parameters\": {\"candidate_labels\": [\"refund\", \"legal\", \"faq\"]}}' \\\n -H 'Content-Type: application/json' \\\n -H 'Authorization: {{ authorizationHeader }}'\n{% if billTo %} \\\n -H 'X-HF-Bill-To: {{ billTo }}'\n{% endif %}"
70
70
  }
71
71
  }
72
72
  } as const;
@@ -1,5 +1,5 @@
1
1
  import type { AudioClassificationInput, AudioClassificationOutput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
3
  import type { BaseArgs, Options } from "../../types";
4
4
  import { innerRequest } from "../../utils/request";
5
5
  import type { LegacyAudioInput } from "./utils";
@@ -15,15 +15,12 @@ export async function audioClassification(
15
15
  args: AudioClassificationArgs,
16
16
  options?: Options
17
17
  ): Promise<AudioClassificationOutput> {
18
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "audio-classification");
18
19
  const payload = preparePayload(args);
19
- const { data: res } = await innerRequest<AudioClassificationOutput>(payload, {
20
+ const { data: res } = await innerRequest<AudioClassificationOutput>(payload, providerHelper, {
20
21
  ...options,
21
22
  task: "audio-classification",
22
23
  });
23
- const isValidOutput =
24
- Array.isArray(res) && res.every((x) => typeof x.label === "string" && typeof x.score === "number");
25
- if (!isValidOutput) {
26
- throw new InferenceOutputError("Expected Array<{label: string, score: number}>");
27
- }
28
- return res;
24
+
25
+ return providerHelper.getResponse(res);
29
26
  }
@@ -1,4 +1,4 @@
1
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
1
+ import { getProviderHelper } from "../../lib/getProviderHelper";
2
2
  import type { BaseArgs, Options } from "../../types";
3
3
  import { innerRequest } from "../../utils/request";
4
4
  import type { LegacyAudioInput } from "./utils";
@@ -36,34 +36,11 @@ export interface AudioToAudioOutput {
36
36
  * Example model: speechbrain/sepformer-wham does audio source separation.
37
37
  */
38
38
  export async function audioToAudio(args: AudioToAudioArgs, options?: Options): Promise<AudioToAudioOutput[]> {
39
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "audio-to-audio");
39
40
  const payload = preparePayload(args);
40
- const { data: res } = await innerRequest<AudioToAudioOutput>(payload, {
41
+ const { data: res } = await innerRequest<AudioToAudioOutput>(payload, providerHelper, {
41
42
  ...options,
42
43
  task: "audio-to-audio",
43
44
  });
44
-
45
- return validateOutput(res);
46
- }
47
-
48
- function validateOutput(output: unknown): AudioToAudioOutput[] {
49
- if (!Array.isArray(output)) {
50
- throw new InferenceOutputError("Expected Array");
51
- }
52
- if (
53
- !output.every((elem): elem is AudioToAudioOutput => {
54
- return (
55
- typeof elem === "object" &&
56
- elem &&
57
- "label" in elem &&
58
- typeof elem.label === "string" &&
59
- "content-type" in elem &&
60
- typeof elem["content-type"] === "string" &&
61
- "blob" in elem &&
62
- typeof elem.blob === "string"
63
- );
64
- })
65
- ) {
66
- throw new InferenceOutputError("Expected Array<{label: string, audio: Blob}>");
67
- }
68
- return output;
45
+ return providerHelper.getResponse(res);
69
46
  }
@@ -1,5 +1,7 @@
1
1
  import type { AutomaticSpeechRecognitionInput, AutomaticSpeechRecognitionOutput } from "@huggingface/tasks";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
2
3
  import { InferenceOutputError } from "../../lib/InferenceOutputError";
4
+ import { FAL_AI_SUPPORTED_BLOB_TYPES } from "../../providers/fal-ai";
3
5
  import type { BaseArgs, Options, RequestArgs } from "../../types";
4
6
  import { base64FromBytes } from "../../utils/base64FromBytes";
5
7
  import { omit } from "../../utils/omit";
@@ -16,8 +18,9 @@ export async function automaticSpeechRecognition(
16
18
  args: AutomaticSpeechRecognitionArgs,
17
19
  options?: Options
18
20
  ): Promise<AutomaticSpeechRecognitionOutput> {
21
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "automatic-speech-recognition");
19
22
  const payload = await buildPayload(args);
20
- const { data: res } = await innerRequest<AutomaticSpeechRecognitionOutput>(payload, {
23
+ const { data: res } = await innerRequest<AutomaticSpeechRecognitionOutput>(payload, providerHelper, {
21
24
  ...options,
22
25
  task: "automatic-speech-recognition",
23
26
  });
@@ -25,11 +28,9 @@ export async function automaticSpeechRecognition(
25
28
  if (!isValidOutput) {
26
29
  throw new InferenceOutputError("Expected {text: string}");
27
30
  }
28
- return res;
31
+ return providerHelper.getResponse(res);
29
32
  }
30
33
 
31
- const FAL_AI_SUPPORTED_BLOB_TYPES = ["audio/mpeg", "audio/mp4", "audio/wav", "audio/x-wav"];
32
-
33
34
  async function buildPayload(args: AutomaticSpeechRecognitionArgs): Promise<RequestArgs> {
34
35
  if (args.provider === "fal-ai") {
35
36
  const blob = "data" in args && args.data instanceof Blob ? args.data : "inputs" in args ? args.inputs : undefined;
@@ -1,7 +1,6 @@
1
1
  import type { TextToSpeechInput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
3
  import type { BaseArgs, Options } from "../../types";
4
- import { omit } from "../../utils/omit";
5
4
  import { innerRequest } from "../../utils/request";
6
5
  type TextToSpeechArgs = BaseArgs & TextToSpeechInput;
7
6
 
@@ -13,34 +12,11 @@ interface OutputUrlTextToSpeechGeneration {
13
12
  * Recommended model: espnet/kan-bayashi_ljspeech_vits
14
13
  */
15
14
  export async function textToSpeech(args: TextToSpeechArgs, options?: Options): Promise<Blob> {
16
- // Replicate models expects "text" instead of "inputs"
17
- const payload =
18
- args.provider === "replicate"
19
- ? {
20
- ...omit(args, ["inputs", "parameters"]),
21
- ...args.parameters,
22
- text: args.inputs,
23
- }
24
- : args;
25
- const { data: res } = await innerRequest<Blob | OutputUrlTextToSpeechGeneration>(payload, {
15
+ const provider = args.provider ?? "hf-inference";
16
+ const providerHelper = getProviderHelper(provider, "text-to-speech");
17
+ const { data: res } = await innerRequest<Blob | OutputUrlTextToSpeechGeneration>(args, providerHelper, {
26
18
  ...options,
27
19
  task: "text-to-speech",
28
20
  });
29
- if (res instanceof Blob) {
30
- return res;
31
- }
32
- if (res && typeof res === "object") {
33
- if ("output" in res) {
34
- if (typeof res.output === "string") {
35
- const urlResponse = await fetch(res.output);
36
- const blob = await urlResponse.blob();
37
- return blob;
38
- } else if (Array.isArray(res.output)) {
39
- const urlResponse = await fetch(res.output[0]);
40
- const blob = await urlResponse.blob();
41
- return blob;
42
- }
43
- }
44
- }
45
- throw new InferenceOutputError("Expected Blob or object with output");
21
+ return providerHelper.getResponse(res);
46
22
  }
@@ -1,4 +1,4 @@
1
- import type { BaseArgs, RequestArgs } from "../../types";
1
+ import type { BaseArgs, InferenceProvider, RequestArgs } from "../../types";
2
2
  import { omit } from "../../utils/omit";
3
3
 
4
4
  /**
@@ -6,6 +6,7 @@ import { omit } from "../../utils/omit";
6
6
  */
7
7
  export interface LegacyAudioInput {
8
8
  data: Blob | ArrayBuffer;
9
+ provider?: InferenceProvider;
9
10
  }
10
11
 
11
12
  export function preparePayload(args: BaseArgs & ({ inputs: Blob } | LegacyAudioInput)): RequestArgs {
@@ -1,3 +1,4 @@
1
+ import { getProviderHelper } from "../../lib/getProviderHelper";
1
2
  import type { InferenceTask, Options, RequestArgs } from "../../types";
2
3
  import { innerRequest } from "../../utils/request";
3
4
 
@@ -10,13 +11,12 @@ export async function request<T>(
10
11
  options?: Options & {
11
12
  /** In most cases (unless we pass a endpointUrl) we know the task */
12
13
  task?: InferenceTask;
13
- /** Is chat completion compatible */
14
- chatCompletion?: boolean;
15
14
  }
16
15
  ): Promise<T> {
17
16
  console.warn(
18
17
  "The request method is deprecated and will be removed in a future version of huggingface.js. Use specific task functions instead."
19
18
  );
20
- const result = await innerRequest<T>(args, options);
19
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", options?.task);
20
+ const result = await innerRequest<T>(args, providerHelper, options);
21
21
  return result.data;
22
22
  }
@@ -1,5 +1,7 @@
1
+ import { getProviderHelper } from "../../lib/getProviderHelper";
1
2
  import type { InferenceTask, Options, RequestArgs } from "../../types";
2
3
  import { innerStreamingRequest } from "../../utils/request";
4
+
3
5
  /**
4
6
  * Primitive to make custom inference calls that expect server-sent events, and returns the response through a generator
5
7
  * @deprecated Use specific task functions instead. This function will be removed in a future version.
@@ -9,12 +11,11 @@ export async function* streamingRequest<T>(
9
11
  options?: Options & {
10
12
  /** In most cases (unless we pass a endpointUrl) we know the task */
11
13
  task?: InferenceTask;
12
- /** Is chat completion compatible */
13
- chatCompletion?: boolean;
14
14
  }
15
15
  ): AsyncGenerator<T> {
16
16
  console.warn(
17
17
  "The streamingRequest method is deprecated and will be removed in a future version of huggingface.js. Use specific task functions instead."
18
18
  );
19
- yield* innerStreamingRequest(args, options);
19
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", options?.task);
20
+ yield* innerStreamingRequest(args, providerHelper, options);
20
21
  }
@@ -1,5 +1,5 @@
1
1
  import type { ImageClassificationInput, ImageClassificationOutput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
3
  import type { BaseArgs, Options } from "../../types";
4
4
  import { innerRequest } from "../../utils/request";
5
5
  import { preparePayload, type LegacyImageInput } from "./utils";
@@ -14,15 +14,11 @@ export async function imageClassification(
14
14
  args: ImageClassificationArgs,
15
15
  options?: Options
16
16
  ): Promise<ImageClassificationOutput> {
17
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "image-classification");
17
18
  const payload = preparePayload(args);
18
- const { data: res } = await innerRequest<ImageClassificationOutput>(payload, {
19
+ const { data: res } = await innerRequest<ImageClassificationOutput>(payload, providerHelper, {
19
20
  ...options,
20
21
  task: "image-classification",
21
22
  });
22
- const isValidOutput =
23
- Array.isArray(res) && res.every((x) => typeof x.label === "string" && typeof x.score === "number");
24
- if (!isValidOutput) {
25
- throw new InferenceOutputError("Expected Array<{label: string, score: number}>");
26
- }
27
- return res;
23
+ return providerHelper.getResponse(res);
28
24
  }
@@ -1,5 +1,5 @@
1
1
  import type { ImageSegmentationInput, ImageSegmentationOutput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
3
  import type { BaseArgs, Options } from "../../types";
4
4
  import { innerRequest } from "../../utils/request";
5
5
  import { preparePayload, type LegacyImageInput } from "./utils";
@@ -14,16 +14,11 @@ export async function imageSegmentation(
14
14
  args: ImageSegmentationArgs,
15
15
  options?: Options
16
16
  ): Promise<ImageSegmentationOutput> {
17
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "image-segmentation");
17
18
  const payload = preparePayload(args);
18
- const { data: res } = await innerRequest<ImageSegmentationOutput>(payload, {
19
+ const { data: res } = await innerRequest<ImageSegmentationOutput>(payload, providerHelper, {
19
20
  ...options,
20
21
  task: "image-segmentation",
21
22
  });
22
- const isValidOutput =
23
- Array.isArray(res) &&
24
- res.every((x) => typeof x.label === "string" && typeof x.mask === "string" && typeof x.score === "number");
25
- if (!isValidOutput) {
26
- throw new InferenceOutputError("Expected Array<{label: string, mask: string, score: number}>");
27
- }
28
- return res;
23
+ return providerHelper.getResponse(res);
29
24
  }
@@ -1,5 +1,5 @@
1
1
  import type { ImageToImageInput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
3
  import type { BaseArgs, Options, RequestArgs } from "../../types";
4
4
  import { base64FromBytes } from "../../utils/base64FromBytes";
5
5
  import { innerRequest } from "../../utils/request";
@@ -11,6 +11,7 @@ export type ImageToImageArgs = BaseArgs & ImageToImageInput;
11
11
  * Recommended model: lllyasviel/sd-controlnet-depth
12
12
  */
13
13
  export async function imageToImage(args: ImageToImageArgs, options?: Options): Promise<Blob> {
14
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "image-to-image");
14
15
  let reqArgs: RequestArgs;
15
16
  if (!args.parameters) {
16
17
  reqArgs = {
@@ -26,13 +27,9 @@ export async function imageToImage(args: ImageToImageArgs, options?: Options): P
26
27
  ),
27
28
  };
28
29
  }
29
- const { data: res } = await innerRequest<Blob>(reqArgs, {
30
+ const { data: res } = await innerRequest<Blob>(reqArgs, providerHelper, {
30
31
  ...options,
31
32
  task: "image-to-image",
32
33
  });
33
- const isValidOutput = res && res instanceof Blob;
34
- if (!isValidOutput) {
35
- throw new InferenceOutputError("Expected Blob");
36
- }
37
- return res;
34
+ return providerHelper.getResponse(res);
38
35
  }
@@ -1,5 +1,5 @@
1
1
  import type { ImageToTextInput, ImageToTextOutput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
3
  import type { BaseArgs, Options } from "../../types";
4
4
  import { innerRequest } from "../../utils/request";
5
5
  import type { LegacyImageInput } from "./utils";
@@ -10,15 +10,12 @@ export type ImageToTextArgs = BaseArgs & (ImageToTextInput | LegacyImageInput);
10
10
  * This task reads some image input and outputs the text caption.
11
11
  */
12
12
  export async function imageToText(args: ImageToTextArgs, options?: Options): Promise<ImageToTextOutput> {
13
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "image-to-text");
13
14
  const payload = preparePayload(args);
14
- const { data: res } = await innerRequest<[ImageToTextOutput]>(payload, {
15
+ const { data: res } = await innerRequest<[ImageToTextOutput]>(payload, providerHelper, {
15
16
  ...options,
16
17
  task: "image-to-text",
17
18
  });
18
19
 
19
- if (typeof res?.[0]?.generated_text !== "string") {
20
- throw new InferenceOutputError("Expected {generated_text: string}");
21
- }
22
-
23
- return res?.[0];
20
+ return providerHelper.getResponse(res[0]);
24
21
  }
@@ -1,5 +1,5 @@
1
1
  import type { ObjectDetectionInput, ObjectDetectionOutput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
3
  import type { BaseArgs, Options } from "../../types";
4
4
  import { innerRequest } from "../../utils/request";
5
5
  import { preparePayload, type LegacyImageInput } from "./utils";
@@ -11,26 +11,11 @@ export type ObjectDetectionArgs = BaseArgs & (ObjectDetectionInput | LegacyImage
11
11
  * Recommended model: facebook/detr-resnet-50
12
12
  */
13
13
  export async function objectDetection(args: ObjectDetectionArgs, options?: Options): Promise<ObjectDetectionOutput> {
14
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "object-detection");
14
15
  const payload = preparePayload(args);
15
- const { data: res } = await innerRequest<ObjectDetectionOutput>(payload, {
16
+ const { data: res } = await innerRequest<ObjectDetectionOutput>(payload, providerHelper, {
16
17
  ...options,
17
18
  task: "object-detection",
18
19
  });
19
- const isValidOutput =
20
- Array.isArray(res) &&
21
- res.every(
22
- (x) =>
23
- typeof x.label === "string" &&
24
- typeof x.score === "number" &&
25
- typeof x.box.xmin === "number" &&
26
- typeof x.box.ymin === "number" &&
27
- typeof x.box.xmax === "number" &&
28
- typeof x.box.ymax === "number"
29
- );
30
- if (!isValidOutput) {
31
- throw new InferenceOutputError(
32
- "Expected Array<{label:string; score:number; box:{xmin:number; ymin:number; xmax:number; ymax:number}}>"
33
- );
34
- }
35
- return res;
20
+ return providerHelper.getResponse(res);
36
21
  }