@huggingface/inference 1.4.3 → 1.5.1

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 TimMikeladze
3
+ Copyright (c) 2022 Tim Mikeladze
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
21
+ SOFTWARE.
package/README.md CHANGED
@@ -2,6 +2,9 @@
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).
6
+
7
+
5
8
  ## Install
6
9
 
7
10
  ```console
@@ -137,6 +140,12 @@ await hf.imageSegmentation({
137
140
  data: readFileSync('test/cats.png'),
138
141
  model: 'facebook/detr-resnet-50-panoptic'
139
142
  })
143
+
144
+ await hf.textToImage({
145
+ inputs: 'award winning high resolution photo of a giant tortoise/((ladybird)) hybrid, [trending on artstation]',
146
+ negative_prompt: 'blurry',
147
+ model: 'stabilityai/stable-diffusion-2',
148
+ })
140
149
  ```
141
150
 
142
151
  ## Supported APIs
@@ -167,589 +176,10 @@ await hf.imageSegmentation({
167
176
  - [x] Image classification
168
177
  - [x] Object detection
169
178
  - [x] Image segmentation
179
+ - [x] Text to image
170
180
 
171
181
  ## Running tests
172
182
 
173
183
  ```console
174
184
  HF_ACCESS_TOKEN="your access token" npm run test
175
185
  ```
176
-
177
- ## Options
178
-
179
- ```typescript
180
- export declare class HfInference {
181
- private readonly apiKey
182
- private readonly defaultOptions
183
- constructor(apiKey: string, defaultOptions?: Options)
184
- /**
185
- * Tries to fill in a hole with a missing word (token to be precise). That’s the base task for BERT models.
186
- */
187
- fillMask(args: FillMaskArgs, options?: Options): Promise<FillMaskReturn>
188
- /**
189
- * 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.
190
- */
191
- summarization(
192
- args: SummarizationArgs,
193
- options?: Options
194
- ): Promise<SummarizationReturn>
195
- /**
196
- * Want to have a nice know-it-all bot that can answer any question?. Recommended model: deepset/roberta-base-squad2
197
- */
198
- questionAnswer(
199
- args: QuestionAnswerArgs,
200
- options?: Options
201
- ): Promise<QuestionAnswerReturn>
202
- /**
203
- * 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.
204
- */
205
- tableQuestionAnswer(
206
- args: TableQuestionAnswerArgs,
207
- options?: Options
208
- ): Promise<TableQuestionAnswerReturn>
209
- /**
210
- * Usually used for sentiment-analysis this will output the likelihood of classes of an input. Recommended model: distilbert-base-uncased-finetuned-sst-2-english
211
- */
212
- textClassification(
213
- args: TextClassificationArgs,
214
- options?: Options
215
- ): Promise<TextClassificationReturn>
216
- /**
217
- * 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).
218
- */
219
- textGeneration(
220
- args: TextGenerationArgs,
221
- options?: Options
222
- ): Promise<TextGenerationReturn>
223
- /**
224
- * 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
225
- */
226
- tokenClassification(
227
- args: TokenClassificationArgs,
228
- options?: Options
229
- ): Promise<TokenClassificationReturn>
230
- /**
231
- * This task is well known to translate text from one language to another. Recommended model: Helsinki-NLP/opus-mt-ru-en.
232
- */
233
- translation(
234
- args: TranslationArgs,
235
- options?: Options
236
- ): Promise<TranslationReturn>
237
- /**
238
- * 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.
239
- */
240
- zeroShotClassification(
241
- args: ZeroShotClassificationArgs,
242
- options?: Options
243
- ): Promise<ZeroShotClassificationReturn>
244
- /**
245
- * 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.
246
- *
247
- */
248
- conversational(
249
- args: ConversationalArgs,
250
- options?: Options
251
- ): Promise<ConversationalReturn>
252
- /**
253
- * This task reads some text and outputs raw float values, that are usually consumed as part of a semantic database/semantic search.
254
- */
255
- featureExtraction(
256
- args: FeatureExtractionArgs,
257
- options?: Options
258
- ): Promise<FeatureExtractionReturn>
259
- /**
260
- * This task reads some audio input and outputs the said words within the audio files.
261
- * Recommended model (english language): facebook/wav2vec2-large-960h-lv60-self
262
- */
263
- automaticSpeechRecognition(
264
- args: AutomaticSpeechRecognitionArgs,
265
- options?: Options
266
- ): Promise<AutomaticSpeechRecognitionReturn>
267
- /**
268
- * This task reads some audio input and outputs the likelihood of classes.
269
- * Recommended model: superb/hubert-large-superb-er
270
- */
271
- audioClassification(
272
- args: AudioClassificationArgs,
273
- options?: Options
274
- ): Promise<AudioClassificationReturn>
275
- /**
276
- * This task reads some image input and outputs the likelihood of classes.
277
- * Recommended model: google/vit-base-patch16-224
278
- */
279
- imageClassification(
280
- args: ImageClassificationArgs,
281
- options?: Options
282
- ): Promise<ImageClassificationReturn>
283
- /**
284
- * This task reads some image input and outputs the likelihood of classes & bounding boxes of detected objects.
285
- * Recommended model: facebook/detr-resnet-50
286
- */
287
- objectDetection(
288
- args: ObjectDetectionArgs,
289
- options?: Options
290
- ): Promise<ObjectDetectionReturn>
291
- /**
292
- * This task reads some image input and outputs the likelihood of classes & bounding boxes of detected objects.
293
- * Recommended model: facebook/detr-resnet-50-panoptic
294
- */
295
- imageSegmentation(
296
- args: ImageSegmentationArgs,
297
- options?: Options
298
- ): Promise<ImageSegmentationReturn>
299
- request(
300
- args: Args & {
301
- data?: any
302
- },
303
- options?: Options & {
304
- binary?: boolean
305
- }
306
- ): Promise<any>
307
- private static toArray
308
- }
309
-
310
- export declare type Options = {
311
- /**
312
- * (Default: false). Boolean to use GPU instead of CPU for inference (requires Startup plan at least).
313
- */
314
- use_gpu?: boolean
315
- /**
316
- * (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.
317
- */
318
- use_cache?: boolean
319
- /**
320
- * (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.
321
- */
322
- wait_for_model?: boolean
323
- /**
324
- * (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.
325
- */
326
- retry_on_error?: boolean
327
- }
328
- export declare type Args = {
329
- model: string
330
- }
331
- export declare type FillMaskArgs = Args & {
332
- inputs: string
333
- }
334
- export declare type FillMaskReturn = {
335
- /**
336
- * The probability for this token.
337
- */
338
- score: number
339
- /**
340
- * The id of the token
341
- */
342
- token: number
343
- /**
344
- * The string representation of the token
345
- */
346
- token_str: string
347
- /**
348
- * The actual sequence of tokens that ran against the model (may contain special tokens)
349
- */
350
- sequence: string
351
- }[]
352
- export declare type SummarizationArgs = Args & {
353
- /**
354
- * A string to be summarized
355
- */
356
- inputs: string
357
- parameters?: {
358
- /**
359
- * (Default: None). Integer to define the minimum length in tokens of the output summary.
360
- */
361
- min_length?: number
362
- /**
363
- * (Default: None). Integer to define the maximum length in tokens of the output summary.
364
- */
365
- max_length?: number
366
- /**
367
- * (Default: None). Integer to define the top tokens considered within the sample operation to create new text.
368
- */
369
- top_k?: number
370
- /**
371
- * (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.
372
- */
373
- top_p?: number
374
- /**
375
- * (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.
376
- */
377
- temperature?: number
378
- /**
379
- * (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.
380
- */
381
- repetition_penalty?: number
382
- /**
383
- * (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.
384
- */
385
- max_time?: number
386
- }
387
- }
388
- export declare type SummarizationReturn = {
389
- /**
390
- * The string after translation
391
- */
392
- summary_text: string
393
- }
394
- export declare type QuestionAnswerArgs = Args & {
395
- inputs: {
396
- question: string
397
- context: string
398
- }
399
- }
400
- export declare type QuestionAnswerReturn = {
401
- /**
402
- * A string that’s the answer within the text.
403
- */
404
- answer: string
405
- /**
406
- * A float that represents how likely that the answer is correct
407
- */
408
- score: number
409
- /**
410
- * The index (string wise) of the start of the answer within context.
411
- */
412
- start: number
413
- /**
414
- * The index (string wise) of the stop of the answer within context.
415
- */
416
- end: number
417
- }
418
- export declare type TableQuestionAnswerArgs = Args & {
419
- inputs: {
420
- /**
421
- * The query in plain text that you want to ask the table
422
- */
423
- query: string
424
- /**
425
- * 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.
426
- */
427
- table: Record<string, string[]>
428
- }
429
- }
430
- export declare type TableQuestionAnswerReturn = {
431
- /**
432
- * The plaintext answer
433
- */
434
- answer: string
435
- /**
436
- * a list of coordinates of the cells referenced in the answer
437
- */
438
- coordinates: number[][]
439
- /**
440
- * A list of coordinates of the cells contents
441
- */
442
- cells: string[]
443
- /**
444
- * The aggregator used to get the answer
445
- */
446
- aggregator: string
447
- }
448
- export declare type TextClassificationArgs = Args & {
449
- /**
450
- * A string to be classified
451
- */
452
- inputs: string
453
- }
454
- export declare type TextClassificationReturn = {
455
- /**
456
- * The label for the class (model specific)
457
- */
458
- label: string
459
- /**
460
- * A floats that represents how likely is that the text belongs to this class.
461
- */
462
- score: number
463
- }[]
464
- export declare type TextGenerationArgs = Args & {
465
- /**
466
- * A string to be generated from
467
- */
468
- inputs: string
469
- parameters?: {
470
- /**
471
- * (Default: None). Integer to define the top tokens considered within the sample operation to create new text.
472
- */
473
- top_k?: number
474
- /**
475
- * (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.
476
- */
477
- top_p?: number
478
- /**
479
- * (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.
480
- */
481
- temperature?: number
482
- /**
483
- * (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.
484
- */
485
- repetition_penalty?: number
486
- /**
487
- * (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.
488
- */
489
- max_new_tokens?: number
490
- /**
491
- * (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.
492
- */
493
- max_time?: number
494
- /**
495
- * (Default: True). Bool. If set to False, the return results will not contain the original query making it easier for prompting.
496
- */
497
- return_full_text?: boolean
498
- /**
499
- * (Default: 1). Integer. The number of proposition you want to be returned.
500
- */
501
- num_return_sequences?: number
502
- /**
503
- * (Optional: True). Bool. Whether or not to use sampling, use greedy decoding otherwise.
504
- */
505
- do_sample?: boolean
506
- }
507
- }
508
- export declare type TextGenerationReturn = {
509
- /**
510
- * The continuated string
511
- */
512
- generated_text: string
513
- }
514
- export declare type TokenClassificationArgs = Args & {
515
- /**
516
- * A string to be classified
517
- */
518
- inputs: string
519
- parameters?: {
520
- /**
521
- * (Default: simple). There are several aggregation strategies:
522
- *
523
- * none: Every token gets classified without further aggregation.
524
- *
525
- * simple: Entities are grouped according to the default schema (B-, I- tags get merged when the tag is similar).
526
- *
527
- * 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.
528
- *
529
- * 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.
530
- *
531
- * max: Same as the simple strategy except words cannot end up with different tags. Word entity will be the token with the maximum score.
532
- */
533
- aggregation_strategy?: 'none' | 'simple' | 'first' | 'average' | 'max'
534
- }
535
- }
536
- export declare type TokenClassificationReturnValue = {
537
- /**
538
- * The type for the entity being recognized (model specific).
539
- */
540
- entity_group: string
541
- /**
542
- * How likely the entity was recognized.
543
- */
544
- score: number
545
- /**
546
- * The string that was captured
547
- */
548
- word: string
549
- /**
550
- * The offset stringwise where the answer is located. Useful to disambiguate if word occurs multiple times.
551
- */
552
- start: number
553
- /**
554
- * The offset stringwise where the answer is located. Useful to disambiguate if word occurs multiple times.
555
- */
556
- end: number
557
- }
558
- export declare type TokenClassificationReturn = TokenClassificationReturnValue[]
559
- export declare type TranslationArgs = Args & {
560
- /**
561
- * A string to be translated
562
- */
563
- inputs: string
564
- }
565
- export declare type TranslationReturn = {
566
- /**
567
- * The string after translation
568
- */
569
- translation_text: string
570
- }
571
- export declare type ZeroShotClassificationArgs = Args & {
572
- /**
573
- * a string or list of strings
574
- */
575
- inputs: string | string[]
576
- parameters: {
577
- /**
578
- * 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.
579
- */
580
- candidate_labels: string[]
581
- /**
582
- * (Default: false) Boolean that is set to True if classes can overlap
583
- */
584
- multi_label?: boolean
585
- }
586
- }
587
- export declare type ZeroShotClassificationReturnValue = {
588
- sequence: string
589
- labels: string[]
590
- scores: number[]
591
- }
592
- export declare type ZeroShotClassificationReturn =
593
- ZeroShotClassificationReturnValue[]
594
- export declare type ConversationalArgs = Args & {
595
- inputs: {
596
- /**
597
- * The last input from the user in the conversation.
598
- */
599
- text: string
600
- /**
601
- * A list of strings corresponding to the earlier replies from the model.
602
- */
603
- generated_responses?: string[]
604
- /**
605
- * A list of strings corresponding to the earlier replies from the user. Should be of the same length of generated_responses.
606
- */
607
- past_user_inputs?: string[]
608
- }
609
- parameters?: {
610
- /**
611
- * (Default: None). Integer to define the minimum length in tokens of the output summary.
612
- */
613
- min_length?: number
614
- /**
615
- * (Default: None). Integer to define the maximum length in tokens of the output summary.
616
- */
617
- max_length?: number
618
- /**
619
- * (Default: None). Integer to define the top tokens considered within the sample operation to create new text.
620
- */
621
- top_k?: number
622
- /**
623
- * (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.
624
- */
625
- top_p?: number
626
- /**
627
- * (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.
628
- */
629
- temperature?: number
630
- /**
631
- * (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.
632
- */
633
- repetition_penalty?: number
634
- /**
635
- * (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.
636
- */
637
- max_time?: number
638
- }
639
- }
640
- export declare type ConversationalReturn = {
641
- generated_text: string
642
- conversation: {
643
- generated_responses: string[]
644
- past_user_inputs: string[]
645
- }
646
- warnings: string[]
647
- }
648
- export declare type FeatureExtractionArgs = Args & {
649
- /**
650
- * The inputs vary based on the model. For example when using sentence-transformers/paraphrase-xlm-r-multilingual-v1 the inputs will look like this:
651
- *
652
- * inputs: {
653
- * "source_sentence": "That is a happy person",
654
- * "sentences": ["That is a happy dog", "That is a very happy person", "Today is a sunny day"]
655
- */
656
- inputs: Record<string, any> | Record<string, any>[]
657
- }
658
- /**
659
- * 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.
660
- */
661
- export declare type FeatureExtractionReturn = (number | number[])[]
662
- export declare type ImageClassificationArgs = Args & {
663
- /**
664
- * Binary image data
665
- */
666
- data: any
667
- }
668
- export declare type ImageClassificationReturnValue = {
669
- /**
670
- * The label for the class (model specific)
671
- */
672
- score: number
673
- /**
674
- * A float that represents how likely it is that the image file belongs to this class.
675
- */
676
- label: string
677
- }
678
- export declare type ImageClassificationReturn = ImageClassificationReturnValue[]
679
- export declare type ObjectDetectionArgs = Args & {
680
- /**
681
- * Binary image data
682
- */
683
- data: any
684
- }
685
- export declare type ObjectDetectionReturnValue = {
686
- /**
687
- * A float that represents how likely it is that the detected object belongs to the given class.
688
- */
689
- score: number
690
- /**
691
- * The label for the class (model specific) of a detected object.
692
- */
693
- label: string
694
- /**
695
- * A dict (with keys [xmin,ymin,xmax,ymax]) representing the bounding box of a detected object.
696
- */
697
- box: {
698
- xmin: number
699
- ymin: number
700
- xmax: number
701
- ymax: number
702
- }
703
- }
704
- export declare type ObjectDetectionReturn = ObjectDetectionReturnValue[]
705
- export declare type ImageSegmentationArgs = Args & {
706
- /**
707
- * Binary image data
708
- */
709
- data: any
710
- }
711
- export declare type ImageSegmentationReturnValue = {
712
- /**
713
- * A float that represents how likely it is that the detected object belongs to the given class.
714
- */
715
- score: number
716
- /**
717
- * The label for the class (model specific) of a segment.
718
- */
719
- label: string
720
- /**
721
- * A str (base64 str of a single channel black-and-white img) representing the mask of a segment.
722
- */
723
- mask: string
724
- }
725
- export declare type ImageSegmentationReturn = ImageSegmentationReturnValue[]
726
- export declare type AutomaticSpeechRecognitionArgs = Args & {
727
- /**
728
- * Binary audio data
729
- */
730
- data: any
731
- }
732
- export declare type AutomaticSpeechRecognitionReturn = {
733
- /**
734
- * The text that was recognized from the audio
735
- */
736
- text: string
737
- }
738
- export declare type AudioClassificationArgs = Args & {
739
- /**
740
- * Binary audio data
741
- */
742
- data: any
743
- }
744
- export declare type AudioClassificationReturnValue = {
745
- /**
746
- * The label for the class (model specific)
747
- */
748
- label: string
749
- /**
750
- * A float that represents how likely it is that the audio file belongs to this class.
751
- */
752
- score: number
753
- }
754
- export declare type AudioClassificationReturn = AudioClassificationReturnValue[]
755
- ```
package/dist/index.d.ts CHANGED
@@ -339,9 +339,10 @@ 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
  };
@@ -442,10 +443,21 @@ type AudioClassificationReturnValue = {
442
443
  score: number;
443
444
  };
444
445
  type AudioClassificationReturn = AudioClassificationReturnValue[];
446
+ type TextToImageArgs = Args & {
447
+ /**
448
+ * The text to generate an image from
449
+ */
450
+ inputs: string;
451
+ /**
452
+ * An optional negative prompt for the image generation
453
+ */
454
+ negative_prompt?: string;
455
+ };
456
+ type TextToImageReturn = ArrayBuffer;
445
457
  declare class HfInference {
446
458
  private readonly apiKey;
447
459
  private readonly defaultOptions;
448
- constructor(apiKey: string, defaultOptions?: Options);
460
+ constructor(apiKey?: string, defaultOptions?: Options);
449
461
  /**
450
462
  * Tries to fill in a hole with a missing word (token to be precise). That’s the base task for BERT models.
451
463
  */
@@ -516,12 +528,18 @@ declare class HfInference {
516
528
  * Recommended model: facebook/detr-resnet-50-panoptic
517
529
  */
518
530
  imageSegmentation(args: ImageSegmentationArgs, options?: Options): Promise<ImageSegmentationReturn>;
531
+ /**
532
+ * This task reads some text input and outputs an image.
533
+ * Recommended model: stabilityai/stable-diffusion-2
534
+ */
535
+ textToImage(args: TextToImageArgs, options?: Options): Promise<TextToImageReturn>;
519
536
  request(args: Args & {
520
537
  data?: any;
521
538
  }, options?: Options & {
522
539
  binary?: boolean;
540
+ blob?: boolean;
523
541
  }): Promise<any>;
524
542
  private static toArray;
525
543
  }
526
544
 
527
- export { Args, AudioClassificationArgs, AudioClassificationReturn, AudioClassificationReturnValue, AutomaticSpeechRecognitionArgs, AutomaticSpeechRecognitionReturn, ConversationalArgs, ConversationalReturn, FeatureExtractionArgs, FeatureExtractionReturn, FillMaskArgs, FillMaskReturn, HfInference, ImageClassificationArgs, ImageClassificationReturn, ImageClassificationReturnValue, ImageSegmentationArgs, ImageSegmentationReturn, ImageSegmentationReturnValue, ObjectDetectionArgs, ObjectDetectionReturn, ObjectDetectionReturnValue, Options, QuestionAnswerArgs, QuestionAnswerReturn, SummarizationArgs, SummarizationReturn, TableQuestionAnswerArgs, TableQuestionAnswerReturn, TextClassificationArgs, TextClassificationReturn, TextGenerationArgs, TextGenerationReturn, TokenClassificationArgs, TokenClassificationReturn, TokenClassificationReturnValue, TranslationArgs, TranslationReturn, ZeroShotClassificationArgs, ZeroShotClassificationReturn, ZeroShotClassificationReturnValue };
545
+ export { Args, AudioClassificationArgs, AudioClassificationReturn, AudioClassificationReturnValue, AutomaticSpeechRecognitionArgs, AutomaticSpeechRecognitionReturn, ConversationalArgs, ConversationalReturn, FeatureExtractionArgs, FeatureExtractionReturn, FillMaskArgs, FillMaskReturn, HfInference, ImageClassificationArgs, ImageClassificationReturn, ImageClassificationReturnValue, ImageSegmentationArgs, ImageSegmentationReturn, ImageSegmentationReturnValue, ObjectDetectionArgs, ObjectDetectionReturn, ObjectDetectionReturnValue, Options, QuestionAnswerArgs, QuestionAnswerReturn, SummarizationArgs, SummarizationReturn, TableQuestionAnswerArgs, TableQuestionAnswerReturn, TextClassificationArgs, TextClassificationReturn, TextGenerationArgs, TextGenerationReturn, TextToImageArgs, TextToImageReturn, TokenClassificationArgs, TokenClassificationReturn, TokenClassificationReturnValue, TranslationArgs, TranslationReturn, ZeroShotClassificationArgs, ZeroShotClassificationReturn, ZeroShotClassificationReturnValue };
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
  }
@@ -148,11 +148,31 @@ var HfInference = class {
148
148
  binary: true
149
149
  });
150
150
  }
151
+ /**
152
+ * This task reads some text input and outputs an image.
153
+ * Recommended model: stabilityai/stable-diffusion-2
154
+ */
155
+ async textToImage(args, options) {
156
+ return await this.request(args, {
157
+ ...options,
158
+ blob: true
159
+ });
160
+ }
151
161
  async request(args, options) {
152
162
  const mergedOptions = { ...this.defaultOptions, ...options };
153
163
  const { model, ...otherArgs } = args;
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
+ }
171
+ if (options?.binary && mergedOptions.wait_for_model) {
172
+ headers["X-Wait-For-Model"] = "true";
173
+ }
154
174
  const response = await fetch(`https://api-inference.huggingface.co/models/${model}`, {
155
- headers: { Authorization: `Bearer ${this.apiKey}` },
175
+ headers,
156
176
  method: "POST",
157
177
  body: options?.binary ? args.data : JSON.stringify({
158
178
  ...otherArgs,
@@ -165,11 +185,19 @@ var HfInference = class {
165
185
  wait_for_model: true
166
186
  });
167
187
  }
168
- const res = await response.json();
169
- if (res.error) {
170
- throw new Error(res.error);
188
+ let output;
189
+ if (options?.blob) {
190
+ if (!response.ok) {
191
+ throw new Error("An error occurred while fetching the blob");
192
+ }
193
+ return await response.arrayBuffer();
194
+ } else {
195
+ output = await response.json();
196
+ if (output.error) {
197
+ throw new Error(output.error);
198
+ }
171
199
  }
172
- return res;
200
+ return output;
173
201
  }
174
202
  static toArray(obj) {
175
203
  if (Array.isArray(obj)) {
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
  }
@@ -123,11 +123,31 @@ var HfInference = class {
123
123
  binary: true
124
124
  });
125
125
  }
126
+ /**
127
+ * This task reads some text input and outputs an image.
128
+ * Recommended model: stabilityai/stable-diffusion-2
129
+ */
130
+ async textToImage(args, options) {
131
+ return await this.request(args, {
132
+ ...options,
133
+ blob: true
134
+ });
135
+ }
126
136
  async request(args, options) {
127
137
  const mergedOptions = { ...this.defaultOptions, ...options };
128
138
  const { model, ...otherArgs } = args;
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
+ }
146
+ if (options?.binary && mergedOptions.wait_for_model) {
147
+ headers["X-Wait-For-Model"] = "true";
148
+ }
129
149
  const response = await fetch(`https://api-inference.huggingface.co/models/${model}`, {
130
- headers: { Authorization: `Bearer ${this.apiKey}` },
150
+ headers,
131
151
  method: "POST",
132
152
  body: options?.binary ? args.data : JSON.stringify({
133
153
  ...otherArgs,
@@ -140,11 +160,19 @@ var HfInference = class {
140
160
  wait_for_model: true
141
161
  });
142
162
  }
143
- const res = await response.json();
144
- if (res.error) {
145
- throw new Error(res.error);
163
+ let output;
164
+ if (options?.blob) {
165
+ if (!response.ok) {
166
+ throw new Error("An error occurred while fetching the blob");
167
+ }
168
+ return await response.arrayBuffer();
169
+ } else {
170
+ output = await response.json();
171
+ if (output.error) {
172
+ throw new Error(output.error);
173
+ }
146
174
  }
147
- return res;
175
+ return output;
148
176
  }
149
177
  static toArray(obj) {
150
178
  if (Array.isArray(obj)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@huggingface/inference",
3
- "version": "1.4.3",
3
+ "version": "1.5.1",
4
4
  "license": "MIT",
5
5
  "author": "Tim Mikeladze <tim.mikeladze@gmail.com>",
6
6
  "description": "Typescript wrapper for the Hugging Face Inference API",
@@ -364,9 +364,10 @@ 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
  };
@@ -485,11 +486,25 @@ export type AudioClassificationReturnValue = {
485
486
 
486
487
  export type AudioClassificationReturn = AudioClassificationReturnValue[];
487
488
 
489
+ export type TextToImageArgs = Args & {
490
+ /**
491
+ * The text to generate an image from
492
+ */
493
+ inputs: string;
494
+
495
+ /**
496
+ * An optional negative prompt for the image generation
497
+ */
498
+ negative_prompt?: string;
499
+ };
500
+
501
+ export type TextToImageReturn = ArrayBuffer;
502
+
488
503
  export class HfInference {
489
504
  private readonly apiKey: string;
490
505
  private readonly defaultOptions: Options;
491
506
 
492
- constructor(apiKey: string, defaultOptions: Options = {}) {
507
+ constructor(apiKey = "", defaultOptions: Options = {}) {
493
508
  this.apiKey = apiKey;
494
509
  this.defaultOptions = defaultOptions;
495
510
  }
@@ -645,18 +660,44 @@ export class HfInference {
645
660
  });
646
661
  }
647
662
 
663
+ /**
664
+ * This task reads some text input and outputs an image.
665
+ * Recommended model: stabilityai/stable-diffusion-2
666
+ */
667
+ public async textToImage(args: TextToImageArgs, options?: Options): Promise<TextToImageReturn> {
668
+ return await this.request(args, {
669
+ ...options,
670
+ blob: true,
671
+ });
672
+ }
673
+
648
674
  public async request(
649
675
  args: Args & { data?: any },
650
676
  options?: Options & {
651
677
  binary?: boolean;
678
+ blob?: boolean;
652
679
  }
653
680
  ): Promise<any> {
654
681
  const mergedOptions = { ...this.defaultOptions, ...options };
655
682
  const { model, ...otherArgs } = args;
683
+
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
+ }
692
+
693
+ if (options?.binary && mergedOptions.wait_for_model) {
694
+ headers["X-Wait-For-Model"] = "true";
695
+ }
696
+
656
697
  const response = await fetch(`https://api-inference.huggingface.co/models/${model}`, {
657
- headers: { Authorization: `Bearer ${this.apiKey}` },
658
- method: "POST",
659
- body: options?.binary
698
+ headers,
699
+ method: "POST",
700
+ body: options?.binary
660
701
  ? args.data
661
702
  : JSON.stringify({
662
703
  ...otherArgs,
@@ -671,11 +712,21 @@ export class HfInference {
671
712
  });
672
713
  }
673
714
 
674
- const res = await response.json();
675
- if (res.error) {
676
- throw new Error(res.error);
715
+ let output;
716
+
717
+ if (options?.blob) {
718
+ if (!response.ok) {
719
+ throw new Error("An error occurred while fetching the blob");
720
+ }
721
+ return await response.arrayBuffer();
722
+ } else {
723
+ output = await response.json();
724
+ if (output.error) {
725
+ throw new Error(output.error);
726
+ }
677
727
  }
678
- return res;
728
+
729
+ return output;
679
730
  }
680
731
 
681
732
  private static toArray(obj: any): any[] {