@huggingface/inference 1.5.1 → 1.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.
package/README.md CHANGED
@@ -2,7 +2,8 @@
2
2
 
3
3
  A Typescript powered wrapper for the Hugging Face Inference API. Learn more about the Inference API at [Hugging Face](https://huggingface.co/docs/api-inference/index).
4
4
 
5
- Check out the [full documentation](https://huggingface.co/docs/huggingface.js/inference/README).
5
+ Check out the [full documentation](https://huggingface.co/docs/huggingface.js/inference/README) or try out a live [interactive notebook](https://observablehq.com/@huggingface/hello-huggingface-js-inference).
6
+
6
7
 
7
8
 
8
9
  ## Install
@@ -17,7 +18,9 @@ pnpm add @huggingface/inference
17
18
 
18
19
  ## Usage
19
20
 
20
- ❗**Important note:** Using an API key is optional to get started (simply provide a random string), however you will be rate limited eventually. Join [Hugging Face](https://huggingface.co/join) and then visit [access tokens](https://huggingface.co/settings/tokens) to generate your API key.
21
+ ❗**Important note:** Using an API key is optional to get started, however you will be rate limited eventually. Join [Hugging Face](https://huggingface.co/join) and then visit [access tokens](https://huggingface.co/settings/tokens) to generate your API key for **free**.
22
+
23
+ Your API key should be kept private. If you need to protect it in front-end applications, we suggest setting up a proxy server that stores the API key.
21
24
 
22
25
  ### Basic examples
23
26
 
@@ -148,7 +151,7 @@ await hf.textToImage({
148
151
  })
149
152
  ```
150
153
 
151
- ## Supported APIs
154
+ ## Supported Tasks
152
155
 
153
156
  ### Natural Language Processing
154
157
 
@@ -183,3 +186,9 @@ await hf.textToImage({
183
186
  ```console
184
187
  HF_ACCESS_TOKEN="your access token" npm run test
185
188
  ```
189
+
190
+ ## Finding appropriate models
191
+
192
+ We have an informative documentation project called [Tasks](https://huggingface.co/tasks) to list available models for each task and explain how each task works in detail.
193
+
194
+ It also contain demos, example outputs and other resources should you want to dig more into the ML-side of things.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- type Options = {
1
+ interface Options {
2
2
  /**
3
3
  * (Default: true) Boolean. If a request 503s and wait_for_model is set to false, the request will be retried with the same parameters but with wait_for_model set to true.
4
4
  */
@@ -15,10 +15,10 @@ type Options = {
15
15
  * (Default: false) Boolean. If the model is not ready, wait for it instead of receiving 503. It limits the number of requests required to get your inference done. It is advised to only set this flag to true after receiving a 503 error as it will limit hanging in your application to known places.
16
16
  */
17
17
  wait_for_model?: boolean;
18
- };
19
- type Args = {
18
+ }
19
+ interface Args {
20
20
  model: string;
21
- };
21
+ }
22
22
  type FillMaskArgs = Args & {
23
23
  inputs: string;
24
24
  };
@@ -76,19 +76,19 @@ type SummarizationArgs = Args & {
76
76
  top_p?: number;
77
77
  };
78
78
  };
79
- type SummarizationReturn = {
79
+ interface SummarizationReturn {
80
80
  /**
81
81
  * The string after translation
82
82
  */
83
83
  summary_text: string;
84
- };
84
+ }
85
85
  type QuestionAnswerArgs = Args & {
86
86
  inputs: {
87
87
  context: string;
88
88
  question: string;
89
89
  };
90
90
  };
91
- type QuestionAnswerReturn = {
91
+ interface QuestionAnswerReturn {
92
92
  /**
93
93
  * A string that’s the answer within the text.
94
94
  */
@@ -105,7 +105,7 @@ type QuestionAnswerReturn = {
105
105
  * The index (string wise) of the start of the answer within context.
106
106
  */
107
107
  start: number;
108
- };
108
+ }
109
109
  type TableQuestionAnswerArgs = Args & {
110
110
  inputs: {
111
111
  /**
@@ -118,7 +118,7 @@ type TableQuestionAnswerArgs = Args & {
118
118
  table: Record<string, string[]>;
119
119
  };
120
120
  };
121
- type TableQuestionAnswerReturn = {
121
+ interface TableQuestionAnswerReturn {
122
122
  /**
123
123
  * The aggregator used to get the answer
124
124
  */
@@ -135,7 +135,7 @@ type TableQuestionAnswerReturn = {
135
135
  * a list of coordinates of the cells referenced in the answer
136
136
  */
137
137
  coordinates: number[][];
138
- };
138
+ }
139
139
  type TextClassificationArgs = Args & {
140
140
  /**
141
141
  * A string to be classified
@@ -196,12 +196,12 @@ type TextGenerationArgs = Args & {
196
196
  top_p?: number;
197
197
  };
198
198
  };
199
- type TextGenerationReturn = {
199
+ interface TextGenerationReturn {
200
200
  /**
201
201
  * The continuated string
202
202
  */
203
203
  generated_text: string;
204
- };
204
+ }
205
205
  type TokenClassificationArgs = Args & {
206
206
  /**
207
207
  * A string to be classified
@@ -224,7 +224,7 @@ type TokenClassificationArgs = Args & {
224
224
  aggregation_strategy?: "none" | "simple" | "first" | "average" | "max";
225
225
  };
226
226
  };
227
- type TokenClassificationReturnValue = {
227
+ interface TokenClassificationReturnValue {
228
228
  /**
229
229
  * The offset stringwise where the answer is located. Useful to disambiguate if word occurs multiple times.
230
230
  */
@@ -245,7 +245,7 @@ type TokenClassificationReturnValue = {
245
245
  * The string that was captured
246
246
  */
247
247
  word: string;
248
- };
248
+ }
249
249
  type TokenClassificationReturn = TokenClassificationReturnValue[];
250
250
  type TranslationArgs = Args & {
251
251
  /**
@@ -253,12 +253,12 @@ type TranslationArgs = Args & {
253
253
  */
254
254
  inputs: string;
255
255
  };
256
- type TranslationReturn = {
256
+ interface TranslationReturn {
257
257
  /**
258
258
  * The string after translation
259
259
  */
260
260
  translation_text: string;
261
- };
261
+ }
262
262
  type ZeroShotClassificationArgs = Args & {
263
263
  /**
264
264
  * a string or list of strings
@@ -275,11 +275,11 @@ type ZeroShotClassificationArgs = Args & {
275
275
  multi_label?: boolean;
276
276
  };
277
277
  };
278
- type ZeroShotClassificationReturnValue = {
278
+ interface ZeroShotClassificationReturnValue {
279
279
  labels: string[];
280
280
  scores: number[];
281
281
  sequence: string;
282
- };
282
+ }
283
283
  type ZeroShotClassificationReturn = ZeroShotClassificationReturnValue[];
284
284
  type ConversationalArgs = Args & {
285
285
  inputs: {
@@ -327,14 +327,14 @@ type ConversationalArgs = Args & {
327
327
  top_p?: number;
328
328
  };
329
329
  };
330
- type ConversationalReturn = {
330
+ interface ConversationalReturn {
331
331
  conversation: {
332
332
  generated_responses: string[];
333
333
  past_user_inputs: string[];
334
334
  };
335
335
  generated_text: string;
336
336
  warnings: string[];
337
- };
337
+ }
338
338
  type FeatureExtractionArgs = Args & {
339
339
  /**
340
340
  * The inputs vary based on the model. For example when using sentence-transformers/paraphrase-xlm-r-multilingual-v1 the inputs will look like this:
@@ -356,7 +356,7 @@ type ImageClassificationArgs = Args & {
356
356
  */
357
357
  data: any;
358
358
  };
359
- type ImageClassificationReturnValue = {
359
+ interface ImageClassificationReturnValue {
360
360
  /**
361
361
  * A float that represents how likely it is that the image file belongs to this class.
362
362
  */
@@ -365,7 +365,7 @@ type ImageClassificationReturnValue = {
365
365
  * The label for the class (model specific)
366
366
  */
367
367
  score: number;
368
- };
368
+ }
369
369
  type ImageClassificationReturn = ImageClassificationReturnValue[];
370
370
  type ObjectDetectionArgs = Args & {
371
371
  /**
@@ -373,7 +373,7 @@ type ObjectDetectionArgs = Args & {
373
373
  */
374
374
  data: any;
375
375
  };
376
- type ObjectDetectionReturnValue = {
376
+ interface ObjectDetectionReturnValue {
377
377
  /**
378
378
  * A dict (with keys [xmin,ymin,xmax,ymax]) representing the bounding box of a detected object.
379
379
  */
@@ -391,7 +391,7 @@ type ObjectDetectionReturnValue = {
391
391
  * A float that represents how likely it is that the detected object belongs to the given class.
392
392
  */
393
393
  score: number;
394
- };
394
+ }
395
395
  type ObjectDetectionReturn = ObjectDetectionReturnValue[];
396
396
  type ImageSegmentationArgs = Args & {
397
397
  /**
@@ -399,7 +399,7 @@ type ImageSegmentationArgs = Args & {
399
399
  */
400
400
  data: any;
401
401
  };
402
- type ImageSegmentationReturnValue = {
402
+ interface ImageSegmentationReturnValue {
403
403
  /**
404
404
  * The label for the class (model specific) of a segment.
405
405
  */
@@ -412,7 +412,7 @@ type ImageSegmentationReturnValue = {
412
412
  * A float that represents how likely it is that the detected object belongs to the given class.
413
413
  */
414
414
  score: number;
415
- };
415
+ }
416
416
  type ImageSegmentationReturn = ImageSegmentationReturnValue[];
417
417
  type AutomaticSpeechRecognitionArgs = Args & {
418
418
  /**
@@ -420,19 +420,19 @@ type AutomaticSpeechRecognitionArgs = Args & {
420
420
  */
421
421
  data: any;
422
422
  };
423
- type AutomaticSpeechRecognitionReturn = {
423
+ interface AutomaticSpeechRecognitionReturn {
424
424
  /**
425
425
  * The text that was recognized from the audio
426
426
  */
427
427
  text: string;
428
- };
428
+ }
429
429
  type AudioClassificationArgs = Args & {
430
430
  /**
431
431
  * Binary audio data
432
432
  */
433
433
  data: any;
434
434
  };
435
- type AudioClassificationReturnValue = {
435
+ interface AudioClassificationReturnValue {
436
436
  /**
437
437
  * The label for the class (model specific)
438
438
  */
@@ -441,7 +441,7 @@ type AudioClassificationReturnValue = {
441
441
  * A float that represents how likely it is that the audio file belongs to this class.
442
442
  */
443
443
  score: number;
444
- };
444
+ }
445
445
  type AudioClassificationReturn = AudioClassificationReturnValue[];
446
446
  type TextToImageArgs = Args & {
447
447
  /**
@@ -453,7 +453,7 @@ type TextToImageArgs = Args & {
453
453
  */
454
454
  negative_prompt?: string;
455
455
  };
456
- type TextToImageReturn = ArrayBuffer;
456
+ type TextToImageReturn = Blob;
457
457
  declare class HfInference {
458
458
  private readonly apiKey;
459
459
  private readonly defaultOptions;
package/dist/index.js CHANGED
@@ -185,17 +185,15 @@ var HfInference = class {
185
185
  wait_for_model: true
186
186
  });
187
187
  }
188
- let output;
189
188
  if (options?.blob) {
190
189
  if (!response.ok) {
191
190
  throw new Error("An error occurred while fetching the blob");
192
191
  }
193
- return await response.arrayBuffer();
194
- } else {
195
- output = await response.json();
196
- if (output.error) {
197
- throw new Error(output.error);
198
- }
192
+ return await response.blob();
193
+ }
194
+ const output = await response.json();
195
+ if (output.error) {
196
+ throw new Error(output.error);
199
197
  }
200
198
  return output;
201
199
  }
package/dist/index.mjs CHANGED
@@ -160,17 +160,15 @@ var HfInference = class {
160
160
  wait_for_model: true
161
161
  });
162
162
  }
163
- let output;
164
163
  if (options?.blob) {
165
164
  if (!response.ok) {
166
165
  throw new Error("An error occurred while fetching the blob");
167
166
  }
168
- return await response.arrayBuffer();
169
- } else {
170
- output = await response.json();
171
- if (output.error) {
172
- throw new Error(output.error);
173
- }
167
+ return await response.blob();
168
+ }
169
+ const output = await response.json();
170
+ if (output.error) {
171
+ throw new Error(output.error);
174
172
  }
175
173
  return output;
176
174
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@huggingface/inference",
3
- "version": "1.5.1",
3
+ "version": "1.6.0",
4
4
  "license": "MIT",
5
5
  "author": "Tim Mikeladze <tim.mikeladze@gmail.com>",
6
6
  "description": "Typescript wrapper for the Hugging Face Inference API",
@@ -39,26 +39,18 @@
39
39
  }
40
40
  },
41
41
  "devDependencies": {
42
- "@types/jest": "29.4.0",
43
42
  "@types/node": "18.13.0",
44
- "@typescript-eslint/eslint-plugin": "^5.51.0",
45
- "@typescript-eslint/parser": "^5.51.0",
46
- "eslint": "8.34.0",
47
- "eslint-config-prettier": "^8.6.0",
48
- "eslint-plugin-prettier": "^4.2.1",
49
- "eslint-plugin-typescript-sort-keys": "^2.1.0",
50
- "jest": "29.4.3",
51
- "prettier": "2.8.4",
52
- "ts-jest": "29.0.5",
53
43
  "tsup": "^6.6.3",
54
- "typescript": "4.9.5"
44
+ "typescript": "4.9.5",
45
+ "vite": "^4.1.4",
46
+ "vitest": "^0.29.2"
55
47
  },
56
48
  "resolutions": {},
57
49
  "scripts": {
58
50
  "build": "tsup src/index.ts --format cjs,esm --clean --dts",
59
51
  "format": "prettier --write . && eslint --quiet --fix --ext .cjs,.ts .",
60
- "test": "jest --passWithNoTests",
61
- "test:ci": "npm run test -- --ci --coverage",
52
+ "test": "vitest run",
53
+ "test:ci": "pnpm run test -- --coverage",
62
54
  "type-check": "tsc"
63
55
  }
64
56
  }
@@ -1,4 +1,4 @@
1
- export type Options = {
1
+ export interface Options {
2
2
  /**
3
3
  * (Default: true) Boolean. If a request 503s and wait_for_model is set to false, the request will be retried with the same parameters but with wait_for_model set to true.
4
4
  */
@@ -16,11 +16,11 @@ export type Options = {
16
16
  * (Default: false) Boolean. If the model is not ready, wait for it instead of receiving 503. It limits the number of requests required to get your inference done. It is advised to only set this flag to true after receiving a 503 error as it will limit hanging in your application to known places.
17
17
  */
18
18
  wait_for_model?: boolean;
19
- };
19
+ }
20
20
 
21
- export type Args = {
21
+ export interface Args {
22
22
  model: string;
23
- };
23
+ }
24
24
 
25
25
  export type FillMaskArgs = Args & {
26
26
  inputs: string;
@@ -82,12 +82,12 @@ export type SummarizationArgs = Args & {
82
82
  };
83
83
  };
84
84
 
85
- export type SummarizationReturn = {
85
+ export interface SummarizationReturn {
86
86
  /**
87
87
  * The string after translation
88
88
  */
89
89
  summary_text: string;
90
- };
90
+ }
91
91
 
92
92
  export type QuestionAnswerArgs = Args & {
93
93
  inputs: {
@@ -96,7 +96,7 @@ export type QuestionAnswerArgs = Args & {
96
96
  };
97
97
  };
98
98
 
99
- export type QuestionAnswerReturn = {
99
+ export interface QuestionAnswerReturn {
100
100
  /**
101
101
  * A string that’s the answer within the text.
102
102
  */
@@ -113,7 +113,7 @@ export type QuestionAnswerReturn = {
113
113
  * The index (string wise) of the start of the answer within context.
114
114
  */
115
115
  start: number;
116
- };
116
+ }
117
117
 
118
118
  export type TableQuestionAnswerArgs = Args & {
119
119
  inputs: {
@@ -128,7 +128,7 @@ export type TableQuestionAnswerArgs = Args & {
128
128
  };
129
129
  };
130
130
 
131
- export type TableQuestionAnswerReturn = {
131
+ export interface TableQuestionAnswerReturn {
132
132
  /**
133
133
  * The aggregator used to get the answer
134
134
  */
@@ -145,7 +145,7 @@ export type TableQuestionAnswerReturn = {
145
145
  * a list of coordinates of the cells referenced in the answer
146
146
  */
147
147
  coordinates: number[][];
148
- };
148
+ }
149
149
 
150
150
  export type TextClassificationArgs = Args & {
151
151
  /**
@@ -210,12 +210,12 @@ export type TextGenerationArgs = Args & {
210
210
  };
211
211
  };
212
212
 
213
- export type TextGenerationReturn = {
213
+ export interface TextGenerationReturn {
214
214
  /**
215
215
  * The continuated string
216
216
  */
217
217
  generated_text: string;
218
- };
218
+ }
219
219
 
220
220
  export type TokenClassificationArgs = Args & {
221
221
  /**
@@ -240,7 +240,7 @@ export type TokenClassificationArgs = Args & {
240
240
  };
241
241
  };
242
242
 
243
- export type TokenClassificationReturnValue = {
243
+ export interface TokenClassificationReturnValue {
244
244
  /**
245
245
  * The offset stringwise where the answer is located. Useful to disambiguate if word occurs multiple times.
246
246
  */
@@ -261,7 +261,7 @@ export type TokenClassificationReturnValue = {
261
261
  * The string that was captured
262
262
  */
263
263
  word: string;
264
- };
264
+ }
265
265
 
266
266
  export type TokenClassificationReturn = TokenClassificationReturnValue[];
267
267
 
@@ -272,12 +272,12 @@ export type TranslationArgs = Args & {
272
272
  inputs: string;
273
273
  };
274
274
 
275
- export type TranslationReturn = {
275
+ export interface TranslationReturn {
276
276
  /**
277
277
  * The string after translation
278
278
  */
279
279
  translation_text: string;
280
- };
280
+ }
281
281
 
282
282
  export type ZeroShotClassificationArgs = Args & {
283
283
  /**
@@ -296,11 +296,11 @@ export type ZeroShotClassificationArgs = Args & {
296
296
  };
297
297
  };
298
298
 
299
- export type ZeroShotClassificationReturnValue = {
299
+ export interface ZeroShotClassificationReturnValue {
300
300
  labels: string[];
301
301
  scores: number[];
302
302
  sequence: string;
303
- };
303
+ }
304
304
 
305
305
  export type ZeroShotClassificationReturn = ZeroShotClassificationReturnValue[];
306
306
 
@@ -351,14 +351,14 @@ export type ConversationalArgs = Args & {
351
351
  };
352
352
  };
353
353
 
354
- export type ConversationalReturn = {
354
+ export interface ConversationalReturn {
355
355
  conversation: {
356
356
  generated_responses: string[];
357
357
  past_user_inputs: string[];
358
358
  };
359
359
  generated_text: string;
360
360
  warnings: string[];
361
- };
361
+ }
362
362
 
363
363
  export type FeatureExtractionArgs = Args & {
364
364
  /**
@@ -384,7 +384,7 @@ export type ImageClassificationArgs = Args & {
384
384
  data: any;
385
385
  };
386
386
 
387
- export type ImageClassificationReturnValue = {
387
+ export interface ImageClassificationReturnValue {
388
388
  /**
389
389
  * A float that represents how likely it is that the image file belongs to this class.
390
390
  */
@@ -393,7 +393,7 @@ export type ImageClassificationReturnValue = {
393
393
  * The label for the class (model specific)
394
394
  */
395
395
  score: number;
396
- };
396
+ }
397
397
 
398
398
  export type ImageClassificationReturn = ImageClassificationReturnValue[];
399
399
 
@@ -404,7 +404,7 @@ export type ObjectDetectionArgs = Args & {
404
404
  data: any;
405
405
  };
406
406
 
407
- export type ObjectDetectionReturnValue = {
407
+ export interface ObjectDetectionReturnValue {
408
408
  /**
409
409
  * A dict (with keys [xmin,ymin,xmax,ymax]) representing the bounding box of a detected object.
410
410
  */
@@ -423,7 +423,7 @@ export type ObjectDetectionReturnValue = {
423
423
  * A float that represents how likely it is that the detected object belongs to the given class.
424
424
  */
425
425
  score: number;
426
- };
426
+ }
427
427
 
428
428
  export type ObjectDetectionReturn = ObjectDetectionReturnValue[];
429
429
 
@@ -434,7 +434,7 @@ export type ImageSegmentationArgs = Args & {
434
434
  data: any;
435
435
  };
436
436
 
437
- export type ImageSegmentationReturnValue = {
437
+ export interface ImageSegmentationReturnValue {
438
438
  /**
439
439
  * The label for the class (model specific) of a segment.
440
440
  */
@@ -447,7 +447,7 @@ export type ImageSegmentationReturnValue = {
447
447
  * A float that represents how likely it is that the detected object belongs to the given class.
448
448
  */
449
449
  score: number;
450
- };
450
+ }
451
451
 
452
452
  export type ImageSegmentationReturn = ImageSegmentationReturnValue[];
453
453
 
@@ -458,12 +458,12 @@ export type AutomaticSpeechRecognitionArgs = Args & {
458
458
  data: any;
459
459
  };
460
460
 
461
- export type AutomaticSpeechRecognitionReturn = {
461
+ export interface AutomaticSpeechRecognitionReturn {
462
462
  /**
463
463
  * The text that was recognized from the audio
464
464
  */
465
465
  text: string;
466
- };
466
+ }
467
467
 
468
468
  export type AudioClassificationArgs = Args & {
469
469
  /**
@@ -472,7 +472,7 @@ export type AudioClassificationArgs = Args & {
472
472
  data: any;
473
473
  };
474
474
 
475
- export type AudioClassificationReturnValue = {
475
+ export interface AudioClassificationReturnValue {
476
476
  /**
477
477
  * The label for the class (model specific)
478
478
  */
@@ -482,7 +482,7 @@ export type AudioClassificationReturnValue = {
482
482
  * A float that represents how likely it is that the audio file belongs to this class.
483
483
  */
484
484
  score: number;
485
- };
485
+ }
486
486
 
487
487
  export type AudioClassificationReturn = AudioClassificationReturnValue[];
488
488
 
@@ -498,7 +498,7 @@ export type TextToImageArgs = Args & {
498
498
  negative_prompt?: string;
499
499
  };
500
500
 
501
- export type TextToImageReturn = ArrayBuffer;
501
+ export type TextToImageReturn = Blob;
502
502
 
503
503
  export class HfInference {
504
504
  private readonly apiKey: string;
@@ -712,20 +712,17 @@ export class HfInference {
712
712
  });
713
713
  }
714
714
 
715
- let output;
716
-
717
715
  if (options?.blob) {
718
716
  if (!response.ok) {
719
717
  throw new Error("An error occurred while fetching the blob");
720
718
  }
721
- return await response.arrayBuffer();
722
- } else {
723
- output = await response.json();
724
- if (output.error) {
725
- throw new Error(output.error);
726
- }
719
+ return await response.blob();
727
720
  }
728
721
 
722
+ const output = await response.json();
723
+ if (output.error) {
724
+ throw new Error(output.error);
725
+ }
729
726
  return output;
730
727
  }
731
728