Repository navigation
Expand file tree
/
Copy pathapp_csv.py
More file actions
41 lines (34 loc) · 1.48 KB
/
Copy pathapp_csv.py
File metadata and controls
41 lines (34 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
###
# Author: Pierre-Henry Soria <hi@ph7.me>
# Local CSV analysis demo. Run with: streamlit run app_csv.py
###
from os import environ as env
import pandas as pd
import streamlit as st
from langchain.agents import create_pandas_dataframe_agent
from langchain.llms import OpenAI
st.title(":sunglasses: Analysis Assistant :sunglasses:")
st.caption("Local demo for trusted CSVs only. Questions and CSV context are sent to OpenAI.")
uploaded_file = st.file_uploader("Choose a CSV file", type=["csv"])
if uploaded_file is not None:
try:
dataframe = pd.read_csv(uploaded_file)
except (pd.errors.ParserError, pd.errors.EmptyDataError, UnicodeDecodeError):
st.error("Choose a non-empty, UTF-8 CSV file with consistent columns.")
st.stop()
st.dataframe(dataframe)
question = st.text_input("What's your question?").strip()
if st.button("Ask :robot_face:"):
if not question:
st.error("Enter a question before asking.")
elif not env.get("OPENAI_API_KEY", "").strip():
st.error("Set OPENAI_API_KEY in the environment before starting the app.")
else:
try:
llm = OpenAI(temperature=0)
agent = create_pandas_dataframe_agent(llm, dataframe, verbose=False)
answer = agent.run(question)
except Exception:
st.error("Analysis failed. Check your API access and question, then try again.")
else:
st.success(answer, icon="🤖")