π Hugging Face Spaces Deployment Guide
This repository is production-ready for deployment on Hugging Face Spaces using Docker runtime.
β Pre-Deployment Checklist
All requirements are already configured:
- β
FastAPI app:
api_server_extended.pydefinesapp = FastAPI(...) - β
Health endpoint:
/healthreturns service status - β Dockerfile: Configured with correct CMD for uvicorn
- β
Requirements: All dependencies listed in
requirements.txt - β
Port handling: Supports
${PORT}environment variable - β Resilient startup: Runs in degraded mode if some services fail
π― Deployment Steps
1. Create a New Space on Hugging Face
- Go to https://huggingface.co/spaces
- Click "Create new Space"
- Configure:
- Space name:
crypto-monitor-api(or your choice) - License: Choose appropriate license
- SDK: Select Docker
- Hardware: CPU Basic (minimum) or CPU Upgrade (recommended)
- Visibility: Public or Private
- Space name:
2. Push Repository to Space
# Clone your new Space
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
cd YOUR_SPACE_NAME
# Copy all files from this repository
cp -r /path/to/crypto-dt-source-main/* .
# Add and commit
git add .
git commit -m "Initial deployment of Crypto Monitor API"
# Push to Hugging Face
git push
3. Configure Space Settings (Optional)
In your Space settings, you can add these environment variables:
PORT- Port number (default: 7860 for HF, 8000 for local)ENABLE_AUTO_DISCOVERY- Enable auto-discovery service (default: false)- Add any API keys as Repository secrets (not in code!)
4. Monitor Deployment
- Go to your Space page
- Check the Logs tab for build progress
- Wait for "Running" status (usually 2-5 minutes)
- Access your API at:
https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space
π API Endpoints
Once deployed, your API will be available at:
Core Endpoints
- Root:
https://your-space.hf.space/ - API Docs:
https://your-space.hf.space/docs(Interactive Swagger UI) - Health Check:
https://your-space.hf.space/health - Status:
https://your-space.hf.space/api/status
Provider Management
GET /api/providers- List all crypto data providersGET /api/providers/{id}- Get provider detailsPOST /api/providers/{id}/health-check- Check provider healthGET /api/providers/category/{category}- Filter by category
Pool Management
GET /api/pools- List all provider poolsPOST /api/pools- Create new poolPOST /api/pools/{id}/members- Add provider to poolPOST /api/pools/{id}/rotate- Rotate pool providers
Real-time Updates
WS /ws- WebSocket connection for live updates
Monitoring & Diagnostics
GET /api/stats- System statisticsGET /api/logs- Application logsPOST /api/diagnostics/run- Run diagnostics
See /docs for complete API documentation with interactive testing.
π§ͺ Local Testing
Test with Docker (Recommended)
# Build the image
docker build -t crypto-monitor-test .
# Run the container
docker run -p 8000:8000 crypto-monitor-test
# Test health endpoint
curl http://localhost:8000/health
# Access API docs
open http://localhost:8000/docs
Test with Python
# Install dependencies
pip install -r requirements.txt
# Run locally
python main.py
# Or with uvicorn directly
uvicorn api_server_extended:app --host 0.0.0.0 --port 8000 --reload
π Troubleshooting
Build Fails
Check logs for specific errors:
- Missing dependencies? Verify
requirements.txt - Import errors? Ensure all local modules exist
- System dependencies? Check Dockerfile
apt-get installsection
Common fixes:
# Rebuild without cache
docker build --no-cache -t crypto-monitor-test .
# Check for syntax errors
python -m py_compile api_server_extended.py
Container Starts but Health Check Fails
Increase startup time:
Edit Dockerfile and increase start-period:
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD curl -f http://localhost:${PORT:-8000}/health || exit 1
Check logs:
docker logs <container_id>
Service Runs in Degraded Mode
This is normal if:
- Some external APIs are unavailable
- Network connectivity is limited
- Optional services fail to start
The service will still work with available providers. Check /health endpoint for details.
WebSocket Connection Issues
If WebSocket connections fail:
- Ensure your client uses
wss://(notws://) for HTTPS spaces - Check CORS settings in
api_server_extended.py - Verify firewall/proxy settings
π Performance Optimization
For Better Performance
- Upgrade Hardware: Use CPU Upgrade or GPU in Space settings
- Disable Auto-Discovery: Set
ENABLE_AUTO_DISCOVERY=false(already default) - Reduce Provider Count: Edit config files to monitor fewer providers
- Enable Caching: Already enabled by default
Resource Usage
- Memory: ~2-4 GB (depends on active providers)
- CPU: Low to moderate (spikes during health checks)
- Storage: ~500 MB (includes models and data)
π Security Best Practices
- Never commit API keys - Use HF Repository secrets
- Use HTTPS - Hugging Face provides this automatically
- Rate limiting - Already implemented via
slowapi - CORS - Configured to allow all origins (adjust if needed)
π Configuration Files
Required Files (Already Present)
Dockerfile- Container configurationrequirements.txt- Python dependenciesapi_server_extended.py- Main FastAPI application.dockerignore- Files to exclude from image
Optional Configuration
.env.example- Environment variable templateproviders_config_*.json- Provider configurationscrypto_resources_*.json- Resource definitions
π Features
This deployment includes:
β 200+ Crypto Data Providers - Comprehensive coverage β Provider Pools - Load balancing and failover β Real-time WebSocket - Live updates β Health Monitoring - Automatic health checks β Auto-Discovery - Find new data sources (optional) β Diagnostics - Built-in troubleshooting β Logging System - Comprehensive logging β Resource Management - Import/export configs β Rate Limiting - Prevent abuse β CORS Support - Cross-origin requests β API Documentation - Interactive Swagger UI
π Support
Check Service Status
# Health check
curl https://your-space.hf.space/health
# Detailed status
curl https://your-space.hf.space/api/status
# Run diagnostics
curl -X POST https://your-space.hf.space/api/diagnostics/run
Common Issues
- Space shows "Building" - Wait 2-5 minutes for first build
- Space shows "Runtime Error" - Check logs tab for details
- API returns 503 - Service starting up, wait 30-60 seconds
- Slow responses - Upgrade hardware or reduce provider count
Get Help
- Check
/api/diagnostics/runfor automatic issue detection - Review Space logs for error messages
- Test locally with Docker to isolate issues
- Check Hugging Face Spaces documentation
π License
See LICENSE file for details.
Ready to deploy! Follow the steps above to get your Crypto Monitor API running on Hugging Face Spaces.