# chanhbui297/dseb65a-group04-customer-churn-predictor architecture

> Architecture diagram of the GitHub repository chanhbui297/dseb65a-group04-customer-churn-predictor, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chanhbui297/dseb65a-group04-customer-churn-predictor
- Repository: https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor

## Overview

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: [`src/models/train_model.py`](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/src/models/train_model.py) (Training pipeline), [`src/api/main.py`](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/src/api/main.py) (FastAPI app), [`k8s/kustomization.yaml`](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/k8s/kustomization.yaml) (Kubernetes stack), [`monitoring/detect_drift.py`](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/monitoring/detect_drift.py) (Drift detection)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data_ml["Data & ML"]
  node_raw_data[("Raw data<br/>dvc data<br/>[train.csv.dvc]")]
  node_preprocessed_data[("Preprocessed data<br/>dvc data<br/>[train.parquet.dvc]")]
  node_feature_engineering["Feature engineering<br/>features<br/>[engineer.py]"]
  node_preprocessor["Preprocessor<br/>prep pipeline<br/>[preprocessor.py]"]
  node_training_pipeline["Training pipeline<br/>train job<br/>[train_model.py]"]
  node_model_artifacts[("Model artifacts<br/>joblib model<br/>[model.pkl.dvc]")]
  node_baseline_stats["Baseline stats<br/>model metadata"]
end

subgraph group_serving["Serving"]
  node_api_main["FastAPI app<br/>[main.py]"]
  node_api_inference["Inference logic<br/>runtime<br/>[inference.py]"]
  node_api_schemas["API schemas<br/>contracts<br/>[schemas.py]"]
  node_streamlit_ui["Streamlit UI<br/>frontend<br/>[app.py]"]
end

subgraph group_monitoring["Monitoring"]
  node_drift_detection["Drift detection<br/>monitor job<br/>[detect_drift.py]"]
  node_prometheus_cfg["Prometheus<br/>metrics<br/>[prometheus.yml]"]
  node_grafana_dashboards["Grafana dashboards<br/>observability"]
end

subgraph group_ops["Ops & Delivery"]
  node_github_actions["GitHub Actions<br/>automation<br/>[workflows]"]
  node_k8s_stack["Kubernetes stack<br/>deploy manifests<br/>[kustomization.yaml]"]
  node_ingress["Ingress<br/>routing<br/>[ingress.yaml]"]
  node_ops_scripts["Ops scripts<br/>helpers<br/>[traffic.py]"]
end

subgraph group_quality["Quality"]
  node_tests["Tests<br/>test suite<br/>[test_api.py]"]
end

node_raw_data -->|"feeds"| node_preprocessed_data
node_raw_data -->|"source"| node_feature_engineering
node_feature_engineering -->|"used by"| node_training_pipeline
node_preprocessor -->|"used by"| node_training_pipeline
node_preprocessed_data -->|"input"| node_training_pipeline
node_training_pipeline -->|"writes"| node_model_artifacts
node_training_pipeline -->|"records"| node_baseline_stats
node_model_artifacts -->|"loads"| node_api_inference
node_feature_engineering -->|"shared logic"| node_api_inference
node_api_schemas -->|"contracts"| node_api_main
node_api_inference -->|"serves"| node_api_main
node_api_main -->|"called by"| node_streamlit_ui
node_baseline_stats -->|"compares against"| node_drift_detection
node_drift_detection -->|"emits"| node_prometheus_cfg
node_prometheus_cfg -->|"visualized in"| node_grafana_dashboards
node_github_actions -->|"automates"| node_training_pipeline
node_github_actions -->|"triggers"| node_drift_detection
node_github_actions -->|"deploys"| node_k8s_stack
node_k8s_stack -->|"deploys"| node_api_main
node_k8s_stack -->|"deploys"| node_streamlit_ui
node_k8s_stack -->|"deploys"| node_prometheus_cfg
node_k8s_stack -->|"deploys"| node_grafana_dashboards
node_ingress -->|"routes to"| node_api_main
node_ingress -->|"routes to"| node_streamlit_ui
node_ops_scripts -->|"prepares"| node_raw_data
node_ops_scripts -->|"supports"| node_drift_detection
node_tests -->|"checks"| node_api_main
node_tests -->|"checks"| node_training_pipeline
node_tests -->|"checks"| node_drift_detection

click node_raw_data "https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/data/raw/train.csv.dvc"
click node_preprocessed_data "https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/data/preprocessed/train.parquet.dvc"
click node_feature_engineering "https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/src/features/engineer.py"
click node_preprocessor "https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/src/preprocess/preprocessor.py"
click node_training_pipeline "https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/src/models/train_model.py"
click node_model_artifacts "https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/models/model.pkl.dvc"
click node_baseline_stats "https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/models/baseline_stats.json"
click node_api_main "https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/src/api/main.py"
click node_api_inference "https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/src/api/inference.py"
click node_api_schemas "https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/src/api/schemas.py"
click node_streamlit_ui "https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/streamlit_app/app.py"
click node_drift_detection "https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/monitoring/detect_drift.py"
click node_prometheus_cfg "https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/monitoring/prometheus.yml"
click node_grafana_dashboards "https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/monitoring/Grafana/dashboards/churn_dashboard.json"
click node_github_actions "https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/.github/workflows"
click node_k8s_stack "https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/k8s/kustomization.yaml"
click node_ingress "https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/k8s/ingress.yaml"
click node_ops_scripts "https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/scripts/traffic.py"
click node_tests "https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/tests/test_api.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_raw_data,node_preprocessed_data,node_feature_engineering,node_preprocessor,node_training_pipeline,node_model_artifacts,node_baseline_stats toneBlue
class node_api_main,node_api_inference,node_api_schemas,node_streamlit_ui toneAmber
class node_drift_detection,node_prometheus_cfg,node_grafana_dashboards toneMint
class node_github_actions,node_k8s_stack,node_ingress,node_ops_scripts toneRose
class node_tests toneIndigo
```

## Components

### Data & ML

Versioned data, feature engineering, preprocessing, training, and model artifacts.

- [Raw data](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/data/raw/train.csv.dvc): dvc data · Versioned raw CSV inputs tracked through DVC. · `data/raw/train.csv.dvc`
- [Preprocessed data](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/data/preprocessed/train.parquet.dvc): dvc data · Versioned engineered dataset used for model training. · `data/preprocessed/train.parquet.dvc`
- [Feature engineering](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/src/features/engineer.py): features · Stateless row-level feature creation shared by training and inference. · `src/features/engineer.py`
- [Preprocessor](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/src/preprocess/preprocessor.py): prep pipeline · Sklearn preprocessing boundary used before model fitting. · `src/preprocess/preprocessor.py`
- [Training pipeline](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/src/models/train_model.py): train job · Fits the model pipeline and serializes artifacts. · `src/models/train_model.py`
- [Model artifacts](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/models/model.pkl.dvc): joblib model · Saved estimator and versioned model bundle. · `models/model.pkl.dvc`
- [Baseline stats](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/models/baseline_stats.json): model metadata · Reference statistics used for later comparison and drift checks. · `models/baseline_stats.json`

### Serving

FastAPI inference boundary and the Streamlit client.

- [FastAPI app](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/src/api/main.py): service · HTTP inference service exposing health and prediction endpoints. · `src/api/main.py`
- [Inference logic](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/src/api/inference.py): runtime · Loads the saved pipeline and produces predictions. · `src/api/inference.py`
- [API schemas](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/src/api/schemas.py): contracts · Request and response models for the inference API. · `src/api/schemas.py`
- [Streamlit UI](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/streamlit_app/app.py): frontend · User-facing app that calls the FastAPI service. · `streamlit_app/app.py`

### Monitoring

Drift detection plus Prometheus/Grafana observability.

- [Drift detection](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/monitoring/detect_drift.py): monitor job · Compares recent data behavior against baseline statistics. · `monitoring/detect_drift.py`
- [Prometheus](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/monitoring/prometheus.yml): metrics · Metrics scrape configuration for monitoring jobs and services. · `monitoring/prometheus.yml`
- [Grafana dashboards](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/monitoring/Grafana/dashboards/churn_dashboard.json): observability · Preconfigured churn and model-behavior dashboards. · `monitoring/Grafana/dashboards/churn_dashboard.json`

### Ops & Delivery

Automation, deployment manifests, and operational helpers.

- [GitHub Actions](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/.github/workflows): automation · CI, train, retrain, and monitoring workflows. · `.github/workflows`
- [Kubernetes stack](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/k8s/kustomization.yaml): deploy manifests · Deployment bundle for API, UI, monitoring, and shared config. · `k8s/kustomization.yaml`
- [Ingress](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/k8s/ingress.yaml): routing · Routes external traffic to the API and UI services. · `k8s/ingress.yaml`
- [Ops scripts](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/scripts/traffic.py): helpers · Data merge, label simulation, and traffic generation helpers. · `scripts/traffic.py`

### Quality

Tests that guard the main contracts.

- [Tests](https://github.com/chanhbui297/dseb65a-group04-customer-churn-predictor/blob/main/tests/test_api.py): test suite · Coverage for API, data pipeline, drift logic, and model quality. · `tests/test_api.py`

## Connections

- 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.
