@huggingface/inference 3.5.1 → 3.6.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 (95) hide show
  1. package/README.md +11 -11
  2. package/dist/browser/index.cjs +1652 -0
  3. package/dist/browser/index.js +1652 -0
  4. package/dist/index.cjs +295 -930
  5. package/dist/index.js +284 -940
  6. package/dist/src/{HfInference.d.ts → InferenceClient.d.ts} +12 -7
  7. package/dist/src/InferenceClient.d.ts.map +1 -0
  8. package/dist/src/index.d.ts +1 -1
  9. package/dist/src/index.d.ts.map +1 -1
  10. package/dist/src/lib/makeRequestOptions.d.ts +16 -1
  11. package/dist/src/lib/makeRequestOptions.d.ts.map +1 -1
  12. package/dist/src/providers/cohere.d.ts.map +1 -1
  13. package/dist/src/providers/novita.d.ts.map +1 -1
  14. package/dist/src/snippets/getInferenceSnippets.d.ts +4 -0
  15. package/dist/src/snippets/getInferenceSnippets.d.ts.map +1 -0
  16. package/dist/src/snippets/index.d.ts +1 -4
  17. package/dist/src/snippets/index.d.ts.map +1 -1
  18. package/dist/src/tasks/cv/textToVideo.d.ts.map +1 -1
  19. package/dist/test/InferenceClient.spec.d.ts +2 -0
  20. package/dist/test/InferenceClient.spec.d.ts.map +1 -0
  21. package/package.json +17 -8
  22. package/src/{HfInference.ts → InferenceClient.ts} +12 -7
  23. package/src/index.ts +2 -2
  24. package/src/lib/makeRequestOptions.ts +37 -10
  25. package/src/providers/black-forest-labs.ts +2 -2
  26. package/src/providers/cohere.ts +0 -1
  27. package/src/providers/fireworks-ai.ts +4 -4
  28. package/src/providers/hf-inference.ts +1 -1
  29. package/src/providers/nebius.ts +3 -3
  30. package/src/providers/novita.ts +7 -6
  31. package/src/providers/sambanova.ts +1 -1
  32. package/src/providers/together.ts +3 -3
  33. package/src/snippets/getInferenceSnippets.ts +398 -0
  34. package/src/snippets/index.ts +1 -5
  35. package/src/snippets/templates/js/fetch/basic.jinja +19 -0
  36. package/src/snippets/templates/js/fetch/basicAudio.jinja +19 -0
  37. package/src/snippets/templates/js/fetch/basicImage.jinja +19 -0
  38. package/src/snippets/templates/js/fetch/textToAudio.jinja +41 -0
  39. package/src/snippets/templates/js/fetch/textToImage.jinja +19 -0
  40. package/src/snippets/templates/js/fetch/zeroShotClassification.jinja +22 -0
  41. package/src/snippets/templates/js/huggingface.js/basic.jinja +11 -0
  42. package/src/snippets/templates/js/huggingface.js/basicAudio.jinja +13 -0
  43. package/src/snippets/templates/js/huggingface.js/basicImage.jinja +13 -0
  44. package/src/snippets/templates/js/huggingface.js/conversational.jinja +11 -0
  45. package/src/snippets/templates/js/huggingface.js/conversationalStream.jinja +19 -0
  46. package/src/snippets/templates/js/huggingface.js/textToImage.jinja +11 -0
  47. package/src/snippets/templates/js/huggingface.js/textToVideo.jinja +10 -0
  48. package/src/snippets/templates/js/openai/conversational.jinja +13 -0
  49. package/src/snippets/templates/js/openai/conversationalStream.jinja +22 -0
  50. package/src/snippets/templates/python/fal_client/textToImage.jinja +11 -0
  51. package/src/snippets/templates/python/huggingface_hub/basic.jinja +4 -0
  52. package/src/snippets/templates/python/huggingface_hub/basicAudio.jinja +1 -0
  53. package/src/snippets/templates/python/huggingface_hub/basicImage.jinja +1 -0
  54. package/src/snippets/templates/python/huggingface_hub/conversational.jinja +6 -0
  55. package/src/snippets/templates/python/huggingface_hub/conversationalStream.jinja +8 -0
  56. package/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.jinja +5 -0
  57. package/src/snippets/templates/python/huggingface_hub/imageToImage.jinja +6 -0
  58. package/src/snippets/templates/python/huggingface_hub/importInferenceClient.jinja +6 -0
  59. package/src/snippets/templates/python/huggingface_hub/textToImage.jinja +5 -0
  60. package/src/snippets/templates/python/huggingface_hub/textToVideo.jinja +4 -0
  61. package/src/snippets/templates/python/openai/conversational.jinja +13 -0
  62. package/src/snippets/templates/python/openai/conversationalStream.jinja +15 -0
  63. package/src/snippets/templates/python/requests/basic.jinja +7 -0
  64. package/src/snippets/templates/python/requests/basicAudio.jinja +7 -0
  65. package/src/snippets/templates/python/requests/basicImage.jinja +7 -0
  66. package/src/snippets/templates/python/requests/conversational.jinja +9 -0
  67. package/src/snippets/templates/python/requests/conversationalStream.jinja +16 -0
  68. package/src/snippets/templates/python/requests/documentQuestionAnswering.jinja +13 -0
  69. package/src/snippets/templates/python/requests/imageToImage.jinja +15 -0
  70. package/src/snippets/templates/python/requests/importRequests.jinja +10 -0
  71. package/src/snippets/templates/python/requests/tabular.jinja +9 -0
  72. package/src/snippets/templates/python/requests/textToAudio.jinja +23 -0
  73. package/src/snippets/templates/python/requests/textToImage.jinja +14 -0
  74. package/src/snippets/templates/python/requests/zeroShotClassification.jinja +8 -0
  75. package/src/snippets/templates/python/requests/zeroShotImageClassification.jinja +14 -0
  76. package/src/snippets/templates/sh/curl/basic.jinja +7 -0
  77. package/src/snippets/templates/sh/curl/basicAudio.jinja +5 -0
  78. package/src/snippets/templates/sh/curl/basicImage.jinja +5 -0
  79. package/src/snippets/templates/sh/curl/conversational.jinja +7 -0
  80. package/src/snippets/templates/sh/curl/conversationalStream.jinja +7 -0
  81. package/src/snippets/templates/sh/curl/zeroShotClassification.jinja +5 -0
  82. package/src/tasks/cv/textToVideo.ts +25 -5
  83. package/src/vendor/fetch-event-source/LICENSE +21 -0
  84. package/dist/src/HfInference.d.ts.map +0 -1
  85. package/dist/src/snippets/curl.d.ts +0 -17
  86. package/dist/src/snippets/curl.d.ts.map +0 -1
  87. package/dist/src/snippets/js.d.ts +0 -21
  88. package/dist/src/snippets/js.d.ts.map +0 -1
  89. package/dist/src/snippets/python.d.ts +0 -23
  90. package/dist/src/snippets/python.d.ts.map +0 -1
  91. package/dist/test/HfInference.spec.d.ts +0 -2
  92. package/dist/test/HfInference.spec.d.ts.map +0 -1
  93. package/src/snippets/curl.ts +0 -177
  94. package/src/snippets/js.ts +0 -475
  95. package/src/snippets/python.ts +0 -487
package/dist/index.cjs CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name2 in all)
@@ -15,14 +17,23 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/index.ts
21
31
  var src_exports = {};
22
32
  __export(src_exports, {
23
33
  HfInference: () => HfInference,
24
- HfInferenceEndpoint: () => HfInferenceEndpoint,
25
34
  INFERENCE_PROVIDERS: () => INFERENCE_PROVIDERS,
35
+ InferenceClient: () => InferenceClient,
36
+ InferenceClientEndpoint: () => InferenceClientEndpoint,
26
37
  InferenceOutputError: () => InferenceOutputError,
27
38
  audioClassification: () => audioClassification,
28
39
  audioToAudio: () => audioToAudio,
@@ -102,7 +113,7 @@ var HF_HUB_URL = "https://huggingface.co";
102
113
  var HF_ROUTER_URL = "https://router.huggingface.co";
103
114
 
104
115
  // src/providers/black-forest-labs.ts
105
- var BLACK_FOREST_LABS_AI_API_BASE_URL = "https://api.us1.bfl.ai/v1";
116
+ var BLACK_FOREST_LABS_AI_API_BASE_URL = "https://api.us1.bfl.ai";
106
117
  var makeBody = (params) => {
107
118
  return params.args;
108
119
  };
@@ -114,7 +125,7 @@ var makeHeaders = (params) => {
114
125
  }
115
126
  };
116
127
  var makeUrl = (params) => {
117
- return `${params.baseUrl}/${params.model}`;
128
+ return `${params.baseUrl}/v1/${params.model}`;
118
129
  };
119
130
  var BLACK_FOREST_LABS_CONFIG = {
120
131
  baseUrl: BLACK_FOREST_LABS_AI_API_BASE_URL,
@@ -186,7 +197,7 @@ var FAL_AI_CONFIG = {
186
197
  };
187
198
 
188
199
  // src/providers/fireworks-ai.ts
189
- var FIREWORKS_AI_API_BASE_URL = "https://api.fireworks.ai/inference";
200
+ var FIREWORKS_AI_API_BASE_URL = "https://api.fireworks.ai";
190
201
  var makeBody5 = (params) => {
191
202
  return {
192
203
  ...params.args,
@@ -197,10 +208,10 @@ var makeHeaders5 = (params) => {
197
208
  return { Authorization: `Bearer ${params.accessToken}` };
198
209
  };
199
210
  var makeUrl5 = (params) => {
200
- if (params.task === "text-generation" && params.chatCompletion) {
201
- return `${params.baseUrl}/v1/chat/completions`;
211
+ if (params.chatCompletion) {
212
+ return `${params.baseUrl}/inference/v1/chat/completions`;
202
213
  }
203
- return params.baseUrl;
214
+ return `${params.baseUrl}/inference`;
204
215
  };
205
216
  var FIREWORKS_AI_CONFIG = {
206
217
  baseUrl: FIREWORKS_AI_API_BASE_URL,
@@ -223,7 +234,7 @@ var makeUrl6 = (params) => {
223
234
  if (params.task && ["feature-extraction", "sentence-similarity"].includes(params.task)) {
224
235
  return `${params.baseUrl}/pipeline/${params.task}/${params.model}`;
225
236
  }
226
- if (params.task === "text-generation" && params.chatCompletion) {
237
+ if (params.chatCompletion) {
227
238
  return `${params.baseUrl}/models/${params.model}/v1/chat/completions`;
228
239
  }
229
240
  return `${params.baseUrl}/models/${params.model}`;
@@ -274,10 +285,10 @@ var makeUrl8 = (params) => {
274
285
  if (params.task === "text-to-image") {
275
286
  return `${params.baseUrl}/v1/images/generations`;
276
287
  }
288
+ if (params.chatCompletion) {
289
+ return `${params.baseUrl}/v1/chat/completions`;
290
+ }
277
291
  if (params.task === "text-generation") {
278
- if (params.chatCompletion) {
279
- return `${params.baseUrl}/v1/chat/completions`;
280
- }
281
292
  return `${params.baseUrl}/v1/completions`;
282
293
  }
283
294
  return params.baseUrl;
@@ -290,7 +301,7 @@ var NEBIUS_CONFIG = {
290
301
  };
291
302
 
292
303
  // src/providers/novita.ts
293
- var NOVITA_API_BASE_URL = "https://api.novita.ai/v3/openai";
304
+ var NOVITA_API_BASE_URL = "https://api.novita.ai";
294
305
  var makeBody9 = (params) => {
295
306
  return {
296
307
  ...params.args,
@@ -301,11 +312,12 @@ var makeHeaders9 = (params) => {
301
312
  return { Authorization: `Bearer ${params.accessToken}` };
302
313
  };
303
314
  var makeUrl9 = (params) => {
304
- if (params.task === "text-generation") {
305
- if (params.chatCompletion) {
306
- return `${params.baseUrl}/chat/completions`;
307
- }
308
- return `${params.baseUrl}/completions`;
315
+ if (params.chatCompletion) {
316
+ return `${params.baseUrl}/v3/openai/chat/completions`;
317
+ } else if (params.task === "text-generation") {
318
+ return `${params.baseUrl}/v3/openai/completions`;
319
+ } else if (params.task === "text-to-video") {
320
+ return `${params.baseUrl}/v3/hf/${params.model}`;
309
321
  }
310
322
  return params.baseUrl;
311
323
  };
@@ -352,7 +364,7 @@ var makeHeaders11 = (params) => {
352
364
  return { Authorization: `Bearer ${params.accessToken}` };
353
365
  };
354
366
  var makeUrl11 = (params) => {
355
- if (params.task === "text-generation" && params.chatCompletion) {
367
+ if (params.chatCompletion) {
356
368
  return `${params.baseUrl}/v1/chat/completions`;
357
369
  }
358
370
  return params.baseUrl;
@@ -379,10 +391,10 @@ var makeUrl12 = (params) => {
379
391
  if (params.task === "text-to-image") {
380
392
  return `${params.baseUrl}/v1/images/generations`;
381
393
  }
394
+ if (params.chatCompletion) {
395
+ return `${params.baseUrl}/v1/chat/completions`;
396
+ }
382
397
  if (params.task === "text-generation") {
383
- if (params.chatCompletion) {
384
- return `${params.baseUrl}/v1/chat/completions`;
385
- }
386
398
  return `${params.baseUrl}/v1/completions`;
387
399
  }
388
400
  return params.baseUrl;
@@ -429,7 +441,7 @@ function isUrl(modelOrUrl) {
429
441
 
430
442
  // package.json
431
443
  var name = "@huggingface/inference";
432
- var version = "3.5.1";
444
+ var version = "3.6.0";
433
445
 
434
446
  // src/providers/consts.ts
435
447
  var HARDCODED_MODEL_ID_MAPPING = {
@@ -517,11 +529,11 @@ var providerConfigs = {
517
529
  together: TOGETHER_CONFIG
518
530
  };
519
531
  async function makeRequestOptions(args, options) {
520
- const { accessToken, endpointUrl, provider: maybeProvider, model: maybeModel, ...remainingArgs } = args;
532
+ const { provider: maybeProvider, model: maybeModel } = args;
521
533
  const provider = maybeProvider ?? "hf-inference";
522
534
  const providerConfig = providerConfigs[provider];
523
- const { includeCredentials, task, chatCompletion: chatCompletion2, signal } = options ?? {};
524
- if (endpointUrl && provider !== "hf-inference") {
535
+ const { task, chatCompletion: chatCompletion2 } = options ?? {};
536
+ if (args.endpointUrl && provider !== "hf-inference") {
525
537
  throw new Error(`Cannot use endpointUrl with a third-party provider.`);
526
538
  }
527
539
  if (maybeModel && isUrl(maybeModel)) {
@@ -537,17 +549,21 @@ async function makeRequestOptions(args, options) {
537
549
  throw new Error(`Provider ${provider} requires a model ID to be passed directly.`);
538
550
  }
539
551
  const hfModel = maybeModel ?? await loadDefaultModel(task);
540
- const model = providerConfig.clientSideRoutingOnly ? (
552
+ const resolvedModel = providerConfig.clientSideRoutingOnly ? (
541
553
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
542
554
  removeProviderPrefix(maybeModel, provider)
543
- ) : (
544
- // For closed-models API providers, one needs to pass the model ID directly (e.g. "gpt-3.5-turbo")
545
- await getProviderModelId({ model: hfModel, provider }, args, {
546
- task,
547
- chatCompletion: chatCompletion2,
548
- fetch: options?.fetch
549
- })
550
- );
555
+ ) : await getProviderModelId({ model: hfModel, provider }, args, {
556
+ task,
557
+ chatCompletion: chatCompletion2,
558
+ fetch: options?.fetch
559
+ });
560
+ return makeRequestOptionsFromResolvedModel(resolvedModel, args, options);
561
+ }
562
+ function makeRequestOptionsFromResolvedModel(resolvedModel, args, options) {
563
+ const { accessToken, endpointUrl, provider: maybeProvider, model, ...remainingArgs } = args;
564
+ const provider = maybeProvider ?? "hf-inference";
565
+ const providerConfig = providerConfigs[provider];
566
+ const { includeCredentials, task, chatCompletion: chatCompletion2, signal } = options ?? {};
551
567
  const authMethod = (() => {
552
568
  if (providerConfig.clientSideRoutingOnly) {
553
569
  if (accessToken && accessToken.startsWith("hf_")) {
@@ -565,7 +581,7 @@ async function makeRequestOptions(args, options) {
565
581
  })();
566
582
  const url = endpointUrl ? chatCompletion2 ? endpointUrl + `/v1/chat/completions` : endpointUrl : providerConfig.makeUrl({
567
583
  baseUrl: authMethod !== "provider-key" ? HF_HUB_INFERENCE_PROXY_TEMPLATE.replace("{{PROVIDER}}", provider) : providerConfig.baseUrl,
568
- model,
584
+ model: resolvedModel,
569
585
  chatCompletion: chatCompletion2,
570
586
  task
571
587
  });
@@ -583,7 +599,7 @@ async function makeRequestOptions(args, options) {
583
599
  const body = binary ? args.data : JSON.stringify(
584
600
  providerConfig.makeBody({
585
601
  args: remainingArgs,
586
- model,
602
+ model: resolvedModel,
587
603
  task,
588
604
  chatCompletion: chatCompletion2
589
605
  })
@@ -1217,14 +1233,14 @@ async function zeroShotImageClassification(args, options) {
1217
1233
  }
1218
1234
 
1219
1235
  // src/tasks/cv/textToVideo.ts
1220
- var SUPPORTED_PROVIDERS = ["fal-ai", "replicate"];
1236
+ var SUPPORTED_PROVIDERS = ["fal-ai", "novita", "replicate"];
1221
1237
  async function textToVideo(args, options) {
1222
1238
  if (!args.provider || !typedInclude(SUPPORTED_PROVIDERS, args.provider)) {
1223
1239
  throw new Error(
1224
1240
  `textToVideo inference is only supported for the following providers: ${SUPPORTED_PROVIDERS.join(", ")}`
1225
1241
  );
1226
1242
  }
1227
- const payload = args.provider === "fal-ai" || args.provider === "replicate" ? { ...omit(args, ["inputs", "parameters"]), ...args.parameters, prompt: args.inputs } : args;
1243
+ const payload = args.provider === "fal-ai" || args.provider === "replicate" || args.provider === "novita" ? { ...omit(args, ["inputs", "parameters"]), ...args.parameters, prompt: args.inputs } : args;
1228
1244
  const res = await request(payload, {
1229
1245
  ...options,
1230
1246
  task: "text-to-video"
@@ -1236,6 +1252,13 @@ async function textToVideo(args, options) {
1236
1252
  }
1237
1253
  const urlResponse = await fetch(res.video.url);
1238
1254
  return await urlResponse.blob();
1255
+ } else if (args.provider === "novita") {
1256
+ const isValidOutput = typeof res === "object" && !!res && "video" in res && typeof res.video === "object" && !!res.video && "video_url" in res.video && typeof res.video.video_url === "string" && isUrl(res.video.video_url);
1257
+ if (!isValidOutput) {
1258
+ throw new InferenceOutputError("Expected { video: { video_url: string } }");
1259
+ }
1260
+ const urlResponse = await fetch(res.video.video_url);
1261
+ return await urlResponse.blob();
1239
1262
  } else {
1240
1263
  const isValidOutput = typeof res === "object" && !!res && "output" in res && typeof res.output === "string" && isUrl(res.output);
1241
1264
  if (!isValidOutput) {
@@ -1582,8 +1605,8 @@ async function tabularClassification(args, options) {
1582
1605
  return res;
1583
1606
  }
1584
1607
 
1585
- // src/HfInference.ts
1586
- var HfInference = class {
1608
+ // src/InferenceClient.ts
1609
+ var InferenceClient = class {
1587
1610
  accessToken;
1588
1611
  defaultOptions;
1589
1612
  constructor(accessToken = "", defaultOptions = {}) {
@@ -1600,13 +1623,13 @@ var HfInference = class {
1600
1623
  }
1601
1624
  }
1602
1625
  /**
1603
- * Returns copy of HfInference tied to a specified endpoint.
1626
+ * Returns copy of InferenceClient tied to a specified endpoint.
1604
1627
  */
1605
1628
  endpoint(endpointUrl) {
1606
- return new HfInferenceEndpoint(endpointUrl, this.accessToken, this.defaultOptions);
1629
+ return new InferenceClientEndpoint(endpointUrl, this.accessToken, this.defaultOptions);
1607
1630
  }
1608
1631
  };
1609
- var HfInferenceEndpoint = class {
1632
+ var InferenceClientEndpoint = class {
1610
1633
  constructor(endpointUrl, accessToken = "", defaultOptions = {}) {
1611
1634
  accessToken;
1612
1635
  defaultOptions;
@@ -1621,6 +1644,8 @@ var HfInferenceEndpoint = class {
1621
1644
  }
1622
1645
  }
1623
1646
  };
1647
+ var HfInference = class extends InferenceClient {
1648
+ };
1624
1649
 
1625
1650
  // src/types.ts
1626
1651
  var INFERENCE_PROVIDERS = [
@@ -1642,936 +1667,276 @@ var INFERENCE_PROVIDERS = [
1642
1667
  // src/snippets/index.ts
1643
1668
  var snippets_exports = {};
1644
1669
  __export(snippets_exports, {
1645
- curl: () => curl_exports,
1646
- js: () => js_exports,
1647
- python: () => python_exports
1670
+ getInferenceSnippets: () => getInferenceSnippets
1648
1671
  });
1649
1672
 
1650
- // src/snippets/curl.ts
1651
- var curl_exports = {};
1652
- __export(curl_exports, {
1653
- curlSnippets: () => curlSnippets,
1654
- getCurlInferenceSnippet: () => getCurlInferenceSnippet,
1655
- snippetBasic: () => snippetBasic,
1656
- snippetFile: () => snippetFile,
1657
- snippetTextGeneration: () => snippetTextGeneration,
1658
- snippetZeroShotClassification: () => snippetZeroShotClassification
1659
- });
1673
+ // src/snippets/getInferenceSnippets.ts
1660
1674
  var import_tasks = require("@huggingface/tasks");
1661
- var import_tasks2 = require("@huggingface/tasks");
1662
- var snippetBasic = (model, accessToken, provider) => {
1663
- if (provider !== "hf-inference") {
1664
- return [];
1665
- }
1666
- return [
1667
- {
1668
- client: "curl",
1669
- content: `curl https://router.huggingface.co/hf-inference/models/${model.id} \\
1670
- -X POST \\
1671
- -d '{"inputs": ${(0, import_tasks2.getModelInputSnippet)(model, true)}}' \\
1672
- -H 'Content-Type: application/json' \\
1673
- -H 'Authorization: Bearer ${accessToken || `{API_TOKEN}`}'`
1675
+ var import_jinja = require("@huggingface/jinja");
1676
+ var import_fs = __toESM(require("fs"), 1);
1677
+ var import_path = __toESM(require("path"), 1);
1678
+ var import_node_fs = require("fs");
1679
+ var import_meta = {};
1680
+ var PYTHON_CLIENTS = ["huggingface_hub", "fal_client", "requests", "openai"];
1681
+ var JS_CLIENTS = ["fetch", "huggingface.js", "openai"];
1682
+ var SH_CLIENTS = ["curl"];
1683
+ var CLIENTS = {
1684
+ js: [...JS_CLIENTS],
1685
+ python: [...PYTHON_CLIENTS],
1686
+ sh: [...SH_CLIENTS]
1687
+ };
1688
+ var rootDirFinder = () => {
1689
+ let currentPath = typeof import_meta !== "undefined" && import_meta.url ? import_path.default.normalize(new URL(import_meta.url).pathname) : __dirname;
1690
+ while (currentPath !== "/") {
1691
+ if ((0, import_node_fs.existsSync)(import_path.default.join(currentPath, "package.json"))) {
1692
+ return currentPath;
1674
1693
  }
1675
- ];
1676
- };
1677
- var snippetTextGeneration = (model, accessToken, provider, providerModelId, opts) => {
1678
- if (model.tags.includes("conversational")) {
1679
- const baseUrl = provider === "hf-inference" ? `https://router.huggingface.co/hf-inference/models/${model.id}/v1/chat/completions` : import_tasks.HF_HUB_INFERENCE_PROXY_TEMPLATE.replace("{{PROVIDER}}", provider) + "/v1/chat/completions";
1680
- const modelId = providerModelId ?? model.id;
1681
- const streaming = opts?.streaming ?? true;
1682
- const exampleMessages = (0, import_tasks2.getModelInputSnippet)(model);
1683
- const messages = opts?.messages ?? exampleMessages;
1684
- const config = {
1685
- ...opts?.temperature ? { temperature: opts.temperature } : void 0,
1686
- max_tokens: opts?.max_tokens ?? 500,
1687
- ...opts?.top_p ? { top_p: opts.top_p } : void 0
1688
- };
1689
- return [
1690
- {
1691
- client: "curl",
1692
- content: `curl '${baseUrl}' \\
1693
- -H 'Authorization: Bearer ${accessToken || `{API_TOKEN}`}' \\
1694
- -H 'Content-Type: application/json' \\
1695
- --data '{
1696
- "model": "${modelId}",
1697
- "messages": ${(0, import_tasks2.stringifyMessages)(messages, {
1698
- indent: " ",
1699
- attributeKeyQuotes: true,
1700
- customContentEscaper: (str) => str.replace(/'/g, "'\\''")
1701
- })},
1702
- ${(0, import_tasks2.stringifyGenerationConfig)(config, {
1703
- indent: "\n ",
1704
- attributeKeyQuotes: true,
1705
- attributeValueConnector: ": "
1706
- })}
1707
- "stream": ${!!streaming}
1708
- }'`
1709
- }
1710
- ];
1711
- } else {
1712
- return snippetBasic(model, accessToken, provider);
1694
+ currentPath = import_path.default.normalize(import_path.default.join(currentPath, ".."));
1713
1695
  }
1696
+ return "/";
1714
1697
  };
1715
- var snippetZeroShotClassification = (model, accessToken, provider) => {
1716
- if (provider !== "hf-inference") {
1717
- return [];
1718
- }
1719
- return [
1720
- {
1721
- client: "curl",
1722
- content: `curl https://router.huggingface.co/hf-inference/models/${model.id} \\
1723
- -X POST \\
1724
- -d '{"inputs": ${(0, import_tasks2.getModelInputSnippet)(model, true)}, "parameters": {"candidate_labels": ["refund", "legal", "faq"]}}' \\
1725
- -H 'Content-Type: application/json' \\
1726
- -H 'Authorization: Bearer ${accessToken || `{API_TOKEN}`}'`
1727
- }
1728
- ];
1729
- };
1730
- var snippetFile = (model, accessToken, provider) => {
1731
- if (provider !== "hf-inference") {
1732
- return [];
1733
- }
1734
- return [
1735
- {
1736
- client: "curl",
1737
- content: `curl https://router.huggingface.co/hf-inference/models/${model.id} \\
1738
- -X POST \\
1739
- --data-binary '@${(0, import_tasks2.getModelInputSnippet)(model, true, true)}' \\
1740
- -H 'Authorization: Bearer ${accessToken || `{API_TOKEN}`}'`
1741
- }
1742
- ];
1743
- };
1744
- var curlSnippets = {
1745
- // Same order as in tasks/src/pipelines.ts
1746
- "text-classification": snippetBasic,
1747
- "token-classification": snippetBasic,
1748
- "table-question-answering": snippetBasic,
1749
- "question-answering": snippetBasic,
1750
- "zero-shot-classification": snippetZeroShotClassification,
1751
- translation: snippetBasic,
1752
- summarization: snippetBasic,
1753
- "feature-extraction": snippetBasic,
1754
- "text-generation": snippetTextGeneration,
1755
- "image-text-to-text": snippetTextGeneration,
1756
- "text2text-generation": snippetBasic,
1757
- "fill-mask": snippetBasic,
1758
- "sentence-similarity": snippetBasic,
1759
- "automatic-speech-recognition": snippetFile,
1760
- "text-to-image": snippetBasic,
1761
- "text-to-speech": snippetBasic,
1762
- "text-to-audio": snippetBasic,
1763
- "audio-to-audio": snippetFile,
1764
- "audio-classification": snippetFile,
1765
- "image-classification": snippetFile,
1766
- "image-to-text": snippetFile,
1767
- "object-detection": snippetFile,
1768
- "image-segmentation": snippetFile
1769
- };
1770
- function getCurlInferenceSnippet(model, accessToken, provider, providerModelId, opts) {
1771
- return model.pipeline_tag && model.pipeline_tag in curlSnippets ? curlSnippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId, opts) ?? [] : [];
1772
- }
1773
-
1774
- // src/snippets/python.ts
1775
- var python_exports = {};
1776
- __export(python_exports, {
1777
- getPythonInferenceSnippet: () => getPythonInferenceSnippet,
1778
- pythonSnippets: () => pythonSnippets,
1779
- snippetBasic: () => snippetBasic2,
1780
- snippetConversational: () => snippetConversational,
1781
- snippetDocumentQuestionAnswering: () => snippetDocumentQuestionAnswering,
1782
- snippetFile: () => snippetFile2,
1783
- snippetTabular: () => snippetTabular,
1784
- snippetTextToAudio: () => snippetTextToAudio,
1785
- snippetTextToImage: () => snippetTextToImage,
1786
- snippetTextToVideo: () => snippetTextToVideo,
1787
- snippetZeroShotClassification: () => snippetZeroShotClassification2,
1788
- snippetZeroShotImageClassification: () => snippetZeroShotImageClassification
1789
- });
1790
- var import_tasks3 = require("@huggingface/tasks");
1791
- var import_tasks4 = require("@huggingface/tasks");
1792
- var HFH_INFERENCE_CLIENT_METHODS = {
1698
+ var templatePath = (language, client, templateName) => import_path.default.join(rootDirFinder(), "src", "snippets", "templates", language, client, `${templateName}.jinja`);
1699
+ var hasTemplate = (language, client, templateName) => (0, import_node_fs.existsSync)(templatePath(language, client, templateName));
1700
+ var loadTemplate = (language, client, templateName) => {
1701
+ const template = import_fs.default.readFileSync(templatePath(language, client, templateName), "utf8");
1702
+ return (data) => new import_jinja.Template(template).render({ ...data });
1703
+ };
1704
+ var snippetImportPythonInferenceClient = loadTemplate("python", "huggingface_hub", "importInferenceClient");
1705
+ var snippetImportRequests = loadTemplate("python", "requests", "importRequests");
1706
+ var HF_PYTHON_METHODS = {
1793
1707
  "audio-classification": "audio_classification",
1794
1708
  "audio-to-audio": "audio_to_audio",
1795
1709
  "automatic-speech-recognition": "automatic_speech_recognition",
1796
- "text-to-speech": "text_to_speech",
1710
+ "document-question-answering": "document_question_answering",
1711
+ "feature-extraction": "feature_extraction",
1712
+ "fill-mask": "fill_mask",
1797
1713
  "image-classification": "image_classification",
1798
1714
  "image-segmentation": "image_segmentation",
1799
1715
  "image-to-image": "image_to_image",
1800
1716
  "image-to-text": "image_to_text",
1801
1717
  "object-detection": "object_detection",
1802
- "text-to-image": "text_to_image",
1803
- "text-to-video": "text_to_video",
1804
- "zero-shot-image-classification": "zero_shot_image_classification",
1805
- "document-question-answering": "document_question_answering",
1806
- "visual-question-answering": "visual_question_answering",
1807
- "feature-extraction": "feature_extraction",
1808
- "fill-mask": "fill_mask",
1809
1718
  "question-answering": "question_answering",
1810
1719
  "sentence-similarity": "sentence_similarity",
1811
1720
  summarization: "summarization",
1812
1721
  "table-question-answering": "table_question_answering",
1722
+ "tabular-classification": "tabular_classification",
1723
+ "tabular-regression": "tabular_regression",
1813
1724
  "text-classification": "text_classification",
1814
1725
  "text-generation": "text_generation",
1726
+ "text-to-image": "text_to_image",
1727
+ "text-to-speech": "text_to_speech",
1728
+ "text-to-video": "text_to_video",
1815
1729
  "token-classification": "token_classification",
1816
1730
  translation: "translation",
1731
+ "visual-question-answering": "visual_question_answering",
1817
1732
  "zero-shot-classification": "zero_shot_classification",
1818
- "tabular-classification": "tabular_classification",
1819
- "tabular-regression": "tabular_regression"
1820
- };
1821
- var snippetImportInferenceClient = (accessToken, provider) => `from huggingface_hub import InferenceClient
1822
-
1823
- client = InferenceClient(
1824
- provider="${provider}",
1825
- api_key="${accessToken || "{API_TOKEN}"}"
1826
- )`;
1827
- var snippetConversational = (model, accessToken, provider, providerModelId, opts) => {
1828
- const streaming = opts?.streaming ?? true;
1829
- const exampleMessages = (0, import_tasks4.getModelInputSnippet)(model);
1830
- const messages = opts?.messages ?? exampleMessages;
1831
- const messagesStr = (0, import_tasks4.stringifyMessages)(messages, { attributeKeyQuotes: true });
1832
- const config = {
1833
- ...opts?.temperature ? { temperature: opts.temperature } : void 0,
1834
- max_tokens: opts?.max_tokens ?? 500,
1835
- ...opts?.top_p ? { top_p: opts.top_p } : void 0
1836
- };
1837
- const configStr = (0, import_tasks4.stringifyGenerationConfig)(config, {
1838
- indent: "\n ",
1839
- attributeValueConnector: "="
1840
- });
1841
- if (streaming) {
1842
- return [
1843
- {
1844
- client: "huggingface_hub",
1845
- content: `${snippetImportInferenceClient(accessToken, provider)}
1846
-
1847
- messages = ${messagesStr}
1848
-
1849
- stream = client.chat.completions.create(
1850
- model="${model.id}",
1851
- messages=messages,
1852
- ${configStr}
1853
- stream=True
1854
- )
1855
-
1856
- for chunk in stream:
1857
- print(chunk.choices[0].delta.content, end="")`
1858
- },
1859
- {
1860
- client: "openai",
1861
- content: `from openai import OpenAI
1862
-
1863
- client = OpenAI(
1864
- base_url="${(0, import_tasks3.openAIbaseUrl)(provider)}",
1865
- api_key="${accessToken || "{API_TOKEN}"}"
1866
- )
1867
-
1868
- messages = ${messagesStr}
1869
-
1870
- stream = client.chat.completions.create(
1871
- model="${providerModelId ?? model.id}",
1872
- messages=messages,
1873
- ${configStr}
1874
- stream=True
1875
- )
1876
-
1877
- for chunk in stream:
1878
- print(chunk.choices[0].delta.content, end="")`
1879
- }
1880
- ];
1881
- } else {
1882
- return [
1883
- {
1884
- client: "huggingface_hub",
1885
- content: `${snippetImportInferenceClient(accessToken, provider)}
1886
-
1887
- messages = ${messagesStr}
1888
-
1889
- completion = client.chat.completions.create(
1890
- model="${model.id}",
1891
- messages=messages,
1892
- ${configStr}
1893
- )
1894
-
1895
- print(completion.choices[0].message)`
1896
- },
1897
- {
1898
- client: "openai",
1899
- content: `from openai import OpenAI
1900
-
1901
- client = OpenAI(
1902
- base_url="${(0, import_tasks3.openAIbaseUrl)(provider)}",
1903
- api_key="${accessToken || "{API_TOKEN}"}"
1904
- )
1905
-
1906
- messages = ${messagesStr}
1907
-
1908
- completion = client.chat.completions.create(
1909
- model="${providerModelId ?? model.id}",
1910
- messages=messages,
1911
- ${configStr}
1912
- )
1913
-
1914
- print(completion.choices[0].message)`
1915
- }
1916
- ];
1917
- }
1918
- };
1919
- var snippetZeroShotClassification2 = (model) => {
1920
- return [
1921
- {
1922
- client: "requests",
1923
- content: `def query(payload):
1924
- response = requests.post(API_URL, headers=headers, json=payload)
1925
- return response.json()
1926
-
1927
- output = query({
1928
- "inputs": ${(0, import_tasks4.getModelInputSnippet)(model)},
1929
- "parameters": {"candidate_labels": ["refund", "legal", "faq"]},
1930
- })`
1931
- }
1932
- ];
1933
- };
1934
- var snippetZeroShotImageClassification = (model) => {
1935
- return [
1936
- {
1937
- client: "requests",
1938
- content: `def query(data):
1939
- with open(data["image_path"], "rb") as f:
1940
- img = f.read()
1941
- payload={
1942
- "parameters": data["parameters"],
1943
- "inputs": base64.b64encode(img).decode("utf-8")
1944
- }
1945
- response = requests.post(API_URL, headers=headers, json=payload)
1946
- return response.json()
1947
-
1948
- output = query({
1949
- "image_path": ${(0, import_tasks4.getModelInputSnippet)(model)},
1950
- "parameters": {"candidate_labels": ["cat", "dog", "llama"]},
1951
- })`
1952
- }
1953
- ];
1954
- };
1955
- var snippetBasic2 = (model, accessToken, provider) => {
1956
- return [
1957
- ...model.pipeline_tag && model.pipeline_tag in HFH_INFERENCE_CLIENT_METHODS ? [
1958
- {
1959
- client: "huggingface_hub",
1960
- content: `${snippetImportInferenceClient(accessToken, provider)}
1961
-
1962
- result = client.${HFH_INFERENCE_CLIENT_METHODS[model.pipeline_tag]}(
1963
- model="${model.id}",
1964
- inputs=${(0, import_tasks4.getModelInputSnippet)(model)},
1965
- provider="${provider}",
1966
- )
1967
-
1968
- print(result)
1969
- `
1970
- }
1971
- ] : [],
1972
- {
1973
- client: "requests",
1974
- content: `def query(payload):
1975
- response = requests.post(API_URL, headers=headers, json=payload)
1976
- return response.json()
1977
-
1978
- output = query({
1979
- "inputs": ${(0, import_tasks4.getModelInputSnippet)(model)},
1980
- })`
1981
- }
1982
- ];
1983
- };
1984
- var snippetFile2 = (model) => {
1985
- return [
1986
- {
1987
- client: "requests",
1988
- content: `def query(filename):
1989
- with open(filename, "rb") as f:
1990
- data = f.read()
1991
- response = requests.post(API_URL, headers=headers, data=data)
1992
- return response.json()
1993
-
1994
- output = query(${(0, import_tasks4.getModelInputSnippet)(model)})`
1995
- }
1996
- ];
1997
- };
1998
- var snippetTextToImage = (model, accessToken, provider, providerModelId) => {
1999
- return [
2000
- {
2001
- client: "huggingface_hub",
2002
- content: `${snippetImportInferenceClient(accessToken, provider)}
2003
-
2004
- # output is a PIL.Image object
2005
- image = client.text_to_image(
2006
- ${(0, import_tasks4.getModelInputSnippet)(model)},
2007
- model="${model.id}"
2008
- )`
2009
- },
2010
- ...provider === "fal-ai" ? [
2011
- {
2012
- client: "fal-client",
2013
- content: `import fal_client
2014
-
2015
- result = fal_client.subscribe(
2016
- "${providerModelId ?? model.id}",
2017
- arguments={
2018
- "prompt": ${(0, import_tasks4.getModelInputSnippet)(model)},
2019
- },
2020
- )
2021
- print(result)
2022
- `
2023
- }
2024
- ] : [],
2025
- ...provider === "hf-inference" ? [
2026
- {
2027
- client: "requests",
2028
- content: `def query(payload):
2029
- response = requests.post(API_URL, headers=headers, json=payload)
2030
- return response.content
2031
-
2032
- image_bytes = query({
2033
- "inputs": ${(0, import_tasks4.getModelInputSnippet)(model)},
2034
- })
2035
-
2036
- # You can access the image with PIL.Image for example
2037
- import io
2038
- from PIL import Image
2039
- image = Image.open(io.BytesIO(image_bytes))`
2040
- }
2041
- ] : []
2042
- ];
2043
- };
2044
- var snippetTextToVideo = (model, accessToken, provider) => {
2045
- return ["fal-ai", "replicate"].includes(provider) ? [
2046
- {
2047
- client: "huggingface_hub",
2048
- content: `${snippetImportInferenceClient(accessToken, provider)}
2049
-
2050
- video = client.text_to_video(
2051
- ${(0, import_tasks4.getModelInputSnippet)(model)},
2052
- model="${model.id}"
2053
- )`
2054
- }
2055
- ] : [];
2056
- };
2057
- var snippetTabular = (model) => {
2058
- return [
2059
- {
2060
- client: "requests",
2061
- content: `def query(payload):
2062
- response = requests.post(API_URL, headers=headers, json=payload)
2063
- return response.content
2064
-
2065
- response = query({
2066
- "inputs": {"data": ${(0, import_tasks4.getModelInputSnippet)(model)}},
2067
- })`
2068
- }
2069
- ];
1733
+ "zero-shot-image-classification": "zero_shot_image_classification"
2070
1734
  };
2071
- var snippetTextToAudio = (model) => {
2072
- if (model.library_name === "transformers") {
2073
- return [
2074
- {
2075
- client: "requests",
2076
- content: `def query(payload):
2077
- response = requests.post(API_URL, headers=headers, json=payload)
2078
- return response.content
2079
-
2080
- audio_bytes = query({
2081
- "inputs": ${(0, import_tasks4.getModelInputSnippet)(model)},
2082
- })
2083
- # You can access the audio with IPython.display for example
2084
- from IPython.display import Audio
2085
- Audio(audio_bytes)`
2086
- }
2087
- ];
2088
- } else {
2089
- return [
2090
- {
2091
- client: "requests",
2092
- content: `def query(payload):
2093
- response = requests.post(API_URL, headers=headers, json=payload)
2094
- return response.json()
2095
-
2096
- audio, sampling_rate = query({
2097
- "inputs": ${(0, import_tasks4.getModelInputSnippet)(model)},
2098
- })
2099
- # You can access the audio with IPython.display for example
2100
- from IPython.display import Audio
2101
- Audio(audio, rate=sampling_rate)`
2102
- }
2103
- ];
2104
- }
2105
- };
2106
- var snippetDocumentQuestionAnswering = (model) => {
2107
- return [
2108
- {
2109
- client: "requests",
2110
- content: `def query(payload):
2111
- with open(payload["image"], "rb") as f:
2112
- img = f.read()
2113
- payload["image"] = base64.b64encode(img).decode("utf-8")
2114
- response = requests.post(API_URL, headers=headers, json=payload)
2115
- return response.json()
2116
-
2117
- output = query({
2118
- "inputs": ${(0, import_tasks4.getModelInputSnippet)(model)},
2119
- })`
2120
- }
2121
- ];
2122
- };
2123
- var pythonSnippets = {
2124
- // Same order as in tasks/src/pipelines.ts
2125
- "text-classification": snippetBasic2,
2126
- "token-classification": snippetBasic2,
2127
- "table-question-answering": snippetBasic2,
2128
- "question-answering": snippetBasic2,
2129
- "zero-shot-classification": snippetZeroShotClassification2,
2130
- translation: snippetBasic2,
2131
- summarization: snippetBasic2,
2132
- "feature-extraction": snippetBasic2,
2133
- "text-generation": snippetBasic2,
2134
- "text2text-generation": snippetBasic2,
2135
- "image-text-to-text": snippetConversational,
2136
- "fill-mask": snippetBasic2,
2137
- "sentence-similarity": snippetBasic2,
2138
- "automatic-speech-recognition": snippetFile2,
2139
- "text-to-image": snippetTextToImage,
2140
- "text-to-video": snippetTextToVideo,
2141
- "text-to-speech": snippetTextToAudio,
2142
- "text-to-audio": snippetTextToAudio,
2143
- "audio-to-audio": snippetFile2,
2144
- "audio-classification": snippetFile2,
2145
- "image-classification": snippetFile2,
2146
- "tabular-regression": snippetTabular,
2147
- "tabular-classification": snippetTabular,
2148
- "object-detection": snippetFile2,
2149
- "image-segmentation": snippetFile2,
2150
- "document-question-answering": snippetDocumentQuestionAnswering,
2151
- "image-to-text": snippetFile2,
2152
- "zero-shot-image-classification": snippetZeroShotImageClassification
2153
- };
2154
- function getPythonInferenceSnippet(model, accessToken, provider, providerModelId, opts) {
2155
- if (model.tags.includes("conversational")) {
2156
- return snippetConversational(model, accessToken, provider, providerModelId, opts);
2157
- } else {
2158
- const snippets = model.pipeline_tag && model.pipeline_tag in pythonSnippets ? pythonSnippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId) ?? [] : [];
2159
- return snippets.map((snippet) => {
2160
- return {
2161
- ...snippet,
2162
- content: snippet.client === "requests" ? `import requests
2163
-
2164
- API_URL = "${(0, import_tasks3.openAIbaseUrl)(provider)}"
2165
- headers = {"Authorization": ${accessToken ? `"Bearer ${accessToken}"` : `f"Bearer {API_TOKEN}"`}}
2166
-
2167
- ${snippet.content}` : snippet.content
2168
- };
2169
- });
2170
- }
2171
- }
2172
-
2173
- // src/snippets/js.ts
2174
- var js_exports = {};
2175
- __export(js_exports, {
2176
- getJsInferenceSnippet: () => getJsInferenceSnippet,
2177
- jsSnippets: () => jsSnippets,
2178
- snippetAutomaticSpeechRecognition: () => snippetAutomaticSpeechRecognition,
2179
- snippetBasic: () => snippetBasic3,
2180
- snippetFile: () => snippetFile3,
2181
- snippetTextGeneration: () => snippetTextGeneration2,
2182
- snippetTextToAudio: () => snippetTextToAudio2,
2183
- snippetTextToImage: () => snippetTextToImage2,
2184
- snippetTextToVideo: () => snippetTextToVideo2,
2185
- snippetZeroShotClassification: () => snippetZeroShotClassification3
2186
- });
2187
- var import_tasks5 = require("@huggingface/tasks");
2188
- var import_tasks6 = require("@huggingface/tasks");
2189
- var HFJS_METHODS = {
2190
- "text-classification": "textClassification",
2191
- "token-classification": "tokenClassification",
2192
- "table-question-answering": "tableQuestionAnswering",
1735
+ var HF_JS_METHODS = {
1736
+ "automatic-speech-recognition": "automaticSpeechRecognition",
1737
+ "feature-extraction": "featureExtraction",
1738
+ "fill-mask": "fillMask",
1739
+ "image-classification": "imageClassification",
2193
1740
  "question-answering": "questionAnswering",
2194
- translation: "translation",
1741
+ "sentence-similarity": "sentenceSimilarity",
2195
1742
  summarization: "summarization",
2196
- "feature-extraction": "featureExtraction",
1743
+ "table-question-answering": "tableQuestionAnswering",
1744
+ "text-classification": "textClassification",
2197
1745
  "text-generation": "textGeneration",
2198
1746
  "text2text-generation": "textGeneration",
2199
- "fill-mask": "fillMask",
2200
- "sentence-similarity": "sentenceSimilarity"
1747
+ "token-classification": "tokenClassification",
1748
+ translation: "translation"
2201
1749
  };
2202
- var snippetBasic3 = (model, accessToken, provider) => {
2203
- return [
2204
- ...model.pipeline_tag && model.pipeline_tag in HFJS_METHODS ? [
2205
- {
2206
- client: "huggingface.js",
2207
- content: `import { HfInference } from "@huggingface/inference";
2208
-
2209
- const client = new HfInference("${accessToken || `{API_TOKEN}`}");
2210
-
2211
- const output = await client.${HFJS_METHODS[model.pipeline_tag]}({
2212
- model: "${model.id}",
2213
- inputs: ${(0, import_tasks6.getModelInputSnippet)(model)},
2214
- provider: "${provider}",
2215
- });
2216
-
2217
- console.log(output);
2218
- `
1750
+ var snippetGenerator = (templateName, inputPreparationFn) => {
1751
+ return (model, accessToken, provider, providerModelId, opts) => {
1752
+ if (model.pipeline_tag && ["text-generation", "image-text-to-text"].includes(model.pipeline_tag) && model.tags.includes("conversational")) {
1753
+ templateName = opts?.streaming ? "conversationalStream" : "conversational";
1754
+ inputPreparationFn = prepareConversationalInput;
1755
+ }
1756
+ const inputs = inputPreparationFn ? inputPreparationFn(model, opts) : { inputs: (0, import_tasks.getModelInputSnippet)(model) };
1757
+ const request2 = makeRequestOptionsFromResolvedModel(
1758
+ providerModelId ?? model.id,
1759
+ { accessToken, provider, ...inputs },
1760
+ { chatCompletion: templateName.includes("conversational"), task: model.pipeline_tag }
1761
+ );
1762
+ let providerInputs = inputs;
1763
+ const bodyAsObj = request2.info.body;
1764
+ if (typeof bodyAsObj === "string") {
1765
+ try {
1766
+ providerInputs = JSON.parse(bodyAsObj);
1767
+ } catch (e) {
1768
+ console.error("Failed to parse body as JSON", e);
2219
1769
  }
2220
- ] : [],
2221
- {
2222
- client: "fetch",
2223
- content: `async function query(data) {
2224
- const response = await fetch(
2225
- "https://router.huggingface.co/hf-inference/models/${model.id}",
2226
- {
2227
- headers: {
2228
- Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
2229
- "Content-Type": "application/json",
2230
- },
2231
- method: "POST",
2232
- body: JSON.stringify(data),
2233
- }
2234
- );
2235
- const result = await response.json();
2236
- return result;
2237
- }
2238
-
2239
- query({"inputs": ${(0, import_tasks6.getModelInputSnippet)(model)}}).then((response) => {
2240
- console.log(JSON.stringify(response));
2241
- });`
2242
1770
  }
2243
- ];
2244
- };
2245
- var snippetTextGeneration2 = (model, accessToken, provider, providerModelId, opts) => {
2246
- if (model.tags.includes("conversational")) {
2247
- const streaming = opts?.streaming ?? true;
2248
- const exampleMessages = (0, import_tasks6.getModelInputSnippet)(model);
2249
- const messages = opts?.messages ?? exampleMessages;
2250
- const messagesStr = (0, import_tasks6.stringifyMessages)(messages, { indent: " " });
2251
- const config = {
2252
- ...opts?.temperature ? { temperature: opts.temperature } : void 0,
2253
- max_tokens: opts?.max_tokens ?? 500,
2254
- ...opts?.top_p ? { top_p: opts.top_p } : void 0
1771
+ const params = {
1772
+ accessToken,
1773
+ authorizationHeader: request2.info.headers?.Authorization,
1774
+ baseUrl: removeSuffix(request2.url, "/chat/completions"),
1775
+ fullUrl: request2.url,
1776
+ inputs: {
1777
+ asObj: inputs,
1778
+ asCurlString: formatBody(inputs, "curl"),
1779
+ asJsonString: formatBody(inputs, "json"),
1780
+ asPythonString: formatBody(inputs, "python"),
1781
+ asTsString: formatBody(inputs, "ts")
1782
+ },
1783
+ providerInputs: {
1784
+ asObj: providerInputs,
1785
+ asCurlString: formatBody(providerInputs, "curl"),
1786
+ asJsonString: formatBody(providerInputs, "json"),
1787
+ asPythonString: formatBody(providerInputs, "python"),
1788
+ asTsString: formatBody(providerInputs, "ts")
1789
+ },
1790
+ model,
1791
+ provider,
1792
+ providerModelId: providerModelId ?? model.id
2255
1793
  };
2256
- const configStr = (0, import_tasks6.stringifyGenerationConfig)(config, {
2257
- indent: "\n ",
2258
- attributeValueConnector: ": "
2259
- });
2260
- if (streaming) {
2261
- return [
2262
- {
2263
- client: "huggingface.js",
2264
- content: `import { HfInference } from "@huggingface/inference";
2265
-
2266
- const client = new HfInference("${accessToken || `{API_TOKEN}`}");
2267
-
2268
- let out = "";
2269
-
2270
- const stream = client.chatCompletionStream({
2271
- model: "${model.id}",
2272
- messages: ${messagesStr},
2273
- provider: "${provider}",
2274
- ${configStr}
2275
- });
2276
-
2277
- for await (const chunk of stream) {
2278
- if (chunk.choices && chunk.choices.length > 0) {
2279
- const newContent = chunk.choices[0].delta.content;
2280
- out += newContent;
2281
- console.log(newContent);
2282
- }
2283
- }`
2284
- },
2285
- {
2286
- client: "openai",
2287
- content: `import { OpenAI } from "openai";
2288
-
2289
- const client = new OpenAI({
2290
- baseURL: "${(0, import_tasks5.openAIbaseUrl)(provider)}",
2291
- apiKey: "${accessToken || `{API_TOKEN}`}"
2292
- });
2293
-
2294
- let out = "";
2295
-
2296
- const stream = await client.chat.completions.create({
2297
- model: "${providerModelId ?? model.id}",
2298
- messages: ${messagesStr},
2299
- ${configStr}
2300
- stream: true,
2301
- });
2302
-
2303
- for await (const chunk of stream) {
2304
- if (chunk.choices && chunk.choices.length > 0) {
2305
- const newContent = chunk.choices[0].delta.content;
2306
- out += newContent;
2307
- console.log(newContent);
2308
- }
2309
- }`
1794
+ return import_tasks.inferenceSnippetLanguages.map((language) => {
1795
+ return CLIENTS[language].map((client) => {
1796
+ if (!hasTemplate(language, client, templateName)) {
1797
+ return;
2310
1798
  }
2311
- ];
2312
- } else {
2313
- return [
2314
- {
2315
- client: "huggingface.js",
2316
- content: `import { HfInference } from "@huggingface/inference";
2317
-
2318
- const client = new HfInference("${accessToken || `{API_TOKEN}`}");
2319
-
2320
- const chatCompletion = await client.chatCompletion({
2321
- model: "${model.id}",
2322
- messages: ${messagesStr},
2323
- provider: "${provider}",
2324
- ${configStr}
2325
- });
2326
-
2327
- console.log(chatCompletion.choices[0].message);
2328
- `
2329
- },
2330
- {
2331
- client: "openai",
2332
- content: `import { OpenAI } from "openai";
2333
-
2334
- const client = new OpenAI({
2335
- baseURL: "${(0, import_tasks5.openAIbaseUrl)(provider)}",
2336
- apiKey: "${accessToken || `{API_TOKEN}`}"
2337
- });
2338
-
2339
- const chatCompletion = await client.chat.completions.create({
2340
- model: "${providerModelId ?? model.id}",
2341
- messages: ${messagesStr},
2342
- ${configStr}
2343
- });
2344
-
2345
- console.log(chatCompletion.choices[0].message);
2346
- `
1799
+ const template = loadTemplate(language, client, templateName);
1800
+ if (client === "huggingface_hub" && templateName.includes("basic")) {
1801
+ if (!(model.pipeline_tag && model.pipeline_tag in HF_PYTHON_METHODS)) {
1802
+ return;
1803
+ }
1804
+ params["methodName"] = HF_PYTHON_METHODS[model.pipeline_tag];
2347
1805
  }
2348
- ];
2349
- }
2350
- } else {
2351
- return snippetBasic3(model, accessToken, provider);
2352
- }
2353
- };
2354
- var snippetZeroShotClassification3 = (model, accessToken) => {
2355
- return [
2356
- {
2357
- client: "fetch",
2358
- content: `async function query(data) {
2359
- const response = await fetch(
2360
- "https://router.huggingface.co/hf-inference/models/${model.id}",
2361
- {
2362
- headers: {
2363
- Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
2364
- "Content-Type": "application/json",
2365
- },
2366
- method: "POST",
2367
- body: JSON.stringify(data),
2368
- }
2369
- );
2370
- const result = await response.json();
2371
- return result;
2372
- }
2373
-
2374
- query({"inputs": ${(0, import_tasks6.getModelInputSnippet)(
2375
- model
2376
- )}, "parameters": {"candidate_labels": ["refund", "legal", "faq"]}}).then((response) => {
2377
- console.log(JSON.stringify(response));
2378
- });`
2379
- }
2380
- ];
1806
+ if (client === "huggingface.js" && templateName.includes("basic")) {
1807
+ if (!(model.pipeline_tag && model.pipeline_tag in HF_JS_METHODS)) {
1808
+ return;
1809
+ }
1810
+ params["methodName"] = HF_JS_METHODS[model.pipeline_tag];
1811
+ }
1812
+ let snippet = template(params).trim();
1813
+ if (!snippet) {
1814
+ return;
1815
+ }
1816
+ if (client === "huggingface_hub") {
1817
+ const importSection = snippetImportPythonInferenceClient({ ...params });
1818
+ snippet = `${importSection}
1819
+
1820
+ ${snippet}`;
1821
+ } else if (client === "requests") {
1822
+ const importSection = snippetImportRequests({
1823
+ ...params,
1824
+ importBase64: snippet.includes("base64"),
1825
+ importJson: snippet.includes("json.")
1826
+ });
1827
+ snippet = `${importSection}
1828
+
1829
+ ${snippet}`;
1830
+ }
1831
+ return { language, client, content: snippet };
1832
+ }).filter((snippet) => snippet !== void 0);
1833
+ }).flat();
1834
+ };
2381
1835
  };
2382
- var snippetTextToImage2 = (model, accessToken, provider) => {
2383
- return [
2384
- {
2385
- client: "huggingface.js",
2386
- content: `import { HfInference } from "@huggingface/inference";
2387
-
2388
- const client = new HfInference("${accessToken || `{API_TOKEN}`}");
2389
-
2390
- const image = await client.textToImage({
2391
- model: "${model.id}",
2392
- inputs: ${(0, import_tasks6.getModelInputSnippet)(model)},
2393
- parameters: { num_inference_steps: 5 },
2394
- provider: "${provider}",
2395
- });
2396
- /// Use the generated image (it's a Blob)
2397
- `
2398
- },
2399
- ...provider === "hf-inference" ? [
2400
- {
2401
- client: "fetch",
2402
- content: `async function query(data) {
2403
- const response = await fetch(
2404
- "https://router.huggingface.co/hf-inference/models/${model.id}",
2405
- {
2406
- headers: {
2407
- Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
2408
- "Content-Type": "application/json",
2409
- },
2410
- method: "POST",
2411
- body: JSON.stringify(data),
2412
- }
2413
- );
2414
- const result = await response.blob();
2415
- return result;
1836
+ var prepareDocumentQuestionAnsweringInput = (model) => {
1837
+ return JSON.parse((0, import_tasks.getModelInputSnippet)(model));
1838
+ };
1839
+ var prepareImageToImageInput = (model) => {
1840
+ const data = JSON.parse((0, import_tasks.getModelInputSnippet)(model));
1841
+ return { inputs: data.image, parameters: { prompt: data.prompt } };
1842
+ };
1843
+ var prepareConversationalInput = (model, opts) => {
1844
+ return {
1845
+ messages: opts?.messages ?? (0, import_tasks.getModelInputSnippet)(model),
1846
+ ...opts?.temperature ? { temperature: opts?.temperature } : void 0,
1847
+ max_tokens: opts?.max_tokens ?? 500,
1848
+ ...opts?.top_p ? { top_p: opts?.top_p } : void 0
1849
+ };
1850
+ };
1851
+ var snippets = {
1852
+ "audio-classification": snippetGenerator("basicAudio"),
1853
+ "audio-to-audio": snippetGenerator("basicAudio"),
1854
+ "automatic-speech-recognition": snippetGenerator("basicAudio"),
1855
+ "document-question-answering": snippetGenerator("documentQuestionAnswering", prepareDocumentQuestionAnsweringInput),
1856
+ "feature-extraction": snippetGenerator("basic"),
1857
+ "fill-mask": snippetGenerator("basic"),
1858
+ "image-classification": snippetGenerator("basicImage"),
1859
+ "image-segmentation": snippetGenerator("basicImage"),
1860
+ "image-text-to-text": snippetGenerator("conversational"),
1861
+ "image-to-image": snippetGenerator("imageToImage", prepareImageToImageInput),
1862
+ "image-to-text": snippetGenerator("basicImage"),
1863
+ "object-detection": snippetGenerator("basicImage"),
1864
+ "question-answering": snippetGenerator("basic"),
1865
+ "sentence-similarity": snippetGenerator("basic"),
1866
+ summarization: snippetGenerator("basic"),
1867
+ "tabular-classification": snippetGenerator("tabular"),
1868
+ "tabular-regression": snippetGenerator("tabular"),
1869
+ "table-question-answering": snippetGenerator("basic"),
1870
+ "text-classification": snippetGenerator("basic"),
1871
+ "text-generation": snippetGenerator("basic"),
1872
+ "text-to-audio": snippetGenerator("textToAudio"),
1873
+ "text-to-image": snippetGenerator("textToImage"),
1874
+ "text-to-speech": snippetGenerator("textToAudio"),
1875
+ "text-to-video": snippetGenerator("textToVideo"),
1876
+ "text2text-generation": snippetGenerator("basic"),
1877
+ "token-classification": snippetGenerator("basic"),
1878
+ translation: snippetGenerator("basic"),
1879
+ "zero-shot-classification": snippetGenerator("zeroShotClassification"),
1880
+ "zero-shot-image-classification": snippetGenerator("zeroShotImageClassification")
1881
+ };
1882
+ function getInferenceSnippets(model, accessToken, provider, providerModelId, opts) {
1883
+ return model.pipeline_tag && model.pipeline_tag in snippets ? snippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId, opts) ?? [] : [];
2416
1884
  }
2417
- query({"inputs": ${(0, import_tasks6.getModelInputSnippet)(model)}}).then((response) => {
2418
- // Use image
2419
- });`
2420
- }
2421
- ] : []
2422
- ];
2423
- };
2424
- var snippetTextToVideo2 = (model, accessToken, provider) => {
2425
- return ["fal-ai", "replicate"].includes(provider) ? [
2426
- {
2427
- client: "huggingface.js",
2428
- content: `import { HfInference } from "@huggingface/inference";
2429
-
2430
- const client = new HfInference("${accessToken || `{API_TOKEN}`}");
2431
-
2432
- const video = await client.textToVideo({
2433
- model: "${model.id}",
2434
- provider: "${provider}",
2435
- inputs: ${(0, import_tasks6.getModelInputSnippet)(model)},
2436
- parameters: { num_inference_steps: 5 },
2437
- });
2438
- // Use the generated video (it's a Blob)
2439
- `
2440
- }
2441
- ] : [];
2442
- };
2443
- var snippetTextToAudio2 = (model, accessToken, provider) => {
2444
- if (provider !== "hf-inference") {
2445
- return [];
2446
- }
2447
- const commonSnippet = `async function query(data) {
2448
- const response = await fetch(
2449
- "https://router.huggingface.co/hf-inference/models/${model.id}",
2450
- {
2451
- headers: {
2452
- Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
2453
- "Content-Type": "application/json",
2454
- },
2455
- method: "POST",
2456
- body: JSON.stringify(data),
2457
- }
2458
- );`;
2459
- if (model.library_name === "transformers") {
2460
- return [
2461
- {
2462
- client: "fetch",
2463
- content: commonSnippet + `
2464
- const result = await response.blob();
2465
- return result;
2466
- }
2467
- query({"inputs": ${(0, import_tasks6.getModelInputSnippet)(model)}}).then((response) => {
2468
- // Returns a byte object of the Audio wavform. Use it directly!
2469
- });`
2470
- }
2471
- ];
2472
- } else {
2473
- return [
2474
- {
2475
- client: "fetch",
2476
- content: commonSnippet + `
2477
- const result = await response.json();
2478
- return result;
2479
- }
2480
-
2481
- query({"inputs": ${(0, import_tasks6.getModelInputSnippet)(model)}}).then((response) => {
2482
- console.log(JSON.stringify(response));
2483
- });`
2484
- }
2485
- ];
1885
+ function formatBody(obj, format) {
1886
+ switch (format) {
1887
+ case "curl":
1888
+ return indentString(formatBody(obj, "json"));
1889
+ case "json":
1890
+ return JSON.stringify(obj, null, 4).split("\n").slice(1, -1).join("\n");
1891
+ case "python":
1892
+ return indentString(
1893
+ Object.entries(obj).map(([key, value]) => {
1894
+ const formattedValue = JSON.stringify(value, null, 4).replace(/"/g, '"');
1895
+ return `${key}=${formattedValue},`;
1896
+ }).join("\n")
1897
+ );
1898
+ case "ts":
1899
+ return formatTsObject(obj).split("\n").slice(1, -1).join("\n");
1900
+ default:
1901
+ throw new Error(`Unsupported format: ${format}`);
2486
1902
  }
2487
- };
2488
- var snippetAutomaticSpeechRecognition = (model, accessToken, provider) => {
2489
- return [
2490
- {
2491
- client: "huggingface.js",
2492
- content: `import { HfInference } from "@huggingface/inference";
2493
-
2494
- const client = new HfInference("${accessToken || `{API_TOKEN}`}");
2495
-
2496
- const data = fs.readFileSync(${(0, import_tasks6.getModelInputSnippet)(model)});
2497
-
2498
- const output = await client.automaticSpeechRecognition({
2499
- data,
2500
- model: "${model.id}",
2501
- provider: "${provider}",
2502
- });
2503
-
2504
- console.log(output);
2505
- `
2506
- },
2507
- ...provider === "hf-inference" ? snippetFile3(model, accessToken, provider) : []
2508
- ];
2509
- };
2510
- var snippetFile3 = (model, accessToken, provider) => {
2511
- if (provider !== "hf-inference") {
2512
- return [];
2513
- }
2514
- return [
2515
- {
2516
- client: "fetch",
2517
- content: `async function query(filename) {
2518
- const data = fs.readFileSync(filename);
2519
- const response = await fetch(
2520
- "https://router.huggingface.co/hf-inference/models/${model.id}",
2521
- {
2522
- headers: {
2523
- Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
2524
- "Content-Type": "application/json",
2525
- },
2526
- method: "POST",
2527
- body: data,
2528
- }
2529
- );
2530
- const result = await response.json();
2531
- return result;
2532
1903
  }
2533
-
2534
- query(${(0, import_tasks6.getModelInputSnippet)(model)}).then((response) => {
2535
- console.log(JSON.stringify(response));
2536
- });`
2537
- }
2538
- ];
2539
- };
2540
- var jsSnippets = {
2541
- // Same order as in tasks/src/pipelines.ts
2542
- "text-classification": snippetBasic3,
2543
- "token-classification": snippetBasic3,
2544
- "table-question-answering": snippetBasic3,
2545
- "question-answering": snippetBasic3,
2546
- "zero-shot-classification": snippetZeroShotClassification3,
2547
- translation: snippetBasic3,
2548
- summarization: snippetBasic3,
2549
- "feature-extraction": snippetBasic3,
2550
- "text-generation": snippetTextGeneration2,
2551
- "image-text-to-text": snippetTextGeneration2,
2552
- "text2text-generation": snippetBasic3,
2553
- "fill-mask": snippetBasic3,
2554
- "sentence-similarity": snippetBasic3,
2555
- "automatic-speech-recognition": snippetAutomaticSpeechRecognition,
2556
- "text-to-image": snippetTextToImage2,
2557
- "text-to-video": snippetTextToVideo2,
2558
- "text-to-speech": snippetTextToAudio2,
2559
- "text-to-audio": snippetTextToAudio2,
2560
- "audio-to-audio": snippetFile3,
2561
- "audio-classification": snippetFile3,
2562
- "image-classification": snippetFile3,
2563
- "image-to-text": snippetFile3,
2564
- "object-detection": snippetFile3,
2565
- "image-segmentation": snippetFile3
2566
- };
2567
- function getJsInferenceSnippet(model, accessToken, provider, providerModelId, opts) {
2568
- return model.pipeline_tag && model.pipeline_tag in jsSnippets ? jsSnippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId, opts) ?? [] : [];
1904
+ function formatTsObject(obj, depth) {
1905
+ depth = depth ?? 0;
1906
+ if (typeof obj !== "object" || obj === null) {
1907
+ return JSON.stringify(obj);
1908
+ }
1909
+ if (Array.isArray(obj)) {
1910
+ const items = obj.map((item) => {
1911
+ const formatted = formatTsObject(item, depth + 1);
1912
+ return `${" ".repeat(4 * (depth + 1))}${formatted},`;
1913
+ }).join("\n");
1914
+ return `[
1915
+ ${items}
1916
+ ${" ".repeat(4 * depth)}]`;
1917
+ }
1918
+ const entries = Object.entries(obj);
1919
+ const lines = entries.map(([key, value]) => {
1920
+ const formattedValue = formatTsObject(value, depth + 1);
1921
+ const keyStr = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key) ? key : `"${key}"`;
1922
+ return `${" ".repeat(4 * (depth + 1))}${keyStr}: ${formattedValue},`;
1923
+ }).join("\n");
1924
+ return `{
1925
+ ${lines}
1926
+ ${" ".repeat(4 * depth)}}`;
1927
+ }
1928
+ function indentString(str) {
1929
+ return str.split("\n").map((line) => " ".repeat(4) + line).join("\n");
1930
+ }
1931
+ function removeSuffix(str, suffix) {
1932
+ return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str;
2569
1933
  }
2570
1934
  // Annotate the CommonJS export names for ESM import in node:
2571
1935
  0 && (module.exports = {
2572
1936
  HfInference,
2573
- HfInferenceEndpoint,
2574
1937
  INFERENCE_PROVIDERS,
1938
+ InferenceClient,
1939
+ InferenceClientEndpoint,
2575
1940
  InferenceOutputError,
2576
1941
  audioClassification,
2577
1942
  audioToAudio,