@huggingface/tasks 0.5.0 → 0.5.2

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.
package/dist/index.d.ts CHANGED
@@ -500,7 +500,7 @@ interface AddedToken {
500
500
  single_word?: boolean;
501
501
  }
502
502
  type SpecialTokensMap = {
503
- [key in (typeof SPECIAL_TOKENS_ATTRIBUTES)[number]]?: string | AddedToken;
503
+ [key in (typeof SPECIAL_TOKENS_ATTRIBUTES)[number]]?: string | AddedToken | null;
504
504
  };
505
505
  /**
506
506
  * Public interface for tokenizer config
@@ -3149,7 +3149,14 @@ interface TaskData {
3149
3149
  }
3150
3150
  type TaskDataCustom = Omit<TaskData, "id" | "label" | "libraries">;
3151
3151
 
3152
- declare function getModelInputSnippet(model: ModelData, noWrap?: boolean, noQuotes?: boolean): string;
3152
+ /**
3153
+ * Minimal model data required for snippets.
3154
+ *
3155
+ * Add more fields as needed.
3156
+ */
3157
+ type ModelDataMinimal = Pick<ModelData, "id" | "pipeline_tag" | "mask_token" | "library_name">;
3158
+
3159
+ declare function getModelInputSnippet(model: ModelDataMinimal, noWrap?: boolean, noQuotes?: boolean): string;
3153
3160
 
3154
3161
  declare const inputs_getModelInputSnippet: typeof getModelInputSnippet;
3155
3162
  declare namespace inputs {
@@ -3158,12 +3165,12 @@ declare namespace inputs {
3158
3165
  };
3159
3166
  }
3160
3167
 
3161
- declare const snippetBasic$2: (model: ModelData, accessToken: string) => string;
3162
- declare const snippetZeroShotClassification$2: (model: ModelData, accessToken: string) => string;
3163
- declare const snippetFile$2: (model: ModelData, accessToken: string) => string;
3164
- declare const curlSnippets: Partial<Record<PipelineType, (model: ModelData, accessToken: string) => string>>;
3165
- declare function getCurlInferenceSnippet(model: ModelData, accessToken: string): string;
3166
- declare function hasCurlInferenceSnippet(model: ModelData): boolean;
3168
+ declare const snippetBasic$2: (model: ModelDataMinimal, accessToken: string) => string;
3169
+ declare const snippetZeroShotClassification$2: (model: ModelDataMinimal, accessToken: string) => string;
3170
+ declare const snippetFile$2: (model: ModelDataMinimal, accessToken: string) => string;
3171
+ declare const curlSnippets: Partial<Record<PipelineType, (model: ModelDataMinimal, accessToken: string) => string>>;
3172
+ declare function getCurlInferenceSnippet(model: ModelDataMinimal, accessToken: string): string;
3173
+ declare function hasCurlInferenceSnippet(model: Pick<ModelDataMinimal, "pipeline_tag">): boolean;
3167
3174
 
3168
3175
  declare const curl_curlSnippets: typeof curlSnippets;
3169
3176
  declare const curl_getCurlInferenceSnippet: typeof getCurlInferenceSnippet;
@@ -3179,17 +3186,17 @@ declare namespace curl {
3179
3186
  };
3180
3187
  }
3181
3188
 
3182
- declare const snippetZeroShotClassification$1: (model: ModelData) => string;
3183
- declare const snippetZeroShotImageClassification: (model: ModelData) => string;
3184
- declare const snippetBasic$1: (model: ModelData) => string;
3185
- declare const snippetFile$1: (model: ModelData) => string;
3186
- declare const snippetTextToImage$1: (model: ModelData) => string;
3187
- declare const snippetTabular: (model: ModelData) => string;
3188
- declare const snippetTextToAudio$1: (model: ModelData) => string;
3189
- declare const snippetDocumentQuestionAnswering: (model: ModelData) => string;
3190
- declare const pythonSnippets: Partial<Record<PipelineType, (model: ModelData) => string>>;
3191
- declare function getPythonInferenceSnippet(model: ModelData, accessToken: string): string;
3192
- declare function hasPythonInferenceSnippet(model: ModelData): boolean;
3189
+ declare const snippetZeroShotClassification$1: (model: ModelDataMinimal) => string;
3190
+ declare const snippetZeroShotImageClassification: (model: ModelDataMinimal) => string;
3191
+ declare const snippetBasic$1: (model: ModelDataMinimal) => string;
3192
+ declare const snippetFile$1: (model: ModelDataMinimal) => string;
3193
+ declare const snippetTextToImage$1: (model: ModelDataMinimal) => string;
3194
+ declare const snippetTabular: (model: ModelDataMinimal) => string;
3195
+ declare const snippetTextToAudio$1: (model: ModelDataMinimal) => string;
3196
+ declare const snippetDocumentQuestionAnswering: (model: ModelDataMinimal) => string;
3197
+ declare const pythonSnippets: Partial<Record<PipelineType, (model: ModelDataMinimal) => string>>;
3198
+ declare function getPythonInferenceSnippet(model: ModelDataMinimal, accessToken: string): string;
3199
+ declare function hasPythonInferenceSnippet(model: ModelDataMinimal): boolean;
3193
3200
 
3194
3201
  declare const python_getPythonInferenceSnippet: typeof getPythonInferenceSnippet;
3195
3202
  declare const python_hasPythonInferenceSnippet: typeof hasPythonInferenceSnippet;
@@ -3213,14 +3220,14 @@ declare namespace python {
3213
3220
  };
3214
3221
  }
3215
3222
 
3216
- declare const snippetBasic: (model: ModelData, accessToken: string) => string;
3217
- declare const snippetZeroShotClassification: (model: ModelData, accessToken: string) => string;
3218
- declare const snippetTextToImage: (model: ModelData, accessToken: string) => string;
3219
- declare const snippetTextToAudio: (model: ModelData, accessToken: string) => string;
3220
- declare const snippetFile: (model: ModelData, accessToken: string) => string;
3221
- declare const jsSnippets: Partial<Record<PipelineType, (model: ModelData, accessToken: string) => string>>;
3222
- declare function getJsInferenceSnippet(model: ModelData, accessToken: string): string;
3223
- declare function hasJsInferenceSnippet(model: ModelData): boolean;
3223
+ declare const snippetBasic: (model: ModelDataMinimal, accessToken: string) => string;
3224
+ declare const snippetZeroShotClassification: (model: ModelDataMinimal, accessToken: string) => string;
3225
+ declare const snippetTextToImage: (model: ModelDataMinimal, accessToken: string) => string;
3226
+ declare const snippetTextToAudio: (model: ModelDataMinimal, accessToken: string) => string;
3227
+ declare const snippetFile: (model: ModelDataMinimal, accessToken: string) => string;
3228
+ declare const jsSnippets: Partial<Record<PipelineType, (model: ModelDataMinimal, accessToken: string) => string>>;
3229
+ declare function getJsInferenceSnippet(model: ModelDataMinimal, accessToken: string): string;
3230
+ declare function hasJsInferenceSnippet(model: ModelDataMinimal): boolean;
3224
3231
 
3225
3232
  declare const js_getJsInferenceSnippet: typeof getJsInferenceSnippet;
3226
3233
  declare const js_hasJsInferenceSnippet: typeof hasJsInferenceSnippet;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@huggingface/tasks",
3
3
  "packageManager": "pnpm@8.10.5",
4
- "version": "0.5.0",
4
+ "version": "0.5.2",
5
5
  "description": "List of ML tasks for huggingface.co/tasks",
6
6
  "repository": "https://github.com/huggingface/huggingface.js.git",
7
7
  "publishConfig": {
@@ -1,8 +1,8 @@
1
- import type { ModelData } from "../model-data.js";
2
1
  import type { PipelineType } from "../pipelines.js";
3
2
  import { getModelInputSnippet } from "./inputs.js";
3
+ import type { ModelDataMinimal } from "./types.js";
4
4
 
5
- export const snippetBasic = (model: ModelData, accessToken: string): string =>
5
+ export const snippetBasic = (model: ModelDataMinimal, accessToken: string): string =>
6
6
  `curl https://api-inference.huggingface.co/models/${model.id} \\
7
7
  -X POST \\
8
8
  -d '{"inputs": ${getModelInputSnippet(model, true)}}' \\
@@ -10,7 +10,7 @@ export const snippetBasic = (model: ModelData, accessToken: string): string =>
10
10
  -H "Authorization: Bearer ${accessToken || `{API_TOKEN}`}"
11
11
  `;
12
12
 
13
- export const snippetZeroShotClassification = (model: ModelData, accessToken: string): string =>
13
+ export const snippetZeroShotClassification = (model: ModelDataMinimal, accessToken: string): string =>
14
14
  `curl https://api-inference.huggingface.co/models/${model.id} \\
15
15
  -X POST \\
16
16
  -d '{"inputs": ${getModelInputSnippet(model, true)}, "parameters": {"candidate_labels": ["refund", "legal", "faq"]}}' \\
@@ -18,14 +18,14 @@ export const snippetZeroShotClassification = (model: ModelData, accessToken: str
18
18
  -H "Authorization: Bearer ${accessToken || `{API_TOKEN}`}"
19
19
  `;
20
20
 
21
- export const snippetFile = (model: ModelData, accessToken: string): string =>
21
+ export const snippetFile = (model: ModelDataMinimal, accessToken: string): string =>
22
22
  `curl https://api-inference.huggingface.co/models/${model.id} \\
23
23
  -X POST \\
24
24
  --data-binary '@${getModelInputSnippet(model, true, true)}' \\
25
25
  -H "Authorization: Bearer ${accessToken || `{API_TOKEN}`}"
26
26
  `;
27
27
 
28
- export const curlSnippets: Partial<Record<PipelineType, (model: ModelData, accessToken: string) => string>> = {
28
+ export const curlSnippets: Partial<Record<PipelineType, (model: ModelDataMinimal, accessToken: string) => string>> = {
29
29
  // Same order as in js/src/lib/interfaces/Types.ts
30
30
  "text-classification": snippetBasic,
31
31
  "token-classification": snippetBasic,
@@ -51,12 +51,12 @@ export const curlSnippets: Partial<Record<PipelineType, (model: ModelData, acces
51
51
  "image-segmentation": snippetFile,
52
52
  };
53
53
 
54
- export function getCurlInferenceSnippet(model: ModelData, accessToken: string): string {
54
+ export function getCurlInferenceSnippet(model: ModelDataMinimal, accessToken: string): string {
55
55
  return model.pipeline_tag && model.pipeline_tag in curlSnippets
56
56
  ? curlSnippets[model.pipeline_tag]?.(model, accessToken) ?? ""
57
57
  : "";
58
58
  }
59
59
 
60
- export function hasCurlInferenceSnippet(model: ModelData): boolean {
60
+ export function hasCurlInferenceSnippet(model: Pick<ModelDataMinimal, "pipeline_tag">): boolean {
61
61
  return !!model.pipeline_tag && model.pipeline_tag in curlSnippets;
62
62
  }
@@ -1,5 +1,5 @@
1
- import type { ModelData } from "../model-data";
2
1
  import type { PipelineType } from "../pipelines";
2
+ import type { ModelDataMinimal } from "./types";
3
3
 
4
4
  const inputsZeroShotClassification = () =>
5
5
  `"Hi, I recently bought a device from your company but it is not working as advertised and I would like to get reimbursed!"`;
@@ -44,7 +44,7 @@ const inputsTextGeneration = () => `"Can you please let us know more details abo
44
44
 
45
45
  const inputsText2TextGeneration = () => `"The answer to the universe is"`;
46
46
 
47
- const inputsFillMask = (model: ModelData) => `"The answer to the universe is ${model.mask_token}."`;
47
+ const inputsFillMask = (model: ModelDataMinimal) => `"The answer to the universe is ${model.mask_token}."`;
48
48
 
49
49
  const inputsSentenceSimilarity = () =>
50
50
  `{
@@ -84,7 +84,7 @@ const inputsTabularPrediction = () =>
84
84
  const inputsZeroShotImageClassification = () => `"cats.jpg"`;
85
85
 
86
86
  const modelInputSnippets: {
87
- [key in PipelineType]?: (model: ModelData) => string;
87
+ [key in PipelineType]?: (model: ModelDataMinimal) => string;
88
88
  } = {
89
89
  "audio-to-audio": inputsAudioToAudio,
90
90
  "audio-classification": inputsAudioClassification,
@@ -116,7 +116,7 @@ const modelInputSnippets: {
116
116
 
117
117
  // Use noWrap to put the whole snippet on a single line (removing new lines and tabulations)
118
118
  // Use noQuotes to strip quotes from start & end (example: "abc" -> abc)
119
- export function getModelInputSnippet(model: ModelData, noWrap = false, noQuotes = false): string {
119
+ export function getModelInputSnippet(model: ModelDataMinimal, noWrap = false, noQuotes = false): string {
120
120
  if (model.pipeline_tag) {
121
121
  const inputs = modelInputSnippets[model.pipeline_tag];
122
122
  if (inputs) {
@@ -1,8 +1,8 @@
1
- import type { ModelData } from "../model-data.js";
2
1
  import type { PipelineType } from "../pipelines.js";
3
2
  import { getModelInputSnippet } from "./inputs.js";
3
+ import type { ModelDataMinimal } from "./types.js";
4
4
 
5
- export const snippetBasic = (model: ModelData, accessToken: string): string =>
5
+ export const snippetBasic = (model: ModelDataMinimal, accessToken: string): string =>
6
6
  `async function query(data) {
7
7
  const response = await fetch(
8
8
  "https://api-inference.huggingface.co/models/${model.id}",
@@ -20,7 +20,7 @@ query({"inputs": ${getModelInputSnippet(model)}}).then((response) => {
20
20
  console.log(JSON.stringify(response));
21
21
  });`;
22
22
 
23
- export const snippetZeroShotClassification = (model: ModelData, accessToken: string): string =>
23
+ export const snippetZeroShotClassification = (model: ModelDataMinimal, accessToken: string): string =>
24
24
  `async function query(data) {
25
25
  const response = await fetch(
26
26
  "https://api-inference.huggingface.co/models/${model.id}",
@@ -40,7 +40,7 @@ query({"inputs": ${getModelInputSnippet(
40
40
  console.log(JSON.stringify(response));
41
41
  });`;
42
42
 
43
- export const snippetTextToImage = (model: ModelData, accessToken: string): string =>
43
+ export const snippetTextToImage = (model: ModelDataMinimal, accessToken: string): string =>
44
44
  `async function query(data) {
45
45
  const response = await fetch(
46
46
  "https://api-inference.huggingface.co/models/${model.id}",
@@ -57,7 +57,7 @@ query({"inputs": ${getModelInputSnippet(model)}}).then((response) => {
57
57
  // Use image
58
58
  });`;
59
59
 
60
- export const snippetTextToAudio = (model: ModelData, accessToken: string): string => {
60
+ export const snippetTextToAudio = (model: ModelDataMinimal, accessToken: string): string => {
61
61
  const commonSnippet = `async function query(data) {
62
62
  const response = await fetch(
63
63
  "https://api-inference.huggingface.co/models/${model.id}",
@@ -93,7 +93,7 @@ export const snippetTextToAudio = (model: ModelData, accessToken: string): strin
93
93
  }
94
94
  };
95
95
 
96
- export const snippetFile = (model: ModelData, accessToken: string): string =>
96
+ export const snippetFile = (model: ModelDataMinimal, accessToken: string): string =>
97
97
  `async function query(filename) {
98
98
  const data = fs.readFileSync(filename);
99
99
  const response = await fetch(
@@ -112,7 +112,7 @@ query(${getModelInputSnippet(model)}).then((response) => {
112
112
  console.log(JSON.stringify(response));
113
113
  });`;
114
114
 
115
- export const jsSnippets: Partial<Record<PipelineType, (model: ModelData, accessToken: string) => string>> = {
115
+ export const jsSnippets: Partial<Record<PipelineType, (model: ModelDataMinimal, accessToken: string) => string>> = {
116
116
  // Same order as in js/src/lib/interfaces/Types.ts
117
117
  "text-classification": snippetBasic,
118
118
  "token-classification": snippetBasic,
@@ -138,12 +138,12 @@ export const jsSnippets: Partial<Record<PipelineType, (model: ModelData, accessT
138
138
  "image-segmentation": snippetFile,
139
139
  };
140
140
 
141
- export function getJsInferenceSnippet(model: ModelData, accessToken: string): string {
141
+ export function getJsInferenceSnippet(model: ModelDataMinimal, accessToken: string): string {
142
142
  return model.pipeline_tag && model.pipeline_tag in jsSnippets
143
143
  ? jsSnippets[model.pipeline_tag]?.(model, accessToken) ?? ""
144
144
  : "";
145
145
  }
146
146
 
147
- export function hasJsInferenceSnippet(model: ModelData): boolean {
147
+ export function hasJsInferenceSnippet(model: ModelDataMinimal): boolean {
148
148
  return !!model.pipeline_tag && model.pipeline_tag in jsSnippets;
149
149
  }
@@ -1,8 +1,8 @@
1
- import type { ModelData } from "../model-data.js";
2
1
  import type { PipelineType } from "../pipelines.js";
3
2
  import { getModelInputSnippet } from "./inputs.js";
3
+ import type { ModelDataMinimal } from "./types.js";
4
4
 
5
- export const snippetZeroShotClassification = (model: ModelData): string =>
5
+ export const snippetZeroShotClassification = (model: ModelDataMinimal): string =>
6
6
  `def query(payload):
7
7
  response = requests.post(API_URL, headers=headers, json=payload)
8
8
  return response.json()
@@ -12,7 +12,7 @@ output = query({
12
12
  "parameters": {"candidate_labels": ["refund", "legal", "faq"]},
13
13
  })`;
14
14
 
15
- export const snippetZeroShotImageClassification = (model: ModelData): string =>
15
+ export const snippetZeroShotImageClassification = (model: ModelDataMinimal): string =>
16
16
  `def query(data):
17
17
  with open(data["image_path"], "rb") as f:
18
18
  img = f.read()
@@ -28,7 +28,7 @@ output = query({
28
28
  "parameters": {"candidate_labels": ["cat", "dog", "llama"]},
29
29
  })`;
30
30
 
31
- export const snippetBasic = (model: ModelData): string =>
31
+ export const snippetBasic = (model: ModelDataMinimal): string =>
32
32
  `def query(payload):
33
33
  response = requests.post(API_URL, headers=headers, json=payload)
34
34
  return response.json()
@@ -37,7 +37,7 @@ output = query({
37
37
  "inputs": ${getModelInputSnippet(model)},
38
38
  })`;
39
39
 
40
- export const snippetFile = (model: ModelData): string =>
40
+ export const snippetFile = (model: ModelDataMinimal): string =>
41
41
  `def query(filename):
42
42
  with open(filename, "rb") as f:
43
43
  data = f.read()
@@ -46,7 +46,7 @@ export const snippetFile = (model: ModelData): string =>
46
46
 
47
47
  output = query(${getModelInputSnippet(model)})`;
48
48
 
49
- export const snippetTextToImage = (model: ModelData): string =>
49
+ export const snippetTextToImage = (model: ModelDataMinimal): string =>
50
50
  `def query(payload):
51
51
  response = requests.post(API_URL, headers=headers, json=payload)
52
52
  return response.content
@@ -58,7 +58,7 @@ import io
58
58
  from PIL import Image
59
59
  image = Image.open(io.BytesIO(image_bytes))`;
60
60
 
61
- export const snippetTabular = (model: ModelData): string =>
61
+ export const snippetTabular = (model: ModelDataMinimal): string =>
62
62
  `def query(payload):
63
63
  response = requests.post(API_URL, headers=headers, json=payload)
64
64
  return response.content
@@ -66,7 +66,7 @@ response = query({
66
66
  "inputs": {"data": ${getModelInputSnippet(model)}},
67
67
  })`;
68
68
 
69
- export const snippetTextToAudio = (model: ModelData): string => {
69
+ export const snippetTextToAudio = (model: ModelDataMinimal): string => {
70
70
  // Transformers TTS pipeline and api-inference-community (AIC) pipeline outputs are diverged
71
71
  // with the latest update to inference-api (IA).
72
72
  // Transformers IA returns a byte object (wav file), whereas AIC returns wav and sampling_rate.
@@ -95,7 +95,7 @@ Audio(audio, rate=sampling_rate)`;
95
95
  }
96
96
  };
97
97
 
98
- export const snippetDocumentQuestionAnswering = (model: ModelData): string =>
98
+ export const snippetDocumentQuestionAnswering = (model: ModelDataMinimal): string =>
99
99
  `def query(payload):
100
100
  with open(payload["image"], "rb") as f:
101
101
  img = f.read()
@@ -107,7 +107,7 @@ output = query({
107
107
  "inputs": ${getModelInputSnippet(model)},
108
108
  })`;
109
109
 
110
- export const pythonSnippets: Partial<Record<PipelineType, (model: ModelData) => string>> = {
110
+ export const pythonSnippets: Partial<Record<PipelineType, (model: ModelDataMinimal) => string>> = {
111
111
  // Same order as in tasks/src/pipelines.ts
112
112
  "text-classification": snippetBasic,
113
113
  "token-classification": snippetBasic,
@@ -137,7 +137,7 @@ export const pythonSnippets: Partial<Record<PipelineType, (model: ModelData) =>
137
137
  "zero-shot-image-classification": snippetZeroShotImageClassification,
138
138
  };
139
139
 
140
- export function getPythonInferenceSnippet(model: ModelData, accessToken: string): string {
140
+ export function getPythonInferenceSnippet(model: ModelDataMinimal, accessToken: string): string {
141
141
  const body =
142
142
  model.pipeline_tag && model.pipeline_tag in pythonSnippets ? pythonSnippets[model.pipeline_tag]?.(model) ?? "" : "";
143
143
 
@@ -149,6 +149,6 @@ headers = {"Authorization": ${accessToken ? `"Bearer ${accessToken}"` : `f"Beare
149
149
  ${body}`;
150
150
  }
151
151
 
152
- export function hasPythonInferenceSnippet(model: ModelData): boolean {
152
+ export function hasPythonInferenceSnippet(model: ModelDataMinimal): boolean {
153
153
  return !!model.pipeline_tag && model.pipeline_tag in pythonSnippets;
154
154
  }
@@ -0,0 +1,8 @@
1
+ import type { ModelData } from "../model-data";
2
+
3
+ /**
4
+ * Minimal model data required for snippets.
5
+ *
6
+ * Add more fields as needed.
7
+ */
8
+ export type ModelDataMinimal = Pick<ModelData, "id" | "pipeline_tag" | "mask_token" | "library_name">;
@@ -21,7 +21,7 @@ export interface AddedToken {
21
21
  single_word?: boolean;
22
22
  }
23
23
  export type SpecialTokensMap = {
24
- [key in (typeof SPECIAL_TOKENS_ATTRIBUTES)[number]]?: string | AddedToken;
24
+ [key in (typeof SPECIAL_TOKENS_ATTRIBUTES)[number]]?: string | AddedToken | null;
25
25
  };
26
26
  /**
27
27
  * Public interface for tokenizer config