Virtual Try-On Demo
A modern, full-stack virtual try-on web application featuring a Next.js frontend and FastAPI backend. Generate realistic virtual try-on images using state-of-the-art AI models.
Overviewโ
The Virtual Try-On Demo is a production-ready web application that showcases OpenTryOn's capabilities with a beautiful, user-friendly interface. Unlike the Gradio demos, this is a complete full-stack application suitable for production deployment.
Key Featuresโ
-
๐ค Multiple AI Models: Choose from 4 powerful image generation models
- Nano Banana (Gemini 2.5 Flash) - Fast, efficient, 1024px
- Nano Banana Pro (Gemini 3 Pro) - Advanced, up to 4K with search grounding
- FLUX 2 Pro - High-quality professional results
- FLUX 2 Flex - Flexible with advanced controls
-
๐ผ๏ธ Modern UI: Beautiful, responsive Next.js interface
-
๐ค Multi-Image Upload: Drag & drop support for model and garment images
-
๐ฐ Real-Time Cost Estimation: Dynamic credit calculation based on resolution and image count
-
๐ Live Processing: View generation progress and results
-
๐ฑ Responsive Design: Works on desktop, tablet, and mobile
Architectureโ
Frontend (Next.js 15)โ
- Modern React/TypeScript application
- Tailwind CSS for styling
- Real-time API communication
- Located in
demo/virtual-tryon/
Backend (FastAPI)โ
- RESTful API server
- Uses OpenTryOn SDK (
tryon.api) - Supports 4 model providers
- Automatic image processing and storage
- Located in project root:
api_server.py
Quick Startโ
Prerequisitesโ
Backend Requirements:
- Python 3.10+
- FastAPI 0.124.0
- Uvicorn 0.38.0
- OpenTryOn installed
Frontend Requirements:
- Node.js 18+
- npm or yarn
API Keys (at least one required):
GEMINI_API_KEYfor Nano Banana modelsBFL_API_KEYfor FLUX 2 models
Step 1: Set Up Environmentโ
Create a .env file in the project root:
# Google Gemini (for Nano Banana models)
GEMINI_API_KEY=your_gemini_api_key
# Black Forest Labs (for FLUX 2 models)
BFL_API_KEY=your_bfl_api_key
Step 2: Start the Backendโ
From the project root:
# Activate your environment
conda activate opentryon
# Start the FastAPI server
python api_server.py
The API server will start at http://localhost:8000
Verify it's running:
curl http://localhost:8000/health
Step 3: Start the Frontendโ
In a new terminal:
cd demo/virtual-tryon
npm install
npm run dev
The frontend will start at http://localhost:3000
Step 4: Try It Out!โ
- Open
http://localhost:3000in your browser - Select a model (Nano Banana, Nano Banana Pro, FLUX 2 Pro, or FLUX 2 Flex)
- Upload a model/person image
- Upload garment images (shirts, pants, accessories, etc.)
- Click "Generate" and wait for the AI-generated result!
API Referenceโ
Endpointsโ
POST /api/v1/virtual-tryonโ
Generate virtual try-on images.
Parameters:
model_image(file, required): Model/person imagegarment_images(files, required): One or more garment imagesprovider(string, optional): Model to usenano-banana(default) - Fast, 1024pxnano-banana-pro- Advanced, up to 4Kflux-2-pro- High qualityflux-2-flex- Advanced controls
prompt(string, optional): Custom generation promptresolution(string, optional): For nano-banana-pro:1K,2K, or4Kaspect_ratio(string, optional): e.g.,16:9,1:1- Additional FLUX 2 parameters:
width,height,seed,guidance,steps,safety_tolerance
Response:
{
"success": true,
"image": "data:image/png;base64,...",
"provider": "nano-banana",
"num_garments": 2,
"saved_path": "outputs/virtual_tryon/tryon_nano-banana_20250119_143022.png",
"filename": "tryon_nano-banana_20250119_143022.png",
"model_dimensions": {"width": 838, "height": 1176}
}
GET /healthโ
Health check endpoint.
Response:
{
"status": "healthy"
}
Model Comparisonโ
| Model | Speed | Quality | Max Resolution | Special Features |
|---|---|---|---|---|
| Nano Banana | โกโกโก Fast | โญโญโญ Good | 1024px | Efficient, low cost |
| Nano Banana Pro | โกโก Medium | โญโญโญโญ Great | 4K | Search grounding, high resolution |
| FLUX 2 Pro | โก Slow | โญโญโญโญโญ Excellent | Custom | Professional quality |
| FLUX 2 Flex | โก Slow | โญโญโญโญโญ Excellent | Custom | Advanced controls, fine-tuning |
Usage Examplesโ
Using curlโ
curl -X POST "http://localhost:8000/api/v1/virtual-tryon" \
-F "model_image=@person.jpg" \
-F "garment_images=@shirt.jpg" \
-F "garment_images=@jeans.jpg" \
-F "provider=nano-banana-pro" \
-F "resolution=2K"
Using Pythonโ
import requests
url = "http://localhost:8000/api/v1/virtual-tryon"
files = {
'model_image': open('person.jpg', 'rb'),
'garment_images': [
open('shirt.jpg', 'rb'),
open('jeans.jpg', 'rb')
]
}
data = {
'provider': 'flux-2-pro'
}
response = requests.post(url, files=files, data=data)
result = response.json()
if result['success']:
print(f"Image saved to: {result['saved_path']}")
Troubleshootingโ
"Failed to fetch" Errorโ
Cause: Frontend cannot connect to backend
Solutions:
- Ensure API server is running:
python api_server.py - Check if port 8000 is available:
lsof -i :8000 - Verify API URL in frontend (should be
http://localhost:8000) - Check browser console (F12) for detailed errors
API Key Errorsโ
Error: No API key provided or Invalid API key
Solution:
- Add API keys to
.envfile in project root - Restart the API server to load new environment variables
Import Errorsโ
Error: ModuleNotFoundError: No module named 'tryon'
Solution: Install OpenTryOn in development mode:
pip install -e .
Production Deploymentโ
Backend (API Server)โ
Deploy using uvicorn or gunicorn:
# Using uvicorn
uvicorn api_server:app --host 0.0.0.0 --port 8000 --workers 4
# Using gunicorn with uvicorn workers
gunicorn api_server:app \
--workers 4 \
--worker-class uvicorn.workers.UvicornWorker \
--bind 0.0.0.0:8000
Frontend (Next.js)โ
Build and deploy:
cd demo/virtual-tryon
npm run build
npm start
Or deploy to Vercel:
vercel deploy
Important: Set NEXT_PUBLIC_API_URL environment variable to your production API URL.
Configurationโ
Pricing Configurationโ
Edit demo/virtual-tryon/pricing_config.json to customize credit pricing for each model:
{
"models": {
"nano-banana": {
"pricing_strategy": "flat_rate",
"credits_per_image": 10
},
"nano-banana-pro": {
"pricing_strategy": "resolution_based",
"credits_by_resolution": {
"1K": 10,
"2K": 20,
"4K": 40
}
}
}
}
CORS Configurationโ
Modify api_server.py to add more allowed origins:
app.add_middleware(
CORSMiddleware,
allow_origins=[
"http://localhost:3000",
"http://127.0.0.1:3000",
"https://your-production-domain.com" # Add your domain
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
Learn Moreโ
- API Reference - OpenTryOn API adapters
- Nano Banana - Nano Banana documentation
Related Demosโ
- Extract Garment Demo - Garment extraction with Gradio
- Model Swap Demo - Model swapping with Gradio
- Outfit Generator Demo - Outfit generation with Gradio