Skip to content

Latest commit

 

History

50 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

huggingface-api-demo

codecov lint

🤗 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.

Prerequisites

Install

Install with uv:

uv sync

Install pre-commit into your git hooks:

uv run pre-commit install

Usage

Set 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.0

This will generate an output_<uuid>.png image.

Available options:

uv run src/main.py --help

Disable caching

The 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-cache

Set image size

Pass --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

License

MIT

Releases

Sponsor this project

Packages

Used by

Contributors

Languages