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
- black-forest-labs/FLUX.1-dev: One of the most powerful image generation models that can generate realistic outputs.
- stabilityai/stable-diffusion-3-medium-diffusers: A powerful text-to-image model.
Explore all available models and find the one that suits you best here.
Using the API
Python
JavaScript
cURL
Using huggingface_hub
:
from huggingface_hub import InferenceClient
client = InferenceClient("black-forest-labs/FLUX.1-dev", token="hf_***")
# output is a PIL.Image object
image = client.text_to_image("Astronaut riding a horse")
Using requests
:
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.
API specification
Request
Payload | ||
---|---|---|
inputs* | string | The input text data (sometimes called “prompt”) |
parameters | object | |
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. |
target_size | object | The size in pixel of the output image |
width* | integer | |
height* | integer | |
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. |