@huggingface/inference 1.4.3 → 1.5.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/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
@@ -137,6 +137,12 @@ await hf.imageSegmentation({
137
137
  data: readFileSync('test/cats.png'),
138
138
  model: 'facebook/detr-resnet-50-panoptic'
139
139
  })
140
+
141
+ await hf.textToImage({
142
+ inputs: 'award winning high resolution photo of a giant tortoise/((ladybird)) hybrid, [trending on artstation]',
143
+ negative_prompt: 'blurry',
144
+ model: 'stabilityai/stable-diffusion-2',
145
+ })
140
146
  ```
141
147
 
142
148
  ## Supported APIs
@@ -167,6 +173,7 @@ await hf.imageSegmentation({
167
173
  - [x] Image classification
168
174
  - [x] Object detection
169
175
  - [x] Image segmentation
176
+ - [x] Text to image
170
177
 
171
178
  ## Running tests
172
179
 
@@ -296,6 +303,12 @@ export declare class HfInference {
296
303
  args: ImageSegmentationArgs,
297
304
  options?: Options
298
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>;
299
312
  request(
300
313
  args: Args & {
301
314
  data?: any
@@ -752,4 +765,15 @@ export declare type AudioClassificationReturnValue = {
752
765
  score: number
753
766
  }
754
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
755
779
  ```
package/dist/index.d.ts CHANGED
@@ -442,6 +442,17 @@ type AudioClassificationReturnValue = {
442
442
  score: number;
443
443
  };
444
444
  type AudioClassificationReturn = AudioClassificationReturnValue[];
445
+ type TextToImageArgs = Args & {
446
+ /**
447
+ * The text to generate an image from
448
+ */
449
+ inputs: string;
450
+ /**
451
+ * An optional negative prompt for the image generation
452
+ */
453
+ negative_prompt?: string;
454
+ };
455
+ type TextToImageReturn = ArrayBuffer;
445
456
  declare class HfInference {
446
457
  private readonly apiKey;
447
458
  private readonly defaultOptions;
@@ -516,12 +527,18 @@ declare class HfInference {
516
527
  * Recommended model: facebook/detr-resnet-50-panoptic
517
528
  */
518
529
  imageSegmentation(args: ImageSegmentationArgs, options?: Options): Promise<ImageSegmentationReturn>;
530
+ /**
531
+ * This task reads some text input and outputs an image.
532
+ * Recommended model: stabilityai/stable-diffusion-2
533
+ */
534
+ textToImage(args: TextToImageArgs, options?: Options): Promise<TextToImageReturn>;
519
535
  request(args: Args & {
520
536
  data?: any;
521
537
  }, options?: Options & {
522
538
  binary?: boolean;
539
+ blob?: boolean;
523
540
  }): Promise<any>;
524
541
  private static toArray;
525
542
  }
526
543
 
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 };
544
+ 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
@@ -148,11 +148,27 @@ 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
+ Authorization: `Bearer ${this.apiKey}`
166
+ };
167
+ if (options?.binary && mergedOptions.wait_for_model) {
168
+ headers["X-Wait-For-Model"] = "true";
169
+ }
154
170
  const response = await fetch(`https://api-inference.huggingface.co/models/${model}`, {
155
- headers: { Authorization: `Bearer ${this.apiKey}` },
171
+ headers,
156
172
  method: "POST",
157
173
  body: options?.binary ? args.data : JSON.stringify({
158
174
  ...otherArgs,
@@ -165,11 +181,19 @@ var HfInference = class {
165
181
  wait_for_model: true
166
182
  });
167
183
  }
168
- const res = await response.json();
169
- if (res.error) {
170
- throw new Error(res.error);
184
+ let output;
185
+ if (options?.blob) {
186
+ if (!response.ok) {
187
+ throw new Error("An error occurred while fetching the blob");
188
+ }
189
+ return await response.arrayBuffer();
190
+ } else {
191
+ output = await response.json();
192
+ if (output.error) {
193
+ throw new Error(output.error);
194
+ }
171
195
  }
172
- return res;
196
+ return output;
173
197
  }
174
198
  static toArray(obj) {
175
199
  if (Array.isArray(obj)) {
package/dist/index.mjs CHANGED
@@ -123,11 +123,27 @@ 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
+ Authorization: `Bearer ${this.apiKey}`
141
+ };
142
+ if (options?.binary && mergedOptions.wait_for_model) {
143
+ headers["X-Wait-For-Model"] = "true";
144
+ }
129
145
  const response = await fetch(`https://api-inference.huggingface.co/models/${model}`, {
130
- headers: { Authorization: `Bearer ${this.apiKey}` },
146
+ headers,
131
147
  method: "POST",
132
148
  body: options?.binary ? args.data : JSON.stringify({
133
149
  ...otherArgs,
@@ -140,11 +156,19 @@ var HfInference = class {
140
156
  wait_for_model: true
141
157
  });
142
158
  }
143
- const res = await response.json();
144
- if (res.error) {
145
- throw new Error(res.error);
159
+ let output;
160
+ if (options?.blob) {
161
+ if (!response.ok) {
162
+ throw new Error("An error occurred while fetching the blob");
163
+ }
164
+ return await response.arrayBuffer();
165
+ } else {
166
+ output = await response.json();
167
+ if (output.error) {
168
+ throw new Error(output.error);
169
+ }
146
170
  }
147
- return res;
171
+ return output;
148
172
  }
149
173
  static toArray(obj) {
150
174
  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.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",
@@ -485,6 +485,20 @@ export type AudioClassificationReturnValue = {
485
485
 
486
486
  export type AudioClassificationReturn = AudioClassificationReturnValue[];
487
487
 
488
+ export type TextToImageArgs = Args & {
489
+ /**
490
+ * The text to generate an image from
491
+ */
492
+ inputs: string;
493
+
494
+ /**
495
+ * An optional negative prompt for the image generation
496
+ */
497
+ negative_prompt?: string;
498
+ };
499
+
500
+ export type TextToImageReturn = ArrayBuffer;
501
+
488
502
  export class HfInference {
489
503
  private readonly apiKey: string;
490
504
  private readonly defaultOptions: Options;
@@ -645,18 +659,39 @@ export class HfInference {
645
659
  });
646
660
  }
647
661
 
662
+ /**
663
+ * This task reads some text input and outputs an image.
664
+ * Recommended model: stabilityai/stable-diffusion-2
665
+ */
666
+ public async textToImage(args: TextToImageArgs, options?: Options): Promise<TextToImageReturn> {
667
+ return await this.request(args, {
668
+ ...options,
669
+ blob: true,
670
+ });
671
+ }
672
+
648
673
  public async request(
649
674
  args: Args & { data?: any },
650
675
  options?: Options & {
651
676
  binary?: boolean;
677
+ blob?: boolean;
652
678
  }
653
679
  ): Promise<any> {
654
680
  const mergedOptions = { ...this.defaultOptions, ...options };
655
681
  const { model, ...otherArgs } = args;
682
+
683
+ const headers = {
684
+ Authorization: `Bearer ${this.apiKey}`,
685
+ }
686
+
687
+ if (options?.binary && mergedOptions.wait_for_model) {
688
+ headers["X-Wait-For-Model"] = "true";
689
+ }
690
+
656
691
  const response = await fetch(`https://api-inference.huggingface.co/models/${model}`, {
657
- headers: { Authorization: `Bearer ${this.apiKey}` },
658
- method: "POST",
659
- body: options?.binary
692
+ headers,
693
+ method: "POST",
694
+ body: options?.binary
660
695
  ? args.data
661
696
  : JSON.stringify({
662
697
  ...otherArgs,
@@ -671,11 +706,21 @@ export class HfInference {
671
706
  });
672
707
  }
673
708
 
674
- const res = await response.json();
675
- if (res.error) {
676
- throw new Error(res.error);
709
+ let output;
710
+
711
+ if (options?.blob) {
712
+ if (!response.ok) {
713
+ throw new Error("An error occurred while fetching the blob");
714
+ }
715
+ return await response.arrayBuffer();
716
+ } else {
717
+ output = await response.json();
718
+ if (output.error) {
719
+ throw new Error(output.error);
720
+ }
677
721
  }
678
- return res;
722
+
723
+ return output;
679
724
  }
680
725
 
681
726
  private static toArray(obj: any): any[] {