title: ML Inference Service with Drift Detection
emoji: π
colorFrom: yellow
colorTo: red
sdk: docker
app_file: Dockerfile
pinned: false
license: mit
ML Inference Service with Drift Detection
Overview
This project is an end-to-end machine learning inference service with post-deployment drift detection, monitoring and logging. It exposes a REST API for model predictions, continuously monitors data for drift using a rolling window, and provides a dashboard to visualize recent predictions and drift metrics. It used kaggle dataset for initial data, which is then prepared and processed for the model and to use as reference.
The system is designed to run locally on venv python 3.9 or on Hugging Face Spaces, with minimal dependencies and a purposely simple front-end dashboard.
Hugging Face Space: LeonardoMdSA / ML Inference Service with Drift Detection
Repository Structure
ML Inference Service with Drift Detection/
ββ app/
β ββ api/ # FastAPI routes, background tasks, and drift logic
β ββ inference/ # Model wrapper and predictor logic
β ββ monitoring/ # Drift checks, governance, and data loaders
β ββ templates/ # HTML templates for dashboard
β ββ utils/ # Utility scripts like validators and alert senders
β ββ core/ # Configurations, constants
β ββ main.py # FastAPI entry point with lifespan tasks
ββ data/
β ββ processed/ # Input CSVs for predictions
β ββ production/ # Predictions log CSV
ββ models/ # Model artifacts and reference datasets
ββ reports/ # Drift and dashboard JSON/HTML outputs
β ββ evidently/ # Drift report JSON
ββ tests/ # Unit and integration tests
ββ Dockerfile # Container configuration
ββ requirements.txt # Python dependencies
ββ README.md
Installation (Local / venv)
Clone the repository.
Create a virtual environment and activate it:
py 3.9 -m venv .venv source .venv/bin/activate # Linux/macOS .\.venv\Scripts\activate # WindowsInstall dependencies:
python -m pip install --upgrade pip pip install -r requirements-dev.txt
Running the API Locally
Start the FastAPI server:
uvicorn app.main:app --reloadOpen the dashboard:
- Localhost: http://127.0.0.1:8000/
Testing
Run all tests with pytest:
pytest -vIntegration tests cover API endpoints, predictions, schema validation, and governance alerts.
How It Works (Logic Layers)
- API Layer: FastAPI routes handle
/predict,/dashboard/dataand/health. Predictions are appended todata/production/predictions_log.csv. - Inference Layer:
Predictorwraps the model, loads features fromFEATURES_PATH, and performs batch predictions. - Background Drift Loop: Continuously monitors recent predictions (rolling window up to 9,000 rows), runs feature-level drift checks, and writes results to
reports/evidently/drift_report.json. - Governance: Checks metrics like PSI, F1, and regression accuracy against thresholds and logs alerts. Sends notifications via email or Slack (if configured).
- Dashboard: Reads
drift_report.jsonand displays recent predictions and drift metrics via Plotly charts.
Technology Stack
- Python 3.9
- FastAPI
- Uvicorn
- Pydantic 1
- Jinja2
- Pandas / NumPy
- Joblib (for model serialization)
- Evidently (drift detection)
- Plotly (frontend charts)
- Docker (for containerized deployment)
- Hugging Face Spaces (deployment)
- GitHub Actions (CI/CD)
Recommendations / Important Notes
- CSV Rolling Window:
MAX_DRIFT_ROWSlimits the predictions log to 9,000 rows. Older rows are removed to prevent oversized files. - Email Alerts: SMTP server must be configured; otherwise, alert sending will fail.
- HF Spaces: The dashboard runs at
/endpoint by default for compatibility.
References / Docs
Contact / Author
- Hugging Face: https://huggingface.co/LeonardoMdSA
- GitHub: https://github.com/LeonardoMdSACode
MIT License
This project is licensed under the MIT License. See the LICENSE file for details.