@huggingface/inference
Advanced tools
Comparing version 1.4.3 to 1.5.0
@@ -445,2 +445,13 @@ type Options = { | ||
type AudioClassificationReturn = AudioClassificationReturnValue[]; | ||
type TextToImageArgs = Args & { | ||
/** | ||
* The text to generate an image from | ||
*/ | ||
inputs: string; | ||
/** | ||
* An optional negative prompt for the image generation | ||
*/ | ||
negative_prompt?: string; | ||
}; | ||
type TextToImageReturn = ArrayBuffer; | ||
declare class HfInference { | ||
@@ -520,2 +531,7 @@ private readonly apiKey; | ||
imageSegmentation(args: ImageSegmentationArgs, options?: Options): Promise<ImageSegmentationReturn>; | ||
/** | ||
* This task reads some text input and outputs an image. | ||
* Recommended model: stabilityai/stable-diffusion-2 | ||
*/ | ||
textToImage(args: TextToImageArgs, options?: Options): Promise<TextToImageReturn>; | ||
request(args: Args & { | ||
@@ -525,2 +541,3 @@ data?: any; | ||
binary?: boolean; | ||
blob?: boolean; | ||
}): Promise<any>; | ||
@@ -530,2 +547,2 @@ private static toArray; | ||
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 }; | ||
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 }; |
@@ -151,7 +151,23 @@ var __defProp = Object.defineProperty; | ||
} | ||
/** | ||
* This task reads some text input and outputs an image. | ||
* Recommended model: stabilityai/stable-diffusion-2 | ||
*/ | ||
async textToImage(args, options) { | ||
return await this.request(args, { | ||
...options, | ||
blob: true | ||
}); | ||
} | ||
async request(args, options) { | ||
const mergedOptions = { ...this.defaultOptions, ...options }; | ||
const { model, ...otherArgs } = args; | ||
const headers = { | ||
Authorization: `Bearer ${this.apiKey}` | ||
}; | ||
if (options?.binary && mergedOptions.wait_for_model) { | ||
headers["X-Wait-For-Model"] = "true"; | ||
} | ||
const response = await fetch(`https://api-inference.huggingface.co/models/${model}`, { | ||
headers: { Authorization: `Bearer ${this.apiKey}` }, | ||
headers, | ||
method: "POST", | ||
@@ -169,7 +185,15 @@ body: options?.binary ? args.data : JSON.stringify({ | ||
} | ||
const res = await response.json(); | ||
if (res.error) { | ||
throw new Error(res.error); | ||
let output; | ||
if (options?.blob) { | ||
if (!response.ok) { | ||
throw new Error("An error occurred while fetching the blob"); | ||
} | ||
return await response.arrayBuffer(); | ||
} else { | ||
output = await response.json(); | ||
if (output.error) { | ||
throw new Error(output.error); | ||
} | ||
} | ||
return res; | ||
return output; | ||
} | ||
@@ -176,0 +200,0 @@ static toArray(obj) { |
{ | ||
"name": "@huggingface/inference", | ||
"version": "1.4.3", | ||
"version": "1.5.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "author": "Tim Mikeladze <tim.mikeladze@gmail.com>", |
@@ -140,2 +140,8 @@ # 🤗 Hugging Face Inference API | ||
}) | ||
await hf.textToImage({ | ||
inputs: 'award winning high resolution photo of a giant tortoise/((ladybird)) hybrid, [trending on artstation]', | ||
negative_prompt: 'blurry', | ||
model: 'stabilityai/stable-diffusion-2', | ||
}) | ||
``` | ||
@@ -171,2 +177,3 @@ | ||
- [x] Image segmentation | ||
- [x] Text to image | ||
@@ -301,2 +308,8 @@ ## Running tests | ||
): Promise<ImageSegmentationReturn> | ||
/** | ||
* This task reads some text input and outputs an image. | ||
* Recommended model: stabilityai/stable-diffusion-2 | ||
*/ | ||
textToImage(args: TextToImageArgs, options?: Options): Promise<TextToImageReturn>; | ||
request( | ||
@@ -758,2 +771,13 @@ args: Args & { | ||
export declare type AudioClassificationReturn = AudioClassificationReturnValue[] | ||
type TextToImageArgs = Args & { | ||
/** | ||
* The text to generate an image from | ||
*/ | ||
inputs: string | ||
/** | ||
* An optional negative prompt for the image generation | ||
*/ | ||
negative_prompt?: string | ||
}; | ||
type TextToImageReturn = ArrayBuffer | ||
``` |
@@ -488,2 +488,16 @@ export type Options = { | ||
export type TextToImageArgs = Args & { | ||
/** | ||
* The text to generate an image from | ||
*/ | ||
inputs: string; | ||
/** | ||
* An optional negative prompt for the image generation | ||
*/ | ||
negative_prompt?: string; | ||
}; | ||
export type TextToImageReturn = ArrayBuffer; | ||
export class HfInference { | ||
@@ -649,2 +663,13 @@ private readonly apiKey: string; | ||
/** | ||
* This task reads some text input and outputs an image. | ||
* Recommended model: stabilityai/stable-diffusion-2 | ||
*/ | ||
public async textToImage(args: TextToImageArgs, options?: Options): Promise<TextToImageReturn> { | ||
return await this.request(args, { | ||
...options, | ||
blob: true, | ||
}); | ||
} | ||
public async request( | ||
@@ -654,2 +679,3 @@ args: Args & { data?: any }, | ||
binary?: boolean; | ||
blob?: boolean; | ||
} | ||
@@ -659,6 +685,15 @@ ): Promise<any> { | ||
const { model, ...otherArgs } = args; | ||
const headers = { | ||
Authorization: `Bearer ${this.apiKey}`, | ||
} | ||
if (options?.binary && mergedOptions.wait_for_model) { | ||
headers["X-Wait-For-Model"] = "true"; | ||
} | ||
const response = await fetch(`https://api-inference.huggingface.co/models/${model}`, { | ||
headers: { Authorization: `Bearer ${this.apiKey}` }, | ||
method: "POST", | ||
body: options?.binary | ||
headers, | ||
method: "POST", | ||
body: options?.binary | ||
? args.data | ||
@@ -678,7 +713,17 @@ : JSON.stringify({ | ||
const res = await response.json(); | ||
if (res.error) { | ||
throw new Error(res.error); | ||
let output; | ||
if (options?.blob) { | ||
if (!response.ok) { | ||
throw new Error("An error occurred while fetching the blob"); | ||
} | ||
return await response.arrayBuffer(); | ||
} else { | ||
output = await response.json(); | ||
if (output.error) { | ||
throw new Error(output.error); | ||
} | ||
} | ||
return res; | ||
return output; | ||
} | ||
@@ -685,0 +730,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
85045
1596
780