# easonlai/chat_with_csv_streamlit_with_chart architecture

> Architecture diagram of the GitHub repository easonlai/chat_with_csv_streamlit_with_chart, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-09-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/easonlai/chat_with_csv_streamlit_with_chart
- Repository: https://github.com/easonlai/chat_with_csv_streamlit_with_chart

## Overview

##### Project Type and Purpose
- This is a single‐page, full‐stack Python web app built with Streamlit.
- It provides an interactive “chat with your CSV” interface: users ask natural language questions about tabular data and receive answers, tables, or visualizations.
- The core logic uses LangChain’s Pandas Dataframe Agent and an Azure OpenAI GPT model to parse queries, run DataFrame operations, and render Matplotlib charts.

##### File Structure Analysis
- Top level:
- run.py – Streamlit application entrypoint and UI layer
- agent.py – Backend logic: data‐agent setup, LangChain integration, prompt engineering
- data/ – Raw CSV dataset and metadata files
- chart_image/ – Directory where generated chart images are saved
- experiment.ipynb – Notebook for prototyping and experimentation
- git-images/ – Static images for the README
- No separate microservices or external containers—everything runs as one monolith.
- No traditional database: the CSV file is loaded directly into Pandas.

##### Key Components
a. Frontend (Streamlit UI)
- run.py hosts the web interface, accepts user input, displays text, tables, and images.
- Interacts with the backend agent over in‐process function calls.
b. Backend Agent
- agent.py initializes a Pandas DataFrame Agent from LangChain.
- Holds references to the dataset (loaded from data/dataset_telco_customer_churn.csv).
c. AI Service (Azure OpenAI)
- External API call: GPT model receives prompts, returns Python code or data analysis responses.
d. Data Layer
- The CSV file is the single source of truth.
- The metadata CSV may inform column types or descriptions.
e. Visualization Layer
- Matplotlib generates charts (bar, pie, scatter, etc.).
- Chart images are stored under chart_image/ and then rendered by Streamlit.

##### Interactions and Data Flow
- 1. User types a query into the Streamlit UI.
- 2. run.py forwards the query to the Pandas Agent in agent.py.
- 3. The agent formats a prompt (includes DataFrame schema/metadata) and calls Azure OpenAI.
- 4. Azure OpenAI returns text or executable Python code.
- 5. The agent executes the code against the Pandas DataFrame.
- If it’s a chart command, Matplotlib produces a PNG in chart_image/.
- 6. The agent returns results (text, DataFrame slice, or chart path) back to run.py.
- 7. Streamlit renders the response.

##### Architectural Patterns and Principles
- Monolithic, modular Python application.
- Separation of concerns:
- UI layer (run.py)
- Business/agent logic (agent.py)
- Data storage (CSV files)
- External services (Azure GPT)
- Prompt engineering as a domain‐specific integration layer for AI tasks.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Components
    UI["Streamlit UI (run.py)"]:::frontend
    Agent["Pandas DataFrame Agent (agent.py)"]:::backend
    AI["Azure OpenAI GPT Service"]:::external
    Data1["Raw Dataset CSV"]:::data
    Data2["Metadata CSV"]:::data
    Viz["Matplotlib Visualization Output"]:::viz

    %% Data Flow
    UI -->|"1. User Query"| Agent
    Agent -->|"2. Load CSV"| Data1
    Agent -->|"2. Load Metadata"| Data2
    Agent -->|"3. Send Prompt"| AI
    AI -->|"4. Return Code/Text"| Agent
    Agent -->|"5a. Execute Code on DataFrame"| Data1
    Agent -->|"5b. Generate Chart"| Viz
    Viz -->|"6. Render Image"| UI
    Agent -->|"6b. Return Results"| UI

    %% Click Events
    click UI "https://github.com/easonlai/chat_with_csv_streamlit_with_chart/blob/main/run.py"
    click Agent "https://github.com/easonlai/chat_with_csv_streamlit_with_chart/blob/main/agent.py"
    click Data1 "https://github.com/easonlai/chat_with_csv_streamlit_with_chart/blob/main/data/dataset_telco_customer_churn.csv"
    click Data2 "https://github.com/easonlai/chat_with_csv_streamlit_with_chart/blob/main/data/metadata_telco_customer_churn.csv"
    click Viz "https://github.com/easonlai/chat_with_csv_streamlit_with_chart/tree/main/chart_image/"

    %% Styles
    classDef frontend fill:#D6EAF8,stroke:#1B4F72,stroke-width:2px;
    classDef backend fill:#D5F5E3,stroke:#196F3D,stroke-width:2px;
    classDef external fill:#FCF3CF,stroke:#7D6608,stroke-width:2px;
    classDef data fill:#E8DAEF,stroke:#512E5F,stroke-width:2px,stroke-dasharray: 5 5;
    classDef viz fill:#FAD7A0,stroke:#B9770E,stroke-width:2px;
```
