@huggingface/inference 1.5.0 → 1.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/README.md CHANGED
@@ -2,6 +2,10 @@
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) or try out a live [interactive notebook](https://observablehq.com/@huggingface/hello-huggingface-js-inference).
6
+
7
+
8
+
5
9
  ## Install
6
10
 
7
11
  ```console
@@ -14,7 +18,7 @@ pnpm add @huggingface/inference
14
18
 
15
19
  ## Usage
16
20
 
17
- ❗**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. Note that this API key should be kept private and used exclusively for experimental purposes on the web. If you need to protect it, we suggest setting up a proxy server since we currently do not provide an OAuth solution.
18
22
 
19
23
  ### Basic examples
20
24
 
@@ -180,600 +184,3 @@ await hf.textToImage({
180
184
  ```console
181
185
  HF_ACCESS_TOKEN="your access token" npm run test
182
186
  ```
183
-
184
- ## Options
185
-
186
- ```typescript
187
- export declare class HfInference {
188
- private readonly apiKey
189
- private readonly defaultOptions
190
- constructor(apiKey: string, defaultOptions?: Options)
191
- /**
192
- * Tries to fill in a hole with a missing word (token to be precise). That’s the base task for BERT models.
193
- */
194
- fillMask(args: FillMaskArgs, options?: Options): Promise<FillMaskReturn>
195
- /**
196
- * This task is well known to summarize longer text into shorter text. Be careful, some models have a maximum length of input. That means that the summary cannot handle full books for instance. Be careful when choosing your model.
197
- */
198
- summarization(
199
- args: SummarizationArgs,
200
- options?: Options
201
- ): Promise<SummarizationReturn>
202
- /**
203
- * Want to have a nice know-it-all bot that can answer any question?. Recommended model: deepset/roberta-base-squad2
204
- */
205
- questionAnswer(
206
- args: QuestionAnswerArgs,
207
- options?: Options
208
- ): Promise<QuestionAnswerReturn>
209
- /**
210
- * Don’t know SQL? Don’t want to dive into a large spreadsheet? Ask questions in plain english! Recommended model: google/tapas-base-finetuned-wtq.
211
- */
212
- tableQuestionAnswer(
213
- args: TableQuestionAnswerArgs,
214
- options?: Options
215
- ): Promise<TableQuestionAnswerReturn>
216
- /**
217
- * Usually used for sentiment-analysis this will output the likelihood of classes of an input. Recommended model: distilbert-base-uncased-finetuned-sst-2-english
218
- */
219
- textClassification(
220
- args: TextClassificationArgs,
221
- options?: Options
222
- ): Promise<TextClassificationReturn>
223
- /**
224
- * Use to continue text from a prompt. This is a very generic task. Recommended model: gpt2 (it’s a simple model, but fun to play with).
225
- */
226
- textGeneration(
227
- args: TextGenerationArgs,
228
- options?: Options
229
- ): Promise<TextGenerationReturn>
230
- /**
231
- * Usually used for sentence parsing, either grammatical, or Named Entity Recognition (NER) to understand keywords contained within text. Recommended model: dbmdz/bert-large-cased-finetuned-conll03-english
232
- */
233
- tokenClassification(
234
- args: TokenClassificationArgs,
235
- options?: Options
236
- ): Promise<TokenClassificationReturn>
237
- /**
238
- * This task is well known to translate text from one language to another. Recommended model: Helsinki-NLP/opus-mt-ru-en.
239
- */
240
- translation(
241
- args: TranslationArgs,
242
- options?: Options
243
- ): Promise<TranslationReturn>
244
- /**
245
- * This task is super useful to try out classification with zero code, you simply pass a sentence/paragraph and the possible labels for that sentence, and you get a result. Recommended model: facebook/bart-large-mnli.
246
- */
247
- zeroShotClassification(
248
- args: ZeroShotClassificationArgs,
249
- options?: Options
250
- ): Promise<ZeroShotClassificationReturn>
251
- /**
252
- * This task corresponds to any chatbot like structure. Models tend to have shorter max_length, so please check with caution when using a given model if you need long range dependency or not. Recommended model: microsoft/DialoGPT-large.
253
- *
254
- */
255
- conversational(
256
- args: ConversationalArgs,
257
- options?: Options
258
- ): Promise<ConversationalReturn>
259
- /**
260
- * This task reads some text and outputs raw float values, that are usually consumed as part of a semantic database/semantic search.
261
- */
262
- featureExtraction(
263
- args: FeatureExtractionArgs,
264
- options?: Options
265
- ): Promise<FeatureExtractionReturn>
266
- /**
267
- * This task reads some audio input and outputs the said words within the audio files.
268
- * Recommended model (english language): facebook/wav2vec2-large-960h-lv60-self
269
- */
270
- automaticSpeechRecognition(
271
- args: AutomaticSpeechRecognitionArgs,
272
- options?: Options
273
- ): Promise<AutomaticSpeechRecognitionReturn>
274
- /**
275
- * This task reads some audio input and outputs the likelihood of classes.
276
- * Recommended model: superb/hubert-large-superb-er
277
- */
278
- audioClassification(
279
- args: AudioClassificationArgs,
280
- options?: Options
281
- ): Promise<AudioClassificationReturn>
282
- /**
283
- * This task reads some image input and outputs the likelihood of classes.
284
- * Recommended model: google/vit-base-patch16-224
285
- */
286
- imageClassification(
287
- args: ImageClassificationArgs,
288
- options?: Options
289
- ): Promise<ImageClassificationReturn>
290
- /**
291
- * This task reads some image input and outputs the likelihood of classes & bounding boxes of detected objects.
292
- * Recommended model: facebook/detr-resnet-50
293
- */
294
- objectDetection(
295
- args: ObjectDetectionArgs,
296
- options?: Options
297
- ): Promise<ObjectDetectionReturn>
298
- /**
299
- * This task reads some image input and outputs the likelihood of classes & bounding boxes of detected objects.
300
- * Recommended model: facebook/detr-resnet-50-panoptic
301
- */
302
- imageSegmentation(
303
- args: ImageSegmentationArgs,
304
- options?: Options
305
- ): Promise<ImageSegmentationReturn>
306
-
307
- /**
308
- * This task reads some text input and outputs an image.
309
- * Recommended model: stabilityai/stable-diffusion-2
310
- */
311
- textToImage(args: TextToImageArgs, options?: Options): Promise<TextToImageReturn>;
312
- request(
313
- args: Args & {
314
- data?: any
315
- },
316
- options?: Options & {
317
- binary?: boolean
318
- }
319
- ): Promise<any>
320
- private static toArray
321
- }
322
-
323
- export declare type Options = {
324
- /**
325
- * (Default: false). Boolean to use GPU instead of CPU for inference (requires Startup plan at least).
326
- */
327
- use_gpu?: boolean
328
- /**
329
- * (Default: true). Boolean. There is a cache layer on the inference API to speedup requests we have already seen. Most models can use those results as is as models are deterministic (meaning the results will be the same anyway). However if you use a non deterministic model, you can set this parameter to prevent the caching mechanism from being used resulting in a real new query.
330
- */
331
- use_cache?: boolean
332
- /**
333
- * (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.
334
- */
335
- wait_for_model?: boolean
336
- /**
337
- * (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.
338
- */
339
- retry_on_error?: boolean
340
- }
341
- export declare type Args = {
342
- model: string
343
- }
344
- export declare type FillMaskArgs = Args & {
345
- inputs: string
346
- }
347
- export declare type FillMaskReturn = {
348
- /**
349
- * The probability for this token.
350
- */
351
- score: number
352
- /**
353
- * The id of the token
354
- */
355
- token: number
356
- /**
357
- * The string representation of the token
358
- */
359
- token_str: string
360
- /**
361
- * The actual sequence of tokens that ran against the model (may contain special tokens)
362
- */
363
- sequence: string
364
- }[]
365
- export declare type SummarizationArgs = Args & {
366
- /**
367
- * A string to be summarized
368
- */
369
- inputs: string
370
- parameters?: {
371
- /**
372
- * (Default: None). Integer to define the minimum length in tokens of the output summary.
373
- */
374
- min_length?: number
375
- /**
376
- * (Default: None). Integer to define the maximum length in tokens of the output summary.
377
- */
378
- max_length?: number
379
- /**
380
- * (Default: None). Integer to define the top tokens considered within the sample operation to create new text.
381
- */
382
- top_k?: number
383
- /**
384
- * (Default: None). Float to define the tokens that are within the sample operation of text generation. Add tokens in the sample for more probable to least probable until the sum of the probabilities is greater than top_p.
385
- */
386
- top_p?: number
387
- /**
388
- * (Default: 1.0). Float (0.0-100.0). The temperature of the sampling operation. 1 means regular sampling, 0 means always take the highest score, 100.0 is getting closer to uniform probability.
389
- */
390
- temperature?: number
391
- /**
392
- * (Default: None). Float (0.0-100.0). The more a token is used within generation the more it is penalized to not be picked in successive generation passes.
393
- */
394
- repetition_penalty?: number
395
- /**
396
- * (Default: None). Float (0-120.0). The amount of time in seconds that the query should take maximum. Network can cause some overhead so it will be a soft limit.
397
- */
398
- max_time?: number
399
- }
400
- }
401
- export declare type SummarizationReturn = {
402
- /**
403
- * The string after translation
404
- */
405
- summary_text: string
406
- }
407
- export declare type QuestionAnswerArgs = Args & {
408
- inputs: {
409
- question: string
410
- context: string
411
- }
412
- }
413
- export declare type QuestionAnswerReturn = {
414
- /**
415
- * A string that’s the answer within the text.
416
- */
417
- answer: string
418
- /**
419
- * A float that represents how likely that the answer is correct
420
- */
421
- score: number
422
- /**
423
- * The index (string wise) of the start of the answer within context.
424
- */
425
- start: number
426
- /**
427
- * The index (string wise) of the stop of the answer within context.
428
- */
429
- end: number
430
- }
431
- export declare type TableQuestionAnswerArgs = Args & {
432
- inputs: {
433
- /**
434
- * The query in plain text that you want to ask the table
435
- */
436
- query: string
437
- /**
438
- * A table of data represented as a dict of list where entries are headers and the lists are all the values, all lists must have the same size.
439
- */
440
- table: Record<string, string[]>
441
- }
442
- }
443
- export declare type TableQuestionAnswerReturn = {
444
- /**
445
- * The plaintext answer
446
- */
447
- answer: string
448
- /**
449
- * a list of coordinates of the cells referenced in the answer
450
- */
451
- coordinates: number[][]
452
- /**
453
- * A list of coordinates of the cells contents
454
- */
455
- cells: string[]
456
- /**
457
- * The aggregator used to get the answer
458
- */
459
- aggregator: string
460
- }
461
- export declare type TextClassificationArgs = Args & {
462
- /**
463
- * A string to be classified
464
- */
465
- inputs: string
466
- }
467
- export declare type TextClassificationReturn = {
468
- /**
469
- * The label for the class (model specific)
470
- */
471
- label: string
472
- /**
473
- * A floats that represents how likely is that the text belongs to this class.
474
- */
475
- score: number
476
- }[]
477
- export declare type TextGenerationArgs = Args & {
478
- /**
479
- * A string to be generated from
480
- */
481
- inputs: string
482
- parameters?: {
483
- /**
484
- * (Default: None). Integer to define the top tokens considered within the sample operation to create new text.
485
- */
486
- top_k?: number
487
- /**
488
- * (Default: None). Float to define the tokens that are within the sample operation of text generation. Add tokens in the sample for more probable to least probable until the sum of the probabilities is greater than top_p.
489
- */
490
- top_p?: number
491
- /**
492
- * (Default: 1.0). Float (0.0-100.0). The temperature of the sampling operation. 1 means regular sampling, 0 means always take the highest score, 100.0 is getting closer to uniform probability.
493
- */
494
- temperature?: number
495
- /**
496
- * (Default: None). Float (0.0-100.0). The more a token is used within generation the more it is penalized to not be picked in successive generation passes.
497
- */
498
- repetition_penalty?: number
499
- /**
500
- * (Default: None). Int (0-250). The amount of new tokens to be generated, this does not include the input length it is a estimate of the size of generated text you want. Each new tokens slows down the request, so look for balance between response times and length of text generated.
501
- */
502
- max_new_tokens?: number
503
- /**
504
- * (Default: None). Float (0-120.0). The amount of time in seconds that the query should take maximum. Network can cause some overhead so it will be a soft limit. Use that in combination with max_new_tokens for best results.
505
- */
506
- max_time?: number
507
- /**
508
- * (Default: True). Bool. If set to False, the return results will not contain the original query making it easier for prompting.
509
- */
510
- return_full_text?: boolean
511
- /**
512
- * (Default: 1). Integer. The number of proposition you want to be returned.
513
- */
514
- num_return_sequences?: number
515
- /**
516
- * (Optional: True). Bool. Whether or not to use sampling, use greedy decoding otherwise.
517
- */
518
- do_sample?: boolean
519
- }
520
- }
521
- export declare type TextGenerationReturn = {
522
- /**
523
- * The continuated string
524
- */
525
- generated_text: string
526
- }
527
- export declare type TokenClassificationArgs = Args & {
528
- /**
529
- * A string to be classified
530
- */
531
- inputs: string
532
- parameters?: {
533
- /**
534
- * (Default: simple). There are several aggregation strategies:
535
- *
536
- * none: Every token gets classified without further aggregation.
537
- *
538
- * simple: Entities are grouped according to the default schema (B-, I- tags get merged when the tag is similar).
539
- *
540
- * first: Same as the simple strategy except words cannot end up with different tags. Words will use the tag of the first token when there is ambiguity.
541
- *
542
- * average: Same as the simple strategy except words cannot end up with different tags. Scores are averaged across tokens and then the maximum label is applied.
543
- *
544
- * max: Same as the simple strategy except words cannot end up with different tags. Word entity will be the token with the maximum score.
545
- */
546
- aggregation_strategy?: 'none' | 'simple' | 'first' | 'average' | 'max'
547
- }
548
- }
549
- export declare type TokenClassificationReturnValue = {
550
- /**
551
- * The type for the entity being recognized (model specific).
552
- */
553
- entity_group: string
554
- /**
555
- * How likely the entity was recognized.
556
- */
557
- score: number
558
- /**
559
- * The string that was captured
560
- */
561
- word: string
562
- /**
563
- * The offset stringwise where the answer is located. Useful to disambiguate if word occurs multiple times.
564
- */
565
- start: number
566
- /**
567
- * The offset stringwise where the answer is located. Useful to disambiguate if word occurs multiple times.
568
- */
569
- end: number
570
- }
571
- export declare type TokenClassificationReturn = TokenClassificationReturnValue[]
572
- export declare type TranslationArgs = Args & {
573
- /**
574
- * A string to be translated
575
- */
576
- inputs: string
577
- }
578
- export declare type TranslationReturn = {
579
- /**
580
- * The string after translation
581
- */
582
- translation_text: string
583
- }
584
- export declare type ZeroShotClassificationArgs = Args & {
585
- /**
586
- * a string or list of strings
587
- */
588
- inputs: string | string[]
589
- parameters: {
590
- /**
591
- * a list of strings that are potential classes for inputs. (max 10 candidate_labels, for more, simply run multiple requests, results are going to be misleading if using too many candidate_labels anyway. If you want to keep the exact same, you can simply run multi_label=True and do the scaling on your end.
592
- */
593
- candidate_labels: string[]
594
- /**
595
- * (Default: false) Boolean that is set to True if classes can overlap
596
- */
597
- multi_label?: boolean
598
- }
599
- }
600
- export declare type ZeroShotClassificationReturnValue = {
601
- sequence: string
602
- labels: string[]
603
- scores: number[]
604
- }
605
- export declare type ZeroShotClassificationReturn =
606
- ZeroShotClassificationReturnValue[]
607
- export declare type ConversationalArgs = Args & {
608
- inputs: {
609
- /**
610
- * The last input from the user in the conversation.
611
- */
612
- text: string
613
- /**
614
- * A list of strings corresponding to the earlier replies from the model.
615
- */
616
- generated_responses?: string[]
617
- /**
618
- * A list of strings corresponding to the earlier replies from the user. Should be of the same length of generated_responses.
619
- */
620
- past_user_inputs?: string[]
621
- }
622
- parameters?: {
623
- /**
624
- * (Default: None). Integer to define the minimum length in tokens of the output summary.
625
- */
626
- min_length?: number
627
- /**
628
- * (Default: None). Integer to define the maximum length in tokens of the output summary.
629
- */
630
- max_length?: number
631
- /**
632
- * (Default: None). Integer to define the top tokens considered within the sample operation to create new text.
633
- */
634
- top_k?: number
635
- /**
636
- * (Default: None). Float to define the tokens that are within the sample operation of text generation. Add tokens in the sample for more probable to least probable until the sum of the probabilities is greater than top_p.
637
- */
638
- top_p?: number
639
- /**
640
- * (Default: 1.0). Float (0.0-100.0). The temperature of the sampling operation. 1 means regular sampling, 0 means always take the highest score, 100.0 is getting closer to uniform probability.
641
- */
642
- temperature?: number
643
- /**
644
- * (Default: None). Float (0.0-100.0). The more a token is used within generation the more it is penalized to not be picked in successive generation passes.
645
- */
646
- repetition_penalty?: number
647
- /**
648
- * (Default: None). Float (0-120.0). The amount of time in seconds that the query should take maximum. Network can cause some overhead so it will be a soft limit.
649
- */
650
- max_time?: number
651
- }
652
- }
653
- export declare type ConversationalReturn = {
654
- generated_text: string
655
- conversation: {
656
- generated_responses: string[]
657
- past_user_inputs: string[]
658
- }
659
- warnings: string[]
660
- }
661
- export declare type FeatureExtractionArgs = Args & {
662
- /**
663
- * The inputs vary based on the model. For example when using sentence-transformers/paraphrase-xlm-r-multilingual-v1 the inputs will look like this:
664
- *
665
- * inputs: {
666
- * "source_sentence": "That is a happy person",
667
- * "sentences": ["That is a happy dog", "That is a very happy person", "Today is a sunny day"]
668
- */
669
- inputs: Record<string, any> | Record<string, any>[]
670
- }
671
- /**
672
- * Returned values are a list of floats, or a list of list of floats (depending on if you sent a string or a list of string, and if the automatic reduction, usually mean_pooling for instance was applied for you or not. This should be explained on the model's README.
673
- */
674
- export declare type FeatureExtractionReturn = (number | number[])[]
675
- export declare type ImageClassificationArgs = Args & {
676
- /**
677
- * Binary image data
678
- */
679
- data: any
680
- }
681
- export declare type ImageClassificationReturnValue = {
682
- /**
683
- * The label for the class (model specific)
684
- */
685
- score: number
686
- /**
687
- * A float that represents how likely it is that the image file belongs to this class.
688
- */
689
- label: string
690
- }
691
- export declare type ImageClassificationReturn = ImageClassificationReturnValue[]
692
- export declare type ObjectDetectionArgs = Args & {
693
- /**
694
- * Binary image data
695
- */
696
- data: any
697
- }
698
- export declare type ObjectDetectionReturnValue = {
699
- /**
700
- * A float that represents how likely it is that the detected object belongs to the given class.
701
- */
702
- score: number
703
- /**
704
- * The label for the class (model specific) of a detected object.
705
- */
706
- label: string
707
- /**
708
- * A dict (with keys [xmin,ymin,xmax,ymax]) representing the bounding box of a detected object.
709
- */
710
- box: {
711
- xmin: number
712
- ymin: number
713
- xmax: number
714
- ymax: number
715
- }
716
- }
717
- export declare type ObjectDetectionReturn = ObjectDetectionReturnValue[]
718
- export declare type ImageSegmentationArgs = Args & {
719
- /**
720
- * Binary image data
721
- */
722
- data: any
723
- }
724
- export declare type ImageSegmentationReturnValue = {
725
- /**
726
- * A float that represents how likely it is that the detected object belongs to the given class.
727
- */
728
- score: number
729
- /**
730
- * The label for the class (model specific) of a segment.
731
- */
732
- label: string
733
- /**
734
- * A str (base64 str of a single channel black-and-white img) representing the mask of a segment.
735
- */
736
- mask: string
737
- }
738
- export declare type ImageSegmentationReturn = ImageSegmentationReturnValue[]
739
- export declare type AutomaticSpeechRecognitionArgs = Args & {
740
- /**
741
- * Binary audio data
742
- */
743
- data: any
744
- }
745
- export declare type AutomaticSpeechRecognitionReturn = {
746
- /**
747
- * The text that was recognized from the audio
748
- */
749
- text: string
750
- }
751
- export declare type AudioClassificationArgs = Args & {
752
- /**
753
- * Binary audio data
754
- */
755
- data: any
756
- }
757
- export declare type AudioClassificationReturnValue = {
758
- /**
759
- * The label for the class (model specific)
760
- */
761
- label: string
762
- /**
763
- * A float that represents how likely it is that the audio file belongs to this class.
764
- */
765
- score: number
766
- }
767
- export declare type AudioClassificationReturn = AudioClassificationReturnValue[]
768
- type TextToImageArgs = Args & {
769
- /**
770
- * The text to generate an image from
771
- */
772
- inputs: string
773
- /**
774
- * An optional negative prompt for the image generation
775
- */
776
- negative_prompt?: string
777
- };
778
- type TextToImageReturn = ArrayBuffer
779
- ```
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,21 +327,22 @@ 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:
341
341
  *
342
- * inputs: {
342
+ * inputs: &#123;
343
343
  * "source_sentence": "That is a happy person",
344
344
  * "sentences": ["That is a happy dog", "That is a very happy person", "Today is a sunny day"]
345
+ * &#125;
345
346
  */
346
347
  inputs: Record<string, any> | Record<string, any>[];
347
348
  };
@@ -355,7 +356,7 @@ type ImageClassificationArgs = Args & {
355
356
  */
356
357
  data: any;
357
358
  };
358
- type ImageClassificationReturnValue = {
359
+ interface ImageClassificationReturnValue {
359
360
  /**
360
361
  * A float that represents how likely it is that the image file belongs to this class.
361
362
  */
@@ -364,7 +365,7 @@ type ImageClassificationReturnValue = {
364
365
  * The label for the class (model specific)
365
366
  */
366
367
  score: number;
367
- };
368
+ }
368
369
  type ImageClassificationReturn = ImageClassificationReturnValue[];
369
370
  type ObjectDetectionArgs = Args & {
370
371
  /**
@@ -372,7 +373,7 @@ type ObjectDetectionArgs = Args & {
372
373
  */
373
374
  data: any;
374
375
  };
375
- type ObjectDetectionReturnValue = {
376
+ interface ObjectDetectionReturnValue {
376
377
  /**
377
378
  * A dict (with keys [xmin,ymin,xmax,ymax]) representing the bounding box of a detected object.
378
379
  */
@@ -390,7 +391,7 @@ type ObjectDetectionReturnValue = {
390
391
  * A float that represents how likely it is that the detected object belongs to the given class.
391
392
  */
392
393
  score: number;
393
- };
394
+ }
394
395
  type ObjectDetectionReturn = ObjectDetectionReturnValue[];
395
396
  type ImageSegmentationArgs = Args & {
396
397
  /**
@@ -398,7 +399,7 @@ type ImageSegmentationArgs = Args & {
398
399
  */
399
400
  data: any;
400
401
  };
401
- type ImageSegmentationReturnValue = {
402
+ interface ImageSegmentationReturnValue {
402
403
  /**
403
404
  * The label for the class (model specific) of a segment.
404
405
  */
@@ -411,7 +412,7 @@ type ImageSegmentationReturnValue = {
411
412
  * A float that represents how likely it is that the detected object belongs to the given class.
412
413
  */
413
414
  score: number;
414
- };
415
+ }
415
416
  type ImageSegmentationReturn = ImageSegmentationReturnValue[];
416
417
  type AutomaticSpeechRecognitionArgs = Args & {
417
418
  /**
@@ -419,19 +420,19 @@ type AutomaticSpeechRecognitionArgs = Args & {
419
420
  */
420
421
  data: any;
421
422
  };
422
- type AutomaticSpeechRecognitionReturn = {
423
+ interface AutomaticSpeechRecognitionReturn {
423
424
  /**
424
425
  * The text that was recognized from the audio
425
426
  */
426
427
  text: string;
427
- };
428
+ }
428
429
  type AudioClassificationArgs = Args & {
429
430
  /**
430
431
  * Binary audio data
431
432
  */
432
433
  data: any;
433
434
  };
434
- type AudioClassificationReturnValue = {
435
+ interface AudioClassificationReturnValue {
435
436
  /**
436
437
  * The label for the class (model specific)
437
438
  */
@@ -440,7 +441,7 @@ type AudioClassificationReturnValue = {
440
441
  * A float that represents how likely it is that the audio file belongs to this class.
441
442
  */
442
443
  score: number;
443
- };
444
+ }
444
445
  type AudioClassificationReturn = AudioClassificationReturnValue[];
445
446
  type TextToImageArgs = Args & {
446
447
  /**
@@ -456,7 +457,7 @@ type TextToImageReturn = ArrayBuffer;
456
457
  declare class HfInference {
457
458
  private readonly apiKey;
458
459
  private readonly defaultOptions;
459
- constructor(apiKey: string, defaultOptions?: Options);
460
+ constructor(apiKey?: string, defaultOptions?: Options);
460
461
  /**
461
462
  * Tries to fill in a hole with a missing word (token to be precise). That’s the base task for BERT models.
462
463
  */
package/dist/index.js CHANGED
@@ -27,7 +27,7 @@ module.exports = __toCommonJS(src_exports);
27
27
  var HfInference = class {
28
28
  apiKey;
29
29
  defaultOptions;
30
- constructor(apiKey, defaultOptions = {}) {
30
+ constructor(apiKey = "", defaultOptions = {}) {
31
31
  this.apiKey = apiKey;
32
32
  this.defaultOptions = defaultOptions;
33
33
  }
@@ -161,9 +161,13 @@ var HfInference = class {
161
161
  async request(args, options) {
162
162
  const mergedOptions = { ...this.defaultOptions, ...options };
163
163
  const { model, ...otherArgs } = args;
164
- const headers = {
165
- Authorization: `Bearer ${this.apiKey}`
166
- };
164
+ const headers = {};
165
+ if (this.apiKey) {
166
+ headers["Authorization"] = `Bearer ${this.apiKey}`;
167
+ }
168
+ if (!options?.binary) {
169
+ headers["Content-Type"] = "application/json";
170
+ }
167
171
  if (options?.binary && mergedOptions.wait_for_model) {
168
172
  headers["X-Wait-For-Model"] = "true";
169
173
  }
package/dist/index.mjs CHANGED
@@ -2,7 +2,7 @@
2
2
  var HfInference = class {
3
3
  apiKey;
4
4
  defaultOptions;
5
- constructor(apiKey, defaultOptions = {}) {
5
+ constructor(apiKey = "", defaultOptions = {}) {
6
6
  this.apiKey = apiKey;
7
7
  this.defaultOptions = defaultOptions;
8
8
  }
@@ -136,9 +136,13 @@ var HfInference = class {
136
136
  async request(args, options) {
137
137
  const mergedOptions = { ...this.defaultOptions, ...options };
138
138
  const { model, ...otherArgs } = args;
139
- const headers = {
140
- Authorization: `Bearer ${this.apiKey}`
141
- };
139
+ const headers = {};
140
+ if (this.apiKey) {
141
+ headers["Authorization"] = `Bearer ${this.apiKey}`;
142
+ }
143
+ if (!options?.binary) {
144
+ headers["Content-Type"] = "application/json";
145
+ }
142
146
  if (options?.binary && mergedOptions.wait_for_model) {
143
147
  headers["X-Wait-For-Model"] = "true";
144
148
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@huggingface/inference",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
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,22 +351,23 @@ 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
  /**
365
365
  * The inputs vary based on the model. For example when using sentence-transformers/paraphrase-xlm-r-multilingual-v1 the inputs will look like this:
366
366
  *
367
- * inputs: {
367
+ * inputs: &#123;
368
368
  * "source_sentence": "That is a happy person",
369
369
  * "sentences": ["That is a happy dog", "That is a very happy person", "Today is a sunny day"]
370
+ * &#125;
370
371
  */
371
372
  inputs: Record<string, any> | Record<string, any>[];
372
373
  };
@@ -383,7 +384,7 @@ export type ImageClassificationArgs = Args & {
383
384
  data: any;
384
385
  };
385
386
 
386
- export type ImageClassificationReturnValue = {
387
+ export interface ImageClassificationReturnValue {
387
388
  /**
388
389
  * A float that represents how likely it is that the image file belongs to this class.
389
390
  */
@@ -392,7 +393,7 @@ export type ImageClassificationReturnValue = {
392
393
  * The label for the class (model specific)
393
394
  */
394
395
  score: number;
395
- };
396
+ }
396
397
 
397
398
  export type ImageClassificationReturn = ImageClassificationReturnValue[];
398
399
 
@@ -403,7 +404,7 @@ export type ObjectDetectionArgs = Args & {
403
404
  data: any;
404
405
  };
405
406
 
406
- export type ObjectDetectionReturnValue = {
407
+ export interface ObjectDetectionReturnValue {
407
408
  /**
408
409
  * A dict (with keys [xmin,ymin,xmax,ymax]) representing the bounding box of a detected object.
409
410
  */
@@ -422,7 +423,7 @@ export type ObjectDetectionReturnValue = {
422
423
  * A float that represents how likely it is that the detected object belongs to the given class.
423
424
  */
424
425
  score: number;
425
- };
426
+ }
426
427
 
427
428
  export type ObjectDetectionReturn = ObjectDetectionReturnValue[];
428
429
 
@@ -433,7 +434,7 @@ export type ImageSegmentationArgs = Args & {
433
434
  data: any;
434
435
  };
435
436
 
436
- export type ImageSegmentationReturnValue = {
437
+ export interface ImageSegmentationReturnValue {
437
438
  /**
438
439
  * The label for the class (model specific) of a segment.
439
440
  */
@@ -446,7 +447,7 @@ export type ImageSegmentationReturnValue = {
446
447
  * A float that represents how likely it is that the detected object belongs to the given class.
447
448
  */
448
449
  score: number;
449
- };
450
+ }
450
451
 
451
452
  export type ImageSegmentationReturn = ImageSegmentationReturnValue[];
452
453
 
@@ -457,12 +458,12 @@ export type AutomaticSpeechRecognitionArgs = Args & {
457
458
  data: any;
458
459
  };
459
460
 
460
- export type AutomaticSpeechRecognitionReturn = {
461
+ export interface AutomaticSpeechRecognitionReturn {
461
462
  /**
462
463
  * The text that was recognized from the audio
463
464
  */
464
465
  text: string;
465
- };
466
+ }
466
467
 
467
468
  export type AudioClassificationArgs = Args & {
468
469
  /**
@@ -471,7 +472,7 @@ export type AudioClassificationArgs = Args & {
471
472
  data: any;
472
473
  };
473
474
 
474
- export type AudioClassificationReturnValue = {
475
+ export interface AudioClassificationReturnValue {
475
476
  /**
476
477
  * The label for the class (model specific)
477
478
  */
@@ -481,7 +482,7 @@ export type AudioClassificationReturnValue = {
481
482
  * A float that represents how likely it is that the audio file belongs to this class.
482
483
  */
483
484
  score: number;
484
- };
485
+ }
485
486
 
486
487
  export type AudioClassificationReturn = AudioClassificationReturnValue[];
487
488
 
@@ -503,7 +504,7 @@ export class HfInference {
503
504
  private readonly apiKey: string;
504
505
  private readonly defaultOptions: Options;
505
506
 
506
- constructor(apiKey: string, defaultOptions: Options = {}) {
507
+ constructor(apiKey = "", defaultOptions: Options = {}) {
507
508
  this.apiKey = apiKey;
508
509
  this.defaultOptions = defaultOptions;
509
510
  }
@@ -680,13 +681,18 @@ export class HfInference {
680
681
  const mergedOptions = { ...this.defaultOptions, ...options };
681
682
  const { model, ...otherArgs } = args;
682
683
 
683
- const headers = {
684
- Authorization: `Bearer ${this.apiKey}`,
685
- }
684
+ const headers: Record<string, string> = {};
685
+ if (this.apiKey) {
686
+ headers["Authorization"] = `Bearer ${this.apiKey}`;
687
+ }
688
+
689
+ if (!options?.binary) {
690
+ headers["Content-Type"] = "application/json";
691
+ }
686
692
 
687
- if (options?.binary && mergedOptions.wait_for_model) {
688
- headers["X-Wait-For-Model"] = "true";
689
- }
693
+ if (options?.binary && mergedOptions.wait_for_model) {
694
+ headers["X-Wait-For-Model"] = "true";
695
+ }
690
696
 
691
697
  const response = await fetch(`https://api-inference.huggingface.co/models/${model}`, {
692
698
  headers,