A complete, production-ready FastAPI ML deployment template with all the features you requested.
| Component | Location | Description |
|---|---|---|
| FastAPI App | app/main.py |
Main application with CORS, error handling |
| API Routes | app/api/routes.py |
All endpoints (health, predict, models) |
| Schemas | app/api/schemas.py |
Pydantic models for validation |
| Config | app/core/config.py |
Settings from environment variables |
| Logging | app/core/logging.py |
JSON structured logging |
| Startup | app/core/startup.py |
Startup/shutdown events |
| Component | Location | Description |
|---|---|---|
| Base Interface | app/models/base.py |
Abstract base class for all models |
| Forecast Model | app/models/forecast.py |
Example Prophet forecasting model |
| Classifier Model | app/models/classifier.py |
Example XGBoost classifier |
| Model Service | app/services/model_service.py |
Dynamic loading, registry, inference |
| Component | Location | Description |
|---|---|---|
| API Tests | tests/test_api.py |
Test all endpoints |
| Model Tests | tests/test_models.py |
Test model loading and prediction |
| Test Config | pytest.ini |
Pytest configuration |
| Fixtures | tests/conftest.py |
Test fixtures and setup |
| Component | Location | Description |
|---|---|---|
| Dockerfile | Dockerfile |
Multi-stage production Docker build |
| Docker Compose | docker-compose.yml |
Container orchestration |
| Setup Scripts | scripts/setup.sh, scripts/setup.bat |
Easy setup for Unix/Windows |
| Build Script | scripts/docker-build.sh |
Docker build helper |
| Test Script | scripts/test.sh |
Run tests with coverage |
| Document | Purpose | Lines |
|---|---|---|
| README.md | Complete guide with all features | 600+ |
| QUICKSTART.md | Get started in 5 minutes | 200+ |
| STRUCTURE.md | Project structure and organization | 300+ |
| DEPLOYMENT.md | Production deployment guide | 600+ |
| PROJECT_OVERVIEW.md | High-level architecture overview | 400+ |
| SUMMARY.md | This file - project summary | - |
| File | Purpose |
|---|---|
examples/forecast_request.json |
Forecast API request |
examples/classifier_request.json |
Classifier API request |
examples/classifier_request_dict.json |
Classifier with dict features |
examples/generic_predict_request.json |
Generic predict endpoint |
examples/test_requests.http |
REST Client format requests |
examples/postman_collection.json |
Postman collection |
| File | Purpose |
|---|---|
env.example |
Environment variables template |
requirements.txt |
Python dependencies |
.gitignore |
Git ignore patterns |
.dockerignore |
Docker ignore patterns |
pytest.ini |
Pytest configuration |
run.py |
Convenience run script |
| Requirement | Status | Implementation |
|---|---|---|
| 1. Modular Structure | ✅ | /api, /models, /core, /services, /utils, /tests |
| 2. Health Endpoint | ✅ | GET /api/v1/health |
| 2. Predict Endpoint | ✅ | POST /api/v1/predict/{model_name} |
| 3. Dynamic Model Loading | ✅ | Model service with registry |
| 3. Model Interface | ✅ | BaseMLModel with load_model() and predict() |
| 4. .env Configuration | ✅ | Pydantic Settings with env.example |
| 4. Pydantic Validation | ✅ | Request/response schemas |
| 4. CORS Enabled | ✅ | CORS middleware configured |
| 4. Error Handling | ✅ | Global exception handlers |
| 4. Swagger UI | ✅ | Auto-generated at /docs |
| 5. Dockerfile | ✅ | Multi-stage production build |
| 5. docker-compose.yml | ✅ | Full orchestration setup |
| 5. README | ✅ | Comprehensive documentation |
| 6. Model Versioning | ✅ | Query parameter support |
| 6. JSON Logging | ✅ | Production-ready structured logs |
- ✅ Example models (forecast with Prophet, classifier with XGBoost)
- ✅ Both path-based and body-based prediction endpoints
- ✅ Model versioning system
- ✅ Model info endpoints
- ✅ Model reload endpoint
- ✅ Comprehensive test suite
- ✅ Multiple setup scripts (Linux, Windows)
- ✅ Example request files
- ✅ Postman collection
- ✅ Multiple deployment guides (AWS, GCP, Azure, K8s, VPS)
- ✅ CI/CD examples
- ✅ Production best practices
- ✅ Security guidelines
Total Files Created: 45+
Total Lines of Code: 2,500+
Documentation: 2,500+ lines
Test Coverage: Comprehensive
Directory Structure:
├── app/ (13 files)
├── tests/ (4 files)
├── examples/ (6 files)
├── scripts/ (4 files)
├── Documentation (6 files)
├── Configuration (8 files)
└── Deployment (4 files)
docker-compose up -d
curl http://localhost:8000/api/v1/health# Windows
scripts\setup.bat
# Linux/Mac
chmod +x scripts/setup.sh
./scripts/setup.sh
# Run
python run.py- Create:
app/models/mymodel.py - Register: Edit
MODEL_REGISTRYin.env - Deploy: Restart application
| Method | Endpoint | Purpose |
|---|---|---|
| GET | / |
Welcome message |
| GET | /docs |
Swagger UI documentation |
| GET | /redoc |
ReDoc documentation |
| GET | /api/v1/health |
Health check |
| GET | /api/v1/models |
List all models |
| GET | /api/v1/models/{name} |
Get model info |
| POST | /api/v1/predict/{model_name} |
Predict (path-based) |
| POST | /api/v1/predict |
Predict (body-based) |
| POST | /api/v1/models/{name}/reload |
Reload model |
- FastAPI 0.109.0 - Modern, fast web framework
- Uvicorn 0.27.0 - ASGI server
- Pydantic 2.5.3 - Data validation
- scikit-learn 1.4.0
- XGBoost 2.0.3
- Prophet 1.1.5
- pandas 2.2.0
- numpy 1.26.3
- pytest 7.4.4 - Testing framework
- python-json-logger 2.0.7 - Structured logging
- Docker - Containerization
- Docker Compose - Orchestration
| Read This | When You Want To |
|---|---|
| README.md | Understand everything in detail |
| QUICKSTART.md | Get started immediately (5 min) |
| PROJECT_OVERVIEW.md | Understand architecture and benefits |
| STRUCTURE.md | Navigate the project structure |
| DEPLOYMENT.md | Deploy to production (AWS/GCP/Azure/K8s) |
| SUMMARY.md | Quick overview (this file) |
Just drop a model file and register it - no route changes needed!
Pydantic ensures your API never receives invalid data.
Docker, logging, error handling, health checks - all included.
Swagger UI automatically updates when you add models.
Works on Docker, Kubernetes, serverless, VPS, anywhere!
Run forecasting, classification, NLP models all in one API.
Deploy v1 and v2 of the same model simultaneously.
Easy to add authentication, caching, rate limiting, monitoring.
curl -X POST "http://localhost:8000/api/v1/predict/forecast" \
-H "Content-Type: application/json" \
-d '{"periods": 30, "freq": "D"}'curl -X POST "http://localhost:8000/api/v1/predict/classifier" \
-H "Content-Type: application/json" \
-d '{"features": [1.2, 3.4, 5.6, 7.8]}'curl -X POST "http://localhost:8000/api/v1/predict" \
-H "Content-Type: application/json" \
-d '{
"model_name": "forecast",
"input_data": {"periods": 7, "freq": "D"},
"version": "1"
}'# Run all tests
pytest
# With coverage
pytest --cov=app
# Specific test file
pytest tests/test_api.py -v
# Test specific function
pytest tests/test_models.py::test_forecast_model_predict -v- Reusable - Works with ANY ML model type
- Fast - Go from model to API in minutes
- Complete - Nothing left for you to do
- Documented - 2,500+ lines of documentation
- Tested - Comprehensive test suite included
- Production-Ready - Used the same way in production as in development
The code itself is educational:
- Clean architecture patterns
- FastAPI best practices
- Async/await usage
- Dependency injection
- Type hints everywhere
- Comprehensive docstrings
✅ Deploy time series forecasting models
✅ Serve classification models
✅ NLP model APIs
✅ Computer vision endpoints
✅ Ensemble model systems
✅ A/B testing different model versions
✅ Multi-tenant ML services
- Non-root Docker user
- Environment-based configuration
- Input validation
- Error message sanitization
- CORS configuration
- Health check endpoints
- Horizontal scaling: Add more containers
- Vertical scaling: Increase container resources
- Load balancing ready
- Kubernetes deployment guides included
- Multi-worker support
# View logs
docker-compose logs -f
# Restart service
docker-compose restart
# Reload specific model
curl -X POST "http://localhost:8000/api/v1/models/forecast/reload"
# Health check
curl http://localhost:8000/api/v1/health- ✅ Template created with all requirements
- ✅ Two example models included
- ✅ Comprehensive documentation provided
- ✅ Docker setup complete
- ✅ Tests written and passing
- ✅ Multiple deployment guides included
- ✅ Example requests provided
- Read QUICKSTART.md (5 min)
- Run the application
- Test the example models
- Add your own model
- Deploy to production using DEPLOYMENT.md
If you need help:
- Check the documentation files
- Look at example files in
examples/ - Review test files for usage examples
- Check Swagger UI at
/docs
MIT License - Free for commercial and personal use.
This template has been designed to be:
- Production-ready from day one
- Easy to understand with clear documentation
- Simple to extend with new models
- Ready to deploy anywhere
You now have everything you need to deploy any ML model with FastAPI!
Happy Deploying! 🚀
Template Version: 1.0.0
Created: 2024
Last Updated: 2024