-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
76 lines (57 loc) · 2.26 KB
/
app.py
File metadata and controls
76 lines (57 loc) · 2.26 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import tempfile
import pandas as pd
import streamlit as st
from executor import execute_code
from llm import generate_code
from prompt import build_prompt
st.set_page_config(page_title="LLM Data Analyst", layout="wide")
st.title("📊 LLM-Powered Data Analyst")
st.markdown("Upload a CSV file and ask questions about it.")
# File uploader
uploaded_file = st.file_uploader("Upload CSV file", type=["csv"])
csv_path = st.session_state.get("csv_path")
if uploaded_file is not None:
# Persist the uploaded file to disk so generated code can read it
with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmp:
tmp.write(uploaded_file.getbuffer())
csv_path = os.path.abspath(tmp.name)
st.session_state["csv_path"] = csv_path
df = pd.read_csv(csv_path, encoding="latin1")
st.session_state["csv_columns"] = list(df.columns)
st.subheader("Dataset Preview")
st.dataframe(df.head())
elif csv_path:
# Reuse previously uploaded file during the same session
df = pd.read_csv(csv_path, encoding="latin1")
st.subheader("Dataset Preview")
st.dataframe(df.head())
else:
st.info("Please upload a CSV file to begin.")
user_query = st.text_input(
"Enter your analysis request:",
placeholder="e.g. Show total sales per region as a bar chart"
)
if st.button("Run Analysis"):
if not user_query:
st.warning("Please enter a query.")
elif not csv_path:
st.warning("Please upload a CSV file first.")
else:
with st.spinner("Generating code with LLM..."):
prompt = build_prompt(user_query, csv_path)
code = generate_code(prompt)
st.subheader("🧠 Generated Code")
st.code(code, language="python")
with st.spinner("Executing code..."):
try:
result = execute_code(code)
if os.path.exists("output.png"):
st.subheader("📈 Visualization")
st.image("output.png")
if result["result_df"] is not None:
st.subheader("📋 Result Table")
st.dataframe(result["result_df"])
st.success("Analysis completed successfully.")
except Exception as e:
st.error(f"Execution failed: {e}")