A scalable backend service for hosting and serving machine learning models through REST APIs, built with Flask, RabbitMQ, MongoDB, and Docker.
- REST API: Clean REST endpoints for model inference with Swagger documentation
- Asynchronous Processing: Handles concurrent requests using Celery with Redis
- Model Management: Easy loading and serving of ML models with versioning support
- Data Persistence: Stores model outputs and user queries in MongoDB
- Containerization: Fully containerized with Docker and Kubernetes support
- Security: API authentication, rate limiting, and input validation
- Monitoring: Built-in health checks, analytics, and comprehensive logging
- Scalability: Horizontal scaling support with multiple workers
- Caching: Redis-based caching for improved performance
- A/B Testing: Built-in support for model A/B testing and traffic splitting
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Flask API │ │ Celery │ │ MongoDB │
│ (Port 5000) │───▶│ Workers │───▶│ (Port 27017) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ Redis │ │ Flower │
│ (Port 6379) │ │ (Port 5555) │
└─────────────────┘ └─────────────────┘
-
Clone and navigate to the project:
cd /Users/ayush/Downloads/Inferno -
Start all services:
docker-compose up -d
-
Initialize sample models:
docker-compose exec web python scripts/init_models.py -
Test the API:
python scripts/test_api.py
-
Install dependencies:
pip install -r requirements.txt
-
Start MongoDB and Redis:
# MongoDB mongod --dbpath /path/to/your/db # Redis redis-server
-
Initialize sample models:
python scripts/init_models.py
-
Start Celery worker (in a separate terminal):
celery -A app.celery worker --loglevel=info
-
Start Flask application:
python app.py
GET /healthReturns the health status of all services.
GET /modelsReturns a list of available ML models.
POST /inference/{model_name}
Content-Type: application/json
{
"features": [1.2, 2.3, 3.4, 4.5]
}GET /inference/{query_id}/statusGET /inference/{query_id}/resultGET /analytics/queriesReturns query statistics and model usage analytics.
- API: http://localhost:5000
- API Documentation: http://localhost:5000/docs (Swagger UI)
- Health Check: http://localhost:5000/health
- Flower Dashboard: http://localhost:5555
- MongoDB: localhost:27017
- Redis: localhost:6379
- API Key:
demo-key-123(for testing) - Admin Key:
admin-key-456(for admin operations)
import requests
import time
# Create inference request
response = requests.post('http://localhost:5000/inference/sample_classifier',
json={'features': [1.2, 2.3, 3.4, 4.5]})
query_id = response.json()['query_id']
# Check status
time.sleep(2)
status = requests.get(f'http://localhost:5000/inference/{query_id}/status')
print(status.json())
# Get result
result = requests.get(f'http://localhost:5000/inference/{query_id}/result')
print(result.json())# Health check
curl http://localhost:5000/health
# List models
curl http://localhost:5000/models
# Create inference
curl -X POST http://localhost:5000/inference/sample_classifier \
-H "Content-Type: application/json" \
-d '{"features": [1.2, 2.3, 3.4, 4.5]}'
# Check status
curl http://localhost:5000/inference/{query_id}/status-
Save your model in the
models/saved_models/directory:import joblib joblib.dump(your_model, 'models/saved_models/your_model.pkl')
-
Restart the service to load the new model:
docker-compose restart web celery_worker
- Scikit-learn models (
.pkl,.joblib) - Custom models with
predict()method
Access the Celery monitoring dashboard at: http://localhost:5555
- API health: http://localhost:5000/health
- Service status included in health response
Environment variables can be set in env.example:
# Flask Configuration
SECRET_KEY=your-secret-key-here
FLASK_ENV=development
# MongoDB Configuration
MONGO_URI=mongodb://localhost:27017/inferno
# Celery Configuration
CELERY_BROKER_URL=redis://localhost:6379/0
CELERY_RESULT_BACKEND=redis://localhost:6379/0Inferno/
├── app.py # Main Flask application
├── models/
│ ├── __init__.py
│ └── ml_model.py # ML model service
├── utils/
│ ├── __init__.py
│ └── validators.py # Input validation
├── scripts/
│ ├── init_models.py # Initialize sample models
│ └── test_api.py # API testing script
├── requirements.txt # Python dependencies
├── Dockerfile # Container configuration
├── docker-compose.yml # Multi-service setup
└── README.md # This file
python scripts/test_api.py- Update environment variables for production
- Use production-grade WSGI server (Gunicorn is already configured)
- Set up proper logging and monitoring
- Configure reverse proxy (nginx) if needed
- Set up SSL/TLS for HTTPS
- Connection refused: Ensure all services are running
- Model not found: Check model files in
models/saved_models/ - Celery worker not processing: Check Redis connection
- MongoDB connection failed: Verify MongoDB is running and accessible
# View all logs
docker-compose logs
# View specific service logs
docker-compose logs web
docker-compose logs celery_workerThis project is open source and available under the MIT License.