Production-Ready AI Service Template with MLOps Best Practices
Features β’ Quick Start β’ API Docs β’ Architecture β’ Contributing
|
|
graph LR
A[Client] -->|HTTP/HTTPS| B[FastAPI]
B --> C[Redis Cache]
B --> D[PostgreSQL]
B --> E[ML Models]
B --> F[Prometheus]
F --> G[Grafana]
Tech Stack:
- Framework: FastAPI, Uvicorn, Pydantic
- ML/AI: PyTorch, Transformers, Pillow
- Database: PostgreSQL, Redis
- Monitoring: Prometheus, Grafana
- Deployment: Docker, Kubernetes
- CI/CD: GitHub Actions, pytest, black, flake8, mypy
- Python 3.11+
- Docker & Docker Compose (recommended)
- 4GB RAM minimum
# Clone repository
git clone https://github.com/Priyanshjain10/ai-fastapi-mlops.git
cd ai-fastapi-mlops
# Configure environment
cp .env.example .env
# Edit .env and set your passwords
# Start all services
docker-compose up -d
# View logs
docker-compose logs -f api
# Stop services
docker-compose downAccess Services:
- π API: http://localhost:8000
- π Docs: http://localhost:8000/docs
- π Grafana: http://localhost:3000 (admin/changeme)
- π Prometheus: http://localhost:9090
# Create virtual environment
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Run application
uvicorn api.main:app --reload
# Run tests
pytest tests/ -v --cov=api
# Format code
black api/
# Lint code
flake8 api/ai-fastapi-mlops/
βββ .github/
β βββ workflows/
β βββ ci.yml # CI/CD pipeline
βββ api/
β βββ main.py # FastAPI application
βββ monitoring/
β βββ prometheus.yml # Prometheus configuration
βββ tests/
β βββ __init__.py
β βββ test_api.py # Comprehensive tests
βββ .env.example # Environment template
βββ .gitignore # Git ignore rules
βββ CONTRIBUTING.md # Contribution guidelines
βββ CODE_OF_CONDUCT.md # Code of conduct
βββ LICENSE # MIT License
βββ docker-compose.yml # Multi-service stack
βββ Dockerfile # Production container
βββ requirements.txt # Python dependencies
βββ README.md # This file
GET / - API Information
{
"message": "AI FastAPI MLOps Service",
"status": "running",
"version": "1.0.0"
}GET /health - Health Check
{
"status": "healthy",
"timestamp": 1699120800.123,
"version": "1.0.0"
}POST /predict/vision - Image Classification
curl -X POST "http://localhost:8000/predict/vision" \
-H "Content-Type: multipart/form-data" \
-F "[email protected]"Response:
{
"prediction": "demo_prediction",
"confidence": 0.85,
"model": "vit-base-patch16-224",
"inference_time_ms": 45.2,
"request_id": "req_a1b2c3d4"
}POST /predict/nlp - Text Analysis
curl -X POST "http://localhost:8000/predict/nlp" \
-H "Content-Type: application/json" \
-d '{"text": "This product is amazing!", "task": "sentiment"}'Response:
{
"prediction": "demo_positive",
"confidence": 0.92,
"model": "distilbert-base-uncased",
"inference_time_ms": 23.8,
"request_id": "req_e5f6g7h8"
}# Run all tests
pytest tests/ -v
# With coverage
pytest tests/ --cov=api --cov-report=html
# Run specific test
pytest tests/test_api.py::test_health_endpoint -vTest Coverage: >80%
- β Input validation with Pydantic
- β File size limits (10MB max)
- β File type validation
- β Non-root Docker container
- β Environment variable configuration
- β Automated security scanning (bandit)
- β CORS configuration
- β Health checks
| Metric | Value |
|---|---|
| Latency (P50) | <50ms |
| Latency (P95) | <120ms |
| Throughput | 1000+ req/s |
| Memory | ~500MB per instance |
| Startup Time | <5s |
- π Request latency (P50, P95, P99)
- π’ Request throughput
- β Error rates (4xx, 5xx)
- β±οΈ Model inference time
- πΎ Memory usage
- π Service health
- API Performance Overview
- Model Inference Metrics
- System Resource Monitoring
- Error Rate Tracking
- Core API with vision & NLP endpoints
- Docker & Docker Compose setup
- CI/CD pipeline with GitHub Actions
- Prometheus metrics integration
- Comprehensive testing suite
- Security scanning & type checking
- Redis caching implementation
- JWT authentication
- Rate limiting
- Kubernetes Helm charts
- Auto-scaling configuration
- More ML models (YOLO, CLIP, GPT)
Contributions are welcome! Please read our Contributing Guidelines and Code of Conduct.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'feat: Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Priyansh Jain
- π GitHub: @Priyanshjain10
- βοΈ Email: [email protected]
- πΌ LinkedIn: Connect with me