File size: 12,082 Bytes
b672bc8 d55ace9 b672bc8 | 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | import streamlit as st
import pandas as pd
import requests
import base64
import io
from PIL import Image
# Configuration
BACKEND_URL = "Not set"
# Streamlit Page Setup
st.set_page_config(page_title="AI CSV Analyst", layout="wide")
def get_chat_history(user_id):
"""Fetch or create a new chat session from backend."""
try:
# If user_id is None, create a new session by calling with 'new' or empty
session_id = user_id if user_id else "new"
response = requests.get(f"{BACKEND_URL}/chat/history/{session_id}", timeout=10)
if response.status_code == 200:
data = response.json()
return data.get("user_id") # Returns the user_id (new or existing)
else:
st.warning("Unable to fetch chat history.")
return None
except requests.exceptions.RequestException as e:
st.warning("Backend not reachable.")
return None
# File Upload Function
def file_upload_interface():
"""Returns file_id if a file is uploaded and selected, None otherwise"""
# Initialize session state for storing multiple files
if "uploaded_files" not in st.session_state:
st.session_state.uploaded_files = {}
if "selected_file_id" not in st.session_state:
st.session_state.selected_file_id = None
# File uploader
uploaded_file = st.file_uploader("Upload CSV file", type=["csv"], accept_multiple_files=False)
# If a file is uploaded by the user
if uploaded_file:
df_preview = pd.read_csv(uploaded_file)
# Check if this file is already uploaded (by name)
file_already_exists = any(
file_data["name"] == uploaded_file.name
for file_data in st.session_state.uploaded_files.values()
)
if not file_already_exists:
with st.spinner(f"Uploading {uploaded_file.name} to backend..."):
files = {"file": (uploaded_file.name, uploaded_file.getvalue(), "text/csv")}
try:
response = requests.post(f"{BACKEND_URL}/upload/", files=files)
if response.status_code == 200:
data = response.json()
file_id = data["file_id"]
st.session_state.uploaded_files[file_id] = {
"name": uploaded_file.name,
"df": df_preview
}
st.session_state.selected_file_id = file_id
st.success(f"{uploaded_file.name} uploaded successfully!")
st.rerun()
else:
st.error(f"Upload failed: {response.text}")
except Exception as e:
st.error(f"Error uploading file: {e}")
else:
st.info(f" {uploaded_file.name} is already uploaded.")
# Display file selector if files are uploaded
if st.session_state.uploaded_files:
st.subheader("π Uploaded Files")
file_options = {
file_id: file_data["name"]
for file_id, file_data in st.session_state.uploaded_files.items()
}
selected_file_name = st.selectbox(
"Select a file to work with:",
options=list(file_options.keys()),
format_func=lambda x: file_options[x],
index=list(file_options.keys()).index(st.session_state.selected_file_id)
if st.session_state.selected_file_id in file_options else 0
)
st.session_state.selected_file_id = selected_file_name
# Display preview
st.subheader(f"Preview: {file_options[selected_file_name]}")
st.dataframe(st.session_state.uploaded_files[selected_file_name]["df"].head())
# Remove file button
col1, col2 = st.columns([3, 1])
with col2:
if st.button("ποΈ Remove Selected File"):
del st.session_state.uploaded_files[st.session_state.selected_file_id]
st.session_state.selected_file_id = (
list(st.session_state.uploaded_files.keys())[0]
if st.session_state.uploaded_files else None
)
st.rerun()
return st.session_state.selected_file_id
return None
# Chat Interface
def run_chat_interface(file_id, chat_name):
"""Display the chat interface for asking questions"""
st.title("π AI-Powered CSV Data Analyst")
st.caption(f"π¬ **{chat_name}** | File ID: `{file_id}`")
# Get file info
if file_id in st.session_state.uploaded_files:
file_name = st.session_state.uploaded_files[file_id]["name"]
st.info(f"π Working with: **{file_name}**")
st.markdown("---")
# Query input
st.subheader("π¬ Ask a question about your data:")
query = st.text_area("Type your question here...", placeholder="e.g., What is the average revenue by region?")
if st.button("Run Query", type="primary"):
if not query.strip():
st.warning("Please enter a question first.")
else:
with st.spinner("Analyzing data..."):
try:
payload = {
"query": query,
"file_id": file_id,
"user_id": st.session_state.user_id
}
response = requests.post(f"{BACKEND_URL}/chat/", json=payload)
if response.status_code == 200:
data = response.json()
# Store result in session state so it persists
st.session_state.last_result = data
else:
st.error(f"Backend error: {response.text}")
except Exception as e:
st.error(f"β οΈ Something went wrong: {e}")
# Display results if they exist in session state
if "last_result" in st.session_state:
data = st.session_state.last_result
st.write("###Generated Code:")
st.code(data.get("generated_code", "No code generated"), language="python")
st.write("### π Result:")
result = data.get("result")
image = data.get("image")
error = data.get("error")
if result is not None:
st.write(result)
if image is not None:
try:
img_data = base64.b64decode(image)
image = Image.open(io.BytesIO(img_data))
st.image(image, caption="Generated plot")
except Exception:
st.error("Could not decode backend image")
if error is not None:
st.error(error)
st.markdown("---")
st.write("### π Was this helpful?")
col1, col2 = st.columns(2)
with col1:
if st.button("π Thumbs Up"):
try:
feedback_payload = {
"query": query,
"code": data.get("generated_code", ""),
"feedback": "thumbs_up"
}
feedback_response = requests.post(
f"{BACKEND_URL}/chat/feedback",
json=feedback_payload
)
if feedback_response.status_code == 200:
st.success("Thanks for your feedback!")
else:
st.error("Failed to submit feedback")
except Exception as e:
st.error(f"Error submitting feedback: {e}")
with col2:
if st.button("π Thumbs Down"):
try:
feedback_payload = {
"query": query,
"code": data.get("generated_code", ""),
"feedback": "thumbs_down"
}
feedback_response = requests.post(
f"{BACKEND_URL}/chat/feedback",
json=feedback_payload
)
if feedback_response.status_code == 200:
st.success("Thanks for your feedback!")
else:
st.error("Failed to submit feedback")
except Exception as e:
st.error(f"Error submitting feedback: {e}")
# Main Application Logic
# Initialize session state
if "chat_sessions" not in st.session_state:
st.session_state.chat_sessions = {} # {user_id: [chat_name, file_id]}
if "user_id" not in st.session_state:
st.session_state.user_id = None
if "creating_new_chat" not in st.session_state:
st.session_state.creating_new_chat = False
# Sidebar
st.sidebar.title("π¬ Chat Sessions")
# New Chat Button
if st.sidebar.button("β New Chat"):
st.session_state.creating_new_chat = True
st.session_state.user_id = None # Deselect current chat
st.rerun()
# Show Existing Chats
if st.session_state.chat_sessions:
st.sidebar.subheader("π Your Chats")
for uid, data in st.session_state.chat_sessions.items():
chat_name, file_id = data
# Highlight the active chat
button_type = "primary" if uid == st.session_state.user_id else "secondary"
if st.sidebar.button(f"{'π’ ' if uid == st.session_state.user_id else ''}{chat_name}", key=uid, type=button_type):
st.session_state.user_id = uid
st.session_state.creating_new_chat = False
st.rerun()
# Main Content Area
# Case 1: Creating a new chat
if st.session_state.creating_new_chat:
st.title("π Create New Chat")
st.caption("Upload a CSV file to start analyzing")
st.markdown("---")
file_id = file_upload_interface()
if file_id is not None:
st.markdown("---")
col1, col2 = st.columns(2)
with col1:
if st.button("β
Start Chat with This File", type="primary", use_container_width=True):
chat_name = f"Chat {len(st.session_state.chat_sessions) + 1}"
new_id = get_chat_history(user_id=None)
st.session_state.chat_sessions[new_id] = [chat_name, file_id]
st.session_state.user_id = new_id
st.session_state.creating_new_chat = False
st.success(f"β
Started {chat_name}!")
st.rerun()
with col2:
if st.button("β Cancel", use_container_width=True):
st.session_state.creating_new_chat = False
st.rerun()
else:
st.info("Please upload a CSV file to continue")
if st.button("β Cancel"):
st.session_state.creating_new_chat = False
st.rerun()
# Case 2: Active chat session
elif st.session_state.user_id and st.session_state.user_id in st.session_state.chat_sessions:
chat_name, file_id = st.session_state.chat_sessions[st.session_state.user_id]
run_chat_interface(file_id, chat_name)
# Case 3: No chat selected
else:
st.title("π AI-Powered CSV Data Analyst")
st.caption("Ask questions about your data in plain English β powered by OpenAI & FastAPI backend")
st.markdown("---")
st.info("Click **'β New Chat'** in the sidebar to get started!")
# Show a welcome message or instructions
st.markdown("""
### Welcome to AI CSV Analyst!
**How to use:**
1. Click "β New Chat" in the sidebar
2. Upload your CSV file
3. Ask questions in plain English
4. Get instant insights and visualizations
**Example questions:**
- "What is the total revenue by region?"
- "Show me a bar chart of sales by category"
- "What are the top 5 customers by purchase amount?"
""")
|