Skip to content

Commit eda8ccd

Browse files
radamescoyotte508
andauthored
fix text-image parameters (#132)
Since the community api now [supports kwargs,](huggingface/api-inference-community@414c5a0), we can pass more diffusers parameters via the api, I choose a couple of them that [make sense](https://github.com/huggingface/diffusers/blob/fbc9a736dd5d8c20144ea44ef5266c87303ecacd/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py#L525-L542) For the test, we could check the output image dimensions, but it would require an import such as `canvas`, to convert the blob image to an image and get its dimensions. wdyt @coyotte508 The test with negative_prompt will fail until this [PR is merged](huggingface/api-inference-community#228), as it is now it requires `negative_prompt: ["...negative prompt"]` to be a list not a single value `negative_prompt: "...negative prompt"` --------- Co-authored-by: coyotte508 <[email protected]>
1 parent 9c606b2 commit eda8ccd

File tree

5 files changed

+86
-10
lines changed

5 files changed

+86
-10
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ await inference.translation({
9292

9393
await inference.textToImage({
9494
inputs: 'award winning high resolution photo of a giant tortoise/((ladybird)) hybrid, [trending on artstation]',
95-
negative_prompt: 'blurry',
9695
model: 'stabilityai/stable-diffusion-2',
96+
parameters: {
97+
negative_prompt: 'blurry',
98+
}
9799
})
98100
```
99101

packages/inference/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ A Typescript powered wrapper for the Hugging Face Inference API. Learn more abou
44

55
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).
66

7-
8-
97
## Install
108

119
```console
@@ -18,7 +16,7 @@ pnpm add @huggingface/inference
1816

1917
## Usage
2018

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 for **free**.
19+
**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 for **free**.
2220

2321
Your API key should be kept private. If you need to protect it in front-end applications, we suggest setting up a proxy server that stores the API key.
2422

@@ -154,8 +152,10 @@ await hf.imageSegmentation({
154152

155153
await hf.textToImage({
156154
inputs: 'award winning high resolution photo of a giant tortoise/((ladybird)) hybrid, [trending on artstation]',
157-
negative_prompt: 'blurry',
158155
model: 'stabilityai/stable-diffusion-2',
156+
parameters: {
157+
negative_prompt: 'blurry',
158+
}
159159
})
160160
```
161161

packages/inference/src/HfInference.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -582,10 +582,28 @@ export type TextToImageArgs = Args & {
582582
*/
583583
inputs: string;
584584

585-
/**
586-
* An optional negative prompt for the image generation
587-
*/
588-
negative_prompt?: string;
585+
parameters?: {
586+
/**
587+
* An optional negative prompt for the image generation
588+
*/
589+
negative_prompt?: string;
590+
/**
591+
* The height in pixels of the generated image
592+
*/
593+
height?: number;
594+
/**
595+
* The width in pixels of the generated image
596+
*/
597+
width?: number;
598+
/**
599+
* The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
600+
*/
601+
num_inference_steps?: number;
602+
/**
603+
* Guidance scale: Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, usually at the expense of lower image quality.
604+
*/
605+
guidance_scale?: number;
606+
};
589607
};
590608

591609
export type TextToImageReturn = Blob;

packages/inference/test/HfInference.spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,26 @@ describe.concurrent(
397397
it("textToImage", async () => {
398398
const res = await hf.textToImage({
399399
inputs: "award winning high resolution photo of a giant tortoise/((ladybird)) hybrid, [trending on artstation]",
400-
negative_prompt: "blurry",
401400
model: "stabilityai/stable-diffusion-2",
402401
});
402+
expect(res).toBeInstanceOf(Blob);
403+
});
403404

405+
it("textToImage with parameters", async () => {
406+
const width = 512;
407+
const height = 128;
408+
const num_inference_steps = 10;
409+
410+
const res = await hf.textToImage({
411+
inputs: "award winning high resolution photo of a giant tortoise/((ladybird)) hybrid, [trending on artstation]",
412+
model: "stabilityai/stable-diffusion-2",
413+
parameters: {
414+
negative_prompt: "blurry",
415+
width,
416+
height,
417+
num_inference_steps,
418+
},
419+
});
404420
expect(res).toBeInstanceOf(Blob);
405421
});
406422
},

packages/inference/test/tapes.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,5 +388,45 @@
388388
"vary": "Origin, Access-Control-Request-Method, Access-Control-Request-Headers"
389389
}
390390
}
391+
},
392+
"5d075f1b72a1944d61597a7bc7c3251c3d6b7f3b4deadce4fb6de54964bfeb74": {
393+
"url": "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2",
394+
"init": {
395+
"headers": {
396+
"Content-Type": "application/json"
397+
},
398+
"method": "POST"
399+
},
400+
"response": {
401+
"body": "",
402+
"status": 200,
403+
"statusText": "OK",
404+
"headers": {
405+
"access-control-allow-credentials": "true",
406+
"connection": "keep-alive",
407+
"content-type": "image/jpeg",
408+
"vary": "Origin, Access-Control-Request-Method, Access-Control-Request-Headers"
409+
}
410+
}
411+
},
412+
"9f9b1e0f1931cca2a77f4f39284fd1ca2e598b393d0c7ed36f05e66897cb762d": {
413+
"url": "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2",
414+
"init": {
415+
"headers": {
416+
"Content-Type": "application/json"
417+
},
418+
"method": "POST"
419+
},
420+
"response": {
421+
"body": "",
422+
"status": 200,
423+
"statusText": "OK",
424+
"headers": {
425+
"access-control-allow-credentials": "true",
426+
"connection": "keep-alive",
427+
"content-type": "image/jpeg",
428+
"vary": "Origin, Access-Control-Request-Method, Access-Control-Request-Headers"
429+
}
430+
}
391431
}
392432
}

0 commit comments

Comments
 (0)