Spaces:
Sleeping
Sleeping
Deploying SwiftOps Backend to Hugging Face Spaces
π Quick Deployment Guide
Prerequisites
- Hugging Face account
- Supabase account (for PostgreSQL database)
- Redis instance (Upstash, Redis Cloud, or similar)
π Step-by-Step Deployment
1. Create a New Space
- Go to https://huggingface.co/spaces
- Click "Create new Space"
- Configure:
- Name:
swiftops-backend - License: MIT
- SDK: Docker
- Hardware: CPU Basic (or upgrade as needed)
- Name:
2. Set Up External Services
A. Supabase (PostgreSQL Database)
- Create project at https://supabase.com
- Get connection string from Settings β Database
- Format:
postgresql://postgres:[PASSWORD]@[HOST]:5432/postgres
B. Redis (Upstash or Redis Cloud)
- Create Redis instance at https://upstash.com or https://redis.com
- Get connection URL
- Format:
redis://default:[PASSWORD]@[HOST]:6379
3. Configure Environment Variables
In your Hugging Face Space settings, add these secrets:
# Required
DATABASE_URL=postgresql://postgres:[PASSWORD]@[HOST]:5432/postgres
SUPABASE_URL=https://[PROJECT].supabase.co
SUPABASE_KEY=[YOUR_ANON_KEY]
SUPABASE_SERVICE_KEY=[YOUR_SERVICE_KEY]
SECRET_KEY=[GENERATE_WITH: openssl rand -hex 32]
REDIS_URL=redis://default:[PASSWORD]@[HOST]:6379
CELERY_BROKER_URL=redis://default:[PASSWORD]@[HOST]:6379
CELERY_RESULT_BACKEND=redis://default:[PASSWORD]@[HOST]:6379
# Optional (for production features)
CLOUDINARY_CLOUD_NAME=[YOUR_CLOUD_NAME]
CLOUDINARY_API_KEY=[YOUR_API_KEY]
CLOUDINARY_API_SECRET=[YOUR_API_SECRET]
RESEND_API_KEY=[YOUR_RESEND_KEY]
GOOGLE_MAPS_API_KEY=[YOUR_GOOGLE_MAPS_KEY]
SENTRY_DSN=[YOUR_SENTRY_DSN]
# Application Settings
ENVIRONMENT=production
DEBUG=False
4. Push Code to Hugging Face
Option A: Using Git
# Add Hugging Face remote
git remote add hf https://huggingface.co/spaces/[YOUR_USERNAME]/swiftops-backend
# Push to Hugging Face
git push hf main
Option B: Using Hugging Face Hub
# Install huggingface_hub
pip install huggingface_hub
# Login
huggingface-cli login
# Upload repository
huggingface-cli upload [YOUR_USERNAME]/swiftops-backend . --repo-type=space
5. Verify Deployment
- Wait for Docker build to complete (5-10 minutes)
- Check build logs in Space settings
- Visit your Space URL:
https://huggingface.co/spaces/[YOUR_USERNAME]/swiftops-backend - Test endpoints:
- Health check:
https://[YOUR_SPACE].hf.space/health - API docs:
https://[YOUR_SPACE].hf.space/api/docs
- Health check:
π§ Important Configuration Changes
1. Port Configuration
Hugging Face Spaces uses port 7860 by default. The Dockerfile has been updated:
EXPOSE 7860
CMD uvicorn src.app.main:app --host 0.0.0.0 --port 7860
2. Database Migrations
Migrations run automatically on startup:
CMD alembic upgrade head && uvicorn src.app.main:app --host 0.0.0.0 --port 7860
3. Files Included in Deployment
Deployed:
- β
src/- Application code - β
alembic/- Database migrations - β
alembic.ini- Migration config - β
requirements.txt- Dependencies - β
Dockerfile- Container definition - β
README.md- Space description
Not Deployed (via .dockerignore):
- β
tests/- Test suite - β
scripts/- Utility scripts - β
docs/- Documentation - β
docker-compose.yml- Local development - β
.pre-commit-config.yaml- Dev tools - β
pytest.ini- Test config
π― Post-Deployment Tasks
1. Run Database Migrations
Migrations run automatically, but you can verify:
# Check Space logs to confirm migrations ran
# Look for: "Running upgrade ... -> ..."
2. Create Admin User
Use the Space's terminal or API to create first admin user:
# Via API (once deployed)
curl -X POST https://[YOUR_SPACE].hf.space/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "admin@swiftops.com",
"password": "secure_password",
"name": "Admin User",
"role": "platform_admin"
}'
3. Test API Endpoints
# Health check
curl https://[YOUR_SPACE].hf.space/health
# API documentation
open https://[YOUR_SPACE].hf.space/api/docs
π Troubleshooting
Build Fails
Check Dockerfile:
- Ensure all paths are correct
- Verify requirements.txt has all dependencies
- Check for syntax errors
Check Logs:
- Go to Space settings β Logs
- Look for error messages during build
Database Connection Issues
Verify Environment Variables:
- Check DATABASE_URL format
- Ensure Supabase allows connections from Hugging Face IPs
- Test connection string locally first
Supabase Configuration:
- Go to Supabase β Settings β Database
- Enable "Allow connections from anywhere" (or add HF IPs)
- Check connection pooling settings
Redis Connection Issues
Verify Redis URL:
- Check REDIS_URL format
- Ensure Redis instance is accessible
- Test connection with redis-cli
Application Errors
Check Logs:
# View Space logs in real-time
# Go to Space β Settings β Logs
Common Issues:
- Missing environment variables
- Database migration failures
- Redis connection timeout
- Port conflicts (should be 7860)
π Monitoring & Maintenance
View Logs
- Go to Space settings β Logs
- Monitor for errors and warnings
Update Application
# Make changes locally
git add .
git commit -m "Update feature"
git push hf main
# Space will automatically rebuild
Scale Resources
- Go to Space settings β Hardware
- Upgrade to CPU/GPU as needed
- Consider persistent storage for uploads
π Security Checklist
- All secrets stored as Space secrets (not in code)
- DEBUG=False in production
- CORS configured properly
- Rate limiting enabled
- Database backups configured (Supabase)
- Redis password protected
- API authentication working
- HTTPS enabled (automatic on HF Spaces)
π Additional Resources
- Hugging Face Spaces Documentation
- Docker Spaces Guide
- Supabase Documentation
- Upstash Redis Documentation
π Success!
Your SwiftOps backend is now deployed on Hugging Face Spaces!
API URL: https://[YOUR_USERNAME]-swiftops-backend.hf.space
API Docs: https://[YOUR_USERNAME]-swiftops-backend.hf.space/api/docs
Need help? Check the troubleshooting section or open an issue on GitHub.