# barath1412/telecom-customer-churn-prediction-and-retention-recommendation architecture

> Architecture diagram of the GitHub repository barath1412/telecom-customer-churn-prediction-and-retention-recommendation, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation
- Repository: https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation

## Overview

#### Purpose and system shape
This is a telecom customer-churn scoring and retention-recommendation system. It combines an ML/policy pipeline that prioritizes customers and recommends offers, a Python backend exposing that data and actions, and a React retention-console UI for operators. Shared API response fixtures/contracts establish the cross-layer boundary: `ml/api-contract/POST_score.json`, `backend/app/routes.py`, `retention-console-frontend/api-contract/POST_score.json`.

#### Entry points and serving layer
The backend application is centered on `backend/app/main.py`, with HTTP endpoint registration in `backend/app/routes.py`. It appears to serve queue, customer-detail, catalog, summary, scoring, and action operations; endpoint-specific orchestration is separated into `backend/app/score.py`, `backend/app/detail.py`, and `backend/app/actions_log.py`. Shell/batch launchers are `backend/run.sh` and `backend/run.bat`.

The browser entry points are `retention-console-frontend/frontend/src/main.tsx` and `retention-console-frontend/frontend/src/App.tsx`; route composition is in `retention-console-frontend/frontend/src/routes.tsx`.

#### ML scoring and recommendation pipeline
The core ML workflow starts from churn data and a trained model: `ml/data/Telco_customer_churn.xlsx`, `ml/src/train.py`, and `ml/artifacts/churn_model_v1.joblib`. Runtime scoring likely derives model-ready fields, predicts churn, and attaches feature attribution through `ml/src/features.py`, `ml/src/predict.py`, and `ml/src/attribution.py`.

A decision layer turns risk/economic inputs into actionable retention outcomes. `ml/src/decision.py`, `ml/src/levers.py`, and `ml/src/gate.py` define the recommendation, eligible intervention levers, and policy/eligibility gates. This is an important boundary: model risk alone does not imply an offer; decision logic can produce review or no-action states.

#### Batch queue and durable artifacts
Queue generation is a separate batch-oriented stage: `ml/src/queue_build.py`, `ml/src/run_batch.py`, and `ml/artifacts/queue_full.csv`. It produces ranked operational views and audit material, including `ml/artifacts/queue_top40.csv` and `ml/artifacts/queue_audit.json`.

The backend appears artifact-backed rather than database-backed: state/access adapters include `backend/app/cache.py`, `backend/app/queue_state.py`, and `backend/app/population.py`. Model registry and evidence identifiers are persisted as files in `ml/artifacts/model_registry.json` and `ml/artifacts/evidence_ids.json`.

#### Explanation, evidence, and narration
Recommendations carry explainability and policy trace data from model attribution and decision stages. Customer-facing detail/narration is handled by `backend/app/detail.py` and `backend/app/narrate.py`, supported in ML by `ml/src/narration_client.py` and `ml/src/kb_retrieval.py`. The knowledge source is `ml/data/kb/knowledge_base.md`, with construction support in `ml/src/kb_build.py`.

This separates deterministic scoring/policy from generated or retrieved explanatory text, allowing the UI to render both recommendation economics and narrative evidence.

#### Operator console and API consumption
The React/Vite frontend organizes workflows by operational task: queue (`retention-console-frontend/frontend/src/features/queue/QueuePage.tsx`), customer review/action (`retention-console-frontend/frontend/src/features/customer/CustomerPage.tsx`), and ad hoc scoring (`retention-console-frontend/frontend/src/features/score/ScorePage.tsx`). API transport and typed response handling are centralized in `retention-console-frontend/frontend/src/lib/api.ts` and `retention-console-frontend/frontend/src/types/api.ts`.

The UI explicitly surfaces expected-value and policy/narration outputs through `retention-console-frontend/frontend/src/components/EVBreakdown.tsx`, `retention-console-frontend/frontend/src/components/PolicyTrace.tsx`, and `retention-console-frontend/frontend/src/components/NarrationPanel.tsx`. Local mock-service-worker infrastructure (`retention-console-frontend/frontend/src/mocks/handlers.ts`) lets the console operate against the shared fixture contracts without the backend.

#### Validation boundaries and external dependencies
Input validity and leakage prevention are first-class constraints: `ml/src/validators.py`, `ml/src/contracts.py`, and `ml/src/fallback.py` protect scoring and response formation; backend errors are represented in `backend/app/errors.py`. Frontend score-form leakage protection is mirrored in `retention-console-frontend/frontend/src/features/score/leakageGuard.ts`.

Material runtimes are Python for the backend/ML pipeline (`backend/requirements.txt`, `ml/requirements.txt`) and Node/TypeScript with Vite for the console (`retention-console-frontend/frontend/package.json`). The only evident external content service is the narration client; its provider/configuration is not identifiable from the supplied tree.

Main entry points: [`backend/app/score.py`](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/backend/app/score.py) (Score, detail & actions), [`retention-console-frontend/frontend/src/lib/api.ts`](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/retention-console-frontend/frontend/src/lib/api.ts) (API client & types), [`ml/src/predict.py`](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/src/predict.py) (Risk scoring & attribution), [`ml/api-contract/POST_score.json`](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/api-contract/POST_score.json) (Score response contract)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ml["ML &amp; policy pipeline"]
  node_churn_data[("Churn training data<br/>dataset")]
  node_training["Model training<br/>Python job<br/>[train.py]"]
  node_scoring["Risk scoring &amp; attribution<br/>Python pipeline<br/>[predict.py]"]
  node_policy{{"Decision policy<br/>policy engine<br/>[decision.py]"}}
  node_batch_queue["Batch queue builder<br/>Python batch job<br/>[run_batch.py]"]
  node_knowledge_base["Retention knowledge base<br/>knowledge source<br/>[knowledge_base.md]"]
  node_narration["Narration &amp; retrieval<br/>explanation service"]
end

subgraph group_artifacts["Durable artifacts"]
  node_model["Churn model<br/>model artifact"]
  node_queue_artifact["Ranked queue<br/>CSV artifact<br/>[queue_full.csv]"]
  node_audit_artifact["Queue audit<br/>JSON artifact<br/>[queue_audit.json]"]
  node_model_registry["Model &amp; evidence registry<br/>JSON registry"]
end

subgraph group_backend["Python backend"]
  node_backend_app["Backend application<br/>Python HTTP service<br/>[main.py]"]
  node_api_routes["HTTP routes<br/>endpoint layer<br/>[routes.py]"]
  node_backend_orchestration["Score, detail &amp; actions<br/>application layer<br/>[score.py]"]
  node_artifact_access["Artifact-backed state<br/>access layer<br/>[queue_state.py]"]
end

subgraph group_frontend["Retention console"]
  node_frontend_app["Console application<br/>React/Vite app<br/>[App.tsx]"]
  node_console_workflows["Operator workflows<br/>React pages<br/>[routes.tsx]"]
  node_frontend_api["API client &amp; types<br/>TypeScript boundary<br/>[api.ts]"]
  node_output_panels["EV, policy &amp; narration<br/>UI components<br/>[EVBreakdown.tsx]"]
  node_mock_api["Mock API handlers<br/>MSW mock layer<br/>[handlers.ts]"]
end

node_score_contract["Score response contract<br/>shared API fixture<br/>[POST_score.json]"]

node_churn_data -->|"trains"| node_training
node_training -->|"produces"| node_model
node_model -->|"loaded for inference"| node_scoring
node_scoring -->|"risk &amp; attribution"| node_policy
node_policy -->|"retention outcomes"| node_batch_queue
node_batch_queue -->|"writes"| node_queue_artifact
node_batch_queue -->|"writes"| node_audit_artifact
node_model -->|"registered with evidence"| node_model_registry
node_knowledge_base -->|"retrieved context"| node_narration
node_policy -->|"policy trace"| node_narration
node_backend_app -->|"registers"| node_api_routes
node_api_routes -->|"dispatches"| node_backend_orchestration
node_backend_orchestration -->|"reads operational state"| node_artifact_access
node_artifact_access -->|"loads"| node_queue_artifact
node_backend_orchestration -->|"requests scoring"| node_scoring
node_backend_orchestration -->|"requests explanation"| node_narration
node_scoring -->|"forms response"| node_score_contract
node_backend_orchestration -->|"returns"| node_score_contract
node_frontend_app -->|"composes"| node_console_workflows
node_console_workflows -->|"uses"| node_frontend_api
node_frontend_api -->|"HTTP requests"| node_api_routes
node_console_workflows -->|"renders"| node_output_panels
node_frontend_api -->|"typed against"| node_score_contract
node_mock_api -.->|"serves fixture"| node_score_contract
node_frontend_api -.->|"local development"| node_mock_api

click node_churn_data "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/data/Telco_customer_churn.xlsx"
click node_training "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/src/train.py"
click node_model "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/artifacts/churn_model_v1.joblib"
click node_scoring "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/src/predict.py"
click node_policy "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/src/decision.py"
click node_batch_queue "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/src/run_batch.py"
click node_queue_artifact "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/artifacts/queue_full.csv"
click node_audit_artifact "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/artifacts/queue_audit.json"
click node_model_registry "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/artifacts/model_registry.json"
click node_knowledge_base "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/data/kb/knowledge_base.md"
click node_narration "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/src/narration_client.py"
click node_backend_app "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/backend/app/main.py"
click node_api_routes "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/backend/app/routes.py"
click node_backend_orchestration "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/backend/app/score.py"
click node_artifact_access "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/backend/app/queue_state.py"
click node_frontend_app "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/retention-console-frontend/frontend/src/App.tsx"
click node_console_workflows "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/retention-console-frontend/frontend/src/routes.tsx"
click node_frontend_api "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/retention-console-frontend/frontend/src/lib/api.ts"
click node_output_panels "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/retention-console-frontend/frontend/src/components/EVBreakdown.tsx"
click node_score_contract "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/api-contract/POST_score.json"
click node_mock_api "https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/retention-console-frontend/frontend/src/mocks/handlers.ts"

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_churn_data,node_training,node_scoring,node_policy,node_batch_queue,node_knowledge_base,node_narration toneBlue
class node_model,node_queue_artifact,node_audit_artifact,node_model_registry toneAmber
class node_backend_app,node_api_routes,node_backend_orchestration,node_artifact_access toneMint
class node_frontend_app,node_console_workflows,node_frontend_api,node_output_panels,node_mock_api toneRose
class node_score_contract toneNeutral
```

## Components

### ML & policy pipeline

- [Churn training data](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/data/Telco_customer_churn.xlsx): dataset · `ml/data/Telco_customer_churn.xlsx`
- [Model training](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/src/train.py): Python job · `ml/src/train.py`
- [Risk scoring & attribution](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/src/predict.py): Python pipeline · Builds features, predicts risk, and attaches attribution. · `ml/src/predict.py`
- [Decision policy](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/src/decision.py): policy engine · Applies levers and eligibility gates to produce offers, review, or no-action. · `ml/src/decision.py`
- [Batch queue builder](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/src/run_batch.py): Python batch job · Ranks scored customers into operational queues and audit output. · `ml/src/run_batch.py`
- [Retention knowledge base](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/data/kb/knowledge_base.md): knowledge source · `ml/data/kb/knowledge_base.md`
- [Narration & retrieval](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/src/narration_client.py): explanation service · Keeps retrieved or generated explanation separate from deterministic policy. · `ml/src/narration_client.py`

### Durable artifacts

- [Churn model](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/artifacts/churn_model_v1.joblib): model artifact · `ml/artifacts/churn_model_v1.joblib`
- [Ranked queue](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/artifacts/queue_full.csv): CSV artifact · `ml/artifacts/queue_full.csv`
- [Queue audit](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/artifacts/queue_audit.json): JSON artifact · `ml/artifacts/queue_audit.json`
- [Model & evidence registry](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/artifacts/model_registry.json): JSON registry · `ml/artifacts/model_registry.json`

### Python backend

- [Backend application](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/backend/app/main.py): Python HTTP service · `backend/app/main.py`
- [HTTP routes](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/backend/app/routes.py): endpoint layer · Serves queue, detail, catalog, summary, scoring, and action operations. · `backend/app/routes.py`
- [Score, detail & actions](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/backend/app/score.py): application layer · `backend/app/score.py`
- [Artifact-backed state](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/backend/app/queue_state.py): access layer · Reads queue, population, and cached artifact state instead of a database. · `backend/app/queue_state.py`

### Retention console

- [Console application](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/retention-console-frontend/frontend/src/App.tsx): React/Vite app · `retention-console-frontend/frontend/src/App.tsx`
- [Operator workflows](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/retention-console-frontend/frontend/src/routes.tsx): React pages · Queue, customer review/action, and ad hoc scoring views. · `retention-console-frontend/frontend/src/routes.tsx`
- [API client & types](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/retention-console-frontend/frontend/src/lib/api.ts): TypeScript boundary · `retention-console-frontend/frontend/src/lib/api.ts`
- [EV, policy & narration](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/retention-console-frontend/frontend/src/components/EVBreakdown.tsx): UI components · Renders economics, policy trace, and narrative evidence. · `retention-console-frontend/frontend/src/components/EVBreakdown.tsx`
- [Mock API handlers](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/retention-console-frontend/frontend/src/mocks/handlers.ts): MSW mock layer · Runs the console against fixture contracts without the backend. · `retention-console-frontend/frontend/src/mocks/handlers.ts`

### Other components

- [Score response contract](https://github.com/barath1412/telecom-customer-churn-prediction-and-retention-recommendation/blob/main/ml/api-contract/POST_score.json): shared API fixture · Cross-layer scoring response boundary. · `ml/api-contract/POST_score.json`

## Connections

- Churn training data → Model training: trains
- Model training → Churn model: produces
- Churn model → Risk scoring & attribution: loaded for inference
- Risk scoring & attribution → Decision policy: risk & attribution
- Decision policy → Batch queue builder: retention outcomes
- Batch queue builder → Ranked queue: writes
- Batch queue builder → Queue audit: writes
- Churn model → Model & evidence registry: registered with evidence
- Retention knowledge base → Narration & retrieval: retrieved context
- Decision policy → Narration & retrieval: policy trace
- Backend application → HTTP routes: registers
- HTTP routes → Score, detail & actions: dispatches
- Score, detail & actions → Artifact-backed state: reads operational state
- Artifact-backed state → Ranked queue: loads
- Score, detail & actions → Risk scoring & attribution: requests scoring
- Score, detail & actions → Narration & retrieval: requests explanation
- Risk scoring & attribution → Score response contract: forms response
- Score, detail & actions → Score response contract: returns
- Console application → Operator workflows: composes
- Operator workflows → API client & types: uses
- API client & types → HTTP routes: HTTP requests
- Operator workflows → EV, policy & narration: renders
- API client & types → Score response contract: typed against
- Mock API handlers → Score response contract: serves fixture
- API client & types → Mock API handlers: local development
