A complete, production-ready FastAPI ML deployment template that lets you deploy ANY machine learning model with just 3 steps:
- Drop your model into
app/models/ - Register it in
.env - Deploy!
# Start the API
docker-compose up -d
# Check it's running
curl http://localhost:8000/api/v1/health
# Test the forecast model
curl -X POST "http://localhost:8000/api/v1/predict/forecast" \
-H "Content-Type: application/json" \
-d '{"periods": 7, "freq": "D"}'
# Open the interactive docs
# Visit: http://localhost:8000/docs# Windows
scripts\setup.bat
# Linux/Mac
chmod +x scripts/setup.sh
./scripts/setup.sh
# Run the API
python run.py
# Visit: http://localhost:8000/docs| Document | Read When | Time |
|---|---|---|
| QUICKSTART.md | You want to start NOW | 5 min |
| README.md | You want complete details | 20 min |
| PROJECT_OVERVIEW.md | You want to understand architecture | 10 min |
| STRUCTURE.md | You want to navigate the project | 5 min |
| DEPLOYMENT.md | You're ready for production | 30 min |
| SUMMARY.md | You want a quick overview | 5 min |
Recommendation: Start with QUICKSTART.md β Then README.md β Then others as needed
http://localhost:8000/docs
Click on POST /api/v1/predict/forecast β "Try it out" β Paste:
{
"periods": 30,
"freq": "D"
}Click "Execute"
Click on POST /api/v1/predict/classifier β "Try it out" β Paste:
{
"features": [1.2, 3.4, 5.6, 7.8]
}Click "Execute"
Create app/models/mymodel.py:
from app.models.base import BaseMLModel
class MymodelModel(BaseMLModel):
def __init__(self):
super().__init__(model_name="mymodel", version="1.0")
self.model = None
async def load_model(self):
# Load your trained model here
# Example:
# import joblib
# self.model = joblib.load("model_storage/mymodel.pkl")
self.is_loaded = True
async def predict(self, input_data):
if not self.is_loaded:
raise RuntimeError(f"Model {self.model_name} is not loaded")
# Your prediction logic here
result = "your_prediction"
return {
"prediction": result,
"model": self.model_name,
"version": self.version
}Copy env.example to .env (if not already done), then edit:
MODEL_REGISTRY={"forecast": "forecast.py", "classifier": "classifier.py", "mymodel": "mymodel.py"}# Docker
docker-compose restart
# Local
# Stop (Ctrl+C) and run again: python run.pyTest your model:
curl -X POST "http://localhost:8000/api/v1/predict/mymodel" \
-H "Content-Type: application/json" \
-d '{"your": "input"}'your-project/
β
βββ app/
β βββ models/ β π― ADD YOUR MODELS HERE
β β βββ base.py (Base class - inherit from this)
β β βββ forecast.py (Example 1)
β β βββ classifier.py (Example 2)
β βββ api/ (Routes & schemas)
β βββ core/ (Config & logging)
β βββ services/ (Model service)
β βββ main.py (FastAPI app)
β
βββ examples/ (Example API requests)
βββ tests/ (Unit tests)
βββ scripts/ (Setup scripts)
βββ model_storage/ β Store your .pkl, .joblib files here
β
βββ Dockerfile (Docker config)
βββ docker-compose.yml (Docker Compose)
βββ requirements.txt (Dependencies)
βββ env.example β Copy to .env and edit
β
βββ Documentation/
βββ START_HERE.md (This file)
βββ QUICKSTART.md (Quick start guide)
βββ README.md (Complete guide)
βββ ...
Once running, you have these endpoints:
| Endpoint | Method | Purpose |
|---|---|---|
/docs |
GET | Interactive API documentation |
/api/v1/health |
GET | Health check |
/api/v1/models |
GET | List all loaded models |
/api/v1/models/{name} |
GET | Get info about a model |
/api/v1/predict/{model_name} |
POST | Make prediction (path-based) |
/api/v1/predict |
POST | Make prediction (body-based) |
/api/v1/models/{name}/reload |
POST | Reload a model |
All examples are in the examples/ folder:
examples/forecast_request.json- Forecast predictionexamples/classifier_request.json- Classificationexamples/test_requests.http- REST Client formatexamples/postman_collection.json- Import into Postman
# Run all tests
pytest
# With coverage report
pytest --cov=app
# Verbose output
pytest -v
# Specific test file
pytest tests/test_api.pyEdit .env:
PORT=8001Or in docker-compose.yml:
ports:
- "8001:8000"- Check
.envexists (copy fromenv.example) - Verify
MODEL_REGISTRYformat is correct JSON - Check model file names match registry
- View logs:
docker-compose logsor check terminal output
# Reinstall dependencies
pip install -r requirements.txt
# Or rebuild Docker
docker-compose build --no-cache- Make sure
HOST=0.0.0.0in.env - Check firewall settings
- Try
http://localhost:8000/docsandhttp://127.0.0.1:8000/docs
- Save your model:
joblib.dump(model, "model_storage/mymodel.pkl") - Create model file in
app/models/mymodel.py - In
load_model():self.model = joblib.load("model_storage/mymodel.pkl") - Register in
.env
Yes! The template works with:
- scikit-learn
- XGBoost
- TensorFlow/Keras
- PyTorch
- Prophet
- Any Python ML library
Just add the library to requirements.txt and use it in your model file.
See DEPLOYMENT.md for detailed guides:
- AWS (EC2, ECS, Lambda)
- Google Cloud (Cloud Run, GKE)
- Azure (Container Instances)
- Kubernetes
- Traditional VPS
Yes! See DEPLOYMENT.md security section for:
- API key authentication
- JWT tokens
- Rate limiting
- HTTPS setup
# In your model registry or code
model_service.load_model("forecast", "forecast.py", version="1")
model_service.load_model("forecast", "forecast_v2.py", version="2")
# Use specific version
curl "http://localhost:8000/api/v1/predict/forecast?version=2"Beginner:
- β Run the template with Docker
- β Test example models
- β
Read through one model file (
forecast.py) - β Modify example to return different data
Intermediate:
- β Create your own simple model
- β Add custom validation schemas
- β Run tests and add new tests
- β Deploy locally with nginx
Advanced:
- β Add authentication
- β Implement caching
- β Set up monitoring
- β Deploy to cloud (AWS/GCP/Azure)
- β Set up CI/CD pipeline
β
17 Python files (application code)
β
4 test files (comprehensive testing)
β
7 documentation files (2,500+ lines)
β
6 example files (request templates)
β
4 setup scripts (easy installation)
β
3 Docker files (containerization)
β
2 example ML models (forecast & classifier)
Total: 48+ files, production-ready!
- Reusable - Works with ANY ML model
- Fast - Deploy in minutes, not days
- Complete - Nothing left to implement
- Documented - Extensive guides included
- Production-Ready - Docker, logging, error handling
- Tested - Unit tests included
- Flexible - Easy to customize
- Template created β
- Run with Docker
- Test example models
- Read QUICKSTART.md
- Add your first model
- Read full README.md
- Run tests
- Customize for your needs
- Deploy to production
- Add authentication
- Set up monitoring
- Add more models
- Interactive Docs: http://localhost:8000/docs
- Alternative Docs: http://localhost:8000/redoc
- Health Check: http://localhost:8000/api/v1/health
- List Models: http://localhost:8000/api/v1/models
- Quick Start Issues: β Read QUICKSTART.md
- How does it work: β Read PROJECT_OVERVIEW.md
- Where is X: β Read STRUCTURE.md
- Deployment: β Read DEPLOYMENT.md
- Everything: β Read README.md
Before you proceed, make sure:
- Template files created β
- You've chosen your path (Docker or local)
- You've started the API
- You've visited http://localhost:8000/docs
- You've tested an example model
- You've read QUICKSTART.md
If all checked, you're ready to go! π
You now have a complete, production-ready ML deployment template that you can use for any project.
Next Step: Open QUICKSTART.md and start using it in 5 minutes!
Happy Coding! π
P.S. - Star this template if you find it useful!