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