@huggingface/tasks 0.15.8 → 0.16.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 (75) hide show
  1. package/dist/commonjs/inference-providers.d.ts +5 -4
  2. package/dist/commonjs/inference-providers.d.ts.map +1 -1
  3. package/dist/commonjs/inference-providers.js +15 -6
  4. package/dist/commonjs/library-to-tasks.d.ts +1 -1
  5. package/dist/commonjs/library-to-tasks.js +1 -1
  6. package/dist/commonjs/model-libraries-snippets.d.ts +1 -0
  7. package/dist/commonjs/model-libraries-snippets.d.ts.map +1 -1
  8. package/dist/commonjs/model-libraries-snippets.js +16 -2
  9. package/dist/commonjs/model-libraries.d.ts +8 -1
  10. package/dist/commonjs/model-libraries.d.ts.map +1 -1
  11. package/dist/commonjs/model-libraries.js +7 -0
  12. package/dist/commonjs/pipelines.d.ts +7 -1
  13. package/dist/commonjs/pipelines.d.ts.map +1 -1
  14. package/dist/commonjs/pipelines.js +6 -0
  15. package/dist/commonjs/snippets/common.js +2 -2
  16. package/dist/commonjs/snippets/curl.d.ts +7 -7
  17. package/dist/commonjs/snippets/curl.d.ts.map +1 -1
  18. package/dist/commonjs/snippets/curl.js +10 -9
  19. package/dist/commonjs/snippets/js.d.ts +9 -9
  20. package/dist/commonjs/snippets/js.d.ts.map +1 -1
  21. package/dist/commonjs/snippets/js.js +16 -14
  22. package/dist/commonjs/snippets/python.d.ts +6 -6
  23. package/dist/commonjs/snippets/python.d.ts.map +1 -1
  24. package/dist/commonjs/snippets/python.js +59 -16
  25. package/dist/commonjs/tasks/index.d.ts.map +1 -1
  26. package/dist/commonjs/tasks/index.js +2 -0
  27. package/dist/commonjs/tasks/text-generation/data.d.ts.map +1 -1
  28. package/dist/commonjs/tasks/text-generation/data.js +1 -3
  29. package/dist/esm/inference-providers.d.ts +5 -4
  30. package/dist/esm/inference-providers.d.ts.map +1 -1
  31. package/dist/esm/inference-providers.js +14 -5
  32. package/dist/esm/library-to-tasks.d.ts +1 -1
  33. package/dist/esm/library-to-tasks.js +1 -1
  34. package/dist/esm/model-libraries-snippets.d.ts +1 -0
  35. package/dist/esm/model-libraries-snippets.d.ts.map +1 -1
  36. package/dist/esm/model-libraries-snippets.js +13 -0
  37. package/dist/esm/model-libraries.d.ts +8 -1
  38. package/dist/esm/model-libraries.d.ts.map +1 -1
  39. package/dist/esm/model-libraries.js +7 -0
  40. package/dist/esm/pipelines.d.ts +7 -1
  41. package/dist/esm/pipelines.d.ts.map +1 -1
  42. package/dist/esm/pipelines.js +6 -0
  43. package/dist/esm/snippets/common.js +2 -2
  44. package/dist/esm/snippets/curl.d.ts +7 -7
  45. package/dist/esm/snippets/curl.d.ts.map +1 -1
  46. package/dist/esm/snippets/curl.js +10 -9
  47. package/dist/esm/snippets/js.d.ts +9 -9
  48. package/dist/esm/snippets/js.d.ts.map +1 -1
  49. package/dist/esm/snippets/js.js +16 -14
  50. package/dist/esm/snippets/python.d.ts +6 -6
  51. package/dist/esm/snippets/python.d.ts.map +1 -1
  52. package/dist/esm/snippets/python.js +60 -17
  53. package/dist/esm/tasks/index.d.ts.map +1 -1
  54. package/dist/esm/tasks/index.js +2 -0
  55. package/dist/esm/tasks/text-generation/data.d.ts.map +1 -1
  56. package/dist/esm/tasks/text-generation/data.js +1 -3
  57. package/package.json +1 -1
  58. package/src/inference-providers.ts +16 -7
  59. package/src/library-to-tasks.ts +1 -1
  60. package/src/model-libraries-snippets.ts +14 -0
  61. package/src/model-libraries.ts +7 -0
  62. package/src/pipelines.ts +6 -0
  63. package/src/snippets/common.ts +2 -2
  64. package/src/snippets/curl.ts +18 -14
  65. package/src/snippets/js.ts +40 -35
  66. package/src/snippets/python.ts +85 -32
  67. package/src/tasks/audio-classification/about.md +1 -1
  68. package/src/tasks/audio-to-audio/about.md +1 -1
  69. package/src/tasks/automatic-speech-recognition/about.md +1 -1
  70. package/src/tasks/feature-extraction/spec/input.json +11 -4
  71. package/src/tasks/image-text-to-text/about.md +1 -1
  72. package/src/tasks/index.ts +2 -0
  73. package/src/tasks/sentence-similarity/about.md +2 -2
  74. package/src/tasks/text-generation/data.ts +1 -3
  75. package/src/tasks/text-to-speech/about.md +1 -1
@@ -31,7 +31,7 @@ const output = await client.${HFJS_METHODS[model.pipeline_tag]}({
31
31
  provider: "${provider}",
32
32
  });
33
33
 
34
- console.log(output)
34
+ console.log(output);
35
35
  `,
36
36
  },
37
37
  ]
@@ -41,7 +41,7 @@ console.log(output)
41
41
  content: `\
42
42
  async function query(data) {
43
43
  const response = await fetch(
44
- "https://api-inference.huggingface.co/models/${model.id}",
44
+ "https://router.huggingface.co/hf-inference/models/${model.id}",
45
45
  {
46
46
  headers: {
47
47
  Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
@@ -61,7 +61,7 @@ query({"inputs": ${getModelInputSnippet(model)}}).then((response) => {
61
61
  },
62
62
  ];
63
63
  };
64
- export const snippetTextGeneration = (model, accessToken, provider, opts) => {
64
+ export const snippetTextGeneration = (model, accessToken, provider, providerModelId, opts) => {
65
65
  if (model.tags.includes("conversational")) {
66
66
  // Conversational model detected, so we display a code snippet that features the Messages API
67
67
  const streaming = opts?.streaming ?? true;
@@ -114,9 +114,9 @@ const client = new OpenAI({
114
114
  let out = "";
115
115
 
116
116
  const stream = await client.chat.completions.create({
117
- model: "${model.id}",
117
+ model: "${providerModelId ?? model.id}",
118
118
  messages: ${messagesStr},
119
- ${configStr},
119
+ ${configStr}
120
120
  stream: true,
121
121
  });
122
122
 
@@ -145,7 +145,8 @@ const chatCompletion = await client.chatCompletion({
145
145
  ${configStr}
146
146
  });
147
147
 
148
- console.log(chatCompletion.choices[0].message);`,
148
+ console.log(chatCompletion.choices[0].message);
149
+ `,
149
150
  },
150
151
  {
151
152
  client: "openai",
@@ -157,12 +158,13 @@ const client = new OpenAI({
157
158
  });
158
159
 
159
160
  const chatCompletion = await client.chat.completions.create({
160
- model: "${model.id}",
161
+ model: "${providerModelId ?? model.id}",
161
162
  messages: ${messagesStr},
162
163
  ${configStr}
163
164
  });
164
165
 
165
- console.log(chatCompletion.choices[0].message);`,
166
+ console.log(chatCompletion.choices[0].message);
167
+ `,
166
168
  },
167
169
  ];
168
170
  }
@@ -177,7 +179,7 @@ export const snippetZeroShotClassification = (model, accessToken) => {
177
179
  client: "fetch",
178
180
  content: `async function query(data) {
179
181
  const response = await fetch(
180
- "https://api-inference.huggingface.co/models/${model.id}",
182
+ "https://router.huggingface.co/hf-inference/models/${model.id}",
181
183
  {
182
184
  headers: {
183
185
  Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
@@ -221,7 +223,7 @@ const image = await client.textToImage({
221
223
  client: "fetch",
222
224
  content: `async function query(data) {
223
225
  const response = await fetch(
224
- "https://api-inference.huggingface.co/models/${model.id}",
226
+ "https://router.huggingface.co/hf-inference/models/${model.id}",
225
227
  {
226
228
  headers: {
227
229
  Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
@@ -248,7 +250,7 @@ export const snippetTextToAudio = (model, accessToken, provider) => {
248
250
  }
249
251
  const commonSnippet = `async function query(data) {
250
252
  const response = await fetch(
251
- "https://api-inference.huggingface.co/models/${model.id}",
253
+ "https://router.huggingface.co/hf-inference/models/${model.id}",
252
254
  {
253
255
  headers: {
254
256
  Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
@@ -323,7 +325,7 @@ export const snippetFile = (model, accessToken, provider) => {
323
325
  content: `async function query(filename) {
324
326
  const data = fs.readFileSync(filename);
325
327
  const response = await fetch(
326
- "https://api-inference.huggingface.co/models/${model.id}",
328
+ "https://router.huggingface.co/hf-inference/models/${model.id}",
327
329
  {
328
330
  headers: {
329
331
  Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
@@ -369,8 +371,8 @@ export const jsSnippets = {
369
371
  "object-detection": snippetFile,
370
372
  "image-segmentation": snippetFile,
371
373
  };
372
- export function getJsInferenceSnippet(model, accessToken, provider, opts) {
374
+ export function getJsInferenceSnippet(model, accessToken, provider, providerModelId, opts) {
373
375
  return model.pipeline_tag && model.pipeline_tag in jsSnippets
374
- ? jsSnippets[model.pipeline_tag]?.(model, accessToken, provider, opts) ?? []
376
+ ? jsSnippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId, opts) ?? []
375
377
  : [];
376
378
  }
@@ -1,8 +1,8 @@
1
- import { type InferenceProvider } from "../inference-providers.js";
1
+ import { type SnippetInferenceProvider } from "../inference-providers.js";
2
2
  import type { PipelineType } from "../pipelines.js";
3
3
  import type { ChatCompletionInputMessage, GenerationParameters } from "../tasks/index.js";
4
4
  import type { InferenceSnippet, ModelDataMinimal } from "./types.js";
5
- export declare const snippetConversational: (model: ModelDataMinimal, accessToken: string, provider: InferenceProvider, opts?: {
5
+ export declare const snippetConversational: (model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider, providerModelId?: string, opts?: {
6
6
  streaming?: boolean;
7
7
  messages?: ChatCompletionInputMessage[];
8
8
  temperature?: GenerationParameters["temperature"];
@@ -11,12 +11,12 @@ export declare const snippetConversational: (model: ModelDataMinimal, accessToke
11
11
  }) => InferenceSnippet[];
12
12
  export declare const snippetZeroShotClassification: (model: ModelDataMinimal) => InferenceSnippet[];
13
13
  export declare const snippetZeroShotImageClassification: (model: ModelDataMinimal) => InferenceSnippet[];
14
- export declare const snippetBasic: (model: ModelDataMinimal) => InferenceSnippet[];
14
+ export declare const snippetBasic: (model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider) => InferenceSnippet[];
15
15
  export declare const snippetFile: (model: ModelDataMinimal) => InferenceSnippet[];
16
- export declare const snippetTextToImage: (model: ModelDataMinimal, accessToken: string, provider: InferenceProvider) => InferenceSnippet[];
16
+ export declare const snippetTextToImage: (model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider, providerModelId?: string) => InferenceSnippet[];
17
17
  export declare const snippetTabular: (model: ModelDataMinimal) => InferenceSnippet[];
18
18
  export declare const snippetTextToAudio: (model: ModelDataMinimal) => InferenceSnippet[];
19
19
  export declare const snippetDocumentQuestionAnswering: (model: ModelDataMinimal) => InferenceSnippet[];
20
- export declare const pythonSnippets: Partial<Record<PipelineType, (model: ModelDataMinimal, accessToken: string, provider: InferenceProvider, opts?: Record<string, unknown>) => InferenceSnippet[]>>;
21
- export declare function getPythonInferenceSnippet(model: ModelDataMinimal, accessToken: string, provider: InferenceProvider, opts?: Record<string, unknown>): InferenceSnippet[];
20
+ export declare const pythonSnippets: Partial<Record<PipelineType, (model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider, providerModelId?: string, opts?: Record<string, unknown>) => InferenceSnippet[]>>;
21
+ export declare function getPythonInferenceSnippet(model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider, providerModelId?: string, opts?: Record<string, unknown>): InferenceSnippet[];
22
22
  //# sourceMappingURL=python.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"python.d.ts","sourceRoot":"","sources":["../../../src/snippets/python.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkD,KAAK,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AACnH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,KAAK,EAAE,0BAA0B,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAG1F,OAAO,KAAK,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAWrE,eAAO,MAAM,qBAAqB,UAC1B,gBAAgB,eACV,MAAM,YACT,iBAAiB,SACpB;IACN,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,QAAQ,CAAC,EAAE,0BAA0B,EAAE,CAAC;IACxC,WAAW,CAAC,EAAE,oBAAoB,CAAC,aAAa,CAAC,CAAC;IAClD,UAAU,CAAC,EAAE,oBAAoB,CAAC,YAAY,CAAC,CAAC;IAChD,KAAK,CAAC,EAAE,oBAAoB,CAAC,OAAO,CAAC,CAAC;CACtC,KACC,gBAAgB,EAiGlB,CAAC;AAEF,eAAO,MAAM,6BAA6B,UAAW,gB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AI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAC1B,gBAAgB,EAAE,CACvB,CA8BD,CAAC;AAEF,wBAAgB,yBAAyB,CACxC,KAAK,EAAE,gBAAgB,EACvB,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,wBAAwB,EAClC,eAAe,CAAC,EAAE,MAAM,EACxB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC5B,gBAAgB,EAAE,CA0BpB"}
@@ -1,6 +1,35 @@
1
- import { HF_HUB_INFERENCE_PROXY_TEMPLATE, openAIbaseUrl } from "../inference-providers.js";
1
+ import { openAIbaseUrl, } from "../inference-providers.js";
2
2
  import { stringifyGenerationConfig, stringifyMessages } from "./common.js";
3
3
  import { getModelInputSnippet } from "./inputs.js";
4
+ const HFH_INFERENCE_CLIENT_METHODS = {
5
+ "audio-classification": "audio_classification",
6
+ "audio-to-audio": "audio_to_audio",
7
+ "automatic-speech-recognition": "automatic_speech_recognition",
8
+ "text-to-speech": "text_to_speech",
9
+ "image-classification": "image_classification",
10
+ "image-segmentation": "image_segmentation",
11
+ "image-to-image": "image_to_image",
12
+ "image-to-text": "image_to_text",
13
+ "object-detection": "object_detection",
14
+ "text-to-image": "text_to_image",
15
+ "text-to-video": "text_to_video",
16
+ "zero-shot-image-classification": "zero_shot_image_classification",
17
+ "document-question-answering": "document_question_answering",
18
+ "visual-question-answering": "visual_question_answering",
19
+ "feature-extraction": "feature_extraction",
20
+ "fill-mask": "fill_mask",
21
+ "question-answering": "question_answering",
22
+ "sentence-similarity": "sentence_similarity",
23
+ summarization: "summarization",
24
+ "table-question-answering": "table_question_answering",
25
+ "text-classification": "text_classification",
26
+ "text-generation": "text_generation",
27
+ "token-classification": "token_classification",
28
+ translation: "translation",
29
+ "zero-shot-classification": "zero_shot_classification",
30
+ "tabular-classification": "tabular_classification",
31
+ "tabular-regression": "tabular_regression",
32
+ };
4
33
  const snippetImportInferenceClient = (accessToken, provider) => `\
5
34
  from huggingface_hub import InferenceClient
6
35
 
@@ -8,7 +37,7 @@ client = InferenceClient(
8
37
  provider="${provider}",
9
38
  api_key="${accessToken || "{API_TOKEN}"}"
10
39
  )`;
11
- export const snippetConversational = (model, accessToken, provider, opts) => {
40
+ export const snippetConversational = (model, accessToken, provider, providerModelId, opts) => {
12
41
  const streaming = opts?.streaming ?? true;
13
42
  const exampleMessages = getModelInputSnippet(model);
14
43
  const messages = opts?.messages ?? exampleMessages;
@@ -34,7 +63,7 @@ messages = ${messagesStr}
34
63
  stream = client.chat.completions.create(
35
64
  model="${model.id}",
36
65
  messages=messages,
37
- ${configStr},
66
+ ${configStr}
38
67
  stream=True
39
68
  )
40
69
 
@@ -54,9 +83,9 @@ client = OpenAI(
54
83
  messages = ${messagesStr}
55
84
 
56
85
  stream = client.chat.completions.create(
57
- model="${model.id}",
86
+ model="${providerModelId ?? model.id}",
58
87
  messages=messages,
59
- ${configStr},
88
+ ${configStr}
60
89
  stream=True
61
90
  )
62
91
 
@@ -95,7 +124,7 @@ client = OpenAI(
95
124
  messages = ${messagesStr}
96
125
 
97
126
  completion = client.chat.completions.create(
98
- model="${model.id}",
127
+ model="${providerModelId ?? model.id}",
99
128
  messages=messages,
100
129
  ${configStr}
101
130
  )
@@ -143,8 +172,26 @@ output = query({
143
172
  },
144
173
  ];
145
174
  };
146
- export const snippetBasic = (model) => {
175
+ export const snippetBasic = (model, accessToken, provider) => {
147
176
  return [
177
+ ...(model.pipeline_tag && model.pipeline_tag in HFH_INFERENCE_CLIENT_METHODS
178
+ ? [
179
+ {
180
+ client: "huggingface_hub",
181
+ content: `\
182
+ ${snippetImportInferenceClient(accessToken, provider)}
183
+
184
+ result = client.${HFH_INFERENCE_CLIENT_METHODS[model.pipeline_tag]}(
185
+ model="${model.id}",
186
+ inputs=${getModelInputSnippet(model)},
187
+ provider="${provider}",
188
+ )
189
+
190
+ print(result)
191
+ `,
192
+ },
193
+ ]
194
+ : []),
148
195
  {
149
196
  client: "requests",
150
197
  content: `\
@@ -173,7 +220,7 @@ output = query(${getModelInputSnippet(model)})`,
173
220
  },
174
221
  ];
175
222
  };
176
- export const snippetTextToImage = (model, accessToken, provider) => {
223
+ export const snippetTextToImage = (model, accessToken, provider, providerModelId) => {
177
224
  return [
178
225
  {
179
226
  client: "huggingface_hub",
@@ -194,8 +241,7 @@ image = client.text_to_image(
194
241
  import fal_client
195
242
 
196
243
  result = fal_client.subscribe(
197
- # replace with correct id from fal.ai
198
- "fal-ai/${model.id}",
244
+ "${providerModelId ?? model.id}",
199
245
  arguments={
200
246
  "prompt": ${getModelInputSnippet(model)},
201
247
  },
@@ -331,18 +377,15 @@ export const pythonSnippets = {
331
377
  "image-to-text": snippetFile,
332
378
  "zero-shot-image-classification": snippetZeroShotImageClassification,
333
379
  };
334
- export function getPythonInferenceSnippet(model, accessToken, provider, opts) {
380
+ export function getPythonInferenceSnippet(model, accessToken, provider, providerModelId, opts) {
335
381
  if (model.tags.includes("conversational")) {
336
382
  // Conversational model detected, so we display a code snippet that features the Messages API
337
- return snippetConversational(model, accessToken, provider, opts);
383
+ return snippetConversational(model, accessToken, provider, providerModelId, opts);
338
384
  }
339
385
  else {
340
386
  const snippets = model.pipeline_tag && model.pipeline_tag in pythonSnippets
341
- ? pythonSnippets[model.pipeline_tag]?.(model, accessToken, provider) ?? []
387
+ ? pythonSnippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId) ?? []
342
388
  : [];
343
- const baseUrl = provider === "hf-inference"
344
- ? `https://api-inference.huggingface.co/models/${model.id}`
345
- : HF_HUB_INFERENCE_PROXY_TEMPLATE.replace("{{PROVIDER}}", provider);
346
389
  return snippets.map((snippet) => {
347
390
  return {
348
391
  ...snippet,
@@ -350,7 +393,7 @@ export function getPythonInferenceSnippet(model, accessToken, provider, opts) {
350
393
  ? `\
351
394
  import requests
352
395
 
353
- API_URL = "${baseUrl}"
396
+ API_URL = "${openAIbaseUrl(provider)}"
354
397
  headers = {"Authorization": ${accessToken ? `"Bearer ${accessToken}"` : `f"Bearer {API_TOKEN}"`}}
355
398
 
356
399
  ${snippet.content}`
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/tasks/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AA4CpD,mBAAmB,qCAAqC,CAAC;AACzD,mBAAmB,6CAA6C,CAAC;AACjE,YAAY,EACX,mBAAmB,EACnB,0BAA0B,EAC1B,oBAAoB,EACpB,4BAA4B,EAC5B,2BAA2B,EAC3B,0BAA0B,EAC1B,gCAAgC,EAChC,+BAA+B,GAC/B,MAAM,gCAAgC,CAAC;AACxC,mBAAmB,4CAA4C,CAAC;AAChE,mBAAmB,mCAAmC,CAAC;AACvD,mBAAmB,0BAA0B,CAAC;AAC9C,YAAY,EACX,wBAAwB,EACxB,yBAAyB,EACzB,gCAAgC,EAChC,6BAA6B,GAC7B,MAAM,qCAAqC,CAAC;AAC7C,mBAAmB,+BAA+B,CAAC;AACnD,YAAY,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAC/G,mBAAmB,mCAAmC,CAAC;AACvD,mBAAmB,iCAAiC,CAAC;AACrD,mBAAmB,iCAAiC,CAAC;AACrD,mBAAmB,mCAAmC,CAAC;AACvD,mBAAmB,oCAAoC,CAAC;AACxD,mBAAmB,8BAA8B,CAAC;AAClD,mBAAmB,yCAAyC,CAAC;AAC7D,YAAY,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAC/G,YAAY,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAC/G,YAAY,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AACnH,mBAAmB,qCAAqC,CAAC;AACzD,YAAY,EAAE,gBA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,CAAC;KACb,CAAC,CAAC;IACH,IAAI,EAAE,kBAAkB,CAAC;CACxB,CAAC;AAEL,MAAM,WAAW,QAAQ;IACxB,MAAM,EAAE,aAAa,EAAE,CAAC;IACxB,OAAO,EAAE,aAAa,EAAE,CAAC;CACzB;AAED,MAAM,WAAW,QAAQ;IACxB,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,IAAI,EAAE,QAAQ,CAAC;IACf,EAAE,EAAE,YAAY,CAAC;IACjB,WAAW,CAAC,EAAE,YAAY,CAAC;IAC3B,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,eAAe,EAAE,CAAC;IAC7B,OAAO,EAAE,WAAW,EAAE,CAAC;IACvB,MAAM,EAAE,WAAW,EAAE,CAAC;IACtB,MAAM,EAAE,WAAW,EAAE,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,SAAS,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,MAAM,cAAc,GAAG,IAAI,CAAC,QAAQ,EAAE,IAAI,GAAG,OAAO,GAAG,WAAW,CAAC,CAAC"}
@@ -104,6 +104,7 @@ export const TASKS_MODEL_LIBRARIES = {
104
104
  "text-to-3d": ["diffusers"],
105
105
  "image-to-3d": ["diffusers"],
106
106
  "any-to-any": ["transformers"],
107
+ "visual-document-retrieval": ["transformers"],
107
108
  };
108
109
  /**
109
110
  * Return the whole TaskData object for a certain task.
@@ -130,6 +131,7 @@ export const TASKS_DATA = {
130
131
  "automatic-speech-recognition": getData("automatic-speech-recognition", automaticSpeechRecognition),
131
132
  "depth-estimation": getData("depth-estimation", depthEstimation),
132
133
  "document-question-answering": getData("document-question-answering", documentQuestionAnswering),
134
+ "visual-document-retrieval": getData("visual-document-retrieval", placeholder),
133
135
  "feature-extraction": getData("feature-extraction", featureExtraction),
134
136
  "fill-mask": getData("fill-mask", fillMask),
135
137
  "graph-ml": undefined,
@@ -1 +1 @@
1
- {"version":3,"file":"data.d.ts","sourceRoot":"","sources":["../../../../src/tasks/text-generation/data.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAElD,QAAA,MAAM,QAAQ,EAAE,cA2Hf,CAAC;AAEF,eAAe,QAAQ,CAAC"}
1
+ {"version":3,"file":"data.d.ts","sourceRoot":"","sources":["../../../../src/tasks/text-generation/data.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAElD,QAAA,MAAM,QAAQ,EAAE,cAyHf,CAAC;AAEF,eAAe,QAAQ,CAAC"}
@@ -56,9 +56,7 @@ const taskData = {
56
56
  },
57
57
  ],
58
58
  models: [
59
- { description: "A text-generation model trained to follow instructions.",
60
- id: "google/gemma-2-2b-it",
61
- },
59
+ { description: "A text-generation model trained to follow instructions.", id: "google/gemma-2-2b-it" },
62
60
  {
63
61
  description: "Smaller variant of one of the most powerful models.",
64
62
  id: "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@huggingface/tasks",
3
3
  "packageManager": "pnpm@8.10.5",
4
- "version": "0.15.8",
4
+ "version": "0.16.0",
5
5
  "description": "List of ML tasks for huggingface.co/tasks",
6
6
  "repository": "https://github.com/huggingface/huggingface.js.git",
7
7
  "publishConfig": {
@@ -1,16 +1,25 @@
1
- export const INFERENCE_PROVIDERS = ["hf-inference", "fal-ai", "replicate", "sambanova", "together"] as const;
1
+ /// This list is for illustration purposes only.
2
+ /// in the `tasks` sub-package, we do not need actual strong typing of the inference providers.
3
+ const INFERENCE_PROVIDERS = [
4
+ "fal-ai",
5
+ "fireworks-ai",
6
+ "hf-inference",
7
+ "hyperbolic",
8
+ "replicate",
9
+ "sambanova",
10
+ "together",
11
+ ] as const;
2
12
 
3
- export type InferenceProvider = (typeof INFERENCE_PROVIDERS)[number];
13
+ export type SnippetInferenceProvider = (typeof INFERENCE_PROVIDERS)[number] | string;
4
14
 
5
- export const HF_HUB_INFERENCE_PROXY_TEMPLATE = `https://huggingface.co/api/inference-proxy/{{PROVIDER}}`;
15
+ export const HF_HUB_INFERENCE_PROXY_TEMPLATE = `https://router.huggingface.co/{{PROVIDER}}`;
6
16
 
7
17
  /**
8
18
  * URL to set as baseUrl in the OpenAI SDK.
9
19
  *
10
20
  * TODO(Expose this from HfInference in the future?)
11
21
  */
12
- export function openAIbaseUrl(provider: InferenceProvider): string {
13
- return provider === "hf-inference"
14
- ? "https://api-inference.huggingface.co/v1/"
15
- : HF_HUB_INFERENCE_PROXY_TEMPLATE.replace("{{PROVIDER}}", provider);
22
+ export function openAIbaseUrl(provider: SnippetInferenceProvider): string {
23
+ const url = HF_HUB_INFERENCE_PROXY_TEMPLATE.replace("{{PROVIDER}}", provider);
24
+ return provider === "hf-inference" ? `${url}/v1` : url;
16
25
  }
@@ -3,7 +3,7 @@ import type { PipelineType } from "./pipelines.js";
3
3
 
4
4
  /**
5
5
  * Mapping from library name to its supported tasks.
6
- * Inference API (serverless) should be disabled for all other (library, task) pairs beyond this mapping.
6
+ * HF-Inference API (serverless) should be disabled for all other (library, task) pairs beyond this mapping.
7
7
  * This mapping is partially generated automatically by "python-api-export-tasks" action in
8
8
  * huggingface/api-inference-community repo upon merge. For transformers, the mapping is manually
9
9
  * based on api-inference (hf_types.rs).
@@ -83,6 +83,20 @@ function get_prompt_from_diffusers_model(model: ModelData): string | undefined {
83
83
  }
84
84
  }
85
85
 
86
+ export const ben2 = (model: ModelData): string[] => [
87
+ `import requests
88
+ from PIL import Image
89
+ from ben2 import AutoModel
90
+
91
+ url = "https://huggingface.co/datasets/mishig/sample_images/resolve/main/teapot.jpg"
92
+ image = Image.open(requests.get(url, stream=True).raw)
93
+
94
+ model = AutoModel.from_pretrained("${model.id}")
95
+ model.to("cuda").eval()
96
+ foreground = model.inference(image)
97
+ `,
98
+ ];
99
+
86
100
  export const bertopic = (model: ModelData): string[] => [
87
101
  `from bertopic import BERTopic
88
102
 
@@ -109,6 +109,13 @@ export const MODEL_LIBRARIES_UI_ELEMENTS = {
109
109
  countDownloads: `path_extension:"pth"`,
110
110
  snippets: snippets.audioseal,
111
111
  },
112
+ ben2: {
113
+ prettyLabel: "BEN2",
114
+ repoName: "BEN2",
115
+ repoUrl: "https://github.com/PramaLLC/BEN2",
116
+ snippets: snippets.ben2,
117
+ filter: false,
118
+ },
112
119
  bertopic: {
113
120
  prettyLabel: "BERTopic",
114
121
  repoName: "BERTopic",
package/src/pipelines.ts CHANGED
@@ -676,6 +676,12 @@ export const PIPELINE_DATA = {
676
676
  color: "red",
677
677
  hideInDatasets: true,
678
678
  },
679
+ "visual-document-retrieval": {
680
+ name: "Visual Document Retrieval",
681
+ modality: "multimodal",
682
+ color: "yellow",
683
+ hideInDatasets: true,
684
+ },
679
685
  "any-to-any": {
680
686
  name: "Any-to-Any",
681
687
  modality: "multimodal",
@@ -34,6 +34,6 @@ export function stringifyGenerationConfig(
34
34
  const quote = opts.attributeKeyQuotes ? `"` : "";
35
35
 
36
36
  return Object.entries(config)
37
- .map(([key, val]) => `${quote}${key}${quote}${opts.attributeValueConnector}${val}`)
38
- .join(`,${opts.indent}`);
37
+ .map(([key, val]) => `${quote}${key}${quote}${opts.attributeValueConnector}${val},`)
38
+ .join(`${opts.indent}`);
39
39
  }
@@ -1,4 +1,4 @@
1
- import { HF_HUB_INFERENCE_PROXY_TEMPLATE, type InferenceProvider } from "../inference-providers.js";
1
+ import { HF_HUB_INFERENCE_PROXY_TEMPLATE, type SnippetInferenceProvider } from "../inference-providers.js";
2
2
  import type { PipelineType } from "../pipelines.js";
3
3
  import type { ChatCompletionInputMessage, GenerationParameters } from "../tasks/index.js";
4
4
  import { stringifyGenerationConfig, stringifyMessages } from "./common.js";
@@ -8,7 +8,7 @@ import type { InferenceSnippet, ModelDataMinimal } from "./types.js";
8
8
  export const snippetBasic = (
9
9
  model: ModelDataMinimal,
10
10
  accessToken: string,
11
- provider: InferenceProvider
11
+ provider: SnippetInferenceProvider
12
12
  ): InferenceSnippet[] => {
13
13
  if (provider !== "hf-inference") {
14
14
  return [];
@@ -17,7 +17,7 @@ export const snippetBasic = (
17
17
  {
18
18
  client: "curl",
19
19
  content: `\
20
- curl https://api-inference.huggingface.co/models/${model.id} \\
20
+ curl https://router.huggingface.co/hf-inference/models/${model.id} \\
21
21
  -X POST \\
22
22
  -d '{"inputs": ${getModelInputSnippet(model, true)}}' \\
23
23
  -H 'Content-Type: application/json' \\
@@ -29,7 +29,8 @@ curl https://api-inference.huggingface.co/models/${model.id} \\
29
29
  export const snippetTextGeneration = (
30
30
  model: ModelDataMinimal,
31
31
  accessToken: string,
32
- provider: InferenceProvider,
32
+ provider: SnippetInferenceProvider,
33
+ providerModelId?: string,
33
34
  opts?: {
34
35
  streaming?: boolean;
35
36
  messages?: ChatCompletionInputMessage[];
@@ -41,8 +42,9 @@ export const snippetTextGeneration = (
41
42
  if (model.tags.includes("conversational")) {
42
43
  const baseUrl =
43
44
  provider === "hf-inference"
44
- ? `https://api-inference.huggingface.co/models/${model.id}/v1/chat/completions`
45
+ ? `https://router.huggingface.co/hf-inference/models/${model.id}/v1/chat/completions`
45
46
  : HF_HUB_INFERENCE_PROXY_TEMPLATE.replace("{{PROVIDER}}", provider) + "/v1/chat/completions";
47
+ const modelId = providerModelId ?? model.id;
46
48
 
47
49
  // Conversational model detected, so we display a code snippet that features the Messages API
48
50
  const streaming = opts?.streaming ?? true;
@@ -61,7 +63,7 @@ export const snippetTextGeneration = (
61
63
  -H 'Authorization: Bearer ${accessToken || `{API_TOKEN}`}' \\
62
64
  -H 'Content-Type: application/json' \\
63
65
  --data '{
64
- "model": "${model.id}",
66
+ "model": "${modelId}",
65
67
  "messages": ${stringifyMessages(messages, {
66
68
  indent: "\t",
67
69
  attributeKeyQuotes: true,
@@ -71,7 +73,7 @@ export const snippetTextGeneration = (
71
73
  indent: "\n ",
72
74
  attributeKeyQuotes: true,
73
75
  attributeValueConnector: ": ",
74
- })},
76
+ })}
75
77
  "stream": ${!!streaming}
76
78
  }'`,
77
79
  },
@@ -84,7 +86,7 @@ export const snippetTextGeneration = (
84
86
  export const snippetZeroShotClassification = (
85
87
  model: ModelDataMinimal,
86
88
  accessToken: string,
87
- provider: InferenceProvider
89
+ provider: SnippetInferenceProvider
88
90
  ): InferenceSnippet[] => {
89
91
  if (provider !== "hf-inference") {
90
92
  return [];
@@ -92,7 +94,7 @@ export const snippetZeroShotClassification = (
92
94
  return [
93
95
  {
94
96
  client: "curl",
95
- content: `curl https://api-inference.huggingface.co/models/${model.id} \\
97
+ content: `curl https://router.huggingface.co/hf-inference/models/${model.id} \\
96
98
  -X POST \\
97
99
  -d '{"inputs": ${getModelInputSnippet(model, true)}, "parameters": {"candidate_labels": ["refund", "legal", "faq"]}}' \\
98
100
  -H 'Content-Type: application/json' \\
@@ -104,7 +106,7 @@ export const snippetZeroShotClassification = (
104
106
  export const snippetFile = (
105
107
  model: ModelDataMinimal,
106
108
  accessToken: string,
107
- provider: InferenceProvider
109
+ provider: SnippetInferenceProvider
108
110
  ): InferenceSnippet[] => {
109
111
  if (provider !== "hf-inference") {
110
112
  return [];
@@ -112,7 +114,7 @@ export const snippetFile = (
112
114
  return [
113
115
  {
114
116
  client: "curl",
115
- content: `curl https://api-inference.huggingface.co/models/${model.id} \\
117
+ content: `curl https://router.huggingface.co/hf-inference/models/${model.id} \\
116
118
  -X POST \\
117
119
  --data-binary '@${getModelInputSnippet(model, true, true)}' \\
118
120
  -H 'Authorization: Bearer ${accessToken || `{API_TOKEN}`}'`,
@@ -126,7 +128,8 @@ export const curlSnippets: Partial<
126
128
  (
127
129
  model: ModelDataMinimal,
128
130
  accessToken: string,
129
- provider: InferenceProvider,
131
+ provider: SnippetInferenceProvider,
132
+ providerModelId?: string,
130
133
  opts?: Record<string, unknown>
131
134
  ) => InferenceSnippet[]
132
135
  >
@@ -160,10 +163,11 @@ export const curlSnippets: Partial<
160
163
  export function getCurlInferenceSnippet(
161
164
  model: ModelDataMinimal,
162
165
  accessToken: string,
163
- provider: InferenceProvider,
166
+ provider: SnippetInferenceProvider,
167
+ providerModelId?: string,
164
168
  opts?: Record<string, unknown>
165
169
  ): InferenceSnippet[] {
166
170
  return model.pipeline_tag && model.pipeline_tag in curlSnippets
167
- ? curlSnippets[model.pipeline_tag]?.(model, accessToken, provider, opts) ?? []
171
+ ? curlSnippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId, opts) ?? []
168
172
  : [];
169
173
  }