# johan-i4/churn-prediction architecture

> Architecture diagram of the GitHub repository johan-i4/churn-prediction, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/johan-i4/churn-prediction
- Repository: https://github.com/johan-i4/churn-prediction

## Overview

Es una solución de churn que transforma datos de clientes en predicciones y decisiones de retención: entrenamiento y selección por beneficio, servicio REST para puntuar clientes y explicarlos, y salidas analíticas para SQL y Power BI. La explicación combina contribuciones del modelo, recuperación de conocimiento y redacción con backend de plantilla o LLM opcional. El README documenta las capacidades analíticas y de dashboard; las conexiones de esos subsistemas se muestran solo donde la documentación las sustenta, pues no se muestreó su código completo.

Main entry points: [`src/train.py`](https://github.com/johan-i4/churn-prediction/blob/main/src/train.py) (Entrenamiento CV), [`src/api.py`](https://github.com/johan-i4/churn-prediction/blob/main/src/api.py) (API REST), [`src/build_db.py`](https://github.com/johan-i4/churn-prediction/blob/main/src/build_db.py) (Constructor SQLite), [`src/export_dashboard_data.py`](https://github.com/johan-i4/churn-prediction/blob/main/src/export_dashboard_data.py) (Exportación Power BI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_training["Entrenamiento y evaluación"]
  node_loader["Carga y limpieza<br/>[data.py]"]
  node_features["Variables derivadas<br/>[features.py]"]
  node_pipeline["Preprocesamiento y modelos<br/>[modeling.py]"]
  node_evaluation["Métricas y umbral<br/>[evaluate.py]"]
  node_trainer["Entrenamiento CV<br/>[train.py]"]
  node_scratch["Logística NumPy<br/>[logreg_scratch.py]"]
  node_model_file["Pipeline serializado"]
  node_reports["Reportes de evaluación"]
end

subgraph group_serving["API y explicaciones"]
  node_api["API REST<br/>[api.py]"]
  node_contracts["Contratos Pydantic<br/>[schemas.py]"]
  node_local_explanation["Factores por cliente<br/>[explain.py]"]
  node_retriever["Recuperación TF-IDF<br/>[retriever.py]"]
  node_knowledge["Documentación de riesgo"]
  node_prompts["Plantillas de prompts<br/>[prompts.py]"]
  node_llm["Generación de explicación<br/>[llm_client.py]"]
end

subgraph group_analytics["Analítica SQL"]
  node_db_builder["Constructor SQLite<br/>[build_db.py]"]
  node_schema["Esquema SQL<br/>[schema.sql]"]
  node_database[("Base SQLite")]
  node_query_runner["Ejecución de consultas<br/>[run_queries.py]"]
  node_queries["Consultas analíticas"]
end

subgraph group_dashboard["Dashboard Power BI"]
  node_exporter["Exportación Power BI"]
  node_powerbi["Dashboard Power BI"]
end

node_analyst(("Analista de datos"))
node_operator(("Consumidor API"))
node_csv["CSV Telco"]
node_anthropic["API Anthropic"]

node_analyst -->|"provee datos"| node_csv
node_csv -->|"carga y limpia"| node_loader
node_trainer -->|"carga datos"| node_loader
node_trainer -->|"obtiene modelos"| node_pipeline
node_pipeline -->|"deriva variables"| node_features
node_trainer -->|"evalúa y ajusta umbral"| node_evaluation
node_trainer -->|"serializa pipeline"| node_model_file
node_trainer -->|"genera métricas"| node_reports
node_trainer -.->|"valida implementación"| node_scratch
node_operator -->|"envía solicitud"| node_api
node_api -->|"valida y responde"| node_contracts
node_api -->|"carga modelo"| node_model_file
node_api -->|"calcula factores"| node_local_explanation
node_api -->|"busca contexto"| node_retriever
node_retriever -->|"lee fragmentos"| node_knowledge
node_api -->|"redacta explicación"| node_llm
node_llm -->|"renderiza instrucciones"| node_prompts
node_llm -.->|"solicita texto"| node_anthropic
node_analyst -->|"construye base"| node_db_builder
node_db_builder -->|"lee datos crudos"| node_csv
node_db_builder -->|"ejecuta esquema"| node_schema
node_db_builder -->|"escribe tablas"| node_database
node_analyst -->|"ejecuta análisis"| node_query_runner
node_query_runner -->|"ejecuta consultas"| node_queries
node_analyst -->|"exporta datos"| node_exporter
node_exporter -->|"puntúa clientes"| node_model_file
node_exporter -->|"carga clientes"| node_csv
node_exporter -->|"prepara datos del dashboard"| node_powerbi

click node_csv "https://github.com/johan-i4/churn-prediction/blob/main/data/WA_Fn-UseC_-Telco-Customer-Churn.csv"
click node_loader "https://github.com/johan-i4/churn-prediction/blob/main/src/data.py"
click node_features "https://github.com/johan-i4/churn-prediction/blob/main/src/features.py"
click node_pipeline "https://github.com/johan-i4/churn-prediction/blob/main/src/modeling.py"
click node_evaluation "https://github.com/johan-i4/churn-prediction/blob/main/src/evaluate.py"
click node_trainer "https://github.com/johan-i4/churn-prediction/blob/main/src/train.py"
click node_scratch "https://github.com/johan-i4/churn-prediction/blob/main/src/logreg_scratch.py"
click node_model_file "https://github.com/johan-i4/churn-prediction/tree/main/models"
click node_reports "https://github.com/johan-i4/churn-prediction/tree/main/reports"
click node_api "https://github.com/johan-i4/churn-prediction/blob/main/src/api.py"
click node_contracts "https://github.com/johan-i4/churn-prediction/blob/main/src/schemas.py"
click node_local_explanation "https://github.com/johan-i4/churn-prediction/blob/main/src/explain.py"
click node_retriever "https://github.com/johan-i4/churn-prediction/blob/main/src/rag/retriever.py"
click node_knowledge "https://github.com/johan-i4/churn-prediction/tree/main/docs/knowledge"
click node_prompts "https://github.com/johan-i4/churn-prediction/blob/main/src/prompts.py"
click node_llm "https://github.com/johan-i4/churn-prediction/blob/main/src/llm_client.py"
click node_db_builder "https://github.com/johan-i4/churn-prediction/blob/main/src/build_db.py"
click node_schema "https://github.com/johan-i4/churn-prediction/blob/main/sql/schema.sql"
click node_database "https://github.com/johan-i4/churn-prediction/tree/main/data"
click node_query_runner "https://github.com/johan-i4/churn-prediction/blob/main/src/run_queries.py"
click node_queries "https://github.com/johan-i4/churn-prediction/tree/main/sql/queries"
click node_exporter "https://github.com/johan-i4/churn-prediction/blob/main/src/export_dashboard_data.py"
click node_powerbi "https://github.com/johan-i4/churn-prediction/blob/main/powerbi/churn_dashboard.pbix"

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_loader,node_features,node_pipeline,node_evaluation,node_trainer,node_scratch,node_model_file,node_reports toneBlue
class node_api,node_contracts,node_local_explanation,node_retriever,node_knowledge,node_prompts,node_llm toneAmber
class node_db_builder,node_schema,node_database,node_query_runner,node_queries,node_operator,node_anthropic toneMint
class node_exporter,node_powerbi toneRose
class node_analyst toneIndigo
class node_csv toneTeal
```

## Components

### Entrenamiento y evaluación

- [Carga y limpieza](https://github.com/johan-i4/churn-prediction/blob/main/src/data.py): `src/data.py`
- [Variables derivadas](https://github.com/johan-i4/churn-prediction/blob/main/src/features.py): `src/features.py`
- [Preprocesamiento y modelos](https://github.com/johan-i4/churn-prediction/blob/main/src/modeling.py): `src/modeling.py`
- [Métricas y umbral](https://github.com/johan-i4/churn-prediction/blob/main/src/evaluate.py): `src/evaluate.py`
- [Entrenamiento CV](https://github.com/johan-i4/churn-prediction/blob/main/src/train.py): `src/train.py`
- [Logística NumPy](https://github.com/johan-i4/churn-prediction/blob/main/src/logreg_scratch.py): `src/logreg_scratch.py`
- [Pipeline serializado](https://github.com/johan-i4/churn-prediction/tree/main/models): `models`
- [Reportes de evaluación](https://github.com/johan-i4/churn-prediction/tree/main/reports): `reports`

### API y explicaciones

- [API REST](https://github.com/johan-i4/churn-prediction/blob/main/src/api.py): `src/api.py`
- [Contratos Pydantic](https://github.com/johan-i4/churn-prediction/blob/main/src/schemas.py): `src/schemas.py`
- [Factores por cliente](https://github.com/johan-i4/churn-prediction/blob/main/src/explain.py): `src/explain.py`
- [Recuperación TF-IDF](https://github.com/johan-i4/churn-prediction/blob/main/src/rag/retriever.py): `src/rag/retriever.py`
- [Documentación de riesgo](https://github.com/johan-i4/churn-prediction/tree/main/docs/knowledge): `docs/knowledge`
- [Plantillas de prompts](https://github.com/johan-i4/churn-prediction/blob/main/src/prompts.py): `src/prompts.py`
- [Generación de explicación](https://github.com/johan-i4/churn-prediction/blob/main/src/llm_client.py): `src/llm_client.py`

### Analítica SQL

- [Constructor SQLite](https://github.com/johan-i4/churn-prediction/blob/main/src/build_db.py): `src/build_db.py`
- [Esquema SQL](https://github.com/johan-i4/churn-prediction/blob/main/sql/schema.sql): `sql/schema.sql`
- [Base SQLite](https://github.com/johan-i4/churn-prediction/tree/main/data): `data`
- [Ejecución de consultas](https://github.com/johan-i4/churn-prediction/blob/main/src/run_queries.py): `src/run_queries.py`
- [Consultas analíticas](https://github.com/johan-i4/churn-prediction/tree/main/sql/queries): `sql/queries`

### Dashboard Power BI

- [Exportación Power BI](https://github.com/johan-i4/churn-prediction/blob/main/src/export_dashboard_data.py): `src/export_dashboard_data.py`
- [Dashboard Power BI](https://github.com/johan-i4/churn-prediction/blob/main/powerbi/churn_dashboard.pbix): `powerbi/churn_dashboard.pbix`

### Other components

- **Analista de datos**
- **Consumidor API**
- [CSV Telco](https://github.com/johan-i4/churn-prediction/blob/main/data/WA_Fn-UseC_-Telco-Customer-Churn.csv): `data/WA_Fn-UseC_-Telco-Customer-Churn.csv`
- **API Anthropic**

## Connections

- Analista de datos → CSV Telco: provee datos (evidence: `README.md`)
- CSV Telco → Carga y limpieza: carga y limpia (evidence: `src/data.py`)
- Entrenamiento CV → Carga y limpieza: carga datos (evidence: `src/train.py`)
- Entrenamiento CV → Preprocesamiento y modelos: obtiene modelos (evidence: `src/train.py`)
- Preprocesamiento y modelos → Variables derivadas: deriva variables (evidence: `src/modeling.py`)
- Entrenamiento CV → Métricas y umbral: evalúa y ajusta umbral (evidence: `src/train.py`)
- Entrenamiento CV → Pipeline serializado: serializa pipeline (evidence: `README.md`)
- Entrenamiento CV → Reportes de evaluación: genera métricas (evidence: `README.md`)
- Entrenamiento CV → Logística NumPy: valida implementación
- Consumidor API → API REST: envía solicitud (evidence: `README.md`)
- API REST → Contratos Pydantic: valida y responde (evidence: `src/api.py`)
- API REST → Pipeline serializado: carga modelo (evidence: `src/api.py`)
- API REST → Factores por cliente: calcula factores (evidence: `src/api.py`)
- API REST → Recuperación TF-IDF: busca contexto (evidence: `src/api.py`)
- Recuperación TF-IDF → Documentación de riesgo: lee fragmentos (evidence: `README.md`)
- API REST → Generación de explicación: redacta explicación (evidence: `src/api.py`)
- Generación de explicación → Plantillas de prompts: renderiza instrucciones (evidence: `src/llm_client.py`)
- Generación de explicación → API Anthropic: solicita texto (evidence: `src/llm_client.py`)
- Analista de datos → Constructor SQLite: construye base (evidence: `README.md`)
- Constructor SQLite → CSV Telco: lee datos crudos (evidence: `src/build_db.py`)
- Constructor SQLite → Esquema SQL: ejecuta esquema (evidence: `src/build_db.py`)
- Constructor SQLite → Base SQLite: escribe tablas (evidence: `src/build_db.py`)
- Analista de datos → Ejecución de consultas: ejecuta análisis (evidence: `README.md`)
- Ejecución de consultas → Consultas analíticas: ejecuta consultas (evidence: `README.md`)
- Analista de datos → Exportación Power BI: exporta datos (evidence: `README.md`)
- Exportación Power BI → Pipeline serializado: puntúa clientes (evidence: `src/export_dashboard_data.py`)
- Exportación Power BI → CSV Telco: carga clientes (evidence: `src/export_dashboard_data.py`)
- Exportación Power BI → Dashboard Power BI: prepara datos del dashboard (evidence: `README.md`)
