A REST API for generating images and videos from text prompts using Hugging Face models.
- 🖼️ Fast image generation using FLUX.1-schnell model
- 🎥 Text-to-video generation using Wan-AI models
- 📐 Customizable image dimensions (default 16:9 aspect ratio)
- 🎨 Adjustable generation parameters (guidance scale, inference steps)
- 📖 Automatic API documentation with Swagger UI
- 🐳 Docker support for easy deployment
- ☁️ Ready for Coolify deployment
Root endpoint with API information
Health check endpoint - verifies API is running and HF_TOKEN is configured
Generate an image from a text prompt
Request Body:
{
"prompt": "Astronaut riding a horse on Mars, cinematic lighting, 4k",
"width": 1024,
"height": 576,
"guidance_scale": 7.5,
"num_inference_steps": 4
}Parameters:
prompt(required): Text description of the image to generatewidth(optional, default: 1024): Image width in pixels (256-2048)height(optional, default: 576): Image height in pixels (256-2048)guidance_scale(optional, default: 7.5): How closely to follow the prompt (1.0-20.0)num_inference_steps(optional, default: 4): Number of denoising steps (1-50)
Response: Returns the generated image directly as a PNG file
Generate a video from a text prompt
Request Body:
{
"prompt": "A young man walking on the street",
"model": "Wan-AI/Wan2.2-T2V-A14B"
}Parameters:
prompt(required): Text description of the video to generatemodel(optional, default: "Wan-AI/Wan2.2-T2V-A14B"): Text-to-video model to use
Response: Returns the generated video directly as an MP4 file
- Python 3.11+
- Hugging Face account and API token
- Go to https://huggingface.co/settings/tokens
- Create a new token with "Read" access
- Copy the token
- Clone the repository:
git clone <your-repo-url>
cd <your-repo-name>- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Create
.envfile:
cp .env.example .env- Edit
.envand add your Hugging Face token:
HF_TOKEN=hf_your_token_here
- Run the application:
# Using Python directly
python main.py
# OR using Uvicorn
uvicorn main:app --reload --host 0.0.0.0 --port 8000- Access the API:
- API Documentation: http://localhost:8000/docs
- Alternative Docs: http://localhost:8000/redoc
- Health Check: http://localhost:8000/health
# Build the image
docker build -t hf-image-generator .
# Run the container
docker run -d \
-p 8000:8000 \
-e HF_TOKEN=your_token_here \
--name hf-image-api \
hf-image-generatorCreate a docker-compose.yml:
version: '3.8'
services:
api:
build: .
ports:
- "8000:8000"
environment:
- HF_TOKEN=${HF_TOKEN}
restart: unless-stoppedRun with:
docker-compose up -d- Push to GitHub:
git init
git add .
git commit -m "Initial commit"
git remote add origin <your-github-repo-url>
git push -u origin main-
In Coolify:
- Create a new application
- Select "Docker" as the build pack
- Connect your GitHub repository
- Set the environment variable:
HF_TOKEN=your_token_here - Deploy!
-
Access your API:
- Your API will be available at your Coolify-provided URL
- Access docs at:
https://your-app.coolify.io/docs
Generate an image:
curl -X POST "http://localhost:8000/generate" \
-H "Content-Type: application/json" \
-d '{
"prompt": "A serene landscape with mountains and a lake at sunset",
"width": 1024,
"height": 576
}' \
--output generated_image.pngGenerate a video:
curl -X POST "http://localhost:8000/generate-video" \
-H "Content-Type: application/json" \
-d '{
"prompt": "A young man walking on the street",
"model": "Wan-AI/Wan2.2-T2V-A14B"
}' \
--output generated_video.mp4Generate an image:
import requests
response = requests.post(
"http://localhost:8000/generate",
json={
"prompt": "A cute cat wearing a wizard hat",
"width": 1024,
"height": 576,
"guidance_scale": 7.5
}
)
if response.status_code == 200:
with open("cat_wizard.png", "wb") as f:
f.write(response.content)
print("Image saved!")
else:
print(f"Error: {response.json()}")Generate a video:
import requests
response = requests.post(
"http://localhost:8000/generate-video",
json={
"prompt": "A young man walking on the street",
"model": "Wan-AI/Wan2.2-T2V-A14B"
}
)
if response.status_code == 200:
with open("generated_video.mp4", "wb") as f:
f.write(response.content)
print("Video saved!")
else:
print(f"Error: {response.json()}")Generate an image:
fetch('http://localhost:8000/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: 'A futuristic city with flying cars',
width: 1024,
height: 576
})
})
.then(response => response.blob())
.then(blob => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'generated_image.png';
a.click();
});Generate a video:
fetch('http://localhost:8000/generate-video', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: 'A young man walking on the street',
model: 'Wan-AI/Wan2.2-T2V-A14B'
})
})
.then(response => response.blob())
.then(blob => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'generated_video.mp4';
a.click();
});Once running, visit http://localhost:8000/docs to access the interactive Swagger UI where you can:
- See all available endpoints
- Test the API directly from your browser
- View request/response schemas
- Download generated images
- Width: 1024 pixels
- Height: 576 pixels
- Aspect Ratio: 16:9 (optimized for widescreen)
- Minimum: 256x256 pixels
- Maximum: 2048x2048 pixels
- You can use any dimension within this range
- 16:9 (Widescreen): 1024x576, 1920x1080
- 4:3 (Standard): 1024x768
- 1:1 (Square): 1024x1024
- 9:16 (Portrait): 576x1024
This API uses the FLUX.1-schnell model from Black Forest Labs:
- Optimized for speed (schnell = fast in German)
- High-quality image generation
- Default 4 inference steps for quick generation
- Hosted on Hugging Face Inference API
This API uses the Wan-AI/Wan2.2-T2V-A14B model by default:
- Advanced text-to-video generation
- High-quality video output
- Supports various video generation scenarios
- Hosted on Hugging Face Inference API
Make sure you've set the HF_TOKEN environment variable with your Hugging Face API token.
The first request may be slower as the model loads. Subsequent requests should be faster. Consider increasing num_inference_steps for higher quality (but slower generation).
Try reducing the image dimensions (width and height parameters).
MIT License - feel free to use this in your projects!
Contributions are welcome! Please feel free to submit a Pull Request.