AI-powered image generation backend using FastAPI and Stable Diffusion.
The Python backend is automatically started by the Blazor application!
From the project root, simply run:
Linux/macOS:
./start.shWindows:
start.batThe Blazor application will automatically:
- Create the Python virtual environment
- Install all dependencies
- Start the Python backend
- Connect everything together
No manual Python setup required!
If you need to run the Python backend independently:
- Python 3.10 or higher
- CUDA-capable GPU (recommended)
- 16GB+ RAM
- 10GB+ free disk space for models
Option 1: Use setup script
From project root:
./setup-python.sh # Linux/macOS
# or
setup-python.bat # WindowsOption 2: Manual setup
- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt# Activate virtual environment
source venv/bin/activate # On Windows: venv\Scripts\activate
# Start the server
python main.pyThe server will start on http://localhost:8000
Note: When using automatic startup, you don't need to run this manually!
Once the server is running, visit:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
Generate an image from a text prompt with optional LoRA models.
Request Body:
{
"prompt": "a fox in pixel art style",
"userId": "user123",
"loras": [
{
"file": "anime_style_v1.safetensors",
"strength": 0.75
}
]
}Train a new LoRA model using DreamBooth-style PEFT training.
Form Data:
lora_name: Name for the LoRA modeluser_id: User identifierimages: 1-5 image filesfast_mode: (optional) Enable fast training mode (default: false)num_train_epochs: (optional) Training epochs (auto-calculated if not specified)learning_rate: (optional) Learning rate (default: 1e-4)lora_rank: (optional) LoRA rank 4-32 (default: 8)with_prior_preservation: (optional) Use prior preservation (default: true)
Same as /train-lora but returns Server-Sent Events with real-time progress updates.
SSE Events:
progress: Training progress updates (phase, step, total_steps, loss, percentage)complete: Training completed successfully (lora_path, trigger_word)error: Training failed (error message)
Get list of all LoRA models for a user.
Get list of all generated images for a user.
Edit main.py to configure:
- Model selection (SDXL, SD Turbo, etc.)
- Default generation parameters
- File storage paths
- GPU settings
This Python backend is designed to be automatically managed by the Blazor application through PythonBackendHostedService.
When the Blazor app starts:
- ✅ Virtual environment is created automatically (if needed)
- ✅ Dependencies are installed automatically (if needed)
- ✅ This Python process is started as a child process
- ✅ Process is monitored and logs are captured
- ✅ Process is cleanly terminated when Blazor stops
Configuration in Blazor's appsettings.json:
{
"PythonBackend": {
"BaseUrl": "http://localhost:8000",
"Path": "../python-backend",
"AutoStart": true,
"AutoInstallDependencies": true
}
}To disable automatic startup, set AutoStart: false and run this backend manually.
The following packages are automatically installed:
- FastAPI (0.109.0) - Web framework
- Uvicorn (0.27.0) - ASGI server
- PyTorch (2.2.0) - Deep learning framework (~200MB)
- Torchvision (0.17.0) - Computer vision models
- Diffusers (0.30.0) - Stable Diffusion pipeline
- Transformers (4.44.0) - AI model library
- Accelerate (0.34.0) - Training acceleration
- Safetensors (0.4.5) - LoRA file format
- PEFT (0.13.0) - Parameter-Efficient Fine-Tuning
- Pillow (10.4.0) - Image processing
- NumPy (1.26.4) - Numerical computing
- First run will download the Stable Diffusion model (~6GB)
- GPU is highly recommended for acceptable generation speed
- LoRA training uses DreamBooth-style PEFT with prior preservation
- Training generates class images for prior preservation (25 in fast mode, 50 in standard)
- Class images are cached in
.class_images/folder and reused on retry - Known Issue: 10GB GPUs may experience OOM errors (12GB+ recommended for training)
- When using automatic startup, the virtual environment is at
venv/ - Python process logs are forwarded to the Blazor application console
- Dependencies are automatically updated to compatible versions
The training pipeline consists of:
- Class Image Generation: Generates 25-50 images of the base concept for prior preservation
- Model Loading: Loads SDXL UNet, VAE, and text encoders
- Training Loop: DreamBooth-style training with LoRA adapters
- Saving: Exports LoRA weights in diffusers-compatible
.safetensorsformat
Memory optimizations for consumer GPUs:
- Text encoders run on CPU (FP32) to save ~2-3GB VRAM
- Gradient checkpointing enabled
- 8-bit Adam optimizer (bitsandbytes)
- Mixed precision (FP16) training
- Aggressive GPU memory cleanup between phases