# aaru5h/customer_churn architecture

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

- Interactive diagram: https://gitdiagram.com/aaru5h/customer_churn
- Repository: https://github.com/aaru5h/customer_churn

## Overview

1. **Overall shape**
   - This repo is split into two runtime surfaces: a **Streamlit frontend** in `app.py` and a **FastAPI backend** in `backend/main.py`.
   - The frontend is the user-facing “control panel”; the backend owns all prediction, retrieval, chat, and persistence logic.

2. **Frontend boundary**
   - `app.py` is the only UI entrypoint.
   - It likely collects user input, forwards requests to the backend over HTTP, and renders responses plus loading/error states.
   - The README indicates it also handles the “Warming Up” experience when the backend returns `503`, so the UI is intentionally tolerant of cold starts.

3. **API / orchestration layer**
   - `backend/main.py` is the central API layer.
   - It exposes the endpoints used by the Streamlit app, including the chat/agent path and probably a prediction path.
   - This layer acts as the boundary between web requests and the internal ML/RAG subsystems.

4. **Machine learning inference path**
   - The churn predictor is pre-trained offline and stored as `.joblib` artifacts under `backend/models/`.
   - `backend/train_and_save.py`, together with `preprocessing.py` and `model.py`, forms the offline training pipeline that prepares features, trains models, and serializes them.
   - At runtime, the backend lazily loads these artifacts instead of retraining, which is a key architectural decision for low-memory deployment.

5. **RAG / agent subsystem**
   - `backend/rag_engine.py` appears to encapsulate the LangGraph-style agent logic and retrieval-augmented generation flow.
   - From the README, this subsystem routes requests to either:
     - historical context retrieval through FAISS, or
     - live churn prediction through the ML model, or
     - direct LLM reasoning for retention strategy responses.
   - This is the “intelligence” layer that combines retrieval and prediction into one conversational interface.

6. **Persistence layer**
   - `backend/db.py` is the backend persistence boundary.
   - The repo contains `bank_churn_rag.db`, which suggests a local SQLite database for chat history or agent state in development.
   - The README also mentions PostgreSQL/Neon in deployment, so the data access layer is designed to swap the underlying database while keeping the app logic stable.

7. **Model and artifact storage**
   - `backend/models/` contains the serialized inference assets: `pipeline.joblib`, `pipeline_lr.joblib`, `pipeline_dt.joblib`, and `metrics.joblib`.
   - This strongly suggests multiple trained estimators plus an evaluation artifact, with one primary pipeline used for production predictions.
   - These files are part of the runtime dependency graph, not just training outputs.

8. **Dataset and offline preparation**
   - `data/BankChurners.csv` is the canonical training dataset.
   - The offline pipeline reads this dataset, performs preprocessing, trains models, and persists artifacts to `backend/models/`.
   - This keeps the production backend independent from the original raw dataset, which reduces startup cost and operational risk.

9. **External AI and retrieval dependencies**
   - The README shows the agent depends on **Groq** for the LLM and **Hugging Face inference** for embedding generation.
   - FAISS is used for local semantic retrieval, so conversational requests may hit both local vector search and remote model APIs.
   - This makes the agent a hybrid system: local search plus cloud-based generation.

10. **Deployment topology**
    - `render.yaml` defines a Render Blueprint deployment, implying separate backend and frontend services.
    - The architecture is cloud-native but lightweight: the backend is optimized for fast boot, while the UI can remain independent and stateless.
    - Environment variables such as `GROQ_API_KEY`, `HUGGINGFACEHUB_API_TOKEN`, and `DATABASE_URL` are operational dependencies that materially shape runtime behavior.

11. **Primary request flow**
    - User interacts with the Streamlit app.
    - Streamlit sends a request to FastAPI.
    - FastAPI dispatches either to the churn prediction pipeline or the agent/RAG engine.
    - The agent may query FAISS, load ML artifacts, and/or call Groq, then returns a structured response back to the UI.

12. **Key architectural boundary**
    - The main separation is between **presentation** (`app.py`) and **decision logic** (`backend/*`).
    - Within the backend, there is another important split between **offline training** (`train_and_save.py`, `preprocessing.py`, `model.py`) and **online inference/agent execution** (`main.py`, `rag_engine.py`, `db.py`).
    - That split is central to understanding the system diagram: training artifacts are prepared ahead of time, while runtime code focuses on orchestration and low-latency inference.

Main entry points: [`backend/train_and_save.py`](https://github.com/aaru5h/customer_churn/blob/main/backend/train_and_save.py) (Training pipeline), [`backend/rag_engine.py`](https://github.com/aaru5h/customer_churn/blob/main/backend/rag_engine.py) (RAG engine), [`backend/main.py`](https://github.com/aaru5h/customer_churn/blob/main/backend/main.py) (FastAPI app), [`backend/models/pipeline.joblib`](https://github.com/aaru5h/customer_churn/blob/main/backend/models/pipeline.joblib) (Churn predictor)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_app["Streamlit app<br/>ui entrypoint<br/>[app.py]"]
end

subgraph group_backend_runtime["Backend Runtime"]
  node_api["FastAPI app<br/>api gateway<br/>[main.py]"]
  node_rag["RAG engine<br/>agent orchestrator<br/>[rag_engine.py]"]
  node_db[("Persistence<br/>sqlite/postgres layer<br/>[db.py]")]
  node_groq(("Groq LLM<br/>external llm"))
  node_hf(("Embeddings API<br/>external embeddings"))
  node_faiss[("FAISS index<br/>local retrieval")]
  node_predictor["Churn predictor<br/>runtime inference<br/>[pipeline.joblib]"]
  node_pipeline_model["Primary pipeline<br/>serialized model<br/>[pipeline.joblib]"]
  node_pipeline_lr["LR model<br/>serialized model<br/>[pipeline_lr.joblib]"]
  node_pipeline_dt["DT model<br/>serialized model<br/>[pipeline_dt.joblib]"]
  node_metrics["Metrics<br/>evaluation artifact<br/>[metrics.joblib]"]
  node_render["Render deploy<br/>deployment spec<br/>[render.yaml]"]
end

subgraph group_backend_offline["Offline ML"]
  node_train["Training pipeline<br/>offline training<br/>[train_and_save.py]"]
  node_prep["Preprocessing<br/>feature prep<br/>[preprocessing.py]"]
  node_model_def["Model code<br/>estimator defs<br/>[model.py]"]
  node_dataset["Training data<br/>csv dataset<br/>[BankChurners.csv]"]
end

node_app -->|"HTTP requests"| node_api
node_api -->|"agent path"| node_rag
node_api -->|"predict path"| node_predictor
node_rag -->|"retrieve"| node_faiss
node_rag -->|"score"| node_predictor
node_rag -->|"generate"| node_groq
node_rag -->|"embed"| node_hf
node_rag -->|"store/load"| node_db
node_predictor -->|"loads"| node_pipeline_model
node_train -->|"uses"| node_prep
node_train -->|"uses"| node_model_def
node_train -->|"reads"| node_dataset
node_train -->|"writes"| node_pipeline_model
node_train -->|"writes"| node_pipeline_lr
node_train -->|"writes"| node_pipeline_dt
node_train -->|"writes"| node_metrics
node_render -->|"deploys"| node_app
node_render -->|"deploys"| node_api

click node_app "https://github.com/aaru5h/customer_churn/blob/main/app.py"
click node_api "https://github.com/aaru5h/customer_churn/blob/main/backend/main.py"
click node_rag "https://github.com/aaru5h/customer_churn/blob/main/backend/rag_engine.py"
click node_db "https://github.com/aaru5h/customer_churn/blob/main/backend/db.py"
click node_predictor "https://github.com/aaru5h/customer_churn/blob/main/backend/models/pipeline.joblib"
click node_train "https://github.com/aaru5h/customer_churn/blob/main/backend/train_and_save.py"
click node_prep "https://github.com/aaru5h/customer_churn/blob/main/preprocessing.py"
click node_model_def "https://github.com/aaru5h/customer_churn/blob/main/model.py"
click node_dataset "https://github.com/aaru5h/customer_churn/blob/main/data/BankChurners.csv"
click node_pipeline_model "https://github.com/aaru5h/customer_churn/blob/main/backend/models/pipeline.joblib"
click node_pipeline_lr "https://github.com/aaru5h/customer_churn/blob/main/backend/models/pipeline_lr.joblib"
click node_pipeline_dt "https://github.com/aaru5h/customer_churn/blob/main/backend/models/pipeline_dt.joblib"
click node_metrics "https://github.com/aaru5h/customer_churn/blob/main/backend/models/metrics.joblib"
click node_render "https://github.com/aaru5h/customer_churn/blob/main/render.yaml"

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_app toneBlue
class node_api,node_rag,node_db,node_groq,node_hf,node_faiss,node_predictor,node_pipeline_model,node_pipeline_lr,node_pipeline_dt,node_metrics,node_render toneAmber
class node_train,node_prep,node_model_def,node_dataset toneMint
```

## Components

### Frontend

User-facing Streamlit control panel.

- [Streamlit app](https://github.com/aaru5h/customer_churn/blob/main/app.py): ui entrypoint · Main user interface and request initiator. · `app.py`

### Backend Runtime

FastAPI API, orchestration, retrieval, and persistence at inference time.

- [FastAPI app](https://github.com/aaru5h/customer_churn/blob/main/backend/main.py): api gateway · HTTP boundary that serves the frontend and routes requests into backend logic. · `backend/main.py`
- [RAG engine](https://github.com/aaru5h/customer_churn/blob/main/backend/rag_engine.py): agent orchestrator · Coordinates retrieval, prediction, and LLM-driven responses. · `backend/rag_engine.py`
- [Persistence](https://github.com/aaru5h/customer_churn/blob/main/backend/db.py): sqlite/postgres layer · Data access boundary for chat history and app state. · `backend/db.py`
- **Groq LLM**: external llm · Cloud LLM used for conversational and retention-response generation.
- **Embeddings API**: external embeddings · Remote embedding generation used to support semantic retrieval.
- **FAISS index**: local retrieval · Local vector search backing historical context lookup.
- [Churn predictor](https://github.com/aaru5h/customer_churn/blob/main/backend/models/pipeline.joblib): runtime inference · Loads trained artifacts and returns churn predictions. · `backend/models/pipeline.joblib`
- [Primary pipeline](https://github.com/aaru5h/customer_churn/blob/main/backend/models/pipeline.joblib): serialized model · Production inference pipeline artifact. · `backend/models/pipeline.joblib`
- [LR model](https://github.com/aaru5h/customer_churn/blob/main/backend/models/pipeline_lr.joblib): serialized model · Alternative logistic regression artifact. · `backend/models/pipeline_lr.joblib`
- [DT model](https://github.com/aaru5h/customer_churn/blob/main/backend/models/pipeline_dt.joblib): serialized model · Alternative decision tree artifact. · `backend/models/pipeline_dt.joblib`
- [Metrics](https://github.com/aaru5h/customer_churn/blob/main/backend/models/metrics.joblib): evaluation artifact · Saved evaluation metrics for trained models. · `backend/models/metrics.joblib`
- [Render deploy](https://github.com/aaru5h/customer_churn/blob/main/render.yaml): deployment spec · Blueprint for split frontend/backend deployment. · `render.yaml`

### Offline ML

Training, preprocessing, and serialized model artifacts.

- [Training pipeline](https://github.com/aaru5h/customer_churn/blob/main/backend/train_and_save.py): offline training · Builds the churn model artifacts from the source dataset. · `backend/train_and_save.py`
- [Preprocessing](https://github.com/aaru5h/customer_churn/blob/main/preprocessing.py): feature prep · Transforms raw customer data into model-ready features. · `preprocessing.py`
- [Model code](https://github.com/aaru5h/customer_churn/blob/main/model.py): estimator defs · Defines the training-time model configuration and evaluation flow. · `model.py`
- [Training data](https://github.com/aaru5h/customer_churn/blob/main/data/BankChurners.csv): csv dataset · Canonical churn dataset used for offline training. · `data/BankChurners.csv`

## Connections

- Streamlit app → FastAPI app: HTTP requests · Frontend sends user actions and queries to the backend API.
- FastAPI app → RAG engine: agent path · Backend routes conversational requests into the orchestration layer.
- FastAPI app → Churn predictor: predict path · Backend serves direct churn prediction requests through the loaded model.
- RAG engine → FAISS index: retrieve · Agent queries local semantic search for historical context.
- RAG engine → Churn predictor: score · Agent can invoke the churn predictor when a live prediction is needed.
- RAG engine → Groq LLM: generate · Agent uses the external LLM for response generation.
- RAG engine → Embeddings API: embed · Agent depends on remote embeddings for retrieval support.
- RAG engine → Persistence: store/load · Conversation or state can be persisted through the data layer.
- Churn predictor → Primary pipeline: loads · Runtime inference uses the primary serialized pipeline.
- Training pipeline → Preprocessing: uses · Training pipeline applies preprocessing before fitting models.
- Training pipeline → Model code: uses · Training pipeline relies on model definitions and evaluation logic.
- Training pipeline → Training data: reads · Offline training consumes the canonical churn dataset.
- Training pipeline → Primary pipeline: writes · Training exports the production pipeline artifact.
- Training pipeline → LR model: writes · Training exports an alternate logistic regression artifact.
- Training pipeline → DT model: writes · Training exports an alternate decision tree artifact.
- Training pipeline → Metrics: writes · Training stores evaluation metrics as a serialized artifact.
- Render deploy → Streamlit app: deploys · Deployment spec provisions the frontend service.
- Render deploy → FastAPI app: deploys · Deployment spec provisions the backend service.
