@huggingface/inference 1.4.2 → 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
@@ -19,9 +19,9 @@ pnpm add @huggingface/inference
19
19
  ### Basic examples
20
20
 
21
21
  ```typescript
22
- import HuggingFace from 'huggingface'
22
+ import { HfInference } from '@huggingface/inference'
23
23
 
24
- const hf = new HuggingFace('your api key')
24
+ const hf = new HfInference('your api key')
25
25
 
26
26
  // Natural Language
27
27
 
@@ -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
 
@@ -177,7 +184,7 @@ HF_ACCESS_TOKEN="your access token" npm run test
177
184
  ## Options
178
185
 
179
186
  ```typescript
180
- export declare class HuggingFace {
187
+ export declare class HfInference {
181
188
  private readonly apiKey
182
189
  private readonly defaultOptions
183
190
  constructor(apiKey: string, defaultOptions?: Options)
@@ -296,6 +303,12 @@ export declare class HuggingFace {
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,7 +442,18 @@ type AudioClassificationReturnValue = {
442
442
  score: number;
443
443
  };
444
444
  type AudioClassificationReturn = AudioClassificationReturnValue[];
445
- declare class HuggingFace {
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;
456
+ declare class HfInference {
446
457
  private readonly apiKey;
447
458
  private readonly defaultOptions;
448
459
  constructor(apiKey: string, defaultOptions?: Options);
@@ -516,12 +527,18 @@ declare class HuggingFace {
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, HuggingFace, 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, HuggingFace as default };
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
@@ -19,13 +19,12 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  // src/index.ts
20
20
  var src_exports = {};
21
21
  __export(src_exports, {
22
- HuggingFace: () => HuggingFace,
23
- default: () => src_default
22
+ HfInference: () => HfInference
24
23
  });
25
24
  module.exports = __toCommonJS(src_exports);
26
25
 
27
- // src/HuggingFace.ts
28
- var HuggingFace = class {
26
+ // src/HfInference.ts
27
+ var HfInference = class {
29
28
  apiKey;
30
29
  defaultOptions;
31
30
  constructor(apiKey, defaultOptions = {}) {
@@ -72,7 +71,7 @@ var HuggingFace = class {
72
71
  * 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
73
72
  */
74
73
  async tokenClassification(args, options) {
75
- return HuggingFace.toArray(await this.request(args, options));
74
+ return HfInference.toArray(await this.request(args, options));
76
75
  }
77
76
  /**
78
77
  * This task is well known to translate text from one language to another. Recommended model: Helsinki-NLP/opus-mt-ru-en.
@@ -84,7 +83,7 @@ var HuggingFace = class {
84
83
  * 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.
85
84
  */
86
85
  async zeroShotClassification(args, options) {
87
- return HuggingFace.toArray(await this.request(args, options));
86
+ return HfInference.toArray(await this.request(args, options));
88
87
  }
89
88
  /**
90
89
  * 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.
@@ -149,11 +148,27 @@ var HuggingFace = class {
149
148
  binary: true
150
149
  });
151
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
+ }
152
161
  async request(args, options) {
153
162
  const mergedOptions = { ...this.defaultOptions, ...options };
154
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
+ }
155
170
  const response = await fetch(`https://api-inference.huggingface.co/models/${model}`, {
156
- headers: { Authorization: `Bearer ${this.apiKey}` },
171
+ headers,
157
172
  method: "POST",
158
173
  body: options?.binary ? args.data : JSON.stringify({
159
174
  ...otherArgs,
@@ -166,11 +181,19 @@ var HuggingFace = class {
166
181
  wait_for_model: true
167
182
  });
168
183
  }
169
- const res = await response.json();
170
- if (res.error) {
171
- 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
+ }
172
195
  }
173
- return res;
196
+ return output;
174
197
  }
175
198
  static toArray(obj) {
176
199
  if (Array.isArray(obj)) {
@@ -179,10 +202,7 @@ var HuggingFace = class {
179
202
  return [obj];
180
203
  }
181
204
  };
182
-
183
- // src/index.ts
184
- var src_default = HuggingFace;
185
205
  // Annotate the CommonJS export names for ESM import in node:
186
206
  0 && (module.exports = {
187
- HuggingFace
207
+ HfInference
188
208
  });
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- // src/HuggingFace.ts
2
- var HuggingFace = class {
1
+ // src/HfInference.ts
2
+ var HfInference = class {
3
3
  apiKey;
4
4
  defaultOptions;
5
5
  constructor(apiKey, defaultOptions = {}) {
@@ -46,7 +46,7 @@ var HuggingFace = class {
46
46
  * 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
47
47
  */
48
48
  async tokenClassification(args, options) {
49
- return HuggingFace.toArray(await this.request(args, options));
49
+ return HfInference.toArray(await this.request(args, options));
50
50
  }
51
51
  /**
52
52
  * This task is well known to translate text from one language to another. Recommended model: Helsinki-NLP/opus-mt-ru-en.
@@ -58,7 +58,7 @@ var HuggingFace = class {
58
58
  * 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.
59
59
  */
60
60
  async zeroShotClassification(args, options) {
61
- return HuggingFace.toArray(await this.request(args, options));
61
+ return HfInference.toArray(await this.request(args, options));
62
62
  }
63
63
  /**
64
64
  * 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.
@@ -123,11 +123,27 @@ var HuggingFace = 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 HuggingFace = 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)) {
@@ -153,10 +177,6 @@ var HuggingFace = class {
153
177
  return [obj];
154
178
  }
155
179
  };
156
-
157
- // src/index.ts
158
- var src_default = HuggingFace;
159
180
  export {
160
- HuggingFace,
161
- src_default as default
181
+ HfInference
162
182
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@huggingface/inference",
3
- "version": "1.4.2",
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,7 +485,21 @@ export type AudioClassificationReturnValue = {
485
485
 
486
486
  export type AudioClassificationReturn = AudioClassificationReturnValue[];
487
487
 
488
- export class HuggingFace {
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
+
502
+ export class HfInference {
489
503
  private readonly apiKey: string;
490
504
  private readonly defaultOptions: Options;
491
505
 
@@ -546,7 +560,7 @@ export class HuggingFace {
546
560
  args: TokenClassificationArgs,
547
561
  options?: Options
548
562
  ): Promise<TokenClassificationReturn> {
549
- return HuggingFace.toArray(await this.request(args, options));
563
+ return HfInference.toArray(await this.request(args, options));
550
564
  }
551
565
 
552
566
  /**
@@ -563,7 +577,7 @@ export class HuggingFace {
563
577
  args: ZeroShotClassificationArgs,
564
578
  options?: Options
565
579
  ): Promise<ZeroShotClassificationReturn> {
566
- return HuggingFace.toArray(await this.request(args, options));
580
+ return HfInference.toArray(await this.request(args, options));
567
581
  }
568
582
 
569
583
  /**
@@ -645,18 +659,39 @@ export class HuggingFace {
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 HuggingFace {
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[] {
package/src/index.ts CHANGED
@@ -1,3 +1 @@
1
- import { HuggingFace } from "./HuggingFace";
2
- export * from "./HuggingFace";
3
- export default HuggingFace;
1
+ export * from "./HfInference";