@huggingface/tasks 0.16.7 → 0.17.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 (36) hide show
  1. package/dist/commonjs/index.d.ts +1 -3
  2. package/dist/commonjs/index.d.ts.map +1 -1
  3. package/dist/commonjs/index.js +5 -15
  4. package/dist/commonjs/snippets/index.d.ts +2 -5
  5. package/dist/commonjs/snippets/index.d.ts.map +1 -1
  6. package/dist/commonjs/snippets/index.js +2 -21
  7. package/dist/esm/index.d.ts +1 -3
  8. package/dist/esm/index.d.ts.map +1 -1
  9. package/dist/esm/index.js +1 -2
  10. package/dist/esm/snippets/index.d.ts +2 -5
  11. package/dist/esm/snippets/index.d.ts.map +1 -1
  12. package/dist/esm/snippets/index.js +2 -5
  13. package/package.json +1 -1
  14. package/src/index.ts +7 -3
  15. package/src/snippets/index.ts +2 -6
  16. package/dist/commonjs/snippets/curl.d.ts +0 -17
  17. package/dist/commonjs/snippets/curl.d.ts.map +0 -1
  18. package/dist/commonjs/snippets/curl.js +0 -129
  19. package/dist/commonjs/snippets/js.d.ts +0 -21
  20. package/dist/commonjs/snippets/js.d.ts.map +0 -1
  21. package/dist/commonjs/snippets/js.js +0 -413
  22. package/dist/commonjs/snippets/python.d.ts +0 -23
  23. package/dist/commonjs/snippets/python.d.ts.map +0 -1
  24. package/dist/commonjs/snippets/python.js +0 -435
  25. package/dist/esm/snippets/curl.d.ts +0 -17
  26. package/dist/esm/snippets/curl.d.ts.map +0 -1
  27. package/dist/esm/snippets/curl.js +0 -121
  28. package/dist/esm/snippets/js.d.ts +0 -21
  29. package/dist/esm/snippets/js.d.ts.map +0 -1
  30. package/dist/esm/snippets/js.js +0 -401
  31. package/dist/esm/snippets/python.d.ts +0 -23
  32. package/dist/esm/snippets/python.d.ts.map +0 -1
  33. package/dist/esm/snippets/python.js +0 -421
  34. package/src/snippets/curl.ts +0 -173
  35. package/src/snippets/js.ts +0 -471
  36. package/src/snippets/python.ts +0 -483
@@ -1,435 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.pythonSnippets = exports.snippetDocumentQuestionAnswering = exports.snippetTextToAudio = exports.snippetTabular = exports.snippetTextToVideo = exports.snippetTextToImage = exports.snippetFile = exports.snippetBasic = exports.snippetZeroShotImageClassification = exports.snippetZeroShotClassification = exports.snippetConversational = void 0;
4
- exports.getPythonInferenceSnippet = getPythonInferenceSnippet;
5
- const inference_providers_js_1 = require("../inference-providers.js");
6
- const common_js_1 = require("./common.js");
7
- const inputs_js_1 = require("./inputs.js");
8
- const HFH_INFERENCE_CLIENT_METHODS = {
9
- "audio-classification": "audio_classification",
10
- "audio-to-audio": "audio_to_audio",
11
- "automatic-speech-recognition": "automatic_speech_recognition",
12
- "text-to-speech": "text_to_speech",
13
- "image-classification": "image_classification",
14
- "image-segmentation": "image_segmentation",
15
- "image-to-image": "image_to_image",
16
- "image-to-text": "image_to_text",
17
- "object-detection": "object_detection",
18
- "text-to-image": "text_to_image",
19
- "text-to-video": "text_to_video",
20
- "zero-shot-image-classification": "zero_shot_image_classification",
21
- "document-question-answering": "document_question_answering",
22
- "visual-question-answering": "visual_question_answering",
23
- "feature-extraction": "feature_extraction",
24
- "fill-mask": "fill_mask",
25
- "question-answering": "question_answering",
26
- "sentence-similarity": "sentence_similarity",
27
- summarization: "summarization",
28
- "table-question-answering": "table_question_answering",
29
- "text-classification": "text_classification",
30
- "text-generation": "text_generation",
31
- "token-classification": "token_classification",
32
- translation: "translation",
33
- "zero-shot-classification": "zero_shot_classification",
34
- "tabular-classification": "tabular_classification",
35
- "tabular-regression": "tabular_regression",
36
- };
37
- const snippetImportInferenceClient = (accessToken, provider) => `\
38
- from huggingface_hub import InferenceClient
39
-
40
- client = InferenceClient(
41
- provider="${provider}",
42
- api_key="${accessToken || "{API_TOKEN}"}"
43
- )`;
44
- const snippetConversational = (model, accessToken, provider, providerModelId, opts) => {
45
- const streaming = opts?.streaming ?? true;
46
- const exampleMessages = (0, inputs_js_1.getModelInputSnippet)(model);
47
- const messages = opts?.messages ?? exampleMessages;
48
- const messagesStr = (0, common_js_1.stringifyMessages)(messages, { attributeKeyQuotes: true });
49
- const config = {
50
- ...(opts?.temperature ? { temperature: opts.temperature } : undefined),
51
- max_tokens: opts?.max_tokens ?? 500,
52
- ...(opts?.top_p ? { top_p: opts.top_p } : undefined),
53
- };
54
- const configStr = (0, common_js_1.stringifyGenerationConfig)(config, {
55
- indent: "\n\t",
56
- attributeValueConnector: "=",
57
- });
58
- if (streaming) {
59
- return [
60
- {
61
- client: "huggingface_hub",
62
- content: `\
63
- ${snippetImportInferenceClient(accessToken, provider)}
64
-
65
- messages = ${messagesStr}
66
-
67
- stream = client.chat.completions.create(
68
- model="${model.id}",
69
- messages=messages,
70
- ${configStr}
71
- stream=True
72
- )
73
-
74
- for chunk in stream:
75
- print(chunk.choices[0].delta.content, end="")`,
76
- },
77
- {
78
- client: "openai",
79
- content: `\
80
- from openai import OpenAI
81
-
82
- client = OpenAI(
83
- base_url="${(0, inference_providers_js_1.openAIbaseUrl)(provider)}",
84
- api_key="${accessToken || "{API_TOKEN}"}"
85
- )
86
-
87
- messages = ${messagesStr}
88
-
89
- stream = client.chat.completions.create(
90
- model="${providerModelId ?? model.id}",
91
- messages=messages,
92
- ${configStr}
93
- stream=True
94
- )
95
-
96
- for chunk in stream:
97
- print(chunk.choices[0].delta.content, end="")`,
98
- },
99
- ];
100
- }
101
- else {
102
- return [
103
- {
104
- client: "huggingface_hub",
105
- content: `\
106
- ${snippetImportInferenceClient(accessToken, provider)}
107
-
108
- messages = ${messagesStr}
109
-
110
- completion = client.chat.completions.create(
111
- model="${model.id}",
112
- messages=messages,
113
- ${configStr}
114
- )
115
-
116
- print(completion.choices[0].message)`,
117
- },
118
- {
119
- client: "openai",
120
- content: `\
121
- from openai import OpenAI
122
-
123
- client = OpenAI(
124
- base_url="${(0, inference_providers_js_1.openAIbaseUrl)(provider)}",
125
- api_key="${accessToken || "{API_TOKEN}"}"
126
- )
127
-
128
- messages = ${messagesStr}
129
-
130
- completion = client.chat.completions.create(
131
- model="${providerModelId ?? model.id}",
132
- messages=messages,
133
- ${configStr}
134
- )
135
-
136
- print(completion.choices[0].message)`,
137
- },
138
- ];
139
- }
140
- };
141
- exports.snippetConversational = snippetConversational;
142
- const snippetZeroShotClassification = (model) => {
143
- return [
144
- {
145
- client: "requests",
146
- content: `\
147
- def query(payload):
148
- response = requests.post(API_URL, headers=headers, json=payload)
149
- return response.json()
150
-
151
- output = query({
152
- "inputs": ${(0, inputs_js_1.getModelInputSnippet)(model)},
153
- "parameters": {"candidate_labels": ["refund", "legal", "faq"]},
154
- })`,
155
- },
156
- ];
157
- };
158
- exports.snippetZeroShotClassification = snippetZeroShotClassification;
159
- const snippetZeroShotImageClassification = (model) => {
160
- return [
161
- {
162
- client: "requests",
163
- content: `\
164
- def query(data):
165
- with open(data["image_path"], "rb") as f:
166
- img = f.read()
167
- payload={
168
- "parameters": data["parameters"],
169
- "inputs": base64.b64encode(img).decode("utf-8")
170
- }
171
- response = requests.post(API_URL, headers=headers, json=payload)
172
- return response.json()
173
-
174
- output = query({
175
- "image_path": ${(0, inputs_js_1.getModelInputSnippet)(model)},
176
- "parameters": {"candidate_labels": ["cat", "dog", "llama"]},
177
- })`,
178
- },
179
- ];
180
- };
181
- exports.snippetZeroShotImageClassification = snippetZeroShotImageClassification;
182
- const snippetBasic = (model, accessToken, provider) => {
183
- return [
184
- ...(model.pipeline_tag && model.pipeline_tag in HFH_INFERENCE_CLIENT_METHODS
185
- ? [
186
- {
187
- client: "huggingface_hub",
188
- content: `\
189
- ${snippetImportInferenceClient(accessToken, provider)}
190
-
191
- result = client.${HFH_INFERENCE_CLIENT_METHODS[model.pipeline_tag]}(
192
- model="${model.id}",
193
- inputs=${(0, inputs_js_1.getModelInputSnippet)(model)},
194
- provider="${provider}",
195
- )
196
-
197
- print(result)
198
- `,
199
- },
200
- ]
201
- : []),
202
- {
203
- client: "requests",
204
- content: `\
205
- def query(payload):
206
- response = requests.post(API_URL, headers=headers, json=payload)
207
- return response.json()
208
-
209
- output = query({
210
- "inputs": ${(0, inputs_js_1.getModelInputSnippet)(model)},
211
- })`,
212
- },
213
- ];
214
- };
215
- exports.snippetBasic = snippetBasic;
216
- const snippetFile = (model) => {
217
- return [
218
- {
219
- client: "requests",
220
- content: `\
221
- def query(filename):
222
- with open(filename, "rb") as f:
223
- data = f.read()
224
- response = requests.post(API_URL, headers=headers, data=data)
225
- return response.json()
226
-
227
- output = query(${(0, inputs_js_1.getModelInputSnippet)(model)})`,
228
- },
229
- ];
230
- };
231
- exports.snippetFile = snippetFile;
232
- const snippetTextToImage = (model, accessToken, provider, providerModelId) => {
233
- return [
234
- {
235
- client: "huggingface_hub",
236
- content: `\
237
- ${snippetImportInferenceClient(accessToken, provider)}
238
-
239
- # output is a PIL.Image object
240
- image = client.text_to_image(
241
- ${(0, inputs_js_1.getModelInputSnippet)(model)},
242
- model="${model.id}"
243
- )`,
244
- },
245
- ...(provider === "fal-ai"
246
- ? [
247
- {
248
- client: "fal-client",
249
- content: `\
250
- import fal_client
251
-
252
- result = fal_client.subscribe(
253
- "${providerModelId ?? model.id}",
254
- arguments={
255
- "prompt": ${(0, inputs_js_1.getModelInputSnippet)(model)},
256
- },
257
- )
258
- print(result)
259
- `,
260
- },
261
- ]
262
- : []),
263
- ...(provider === "hf-inference"
264
- ? [
265
- {
266
- client: "requests",
267
- content: `\
268
- def query(payload):
269
- response = requests.post(API_URL, headers=headers, json=payload)
270
- return response.content
271
-
272
- image_bytes = query({
273
- "inputs": ${(0, inputs_js_1.getModelInputSnippet)(model)},
274
- })
275
-
276
- # You can access the image with PIL.Image for example
277
- import io
278
- from PIL import Image
279
- image = Image.open(io.BytesIO(image_bytes))`,
280
- },
281
- ]
282
- : []),
283
- ];
284
- };
285
- exports.snippetTextToImage = snippetTextToImage;
286
- const snippetTextToVideo = (model, accessToken, provider) => {
287
- return ["fal-ai", "replicate"].includes(provider)
288
- ? [
289
- {
290
- client: "huggingface_hub",
291
- content: `\
292
- ${snippetImportInferenceClient(accessToken, provider)}
293
-
294
- video = client.text_to_video(
295
- ${(0, inputs_js_1.getModelInputSnippet)(model)},
296
- model="${model.id}"
297
- )`,
298
- },
299
- ]
300
- : [];
301
- };
302
- exports.snippetTextToVideo = snippetTextToVideo;
303
- const snippetTabular = (model) => {
304
- return [
305
- {
306
- client: "requests",
307
- content: `\
308
- def query(payload):
309
- response = requests.post(API_URL, headers=headers, json=payload)
310
- return response.content
311
-
312
- response = query({
313
- "inputs": {"data": ${(0, inputs_js_1.getModelInputSnippet)(model)}},
314
- })`,
315
- },
316
- ];
317
- };
318
- exports.snippetTabular = snippetTabular;
319
- const snippetTextToAudio = (model) => {
320
- // Transformers TTS pipeline and api-inference-community (AIC) pipeline outputs are diverged
321
- // with the latest update to inference-api (IA).
322
- // Transformers IA returns a byte object (wav file), whereas AIC returns wav and sampling_rate.
323
- if (model.library_name === "transformers") {
324
- return [
325
- {
326
- client: "requests",
327
- content: `\
328
- def query(payload):
329
- response = requests.post(API_URL, headers=headers, json=payload)
330
- return response.content
331
-
332
- audio_bytes = query({
333
- "inputs": ${(0, inputs_js_1.getModelInputSnippet)(model)},
334
- })
335
- # You can access the audio with IPython.display for example
336
- from IPython.display import Audio
337
- Audio(audio_bytes)`,
338
- },
339
- ];
340
- }
341
- else {
342
- return [
343
- {
344
- client: "requests",
345
- content: `\
346
- def query(payload):
347
- response = requests.post(API_URL, headers=headers, json=payload)
348
- return response.json()
349
-
350
- audio, sampling_rate = query({
351
- "inputs": ${(0, inputs_js_1.getModelInputSnippet)(model)},
352
- })
353
- # You can access the audio with IPython.display for example
354
- from IPython.display import Audio
355
- Audio(audio, rate=sampling_rate)`,
356
- },
357
- ];
358
- }
359
- };
360
- exports.snippetTextToAudio = snippetTextToAudio;
361
- const snippetDocumentQuestionAnswering = (model) => {
362
- return [
363
- {
364
- client: "requests",
365
- content: `\
366
- def query(payload):
367
- with open(payload["image"], "rb") as f:
368
- img = f.read()
369
- payload["image"] = base64.b64encode(img).decode("utf-8")
370
- response = requests.post(API_URL, headers=headers, json=payload)
371
- return response.json()
372
-
373
- output = query({
374
- "inputs": ${(0, inputs_js_1.getModelInputSnippet)(model)},
375
- })`,
376
- },
377
- ];
378
- };
379
- exports.snippetDocumentQuestionAnswering = snippetDocumentQuestionAnswering;
380
- exports.pythonSnippets = {
381
- // Same order as in tasks/src/pipelines.ts
382
- "text-classification": exports.snippetBasic,
383
- "token-classification": exports.snippetBasic,
384
- "table-question-answering": exports.snippetBasic,
385
- "question-answering": exports.snippetBasic,
386
- "zero-shot-classification": exports.snippetZeroShotClassification,
387
- translation: exports.snippetBasic,
388
- summarization: exports.snippetBasic,
389
- "feature-extraction": exports.snippetBasic,
390
- "text-generation": exports.snippetBasic,
391
- "text2text-generation": exports.snippetBasic,
392
- "image-text-to-text": exports.snippetConversational,
393
- "fill-mask": exports.snippetBasic,
394
- "sentence-similarity": exports.snippetBasic,
395
- "automatic-speech-recognition": exports.snippetFile,
396
- "text-to-image": exports.snippetTextToImage,
397
- "text-to-video": exports.snippetTextToVideo,
398
- "text-to-speech": exports.snippetTextToAudio,
399
- "text-to-audio": exports.snippetTextToAudio,
400
- "audio-to-audio": exports.snippetFile,
401
- "audio-classification": exports.snippetFile,
402
- "image-classification": exports.snippetFile,
403
- "tabular-regression": exports.snippetTabular,
404
- "tabular-classification": exports.snippetTabular,
405
- "object-detection": exports.snippetFile,
406
- "image-segmentation": exports.snippetFile,
407
- "document-question-answering": exports.snippetDocumentQuestionAnswering,
408
- "image-to-text": exports.snippetFile,
409
- "zero-shot-image-classification": exports.snippetZeroShotImageClassification,
410
- };
411
- function getPythonInferenceSnippet(model, accessToken, provider, providerModelId, opts) {
412
- if (model.tags.includes("conversational")) {
413
- // Conversational model detected, so we display a code snippet that features the Messages API
414
- return (0, exports.snippetConversational)(model, accessToken, provider, providerModelId, opts);
415
- }
416
- else {
417
- const snippets = model.pipeline_tag && model.pipeline_tag in exports.pythonSnippets
418
- ? exports.pythonSnippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId) ?? []
419
- : [];
420
- return snippets.map((snippet) => {
421
- return {
422
- ...snippet,
423
- content: snippet.client === "requests"
424
- ? `\
425
- import requests
426
-
427
- API_URL = "${(0, inference_providers_js_1.openAIbaseUrl)(provider)}"
428
- headers = {"Authorization": ${accessToken ? `"Bearer ${accessToken}"` : `f"Bearer {API_TOKEN}"`}}
429
-
430
- ${snippet.content}`
431
- : snippet.content,
432
- };
433
- });
434
- }
435
- }
@@ -1,17 +0,0 @@
1
- import { type SnippetInferenceProvider } from "../inference-providers.js";
2
- import type { PipelineType } from "../pipelines.js";
3
- import type { ChatCompletionInputMessage, GenerationParameters } from "../tasks/index.js";
4
- import type { InferenceSnippet, ModelDataMinimal } from "./types.js";
5
- export declare const snippetBasic: (model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider) => InferenceSnippet[];
6
- export declare const snippetTextGeneration: (model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider, providerModelId?: string, opts?: {
7
- streaming?: boolean;
8
- messages?: ChatCompletionInputMessage[];
9
- temperature?: GenerationParameters["temperature"];
10
- max_tokens?: GenerationParameters["max_tokens"];
11
- top_p?: GenerationParameters["top_p"];
12
- }) => InferenceSnippet[];
13
- export declare const snippetZeroShotClassification: (model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider) => InferenceSnippet[];
14
- export declare const snippetFile: (model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider) => InferenceSnippet[];
15
- export declare const curlSnippets: Partial<Record<PipelineType, (model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider, providerModelId?: string, opts?: Record<string, unknown>) => InferenceSnippet[]>>;
16
- export declare function getCurlInferenceSnippet(model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider, providerModelId?: string, opts?: Record<string, unknown>): InferenceSnippet[];
17
- //# sourceMappingURL=curl.d.ts.map
@@ -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gB,EAAE,CACvB,CA0BD,CAAC;AAEF,wBAAgB,uBAAuB,CACtC,KAAK,EAAE,gBAAgB,EACvB,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,wBAAwB,EAClC,eAAe,CAAC,EAAE,MAAM,EACxB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC5B,gBAAgB,EAAE,CAIpB"}
@@ -1,121 +0,0 @@
1
- import { HF_HUB_INFERENCE_PROXY_TEMPLATE } from "../inference-providers.js";
2
- import { stringifyGenerationConfig, stringifyMessages } from "./common.js";
3
- import { getModelInputSnippet } from "./inputs.js";
4
- export const snippetBasic = (model, accessToken, provider) => {
5
- if (provider !== "hf-inference") {
6
- return [];
7
- }
8
- return [
9
- {
10
- client: "curl",
11
- content: `\
12
- curl https://router.huggingface.co/hf-inference/models/${model.id} \\
13
- -X POST \\
14
- -d '{"inputs": ${getModelInputSnippet(model, true)}}' \\
15
- -H 'Content-Type: application/json' \\
16
- -H 'Authorization: Bearer ${accessToken || `{API_TOKEN}`}'`,
17
- },
18
- ];
19
- };
20
- export const snippetTextGeneration = (model, accessToken, provider, providerModelId, opts) => {
21
- if (model.tags.includes("conversational")) {
22
- const baseUrl = provider === "hf-inference"
23
- ? `https://router.huggingface.co/hf-inference/models/${model.id}/v1/chat/completions`
24
- : HF_HUB_INFERENCE_PROXY_TEMPLATE.replace("{{PROVIDER}}", provider) + "/v1/chat/completions";
25
- const modelId = providerModelId ?? model.id;
26
- // Conversational model detected, so we display a code snippet that features the Messages API
27
- const streaming = opts?.streaming ?? true;
28
- const exampleMessages = getModelInputSnippet(model);
29
- const messages = opts?.messages ?? exampleMessages;
30
- const config = {
31
- ...(opts?.temperature ? { temperature: opts.temperature } : undefined),
32
- max_tokens: opts?.max_tokens ?? 500,
33
- ...(opts?.top_p ? { top_p: opts.top_p } : undefined),
34
- };
35
- return [
36
- {
37
- client: "curl",
38
- content: `curl '${baseUrl}' \\
39
- -H 'Authorization: Bearer ${accessToken || `{API_TOKEN}`}' \\
40
- -H 'Content-Type: application/json' \\
41
- --data '{
42
- "model": "${modelId}",
43
- "messages": ${stringifyMessages(messages, {
44
- indent: "\t",
45
- attributeKeyQuotes: true,
46
- customContentEscaper: (str) => str.replace(/'/g, "'\\''"),
47
- })},
48
- ${stringifyGenerationConfig(config, {
49
- indent: "\n ",
50
- attributeKeyQuotes: true,
51
- attributeValueConnector: ": ",
52
- })}
53
- "stream": ${!!streaming}
54
- }'`,
55
- },
56
- ];
57
- }
58
- else {
59
- return snippetBasic(model, accessToken, provider);
60
- }
61
- };
62
- export const snippetZeroShotClassification = (model, accessToken, provider) => {
63
- if (provider !== "hf-inference") {
64
- return [];
65
- }
66
- return [
67
- {
68
- client: "curl",
69
- content: `curl https://router.huggingface.co/hf-inference/models/${model.id} \\
70
- -X POST \\
71
- -d '{"inputs": ${getModelInputSnippet(model, true)}, "parameters": {"candidate_labels": ["refund", "legal", "faq"]}}' \\
72
- -H 'Content-Type: application/json' \\
73
- -H 'Authorization: Bearer ${accessToken || `{API_TOKEN}`}'`,
74
- },
75
- ];
76
- };
77
- export const snippetFile = (model, accessToken, provider) => {
78
- if (provider !== "hf-inference") {
79
- return [];
80
- }
81
- return [
82
- {
83
- client: "curl",
84
- content: `curl https://router.huggingface.co/hf-inference/models/${model.id} \\
85
- -X POST \\
86
- --data-binary '@${getModelInputSnippet(model, true, true)}' \\
87
- -H 'Authorization: Bearer ${accessToken || `{API_TOKEN}`}'`,
88
- },
89
- ];
90
- };
91
- export const curlSnippets = {
92
- // Same order as in tasks/src/pipelines.ts
93
- "text-classification": snippetBasic,
94
- "token-classification": snippetBasic,
95
- "table-question-answering": snippetBasic,
96
- "question-answering": snippetBasic,
97
- "zero-shot-classification": snippetZeroShotClassification,
98
- translation: snippetBasic,
99
- summarization: snippetBasic,
100
- "feature-extraction": snippetBasic,
101
- "text-generation": snippetTextGeneration,
102
- "image-text-to-text": snippetTextGeneration,
103
- "text2text-generation": snippetBasic,
104
- "fill-mask": snippetBasic,
105
- "sentence-similarity": snippetBasic,
106
- "automatic-speech-recognition": snippetFile,
107
- "text-to-image": snippetBasic,
108
- "text-to-speech": snippetBasic,
109
- "text-to-audio": snippetBasic,
110
- "audio-to-audio": snippetFile,
111
- "audio-classification": snippetFile,
112
- "image-classification": snippetFile,
113
- "image-to-text": snippetFile,
114
- "object-detection": snippetFile,
115
- "image-segmentation": snippetFile,
116
- };
117
- export function getCurlInferenceSnippet(model, accessToken, provider, providerModelId, opts) {
118
- return model.pipeline_tag && model.pipeline_tag in curlSnippets
119
- ? curlSnippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId, opts) ?? []
120
- : [];
121
- }
@@ -1,21 +0,0 @@
1
- import { type SnippetInferenceProvider } from "../inference-providers.js";
2
- import type { PipelineType } from "../pipelines.js";
3
- import type { ChatCompletionInputMessage, GenerationParameters } from "../tasks/index.js";
4
- import type { InferenceSnippet, ModelDataMinimal } from "./types.js";
5
- export declare const snippetBasic: (model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider) => InferenceSnippet[];
6
- export declare const snippetTextGeneration: (model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider, providerModelId?: string, opts?: {
7
- streaming?: boolean;
8
- messages?: ChatCompletionInputMessage[];
9
- temperature?: GenerationParameters["temperature"];
10
- max_tokens?: GenerationParameters["max_tokens"];
11
- top_p?: GenerationParameters["top_p"];
12
- }) => InferenceSnippet[];
13
- export declare const snippetZeroShotClassification: (model: ModelDataMinimal, accessToken: string) => InferenceSnippet[];
14
- export declare const snippetTextToImage: (model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider) => InferenceSnippet[];
15
- export declare const snippetTextToVideo: (model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider) => InferenceSnippet[];
16
- export declare const snippetTextToAudio: (model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider) => InferenceSnippet[];
17
- export declare const snippetAutomaticSpeechRecognition: (model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider) => InferenceSnippet[];
18
- export declare const snippetFile: (model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider) => InferenceSnippet[];
19
- export declare const jsSnippets: Partial<Record<PipelineType, (model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider, providerModelId?: string, opts?: Record<string, unknown>) => InferenceSnippet[]>>;
20
- export declare function getJsInferenceSnippet(model: ModelDataMinimal, accessToken: string, provider: SnippetInferenceProvider, providerModelId?: string, opts?: Record<string, unknown>): InferenceSnippet[];
21
- //# sourceMappingURL=js.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"js.d.ts","sourceRoot":"","sources":["../../../src/snippets/js.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,KAAK,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AACzF,OAAO,KAAK,EAAE,YAAY,EAAc,MAAM,iBAAiB,CAAC;AAChE,OAAO,KAAK,EAAE,0BAA0B,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAG1F,OAAO,KAAK,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAgBrE,eAAO,MAAM,YAAY,UACjB,gBAAgB,eACV,MAAM,YACT,wBAAwB,KAChC,gBAAgB,EA8ClB,CAAC;AAEF,eAAO,MAAM,qBAAqB,UAC1B,gBAAgB,eACV,MAAM,YACT,wBAAwB,oBAChB,MAAM,SACjB;IACN,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,QAAQ,CAAC,EAAE,0BAA0B,EAAE,CAAC;IACxC,WAAW,CAAC,EAAE,oBAAoB,CAAC,aAAa,CAAC,CAAC;IAClD,UAAU,CAAC,EAAE,oBAAoB,CAAC,YAAY,CAAC,CAAC;IAChD,KAAK,CAAC,EAAE,oBAAoB,CAAC,OAAO,CAAC,CAAC;CACtC,KACC,gBAAgB,EA+GlB,CAAC;AAEF,eAAO,MAAM,6BAA6B,UAAW,gBAAgB,eAAe,MAAM,KA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