Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,13 +32,20 @@ model = genai.GenerativeModel(
|
|
| 32 |
model_name="gemini-2.0-flash-thinking-exp",
|
| 33 |
generation_config=generation_config,
|
| 34 |
)
|
|
|
|
| 35 |
|
| 36 |
def load_data():
|
| 37 |
-
"""Load data from CSV files"""
|
| 38 |
try:
|
| 39 |
events_df = pd.read_csv("Delta-Events.csv")
|
| 40 |
customers_df = pd.read_csv("delta_customers.csv")
|
| 41 |
products_df = pd.read_csv("Customer_Products.csv")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
return {
|
| 43 |
'events': events_df,
|
| 44 |
'customers': customers_df,
|
|
@@ -182,8 +189,14 @@ class StreamLitResponse(ResponseParser):
|
|
| 182 |
def generateResponse(prompt, data):
|
| 183 |
"""Generate response using PandasAI with SmartDataLake"""
|
| 184 |
llm = GoogleGemini(api_key=gemini_api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
pandas_agent = SmartDatalake(
|
| 186 |
-
data.values(),
|
| 187 |
config={
|
| 188 |
"llm": llm,
|
| 189 |
"response_parser": StreamLitResponse
|
|
@@ -225,7 +238,6 @@ def handle_userinput(question, data):
|
|
| 225 |
})
|
| 226 |
|
| 227 |
result = generateResponse(question, data)
|
| 228 |
-
# ... [Keep the rest of handle_userinput same as before]
|
| 229 |
if isinstance(result, dict):
|
| 230 |
response_type = result.get('type', 'text')
|
| 231 |
response_value = result.get('value')
|
|
@@ -252,7 +264,7 @@ def handle_userinput(question, data):
|
|
| 252 |
"content": str(result)
|
| 253 |
})
|
| 254 |
else:
|
| 255 |
-
st.
|
| 256 |
except Exception as e:
|
| 257 |
st.error(f"Error processing input: {e}")
|
| 258 |
|
|
@@ -266,7 +278,6 @@ def main():
|
|
| 266 |
if "data" not in st.session_state:
|
| 267 |
st.session_state.data = load_data()
|
| 268 |
|
| 269 |
-
|
| 270 |
# Create tabs
|
| 271 |
tab_dashboard, tab_chat, tab_reports = st.tabs(["📊 Dashboard", "💬 Chat", "📈 Reports"])
|
| 272 |
|
|
@@ -280,22 +291,21 @@ def main():
|
|
| 280 |
# Chat Tab
|
| 281 |
with tab_chat:
|
| 282 |
st.title("AI Data Analyst")
|
| 283 |
-
# ... [Keep chat interface similar but update handle_userinput calls to use st.session_state.data]
|
| 284 |
chat_container = st.container()
|
| 285 |
with chat_container:
|
| 286 |
for message in st.session_state.chat_history:
|
| 287 |
with st.chat_message(message["role"]):
|
| 288 |
render_chat_message(message)
|
| 289 |
-
|
| 290 |
user_question = st.chat_input("Ask a question about your data:")
|
| 291 |
if user_question:
|
| 292 |
handle_userinput(user_question, st.session_state.data)
|
| 293 |
-
# Update chat container immediately after processing the input
|
| 294 |
chat_container.empty()
|
| 295 |
with chat_container:
|
| 296 |
for message in st.session_state.chat_history:
|
| 297 |
with st.chat_message(message["role"]):
|
| 298 |
render_chat_message(message)
|
|
|
|
| 299 |
# Reports Tab
|
| 300 |
with tab_reports:
|
| 301 |
st.title("Custom Reports")
|
|
@@ -333,16 +343,15 @@ def main():
|
|
| 333 |
report = response.text
|
| 334 |
|
| 335 |
# PDF Generation and display
|
| 336 |
-
# ... [Keep the PDF generation code from original]
|
| 337 |
try:
|
| 338 |
-
pdf_bytes = generate_pdf(report)
|
| 339 |
st.download_button(
|
| 340 |
label="Download Report as PDF",
|
| 341 |
data=pdf_bytes,
|
| 342 |
file_name="report.pdf",
|
| 343 |
mime="application/pdf"
|
| 344 |
)
|
| 345 |
-
st.markdown(report)
|
| 346 |
except Exception as e:
|
| 347 |
st.error(f"Error generating PDF: {e}")
|
| 348 |
st.markdown(report)
|
|
|
|
| 32 |
model_name="gemini-2.0-flash-thinking-exp",
|
| 33 |
generation_config=generation_config,
|
| 34 |
)
|
| 35 |
+
|
| 36 |
|
| 37 |
def load_data():
|
| 38 |
+
"""Load data from CSV files and validate"""
|
| 39 |
try:
|
| 40 |
events_df = pd.read_csv("Delta-Events.csv")
|
| 41 |
customers_df = pd.read_csv("delta_customers.csv")
|
| 42 |
products_df = pd.read_csv("Customer_Products.csv")
|
| 43 |
+
|
| 44 |
+
# Validate data
|
| 45 |
+
if events_df.empty or customers_df.empty or products_df.empty:
|
| 46 |
+
st.error("One or more data files are empty.")
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
return {
|
| 50 |
'events': events_df,
|
| 51 |
'customers': customers_df,
|
|
|
|
| 189 |
def generateResponse(prompt, data):
|
| 190 |
"""Generate response using PandasAI with SmartDataLake"""
|
| 191 |
llm = GoogleGemini(api_key=gemini_api_key)
|
| 192 |
+
|
| 193 |
+
# Ensure data is a dictionary of DataFrames
|
| 194 |
+
if not isinstance(data, dict) or not all(isinstance(df, pd.DataFrame) for df in data.values()):
|
| 195 |
+
st.error("Invalid data format. Expected a dictionary of DataFrames.")
|
| 196 |
+
return None
|
| 197 |
+
|
| 198 |
pandas_agent = SmartDatalake(
|
| 199 |
+
list(data.values()), # Pass list of DataFrames
|
| 200 |
config={
|
| 201 |
"llm": llm,
|
| 202 |
"response_parser": StreamLitResponse
|
|
|
|
| 238 |
})
|
| 239 |
|
| 240 |
result = generateResponse(question, data)
|
|
|
|
| 241 |
if isinstance(result, dict):
|
| 242 |
response_type = result.get('type', 'text')
|
| 243 |
response_value = result.get('value')
|
|
|
|
| 264 |
"content": str(result)
|
| 265 |
})
|
| 266 |
else:
|
| 267 |
+
st.error("No valid data available for analysis.")
|
| 268 |
except Exception as e:
|
| 269 |
st.error(f"Error processing input: {e}")
|
| 270 |
|
|
|
|
| 278 |
if "data" not in st.session_state:
|
| 279 |
st.session_state.data = load_data()
|
| 280 |
|
|
|
|
| 281 |
# Create tabs
|
| 282 |
tab_dashboard, tab_chat, tab_reports = st.tabs(["📊 Dashboard", "💬 Chat", "📈 Reports"])
|
| 283 |
|
|
|
|
| 291 |
# Chat Tab
|
| 292 |
with tab_chat:
|
| 293 |
st.title("AI Data Analyst")
|
|
|
|
| 294 |
chat_container = st.container()
|
| 295 |
with chat_container:
|
| 296 |
for message in st.session_state.chat_history:
|
| 297 |
with st.chat_message(message["role"]):
|
| 298 |
render_chat_message(message)
|
| 299 |
+
|
| 300 |
user_question = st.chat_input("Ask a question about your data:")
|
| 301 |
if user_question:
|
| 302 |
handle_userinput(user_question, st.session_state.data)
|
|
|
|
| 303 |
chat_container.empty()
|
| 304 |
with chat_container:
|
| 305 |
for message in st.session_state.chat_history:
|
| 306 |
with st.chat_message(message["role"]):
|
| 307 |
render_chat_message(message)
|
| 308 |
+
|
| 309 |
# Reports Tab
|
| 310 |
with tab_reports:
|
| 311 |
st.title("Custom Reports")
|
|
|
|
| 343 |
report = response.text
|
| 344 |
|
| 345 |
# PDF Generation and display
|
|
|
|
| 346 |
try:
|
| 347 |
+
pdf_bytes = generate_pdf(report)
|
| 348 |
st.download_button(
|
| 349 |
label="Download Report as PDF",
|
| 350 |
data=pdf_bytes,
|
| 351 |
file_name="report.pdf",
|
| 352 |
mime="application/pdf"
|
| 353 |
)
|
| 354 |
+
st.markdown(report)
|
| 355 |
except Exception as e:
|
| 356 |
st.error(f"Error generating PDF: {e}")
|
| 357 |
st.markdown(report)
|