File size: 4,849 Bytes
a0f3d24 8e70432 a0f3d24 b2715a4 8e70432 b2715a4 8e70432 b2715a4 7c39b61 b2715a4 6813c02 b2715a4 8e70432 b2715a4 7c39b61 b2715a4 7c39b61 b2715a4 7c39b61 b2715a4 8e70432 b2715a4 2c04cce b2715a4 8e70432 b2715a4 8e70432 b2715a4 2c04cce 8e70432 b2715a4 8e70432 b2715a4 8e70432 b2715a4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | ---
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](https://www.kaggle.com/datasets/uciml/default-of-credit-card-clients-dataset?select=UCI_Credit_Card.csv) 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](https://huggingface.co/spaces/LeonardoMdSA/ML-Inference-Service-with-Drift-Detection)
---
## Repository Structure
```text
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)
1. Clone the repository.
2. Create a virtual environment and activate it:
```bash
py 3.9 -m venv .venv
source .venv/bin/activate # Linux/macOS
.\.venv\Scripts\activate # Windows
```
3. Install dependencies:
```bash
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
```
## Running the API Locally
1. Start the FastAPI server:
```bash
uvicorn app.main:app --reload
```
2. Open the dashboard:
* Localhost: [http://127.0.0.1:8000/](http://127.0.0.1:8000/)
## Testing
1. Run all tests with pytest:
```bash
pytest -v
```
2. Integration tests cover API endpoints, predictions, schema validation, and governance alerts.
## How It Works (Logic Layers)
1. **API Layer**: FastAPI routes handle `/predict`, `/dashboard/data` and `/health`. Predictions are appended to `data/production/predictions_log.csv`.
2. **Inference Layer**: `Predictor` wraps the model, loads features from `FEATURES_PATH`, and performs batch predictions.
3. **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`.
4. **Governance**: Checks metrics like PSI, F1, and regression accuracy against thresholds and logs alerts. Sends notifications via email or Slack (if configured).
5. **Dashboard**: Reads `drift_report.json` and 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_ROWS` limits 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
* [FastAPI Documentation](https://fastapi.tiangolo.com/)
* [Evidently AI](https://evidentlyai.com/)
* [Plotly Charts](https://plotly.com/javascript/)
## Contact / Author
* Hugging Face: [https://huggingface.co/LeonardoMdSA](https://huggingface.co/LeonardoMdSA)
* GitHub: [https://github.com/LeonardoMdSACode](https://github.com/LeonardoMdSACode)
## MIT License
This project is licensed under the MIT License. See the LICENSE file for details.
|