How aaru5h/customer_churn works
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: train_and_save.py Training pipeline · rag_engine.py RAG engine · main.py FastAPI app · pipeline.joblib Churn predictor
Components 17
Frontend
User-facing Streamlit control panel.
- Streamlit app · Main user interface and request initiator.
app.py
Backend Runtime
FastAPI API, orchestration, retrieval, and persistence at inference time.
- FastAPI app · HTTP boundary that serves the frontend and routes requests into backend logic.
backend/main.py - RAG engine · Coordinates retrieval, prediction, and LLM-driven responses.
backend/rag_engine.py - Persistence · Data access boundary for chat history and app state.
backend/db.py - Groq LLM · Cloud LLM used for conversational and retention-response generation.
- Embeddings API · Remote embedding generation used to support semantic retrieval.
- FAISS index · Local vector search backing historical context lookup.
- Churn predictor · Loads trained artifacts and returns churn predictions.
backend/models/pipeline.joblib - Primary pipeline · Production inference pipeline artifact.
backend/models/pipeline.joblib - LR model · Alternative logistic regression artifact.
backend/models/pipeline_lr.joblib - DT model · Alternative decision tree artifact.
backend/models/pipeline_dt.joblib - Metrics · Saved evaluation metrics for trained models.
backend/models/metrics.joblib - Render deploy · Blueprint for split frontend/backend deployment.
render.yaml
Offline ML
Training, preprocessing, and serialized model artifacts.
- Training pipeline · Builds the churn model artifacts from the source dataset.
backend/train_and_save.py - Preprocessing · Transforms raw customer data into model-ready features.
preprocessing.py - Model code · Defines the training-time model configuration and evaluation flow.
model.py - Training data · Canonical churn dataset used for offline training.
data/BankChurners.csv
Connections 18
- 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.