# Hybrid Deployment: Gradio UI + FastAPI Endpoints ## Solution Overview The Hugging Face Space now runs in **hybrid mode**, providing BOTH: 1. **Gradio UI** - Interactive web interface for users 2. **FastAPI Endpoints** - Programmatic API access for integrations and monitoring ## Changes Made ### 1. Modified `hf_demo.py` Added FastAPI app mounting to the Gradio demo: ```python def start_demo(): # Mount FastAPI app for API endpoints from backend.api.web import app as fastapi_app # Launch demo with API enabled and FastAPI mounted demo.queue() demo.launch( share=False, app_kwargs={"app": fastapi_app} # Mount FastAPI app ) ``` ### 2. Reverted `Dockerfile` Changed back to `--hfdemo` mode (which now includes API endpoints): ```dockerfile CMD ["python", "src/app.py","--hfdemo"] ``` ## How It Works Gradio's `app_kwargs` parameter allows mounting a FastAPI application alongside the Gradio interface. This means: - **Root URL** (`/`) → Gradio UI (interactive interface) - **API Routes** (`/api/*`) → FastAPI endpoints (programmatic access) ## Available Endpoints ### User Interface - **URL**: `https://gsstec-sci-image.hf.space/` - **Type**: Gradio interactive UI - **Purpose**: User-friendly image generation interface ### API Endpoints All FastAPI endpoints are accessible at `/api/*`: 1. **GET /api/ping** - Health check for UptimeRobot 2. **GET /api/health** - Detailed health status 3. **GET /api/info** - System information 4. **GET /api/config** - Current configuration 5. **GET /api/models** - Available models 6. **POST /api/generate** - General image generation 7. **POST /api/window7/generate-pathogen** - Pathogen visualization 8. **POST /api/window9/generate-molecule** - Molecular visualization 9. **GET /api/docs** - Interactive API documentation (Swagger UI) 10. **GET /api/redoc** - ReDoc documentation ## UptimeRobot Configuration **Monitor Type**: HTTP(s) **URL**: `https://gsstec-sci-image.hf.space/api/ping` **Method**: GET **Interval**: 5 minutes **Expected Response**: `200 OK` with `{"status": "ok"}` ## Testing ### Test the UI Visit: `https://gsstec-sci-image.hf.space/` ### Test the API ```bash # Ping endpoint curl https://gsstec-sci-image.hf.space/api/ping # Health endpoint curl https://gsstec-sci-image.hf.space/api/health # API documentation # Open in browser: https://gsstec-sci-image.hf.space/api/docs ``` ### Run Test Script ```powershell cd sci-image powershell -NoProfile -ExecutionPolicy Bypass -File "test-hf-space-endpoint.ps1" ``` ## Deployment Steps 1. **Commit Changes**: ```bash cd sci-image git add src/frontend/webui/hf_demo.py Dockerfile git commit -m "Add hybrid mode: Gradio UI + FastAPI endpoints" ``` 2. **Push to Hugging Face Space**: ```bash git push hf main ``` 3. **Wait for Rebuild** (5-10 minutes) 4. **Verify**: - UI: Visit `https://gsstec-sci-image.hf.space/` - API: `curl https://gsstec-sci-image.hf.space/api/ping` ## Benefits ✓ **User-Friendly**: Gradio UI for interactive use ✓ **Programmatic Access**: FastAPI endpoints for integrations ✓ **Monitoring**: `/api/ping` endpoint for UptimeRobot ✓ **Documentation**: Auto-generated API docs at `/api/docs` ✓ **Single Deployment**: One Space serves both purposes ## Integration Examples ### AEGIS Bio-Digital Lab 10 Integration ```javascript // Window 7: Generate pathogen visualization const response = await fetch('https://gsstec-sci-image.hf.space/api/window7/generate-pathogen', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ prompt: "Microscopic view of Ebola virus, scientific illustration", num_inference_steps: 12, guidance_scale: 2.0, width: 512, height: 512 }) }); const result = await response.json(); console.log('Generated image:', result.images[0]); // Base64 image ``` ## Troubleshooting ### Issue: API endpoints return 404 **Solution**: Ensure the Space has rebuilt after pushing the changes to `hf_demo.py` ### Issue: UI not loading **Solution**: Check that Gradio is properly launching. View Space logs for errors. ### Issue: Both UI and API slow **Solution**: This is expected on CPU-only instances. Consider upgrading to GPU Space for faster generation. ## Version History - **v1.2.0** (2026-06-05): Hybrid mode implementation - Added FastAPI mounting to Gradio demo - Provides both UI and API endpoints - Reverted to `--hfdemo` mode with enhanced functionality