How easonlai/chat_with_csv_streamlit_with_chart works
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.