Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,7 @@ import pandas as pd
|
|
9 |
import plotly.express as px
|
10 |
import tempfile
|
11 |
import time
|
|
|
12 |
import logging
|
13 |
|
14 |
# Setup logging
|
@@ -241,38 +242,56 @@ def create_visualizations(analyst_output):
|
|
241 |
category = item["Category"]
|
242 |
values = item["Values"]
|
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 |
-
chart_paths.append(temp_chart.name)
|
272 |
|
273 |
return chart_paths
|
274 |
|
275 |
|
|
|
276 |
def display_table(analyst_output):
|
277 |
table_data = []
|
278 |
validated_data = validate_analyst_output(analyst_output)
|
@@ -282,22 +301,23 @@ def display_table(analyst_output):
|
|
282 |
category = item["Category"]
|
283 |
values = item["Values"]
|
284 |
|
|
|
285 |
try:
|
286 |
-
#
|
287 |
if isinstance(values, dict):
|
288 |
df = pd.DataFrame(list(values.items()), columns=["Label", "Count"])
|
289 |
st.subheader(f"{category} (Table View)")
|
290 |
st.dataframe(df)
|
291 |
table_data.extend(df.to_dict(orient="records"))
|
292 |
|
293 |
-
#
|
294 |
elif isinstance(values, list):
|
295 |
df = pd.DataFrame(values, columns=["Items"])
|
296 |
st.subheader(f"{category} (List View)")
|
297 |
st.dataframe(df)
|
298 |
table_data.extend(df.to_dict(orient="records"))
|
299 |
|
300 |
-
#
|
301 |
elif isinstance(values, str):
|
302 |
st.subheader(f"{category} (Summary)")
|
303 |
st.table(pd.DataFrame({"Insights": [values]}))
|
@@ -307,49 +327,51 @@ def display_table(analyst_output):
|
|
307 |
st.warning(f"Unsupported data format for category: {category}")
|
308 |
|
309 |
except Exception as e:
|
310 |
-
logging.error(f"Error
|
311 |
-
st.error(f"Failed to display {category} as a table.")
|
312 |
|
313 |
return table_data
|
314 |
|
315 |
|
|
|
316 |
def parse_analyst_output(raw_output):
|
317 |
structured_data = []
|
318 |
current_category = None
|
319 |
current_values = []
|
320 |
|
|
|
321 |
lines = raw_output.split('\n')
|
322 |
|
323 |
for line in lines:
|
324 |
line = line.strip()
|
325 |
|
326 |
-
# Detect new category
|
327 |
if line.startswith("Category:"):
|
328 |
-
# Save previous category
|
329 |
if current_category and current_values:
|
330 |
structured_data.append({
|
331 |
"Category": current_category,
|
332 |
"Values": current_values if len(current_values) > 1 else current_values[0]
|
333 |
})
|
334 |
-
# Start new category
|
335 |
current_category = line.replace("Category:", "").strip()
|
336 |
current_values = []
|
337 |
|
338 |
-
#
|
339 |
elif line.startswith("Values:"):
|
340 |
continue
|
341 |
|
342 |
-
#
|
343 |
elif line and current_category:
|
344 |
try:
|
345 |
-
# Attempt to
|
346 |
parsed_value = ast.literal_eval(line)
|
347 |
current_values.append(parsed_value)
|
348 |
except (ValueError, SyntaxError):
|
349 |
# If parsing fails, treat it as plain text
|
350 |
current_values.append(line)
|
351 |
|
352 |
-
# Save the last category
|
353 |
if current_category and current_values:
|
354 |
structured_data.append({
|
355 |
"Category": current_category,
|
|
|
9 |
import plotly.express as px
|
10 |
import tempfile
|
11 |
import time
|
12 |
+
import ast
|
13 |
import logging
|
14 |
|
15 |
# Setup logging
|
|
|
242 |
category = item["Category"]
|
243 |
values = item["Values"]
|
244 |
|
245 |
+
try:
|
246 |
+
# Handle dictionary data
|
247 |
+
if isinstance(values, dict):
|
248 |
+
df = pd.DataFrame(list(values.items()), columns=["Label", "Count"])
|
249 |
|
250 |
+
# Choose Pie Chart for fewer categories, else Bar Chart
|
251 |
+
if len(df) <= 5:
|
252 |
+
chart = px.pie(df, names="Label", values="Count", title=f"{category} Distribution")
|
253 |
+
else:
|
254 |
+
chart = px.bar(df, x="Label", y="Count", title=f"{category} Analysis")
|
255 |
|
256 |
+
# Handle list data
|
257 |
+
elif isinstance(values, list):
|
258 |
+
# Convert the list into a frequency count without dummy values
|
259 |
+
df = pd.DataFrame(values, columns=["Label"])
|
260 |
+
df = df["Label"].value_counts().reset_index()
|
261 |
+
df.columns = ["Label", "Count"]
|
262 |
+
|
263 |
+
# Plot as a bar chart or pie chart
|
264 |
+
if len(df) <= 5:
|
265 |
+
chart = px.pie(df, names="Label", values="Count", title=f"{category} Distribution")
|
266 |
+
else:
|
267 |
+
chart = px.bar(df, x="Label", y="Count", title=f"{category} Frequency")
|
268 |
+
|
269 |
+
# Handle text data
|
270 |
+
elif isinstance(values, str):
|
271 |
+
st.subheader(f"{category} Insights")
|
272 |
+
st.table(pd.DataFrame({"Insights": [values]}))
|
273 |
+
continue # No chart for text data
|
274 |
|
275 |
+
else:
|
276 |
+
st.warning(f"Unsupported data format for category: {category}")
|
277 |
+
continue
|
278 |
+
|
279 |
+
# Display the chart in Streamlit
|
280 |
+
st.plotly_chart(chart)
|
281 |
|
282 |
+
# Save the chart for PDF export
|
283 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
|
284 |
+
chart.write_image(temp_chart.name)
|
285 |
+
chart_paths.append(temp_chart.name)
|
286 |
|
287 |
+
except Exception as e:
|
288 |
+
st.error(f"Failed to generate visualization for {category}: {e}")
|
289 |
+
logging.error(f"Error in {category} visualization: {e}")
|
|
|
290 |
|
291 |
return chart_paths
|
292 |
|
293 |
|
294 |
+
|
295 |
def display_table(analyst_output):
|
296 |
table_data = []
|
297 |
validated_data = validate_analyst_output(analyst_output)
|
|
|
301 |
category = item["Category"]
|
302 |
values = item["Values"]
|
303 |
|
304 |
+
# Error handling to prevent crashes
|
305 |
try:
|
306 |
+
# Handle dictionary data (Table View)
|
307 |
if isinstance(values, dict):
|
308 |
df = pd.DataFrame(list(values.items()), columns=["Label", "Count"])
|
309 |
st.subheader(f"{category} (Table View)")
|
310 |
st.dataframe(df)
|
311 |
table_data.extend(df.to_dict(orient="records"))
|
312 |
|
313 |
+
# Handle list data (List View)
|
314 |
elif isinstance(values, list):
|
315 |
df = pd.DataFrame(values, columns=["Items"])
|
316 |
st.subheader(f"{category} (List View)")
|
317 |
st.dataframe(df)
|
318 |
table_data.extend(df.to_dict(orient="records"))
|
319 |
|
320 |
+
# Handle text data (Summary View)
|
321 |
elif isinstance(values, str):
|
322 |
st.subheader(f"{category} (Summary)")
|
323 |
st.table(pd.DataFrame({"Insights": [values]}))
|
|
|
327 |
st.warning(f"Unsupported data format for category: {category}")
|
328 |
|
329 |
except Exception as e:
|
330 |
+
logging.error(f"Error processing {category}: {e}")
|
331 |
+
st.error(f"Failed to display {category} as a table due to an error.")
|
332 |
|
333 |
return table_data
|
334 |
|
335 |
|
336 |
+
|
337 |
def parse_analyst_output(raw_output):
|
338 |
structured_data = []
|
339 |
current_category = None
|
340 |
current_values = []
|
341 |
|
342 |
+
# Split raw output by line
|
343 |
lines = raw_output.split('\n')
|
344 |
|
345 |
for line in lines:
|
346 |
line = line.strip()
|
347 |
|
348 |
+
# Detect the start of a new category
|
349 |
if line.startswith("Category:"):
|
350 |
+
# Save the previous category and its values
|
351 |
if current_category and current_values:
|
352 |
structured_data.append({
|
353 |
"Category": current_category,
|
354 |
"Values": current_values if len(current_values) > 1 else current_values[0]
|
355 |
})
|
356 |
+
# Start processing the new category
|
357 |
current_category = line.replace("Category:", "").strip()
|
358 |
current_values = []
|
359 |
|
360 |
+
# Skip 'Values:' header
|
361 |
elif line.startswith("Values:"):
|
362 |
continue
|
363 |
|
364 |
+
# Process the values under the current category
|
365 |
elif line and current_category:
|
366 |
try:
|
367 |
+
# Attempt to convert the line into Python data (dict/list)
|
368 |
parsed_value = ast.literal_eval(line)
|
369 |
current_values.append(parsed_value)
|
370 |
except (ValueError, SyntaxError):
|
371 |
# If parsing fails, treat it as plain text
|
372 |
current_values.append(line)
|
373 |
|
374 |
+
# Save the last processed category
|
375 |
if current_category and current_values:
|
376 |
structured_data.append({
|
377 |
"Category": current_category,
|