# Deployment Guide for Hugging Face Spaces ## Step-by-Step Deployment ### 1. Create a Hugging Face Account - Go to https://huggingface.co - Sign up or log in ### 2. Create a New Space 1. Click on your profile → "New Space" 2. Fill in the details: - **Space name**: `virtual-tryon-api` (or your preferred name) - **License**: Apache 2.0 - **SDK**: Select **Docker** - **Hardware**: CPU (free) or upgrade to GPU for faster processing 3. Click "Create Space" ### 3. Upload Files to Your Space You can upload files via: #### Option A: Web Interface 1. In your Space, click "Files" → "Add file" → "Upload files" 2. Upload these files: - `app.py` - `requirements.txt` - `Dockerfile` - `README.md` 3. Commit the changes #### Option B: Git (Recommended) ```bash # Clone your Space repository git clone https://huggingface.co/spaces/YOUR-USERNAME/virtual-tryon-api cd virtual-tryon-api # Copy files cp /path/to/app.py . cp /path/to/requirements.txt . cp /path/to/Dockerfile . cp /path/to/README.md . # Commit and push git add . git commit -m "Initial commit: Virtual Try-On API" git push ``` ### 4. Wait for Build - Your Space will automatically start building - This may take 10-20 minutes for the first build - You can view build logs in the "Logs" tab ### 5. Test Your Deployment Once built, your API will be available at: ``` https://YOUR-USERNAME-virtual-tryon-api.hf.space ``` Test the health endpoint: ```bash curl https://YOUR-USERNAME-virtual-tryon-api.hf.space/health ``` ### 6. Update Your React Native App In your React Native app, update the API URL: ```javascript const API_URL = 'https://YOUR-USERNAME-virtual-tryon-api.hf.space'; ``` ## File Structure ``` your-space/ ├── app.py # Main FastAPI application ├── requirements.txt # Python dependencies ├── Dockerfile # Docker configuration └── README.md # Space documentation ``` ## Important Notes ### CPU vs GPU - **CPU (Free)**: - Free tier available - Slower processing (30-60 seconds per image) - Suitable for testing and low-volume use - **GPU (Paid)**: - Faster processing (5-15 seconds per image) - Costs apply (check HF pricing) - Recommended for production To enable GPU, modify your Space settings: 1. Go to Space Settings 2. Select GPU hardware 3. Rebuild ### Memory Considerations The models are large (~10GB). Ensure your Space has enough memory: - Minimum: 16GB RAM - Recommended: 32GB RAM (especially for CPU) ### Model Download Time On first run, models will download from Hugging Face: - Stable Diffusion XL: ~7GB - IP-Adapter: ~3GB - Body Segmentation: ~100MB This happens automatically but adds to initial startup time. ### Rate Limiting For production use, consider adding rate limiting: ```python from slowapi import Limiter from slowapi.util import get_remote_address limiter = Limiter(key_func=get_remote_address) app.state.limiter = limiter @app.post("/tryon") @limiter.limit("5/minute") # 5 requests per minute async def virtual_tryon(...): # ... existing code ``` ### Authentication For secure API access, add authentication: ```python from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials from fastapi import Security security = HTTPBearer() def verify_token(credentials: HTTPAuthorizationCredentials = Security(security)): if credentials.credentials != "your-secret-token": raise HTTPException(status_code=401, detail="Invalid token") @app.post("/tryon") async def virtual_tryon( token: HTTPAuthorizationCredentials = Security(verify_token), ... ): # ... existing code ``` ## Troubleshooting ### Build Fails - Check build logs in the "Logs" tab - Ensure all dependencies in requirements.txt are compatible - Verify Dockerfile syntax ### Out of Memory - Reduce model precision (use float16 instead of float32) - Reduce batch size - Upgrade to larger hardware tier ### Slow Performance - Consider upgrading to GPU hardware - Reduce `num_steps` parameter (trade quality for speed) - Implement caching for repeated requests ### API Not Responding - Check if Space is running (green status) - Verify the URL is correct - Check CORS settings if accessing from web ## Monitoring Monitor your Space: 1. **Usage**: Check Space settings for API call statistics 2. **Logs**: View application logs in real-time 3. **Performance**: Monitor response times ## Updating Your Space To update your deployed API: ```bash # Make changes to your files git add . git commit -m "Update: description of changes" git push ``` The Space will automatically rebuild with your changes. ## Support - Hugging Face Docs: https://huggingface.co/docs/hub/spaces - Community Forum: https://discuss.huggingface.co - Discord: https://discord.gg/hugging-face ## Next Steps 1. Test your API thoroughly 2. Implement authentication for production 3. Add rate limiting 4. Monitor usage and optimize 5. Consider upgrading to GPU for better performance