All checks were successful
Build and Push Docker Image / build (push) Successful in 8m35s
5.3 KiB
5.3 KiB
CLAUDE.md
This file provides guidance to Claude Code when working with this repository.
Overview
This is the Real-ESRGAN API project - a sophisticated, full-featured REST API for image upscaling using Real-ESRGAN. The API supports both synchronous and asynchronous (job-based) processing with Docker containerization for CPU deployments.
Architecture
Core Components
- app/main.py: FastAPI application with lifecycle management
- app/routers/: API endpoint handlers
upscale.py: Synchronous and async upscaling endpointsmodels.py: Model management endpointshealth.py: Health checks and system monitoring
- app/services/: Business logic
realesrgan_bridge.py: Real-ESRGAN model loading and inferencefile_manager.py: File handling and directory managementworker.py: Async job queue with thread poolmodel_manager.py: Model downloading and metadata
- app/schemas/: Pydantic request/response models
Data Directories
/data/uploads: User uploaded files/data/outputs: Processed output images/data/models: Real-ESRGAN model weights (.pth files)/data/temp: Temporary processing files/data/jobs: Async job metadata and status
Development Workflow
Local Setup
# Install dependencies
pip install -r requirements.txt
# Run development server
python -m uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
# Access API
# Swagger: http://localhost:8000/docs
# ReDoc: http://localhost:8000/redoc
Docker Development
# Build image
docker compose build
# Run container
docker compose up -d
# View logs
docker compose logs -f api
# Stop container
docker compose down
Configuration
Environment Variables (prefix: RSR_)
All settings from app/config.py can be configured via environment:
RSR_UPLOAD_DIR=/data/uploads
RSR_OUTPUT_DIR=/data/outputs
RSR_MODELS_DIR=/data/models
RSR_EXECUTION_PROVIDERS=["cpu"]
RSR_TILE_SIZE=400 # Tile size for large images
RSR_MAX_UPLOAD_SIZE_MB=500
RSR_SYNC_TIMEOUT_SECONDS=300
Docker Compose Environment
Set in docker-compose.yml environment section.
API Endpoints
Key Endpoints
- POST /api/v1/upscale: Synchronous upscaling (direct response)
- POST /api/v1/jobs: Create async upscaling job
- GET /api/v1/jobs/{job_id}: Check job status
- GET /api/v1/jobs/{job_id}/result: Download result
- GET /api/v1/models: List available models
- POST /api/v1/models/download: Download models
- GET /api/v1/health: Health check
- GET /api/v1/system: System information
Model Management
Available Models
'RealESRGAN_x2plus' # 2x upscaling
'RealESRGAN_x3plus' # 3x upscaling
'RealESRGAN_x4plus' # 4x general purpose (default)
'RealESRGAN_x4plus_anime_6B' # 4x anime/art (lighter)
Downloading Models
# Download specific model
curl -X POST http://localhost:8000/api/v1/models/download \
-H 'Content-Type: application/json' \
-d '{"models": ["RealESRGAN_x4plus"]}'
# List available models
curl http://localhost:8000/api/v1/models
Async Job Processing
Workflow
- Submit image → POST /api/v1/jobs → returns
job_id - Poll status → GET /api/v1/jobs/{job_id}
- When status=completed → GET /api/v1/jobs/{job_id}/result
Job States
queued: Waiting in queueprocessing: Currently being processedcompleted: Successfully processedfailed: Processing failed
Integration with facefusion-api
This project follows similar patterns to facefusion-api:
- File Management: Same
file_manager.pyutilities - Worker Queue: Similar async job processing architecture
- Docker Setup: CPU-only builds
- Configuration: Environment-based settings with pydantic
- Gitea CI/CD: Automatic Docker image building
- API Structure: Organized routers and services
Development Tips
Adding New Endpoints
- Create handler in
app/routers/{domain}.py - Define request/response schemas in
app/schemas/{domain}.py - Include router in
app/main.py:app.include_router(router)
Adding Services
- Create service module in
app/services/{service}.py - Import and use in routers
Testing
Run a quick test:
# Check API is running
curl http://localhost:8000/
# Check health
curl http://localhost:8000/api/v1/health
# Check system info
curl http://localhost:8000/api/v1/system
Troubleshooting
Models Not Loading
# Check if models directory exists and has permission
ls -la /data/models/
# Download models manually
curl -X POST http://localhost:8000/api/v1/models/download \
-H 'Content-Type: application/json' \
-d '{"models": ["RealESRGAN_x4plus"]}'
Permission Issues with Volumes
# Fix volume ownership
docker compose exec api chown -R 1000:1000 /data/
Git Workflow
The project uses Gitea as the remote repository:
# Add remote
git remote add gitea <gitea-repo-url>
# Commit and push
git add .
git commit -m "Add feature"
git push gitea main
Gitea workflows automatically:
- Build Docker image
- Run tests
- Publish to Container Registry
Important Notes
- Model Weights: Downloaded from GitHub releases (~100MB each)
- Async Processing: Uses thread pool (configurable workers)
- Tile Processing: Handles large images by splitting into tiles
- Data Persistence: Volumes recommended for production