Production-style machine learning system for predicting e-commerce purchase intent in real time.
The project includes data validation and transformation, model training with MLflow tracking, a FastAPI prediction service, prediction logging, drift detection, and a Streamlit monitoring dashboard.
- Real-time prediction API with FastAPI (
/predict,/predict/batch,/health,/stats) - End-to-end training pipeline using XGBoost
- MLflow experiment tracking for params, metrics, and artifacts
- SQLite prediction logging for monitoring and drift analysis
- Drift detection on recent production predictions
- Streamlit dashboard for live model and system monitoring
- Docker + Docker Compose deployment
real-time-purchase-predictor/
├── data/
│ ├── raw/
│ ├── processed/
│ └── predictions.db
├── artifacts/
│ ├── train_data.csv
│ ├── test_data.csv
│ └── drift_report.json
├── models/
│ └── model.pkl
├── mlruns/
├── src/
│ ├── app.py
│ ├── components/
│ │ ├── data_ingestion.py
│ │ ├── data_transformation.py
│ │ ├── data_validation.py
│ │ ├── model_trainer.py
│ │ ├── model_evaluation.py
│ │ └── drift_detection.py
│ ├── pipeline/
│ │ ├── training_pipeline.py
│ │ └── prediction_pipeline.py
│ └── utils/
│ ├── logger.py
│ └── database.py
├── dashboard.py
├── demo_train.py
├── demo_training.py
├── demo_api.py
├── demo_database.py
├── demo_drift.py
├── demo_mlflow.py
├── Dockerfile
├── docker-compose.yml
└── requirements.txt
- Python 3.12
- FastAPI + Uvicorn
- Streamlit + Plotly
- XGBoost + scikit-learn
- MLflow
- SQLite
- Docker / Docker Compose
git clone https://github.com/M-Wajeeh/real-time-purchase-predictor.git
cd real-time-purchase-predictor
python -m venv .venv
# Windows PowerShell
.\.venv\Scripts\Activate.ps1
pip install -r requirements.txtEither command works:
python demo_train.py
# or
python demo_training.pyThis creates:
models/model.pklartifacts/train_data.csvartifacts/test_data.csv- MLflow run in
mlruns/
python -m uvicorn src.app:app --host 0.0.0.0 --port 8000 --reloadAPI docs: http://localhost:8000/docs
python -m streamlit run dashboard.pyDashboard: http://localhost:8501
Returns API and model health.
Single prediction for one transformed feature row.
Example request:
{
"features": {
"0": 0.12,
"1": 1.0,
"2": 0.0
},
"session_id": "optional-session-id"
}Batch predictions.
Returns prediction statistics from logged production data.
The Streamlit dashboard includes:
- API/model/database health status
- Key prediction metrics (volume, purchase rate, confidence)
- Model performance cards (from
artifacts/metrics.jsonor latest MLflow run) - Trend charts over time
- Drift report display
- Recent predictions table with CSV download
Training automatically logs to MLflow experiment purchase_prediction.
Run UI:
mlflow uiOpen: http://localhost:5000
Generate and save drift report:
python demo_drift.pyOutput file:
artifacts/drift_report.json
Build and run all services:
docker compose up --build -dServices:
- API:
http://localhost:8000 - Dashboard:
http://localhost:8501 - MLflow UI:
http://localhost:5000
Stop:
docker compose downRun training first:
python demo_train.pyDashboard will read from latest MLflow run even if artifacts/metrics.json is missing.
Start API before opening dashboard:
python -m uvicorn src.app:app --host 0.0.0.0 --port 8000Use python demo_train.py or updated compatibility script demo_training.py.
- Algorithm: XGBoost Classifier
- Features: 66 transformed features
- Last observed metrics:
- Accuracy: ~90.23%
- Precision: ~72.10%
- Recall: ~60.21%
- F1: ~65.62%
This project is licensed under the MIT License. See LICENSE for details.