How chanhbui297/dseb65a-group04-customer-churn-predictor works
1. This repository is an end-to-end customer churn prediction system with three primary runtime surfaces: an offline training pipeline, an online FastAPI inference service, and a Streamlit user interface. Around those are monitoring, retraining, and Kubernetes deployment assets.
2. The core machine-learning flow starts with versioned data under data/raw and data/preprocessed, managed with DVC. The repository tracks the raw CSVs, the preprocessed parquet, and the trained model artifact through .dvc pointers rather than committing the full files directly.
3. Training logic lives in src/models/train_model.py. Per the README, training first applies stateless feature creation through src/features/engineer.py, then fits an sklearn Pipeline consisting of preprocessing plus model, and finally serializes the best estimator with joblib into models/. Model metadata and baseline statistics are also stored there for later comparison.
4. The inference boundary is the FastAPI app in src/api/main.py, with request/response schemas in src/api/schemas.py and model loading/prediction logic in src/api/inference.py. The API exposes at least /health and /predict, and it expects the same row-level feature engineering used during training before calling the saved sklearn pipeline.
5. The Streamlit app in streamlit_app/app.py is the end-user front end. Based on the Kubernetes manifests and README, it is deployed separately from the API and communicates with the FastAPI service rather than embedding model logic itself.
6. Monitoring is a separate subsystem under monitoring/ and mirrored in k8s/monitoring/. Prometheus scrapes metrics, and Grafana dashboards visualize churn/model behavior; the included churn_dashboard.json and provisioning files indicate the dashboards and datasources are preconfigured rather than set up manually at runtime.
7. Drift detection is handled by monitoring/detect_drift.py, which likely compares incoming or recent data distributions against the stored baseline stats in models/baseline_stats.json. That makes model monitoring a distinct step from inference: predictions are served by the API, while drift checks are run as a separate monitoring/retraining concern.
8. Automation is split across GitHub Actions workflows: train.yml, retrain.yml, monitor.yml, and ci.yml. That suggests a lifecycle where code changes are tested, models can be trained or retrained automatically, and monitoring jobs can be triggered independently of serving.
9. The scripts/ directory contains operational helpers rather than core ML logic: merge_data.py, simulate_labels.py, and traffic.py. These are likely used to prepare datasets, generate labeled events, or simulate load/traffic for monitoring and retraining experiments.
10. Deployment is containerized and Kubernetes-native. There are separate manifests for the API, Streamlit UI, Prometheus, Grafana, namespaces, ingress, and kustomization, which means the runtime is split into independently deployable pods with ingress-based routing to ui.churn.local and api.churn.local.
11. The tests/ folder covers the main architectural boundaries: API behavior, data pipeline correctness, drift logic, and model quality. That gives a good picture of the repo’s contract points—data prep, serving, monitoring, and prediction quality are all treated as testable subsystems.
12. notebooks/ contain EDA, preprocessing/feature-engineering exploration, and training experimentation. They are part of the development workflow, not the production path, which is implemented in src/, monitoring/, and the deployment/configuration directories.
Main entry points: train_model.py Training pipeline · main.py FastAPI app · kustomization.yaml Kubernetes stack · detect_drift.py Drift detection
Components 19
Data & ML
Versioned data, feature engineering, preprocessing, training, and model artifacts.
- Raw data · Versioned raw CSV inputs tracked through DVC.
data/raw/train.csv.dvc - Preprocessed data · Versioned engineered dataset used for model training.
data/preprocessed/train.parquet.dvc - Feature engineering · Stateless row-level feature creation shared by training and inference.
src/features/engineer.py - Preprocessor · Sklearn preprocessing boundary used before model fitting.
src/preprocess/preprocessor.py - Training pipeline · Fits the model pipeline and serializes artifacts.
src/models/train_model.py - Model artifacts · Saved estimator and versioned model bundle.
models/model.pkl.dvc - Baseline stats · Reference statistics used for later comparison and drift checks.
models/baseline_stats.json
Serving
FastAPI inference boundary and the Streamlit client.
- FastAPI app · HTTP inference service exposing health and prediction endpoints.
src/api/main.py - Inference logic · Loads the saved pipeline and produces predictions.
src/api/inference.py - API schemas · Request and response models for the inference API.
src/api/schemas.py - Streamlit UI · User-facing app that calls the FastAPI service.
streamlit_app/app.py
Monitoring
Drift detection plus Prometheus/Grafana observability.
- Drift detection · Compares recent data behavior against baseline statistics.
monitoring/detect_drift.py - Prometheus · Metrics scrape configuration for monitoring jobs and services.
monitoring/prometheus.yml - Grafana dashboards · Preconfigured churn and model-behavior dashboards.
monitoring/Grafana/dashboards/churn_dashboard.json
Ops & Delivery
Automation, deployment manifests, and operational helpers.
- GitHub Actions · CI, train, retrain, and monitoring workflows.
.github/workflows - Kubernetes stack · Deployment bundle for API, UI, monitoring, and shared config.
k8s/kustomization.yaml - Ingress · Routes external traffic to the API and UI services.
k8s/ingress.yaml - Ops scripts · Data merge, label simulation, and traffic generation helpers.
scripts/traffic.py
Quality
Tests that guard the main contracts.
- Tests · Coverage for API, data pipeline, drift logic, and model quality.
tests/test_api.py
Connections 29
- Raw data → Preprocessed data: feeds · Raw inputs are transformed into the tracked preprocessed dataset.
- Raw data → Feature engineering: source · Feature creation starts from row-level raw customer records.
- Feature engineering → Training pipeline: used by · Training applies the same stateless feature engineering as inference.
- Preprocessor → Training pipeline: used by · The sklearn preprocessing stage is composed into the training pipeline.
- Preprocessed data → Training pipeline: input · Training consumes the versioned preprocessed training set.
- Training pipeline → Model artifacts: writes · The best estimator is serialized into the model artifact bundle.
- Training pipeline → Baseline stats: records · Training also stores baseline statistics for later monitoring.
- Model artifacts → Inference logic: loads · Inference reads the saved trained model pipeline.
- Feature engineering → Inference logic: shared logic · Inference reuses the same feature engineering boundary as training.
- API schemas → FastAPI app: contracts · The FastAPI app validates requests and shapes responses with schemas.
- Inference logic → FastAPI app: serves · Prediction logic is wired into the HTTP service.
- FastAPI app → Streamlit UI: called by · The Streamlit frontend depends on the API for predictions.
- Baseline stats → Drift detection: compares against · Drift checks use stored baseline statistics as reference.
- Drift detection → Prometheus: emits · Monitoring jobs surface metrics through Prometheus scraping.
- Prometheus → Grafana dashboards: visualized in · Grafana dashboards consume Prometheus-backed metrics.
- GitHub Actions → Training pipeline: automates · Workflows can run training and retraining jobs.
- GitHub Actions → Drift detection: triggers · Monitoring workflows can execute drift checks independently.
- GitHub Actions → Kubernetes stack: deploys · CI/CD supports the Kubernetes deployment bundle.
- Kubernetes stack → FastAPI app: deploys · The API runs as its own Kubernetes workload.
- Kubernetes stack → Streamlit UI: deploys · The UI is deployed separately from the API.
- Kubernetes stack → Prometheus: deploys · Prometheus is included in the cluster monitoring stack.
- Kubernetes stack → Grafana dashboards: deploys · Grafana is provisioned with dashboards and datasources.
- Ingress → FastAPI app: routes to · Ingress exposes the API service externally.
- Ingress → Streamlit UI: routes to · Ingress exposes the Streamlit frontend externally.
- Ops scripts → Raw data: prepares · Operational scripts can merge or synthesize data for experiments.
- Ops scripts → Drift detection: supports · Traffic and label simulation help exercise monitoring and retraining paths.
- Tests → FastAPI app: checks · API tests cover the serving boundary.
- Tests → Training pipeline: checks · Pipeline tests cover data prep and model training behavior.
- Tests → Drift detection: checks · Drift logic is covered by dedicated tests.