Flask-based ML observability service for collecting prediction telemetry, tracking performance/drift metrics, and exporting a deep JSON report.
- Ingests model telemetry through a REST API (
/api/v1/telemetry) - Stores events in SQLite (
database/predictions.db) - Serves a dashboard (
/dashboard) with: - Summary metrics (total/correct/wrong/accuracy)
- Accuracy trend
- Confusion matrix
- Data drift (confidence mean shift)
- Concept drift
- Latency trend
- Feature drift snapshot
- Class distribution
- Calibration bins
- Top FP/FN error pairs
- Exports a deep report JSON (
/api/report/download) - Includes a small Python SDK (
sdk/ml_monitor.py) and load simulator (sdk/test_integration.py)
- Python 3
- Flask
- SQLite
- Chart.js (frontend chart rendering)
ml-monitoring-system/
|- app.py # Flask app and API routes
|- database/
| |- db.py # DB init + CRUD + summary metrics
| `- schema.sql # Legacy schema snippet
|- services/
| |- advanced_monitor.py # Drift, trends, calibration, confusion matrix
| |- reporting.py # Deep report JSON builder
| |- retrain_advisor.py # Simple retrain recommendation helper
| |- retrain_dataset.py # Retrain-candidate storage/sync logic (not wired to routes)
| `- monitor.py # Low-confidence utility
|- sdk/
| |- ml_monitor.py # Client SDK for telemetry logging
| `- test_integration.py # Sends 100 synthetic logs
|- templates/ # Flask HTML templates
|- static/ # CSS/JS/assets
`- requirements.txt
- Create and activate a virtual environment.
- Install dependencies.
- Run the Flask app.
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt
python app.pyApp URL: http://127.0.0.1:5000
On startup, init_db() creates a default project if none exists:
- Name:
Default Project - API Key:
default-api-key
Logs one prediction event.
Example request:
{
"api_key": "default-api-key",
"prediction": "fraud",
"actual": "safe",
"confidence": 0.42,
"latency_ms": 118.4,
"features": {
"tx_amount": 250.0,
"user_age": 32
}
}Success response:
{
"status": "success",
"message": "Telemetry logged."
}Validation behavior:
- Missing
api_keyorpredictionreturns HTTP400 - Invalid API key returns HTTP
401
GET /- landing pageGET /dashboard- observability dashboard UIGET /api/projects- project listGET /metrics/summary?project_id=1GET /metrics/accuracy-trend?project_id=1GET /metrics/confusion-matrix?project_id=1GET /metrics/data-drift?project_id=1GET /metrics/outliers?project_id=1GET /metrics/concept-drift?project_id=1GET /metrics/latency-trend?project_id=1GET /metrics/feature-drift?project_id=1GET /metrics/class-distribution?project_id=1GET /metrics/model-calibration?project_id=1GET /metrics/fp-fn-trends?project_id=1GET /api/report/download?project_id=1- downloadable deep JSON report
from sdk.ml_monitor import MLMonitorClient
client = MLMonitorClient(api_key="default-api-key", host="http://127.0.0.1:5000", verify_ssl=False)
client.log_prediction(
prediction="fraud",
actual="safe",
confidence=0.73,
features={"tx_amount": 199.5, "user_age": 41},
execution_time_ms=92.6
)Run simulator:
python sdk\test_integration.pydatabase/db.py creates:
projects(id, name, api_key, created_at)predictions(id, project_id, prediction, actual, confidence, is_correct, is_outlier, latency_ms, features, timestamp)
Derived fields:
is_correct: set whenactualis providedis_outlier:1when confidence< 0.3or> 0.95
requirements.txtis missingrequests(required bysdk/ml_monitor.py).- Retrain UI/API assets exist (
templates/retrain_candidates.html,static/js/retrain_candidates.js) but corresponding Flask routes are not present inapp.py. services/retrain_dataset.pyexpects animage_pathcolumn inpredictions, but runtime schema indatabase/db.pydoes not include that column.database/schema.sqldoes not match the runtime schema used bydatabase/db.pyand appears to be a legacy file.reports/report_generator.pycallsfetch_metrics()without requiredproject_idand appears unused by the Flask app.
- Start server:
python app.py - Send telemetry (via SDK or HTTP POST).
- Open
http://127.0.0.1:5000/dashboard - Verify charts and summary populate.
- Download report from
/api/report/download?project_id=1