Customer Retention Intelligence Platform is an end-to-end ML service that predicts customer churn risk for subscription businesses and exposes real-time and batch inference APIs.
- Reproducible training pipeline (
scripts/train.py) with persisted model + metrics metadata. - FastAPI service with health and readiness probes.
- Single and batch prediction endpoints.
- Test suite and lint checks for CI.
- Dockerfile + Compose support.
- Committed test dataset under
data/test/for deterministic local testing.
.
├── data/
│ ├── raw/
│ ├── processed/
│ └── test/
│ ├── customers_test.csv
│ └── batch_prediction_payload.json
├── models/
├── scripts/
│ └── train.py
├── src/customer_retention_intelligence_platform/
│ ├── api/
│ ├── pipeline/
│ └── utils/
├── tests/
├── Dockerfile
├── docker-compose.yml
├── Makefile
└── README.md
- Python 3.10+
pip- Optional: Docker 24+
- Create and activate virtual environment:
python -m venv .venv
source .venv/bin/activate- Install dependencies:
pip install -e '.[dev]'- Create environment file:
cp .env.example .envEnvironment variables:
APP_ENV:dev|staging|prod|testLOG_LEVEL:DEBUG|INFO|WARNING|ERRORMODEL_PATH: model artifact path (default expected:models/churn_model.joblib)THRESHOLD: decision threshold for predicted churn (0 < threshold < 1)
- Train a model:
make train- Start API:
make serve- Check service:
curl -s http://localhost:8000/health
curl -s http://localhost:8000/ready- Run tests:
make testTwo committed fixtures are available:
data/test/customers_test.csv: 20 labeled rows with full training schema (churnedincluded).data/test/batch_prediction_payload.json: ready-to-send API payload (itemsonly, nochurned).
- Start API (
make serve) - Send fixture payload:
curl -s -X POST http://localhost:8000/predict/batch \
-H 'Content-Type: application/json' \
-d @data/test/batch_prediction_payload.jsonhead -n 5 data/test/customers_test.csvDefault training (synthetic data generation + artifact persistence):
make trainCustom training data generation parameters:
python scripts/train.py --rows 10000 --seed 11 --raw-output data/raw/customers_seed11.csvArtifacts written:
- Model:
models/churn_model.joblib - Metrics/metadata:
models/churn_model.metrics.json
Base URL: http://localhost:8000
Liveness and model visibility.
Example response:
{
"status": "ok",
"model_loaded": true,
"model_path": "models/churn_model.joblib",
"model_version": "1"
}Readiness probe. Returns 503 if model artifact is missing.
Single prediction.
Request example:
{
"tenure_months": 5,
"monthly_spend": 130,
"support_tickets": 4,
"payment_failures": 2,
"used_mobile_app": 0,
"has_family_plan": 0,
"region": "east"
}Batch prediction for up to 500 records.
Request shape:
{
"items": [
{
"tenure_months": 12,
"monthly_spend": 70,
"support_tickets": 1,
"payment_failures": 0,
"used_mobile_app": 1,
"has_family_plan": 1,
"region": "north"
}
]
}make install: install editable package + dev dependencies.make train: train and persist artifacts.make serve: run API with Uvicorn on port8000.make test: runpytest -q.make lint: run Ruff lint checks.make format: run Ruff autofix.make docker-build: build container image.make docker-run: run container onlocalhost:8000.
Build image:
make docker-buildRun container:
make docker-runOr run with compose:
docker compose up --build503 Model artifact missing: runmake trainfirst.- Import errors in scripts: confirm virtual environment is active.
- Threshold behavior unexpected: verify
THRESHOLDin.env. - API starts but predictions fail: ensure
models/churn_model.joblibandmodels/churn_model.metrics.jsonboth exist.
- Replace synthetic generation with warehouse/feature-store ingestion.
- Add auth and rate limiting to prediction endpoints.
- Add monitoring for drift and retraining triggers.
- Track online/offline model performance over time.