Skip to main content

Chat with Your CSV: Natural-Language Data Q&A

Level: Intermediate · Time: 40–50 min · Category: Web app

Tags: Python · Streamlit · pandas · OpenAI SDK

Build a Streamlit app where you upload a CSV and ask questions about it in plain English. The app computes a compact data profile locally with pandas and sends that profile to the Research Computing OpenAI-compatible API, which answers grounded in the profile.

Safe by design

This app does not run code the model writes. It computes a data profile with pandas and asks the model to reason over that profile. That keeps a hackathon demo safe and predictable.

Prerequisites

Confirm Python 3 and pip are available before you start:

python3 --version      # expect 3.9 or newer
python3 -m pip --version

If Python is not found, install it from python.org. On the supercomputer, load a Python module instead (for example module load python), then run the checks again.

What you will do

  • Create a Streamlit workspace with pandas and the OpenAI SDK.
  • Compute a compact profile of an uploaded CSV.
  • Ask questions in English and get grounded answers.
  • Add transparency and column controls.

Build it step by step

1. Create the workspace

mkdir csv-qa
cd csv-qa
python3 -m venv .venv
source .venv/bin/activate
pip install streamlit pandas openai python-dotenv
Keep this out of Git

If you track this project with Git, do not commit your virtual environment or any secrets. Create a .gitignore file in the project folder with at least these lines:

.gitignore
.venv/
.env
__pycache__/

.venv/ is large and specific to your machine, and .env holds your API key — neither belongs in a shared repository. New to Git? See Track a New Project with Git.

2. Add API connection values

Your program needs three values to reach the API: your API key, the gateway URL, and a model ID. You keep them in a small text file named .env so they stay out of your code — and out of Git.

What is a .env file, and where does it go?

A .env ("dot-env") file is a plain text file that stores settings as NAME=value lines, one per line. Programs read it at startup so you never have to paste secrets like API keys directly into your code.

  • The filename is literally .env — a leading dot with nothing before it (not config.env or .env.txt).
  • It goes in your project folder: the same directory you just created, where the .venv lives and where you run your commands.
  • The python-dotenv package loads it with load_dotenv(), which looks for .env in the folder you run the program from.
  • Because it holds a secret, never commit it to Git (see the note below).

Create a new file named .env in your project folder — in VS Code use File → New File, or run code .env in the terminal — then paste these three lines and replace the placeholders:

.env
OPENAI_API_KEY="paste-your-key-here"
OPENAI_BASE_URL="https://openai.rc.asu.edu/v1"
RC_LLM_MODEL="paste-model-id-here"
Where do the values come from?

Get your API key and a valid model ID from the LLM API guide. The OPENAI_BASE_URL above is already correct for the Research Computing gateway.

  • Use the /v1 base URL, not the full /chat/completions URL, for the Python SDK.
  • Use a model ID that appears in the portal model list for your account.
  • Keep .env out of Git: add a line containing .env to your .gitignore (see Track a New Project with Git).

3. Build the profile view

Start by showing the profile you will later send to the model. This keeps the data step separate from the API step.

You are helping me build a beginner Streamlit app that profiles a CSV.

Project goal:
- Let a user upload a CSV.
- Load it with pandas.
- Build a compact text profile that includes: row and column counts, column names with dtypes,
df.describe(include="all") as text, missing value counts, and the first 5 rows as CSV text.
- Show the profile on the page.

Constraints:
- Keep everything in app.py.
- Add a friendly error for a malformed CSV.
- I will test with a small sample CSV.

Please:
1. Create app.py.
2. Explain how to run it.

4. Add the question box

Now connect the profile to the API. The model answers using only the profile as context.

Please add natural-language Q&A to app.py:

Behavior:
- Add a text input for a question about the data.
- Send the question plus the profile from step 3 to the chat completions API through the OpenAI SDK.
- Read OPENAI_API_KEY, OPENAI_BASE_URL, and RC_LLM_MODEL from a .env file.
- Instruct the model to answer only from the profile, and to say clearly when the profile does
not contain enough detail (for example, questions that need individual rows).

Constraints:
- Do not execute any code the model returns.
- Keep the app beginner-readable.
- Explain how to test it with a sample question.

5. Add column controls

Please improve the app with one change:

- Add a multiselect so the user can choose which columns to include in the profile.

Constraints:
- Rebuild the profile from the selected columns before sending it.
- Keep the default behavior (all columns) when nothing is selected.
- Explain how to test it.

6. Add a transparency panel

Please add one focused feature:

Feature:
- Add an expander labeled "Context sent to the model" that shows the exact profile text used for the last answer.

Constraints:
- Keep the code beginner-readable.
- Explain how this helps a user judge whether an answer is trustworthy.

More prompts to try

  • Add a few suggested questions as buttons that fill the question box.
  • Show basic charts (histogram, value counts) for a selected column.
  • Add a note when the CSV is large explaining that the model sees a profile, not every row.

Troubleshooting

  • If answers seem too general, remember the model only sees the profile, not the full dataset.
  • For very large files, sample rows and summarize before profiling so the context stays small.
  • If the model answers questions it cannot know from the profile, strengthen the "answer only from the profile" instruction.
  • If you get a model error, use a model ID from the portal model list for your account.

Next steps