Spaces:
Sleeping
π Deployment Guide for Render.com
Pre-Deployment Checklist
- Updated
requirements.txtwithpsycopg2-binaryandgunicorn - Modified
database.pyfor hybrid SQLite/PostgreSQL support - Updated
config.pyto readSECRET_KEYfrom environment - Auto-migration enabled in
main.py - Push code to GitHub repository
- Create Render account
π¦ Step 1: Prepare Your Repository
- Commit all changes:
git add .
git commit -m "Prepare for Render deployment"
git push origin master
- Ensure these files exist:
- β
requirements.txt(with psycopg2-binary, gunicorn) - β
main.py(with Base.metadata.create_all) - β
app/database.py(hybrid support) - β
app/config.py(environment variables)
π Step 2: Deploy on Render
A. Create New Web Service
- Go to https://dashboard.render.com/
- Click "New +" β "Web Service"
- Connect your GitHub repository
- Select your repository:
Predict-Rating-Web-App
B. Configure Web Service
Fill in the following settings:
| Setting | Value |
|---|---|
| Name | vietnamese-rating-prediction (or your choice) |
| Region | Singapore / Oregon (closest to you) |
| Branch | master |
| Root Directory | (leave blank) |
| Runtime | Python 3 |
| Build Command | pip install -r requirements.txt |
| Start Command | gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:$PORT |
| Instance Type | Free |
C. Add Environment Variables
Click "Environment" tab and add:
| Key | Value | Notes |
|---|---|---|
SECRET_KEY |
your-super-secret-random-key-here-2024 |
Generate with: openssl rand -hex 32 |
PYTHON_VERSION |
3.11.0 |
Specify Python version |
DO NOT set DATABASE_URL manually - Render will auto-create it when you add PostgreSQL.
ποΈ Step 3: Add PostgreSQL Database
A. Create Database
In Render Dashboard, click "New +" β "PostgreSQL"
Configure:
- Name:
vietnamese-rating-db - Database:
rating_prediction - User: (auto-generated)
- Region: Same as web service
- PostgreSQL Version:
15 - Instance Type:
Free
- Name:
Click "Create Database"
B. Link Database to Web Service
- Go back to your Web Service
- Click "Environment" tab
- Click "Add Environment Variable"
- Select "Add from Database"
- Choose your
vietnamese-rating-db - It will auto-populate
DATABASE_URL
C. Verify Connection
The database.py will automatically:
- Detect
DATABASE_URLenvironment variable - Replace
postgres://withpostgresql:// - Connect to PostgreSQL
- Create all tables automatically
π― Step 4: Deploy & Monitor
A. Trigger Deployment
- After adding database, click "Manual Deploy" β "Deploy latest commit"
- Watch the build logs:
- β Installing dependencies
- β Creating database tables
- β Starting Gunicorn server
B. Check Deployment Logs
Look for these success messages:
π Running in PRODUCTION mode
π Creating database tables...
β
Database tables created successfully!
[INFO] Starting gunicorn
[INFO] Booting worker with pid: 123
C. Access Your Application
Your app will be available at:
https://vietnamese-rating-prediction.onrender.com
Important endpoints:
- Dashboard:
https://your-app.onrender.com/dashboard - API Docs (Swagger):
https://your-app.onrender.com/docs - Health Check:
https://your-app.onrender.com/health
π Troubleshooting
Issue 1: "Module not found" errors
Solution: Ensure all imports are in requirements.txt
pip freeze > requirements.txt
Issue 2: "Connection refused" to database
Solution:
- Verify
DATABASE_URLis set in environment variables - Check database status in Render dashboard
- Restart web service
Issue 3: "Port binding" errors
Solution: Use $PORT environment variable:
gunicorn main:app --bind 0.0.0.0:$PORT
Issue 4: ML model takes too long to load
Solution: Render Free Tier has limited RAM (512MB). Consider:
- Using a lighter model
- Lazy loading (load model on first request)
- Upgrading to Starter plan ($7/month)
Issue 5: Static files not loading
Solution: Ensure app/static/ directory exists and is committed to git
βοΈ Alternative Start Commands
Option 1: Basic Uvicorn (Single Worker)
uvicorn main:app --host 0.0.0.0 --port $PORT
Option 2: Gunicorn with Uvicorn Workers (Recommended)
gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:$PORT
Option 3: Gunicorn with Auto-scaling Workers
gunicorn main:app --workers 2 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:$PORT --timeout 120
π Performance Optimization
1. Reduce Model Loading Time
Edit app/services/ml_service.py:
# Lazy load model on first request instead of on startup
class MLPredictionService:
def __init__(self):
self.model = None
self.tokenizer = None
def _ensure_loaded(self):
if self.model is None:
# Load model here
pass
2. Enable Connection Pooling
Already configured in database.py:
engine = create_engine(
DATABASE_URL,
pool_pre_ping=True,
pool_recycle=300
)
3. Use Caching for Predictions
Consider adding Redis (Render add-on) for caching frequent predictions.
π Security Checklist
- Set strong
SECRET_KEYin environment variables - Restrict CORS origins in production (edit
main.py) - Enable HTTPS (automatic on Render)
- Set up database backups (Render PostgreSQL backups)
- Add rate limiting (consider using Render's DDoS protection)
- Review and sanitize all user inputs
π° Cost Breakdown (Free Tier)
| Service | Cost | Limitations |
|---|---|---|
| Web Service | FREE | 512MB RAM, Sleeps after 15min inactivity |
| PostgreSQL | FREE | 1GB storage, 97 connections |
| Bandwidth | FREE | 100GB/month |
Upgrade Considerations:
- If app sleeps: Upgrade to Starter ($7/month, always-on)
- If RAM issues: Upgrade to Standard ($25/month, 2GB RAM)
- If storage full: Upgrade database ($7/month, 10GB)
π Post-Deployment Testing
Test 1: Health Check
curl https://your-app.onrender.com/health
Expected: {"status":"healthy","service":"rating-prediction","version":"1.0.0"}
Test 2: Swagger UI
Visit: https://your-app.onrender.com/docs
- Try registering a user
- Login to get JWT token
- Test prediction endpoints
Test 3: Database Connection
Check logs for:
π Production Mode: Using PostgreSQL
β
Database tables created successfully!
π Additional Resources
- Render Docs: https://render.com/docs/deploy-fastapi
- PostgreSQL Guide: https://render.com/docs/databases
- Environment Variables: https://render.com/docs/environment-variables
- Custom Domains: https://render.com/docs/custom-domains
π Support
If you encounter issues:
- Check Render logs (Dashboard β Logs tab)
- Review this guide carefully
- Check Render community forum: https://community.render.com/
- Contact Render support (for paid plans)
Good luck with your deployment! π