🤗 Hugging Face Serverless Inference API demo.
This project demonstrates how to generate images with text-to-image models (e.g. Stable Diffusion) using the Serverless Inference API and the huggingface_hub Python library.
It follows the Creating Images with Stable Diffusion section of the cookbook.
Install with uv:
uv syncInstall pre-commit into your git hooks:
uv run pre-commit installSet your Hugging Face token as an environment variable:
export HF_TOKEN=<your_huggingface_token>Run the demo:
uv run src/main.py "Astronaut riding a horse in space" \
--model stabilityai/stable-diffusion-xl-base-1.0This will generate an output_<uuid>.png image.
Available options:
uv run src/main.py --helpThe InferenceClient caches responses by default. To force a new generation with the same prompt, pass --no-cache:
uv run src/main.py "Astronaut riding a horse in space" \
--model stabilityai/stable-diffusion-xl-base-1.0 \
--no-cachePass --width and --height (in pixels). Model default sizes are used if omitted:
uv run src/main.py "Astronaut riding a horse in space" \
--model stabilityai/stable-diffusion-xl-base-1.0 \
--width 1024 \
--height 1024