Skip to content

Latest commit

 

History

History
130 lines (99 loc) · 6.25 KB

File metadata and controls

130 lines (99 loc) · 6.25 KB

Text to Image

Generate an image based on a given text prompt.

For more details about the text-to-image task, check out its dedicated page! You will find examples and related materials.

Recommended models

Explore all available models and find the one that suits you best here.

Using the API

```bash curl https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev \ -X POST \ -d '{"inputs": "Astronaut riding a horse"}' \ -H 'Content-Type: application/json' \ -H "Authorization: Bearer hf_***" ``` ```py import requests

API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev" headers = {"Authorization": "Bearer hf_***"}

def query(payload): response = requests.post(API_URL, headers=headers, json=payload) return response.content image_bytes = query({ "inputs": "Astronaut riding a horse", })

You can access the image with PIL.Image for example

import io from PIL import Image image = Image.open(io.BytesIO(image_bytes))


To use the Python client, see `huggingface_hub`'s [package reference](https://huggingface.co/docs/huggingface_hub/package_reference/inference_client#huggingface_hub.InferenceClient.text_to-image).
</python>

<js>
```js
async function query(data) {
	const response = await fetch(
		"https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev",
		{
			headers: {
				Authorization: "Bearer hf_***"
				"Content-Type": "application/json",
			},
			method: "POST",
			body: JSON.stringify(data),
		}
	);
	const result = await response.blob();
	return result;
}
query({"inputs": "Astronaut riding a horse"}).then((response) => {
	// Use image
});

To use the JavaScript client, see huggingface.js's package reference.

API specification

Request

Payload
inputs* string The input text data (sometimes called "prompt")
parameters object Additional inference parameters for Text To Image
        guidance_scale number A higher guidance scale value encourages the model to generate images closely linked to the text prompt, but values too high may cause saturation and other artifacts.
        negative_prompt string[] One or several prompt to guide what NOT to include in image generation.
        num_inference_steps integer The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
        width integer Image width.
        height integer Image height.
        scheduler string Override the scheduler with a compatible one.
        seed integer Seed for the random number generator.

Some options can be configured by passing headers to the Inference API. Here are the available headers:

Headers
authorization string Authentication header in the form 'Bearer: hf_****' when hf_**** is a personal user access token with Inference API permission. You can generate one from your settings page.
x-use-cache boolean, default to true There is a cache layer on the inference API to speed up requests we have already seen. Most models can use those results as they are deterministic (meaning the outputs will be the same anyway). However, if you use a nondeterministic model, you can set this parameter to prevent the caching mechanism from being used, resulting in a real new query. Read more about caching here.
x-wait-for-model boolean, default to false 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. Read more about model availability here.

For more information about Inference API headers, check out the parameters guide.

Response

| Body | | | :--- | :--- | :--- | | image | unknown | The generated image returned as raw bytes in the payload. |