DrishtiSharma commited on
Commit
2ef41d6
·
verified ·
1 Parent(s): e45c62b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -18
app.py CHANGED
@@ -392,50 +392,52 @@ def create_visualizations(analyst_output):
392
  values = item["Values"]
393
 
394
  try:
395
- # Handle dictionary data
396
  if isinstance(values, dict):
397
  df = pd.DataFrame(list(values.items()), columns=["Label", "Count"])
398
-
399
- # Choose Pie Chart for fewer categories, else Bar Chart
400
  if len(df) <= 5:
401
  chart = px.pie(df, names="Label", values="Count", title=f"{category} Distribution")
402
  else:
403
  chart = px.bar(df, x="Label", y="Count", title=f"{category} Analysis")
404
 
405
- # Handle list data
406
  elif isinstance(values, list):
407
- # Convert the list into a frequency count without dummy values
408
- df = pd.DataFrame(values, columns=["Label"])
409
- df = df["Label"].value_counts().reset_index()
410
- df.columns = ["Label", "Count"]
411
-
412
- # Plot as a bar chart or pie chart
413
- if len(df) <= 5:
414
- chart = px.pie(df, names="Label", values="Count", title=f"{category} Distribution")
415
  else:
416
- chart = px.bar(df, x="Label", y="Count", title=f"{category} Frequency")
 
 
 
417
 
418
- # Handle text data
419
  elif isinstance(values, str):
420
  st.subheader(f"{category} Insights")
421
  st.table(pd.DataFrame({"Insights": [values]}))
422
- continue # No chart for text data
423
 
424
  else:
425
  st.warning(f"Unsupported data format for category: {category}")
 
426
  continue
427
 
428
- # Display the chart in Streamlit
429
  st.plotly_chart(chart)
430
 
431
- # Save the chart for PDF export
432
  with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
433
  chart.write_image(temp_chart.name)
434
  chart_paths.append(temp_chart.name)
435
 
436
  except Exception as e:
437
  st.error(f"Failed to generate visualization for {category}: {e}")
438
- logging.error(f"Error in {category} visualization: {e}")
439
 
440
  return chart_paths
441
 
 
392
  values = item["Values"]
393
 
394
  try:
395
+ # Handle dictionary data for bar charts
396
  if isinstance(values, dict):
397
  df = pd.DataFrame(list(values.items()), columns=["Label", "Count"])
 
 
398
  if len(df) <= 5:
399
  chart = px.pie(df, names="Label", values="Count", title=f"{category} Distribution")
400
  else:
401
  chart = px.bar(df, x="Label", y="Count", title=f"{category} Analysis")
402
 
403
+ # Handle list data for bar/pie charts
404
  elif isinstance(values, list):
405
+ # Check if it's a list of dictionaries (e.g., Technology Spotlight)
406
+ if all(isinstance(v, dict) for v in values):
407
+ df = pd.DataFrame(values)
408
+ st.subheader(f"{category} (Detailed View)")
409
+ st.dataframe(df)
410
+ continue # Skip chart for detailed data
411
+
412
+ # Frequency analysis for simple lists
413
  else:
414
+ df = pd.DataFrame(values, columns=["Items"])
415
+ df = df["Items"].value_counts().reset_index()
416
+ df.columns = ["Label", "Count"]
417
+ chart = px.pie(df, names="Label", values="Count", title=f"{category} Distribution") if len(df) <= 5 else px.bar(df, x="Label", y="Count", title=f"{category} Frequency")
418
 
419
+ # Handle string data (Insights)
420
  elif isinstance(values, str):
421
  st.subheader(f"{category} Insights")
422
  st.table(pd.DataFrame({"Insights": [values]}))
423
+ continue
424
 
425
  else:
426
  st.warning(f"Unsupported data format for category: {category}")
427
+ logging.warning(f"Unsupported data format in {category}: {values}")
428
  continue
429
 
430
+ # Display in Streamlit
431
  st.plotly_chart(chart)
432
 
433
+ # Save for PDF export
434
  with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
435
  chart.write_image(temp_chart.name)
436
  chart_paths.append(temp_chart.name)
437
 
438
  except Exception as e:
439
  st.error(f"Failed to generate visualization for {category}: {e}")
440
+ logging.error(f"Visualization error in {category}: {e}")
441
 
442
  return chart_paths
443