DrishtiSharma commited on
Commit
5cb28b0
·
verified ·
1 Parent(s): a01a041

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -26
app.py CHANGED
@@ -235,41 +235,44 @@ def validate_analyst_output(analyst_output):
235
  def create_visualizations(analyst_output):
236
  chart_paths = []
237
  validated_data = validate_analyst_output(analyst_output)
238
- if validated_data:
239
- data = pd.DataFrame(validated_data)
240
- try:
241
- if data.empty:
242
- raise ValueError("Data for visualizations is empty.")
243
 
244
- bar_chart = px.bar(data, x="Category", y="Values", title="Patent Trends by Category")
245
- st.plotly_chart(bar_chart)
246
- with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
247
- bar_chart.write_image(temp_chart.name)
248
- chart_paths.append(temp_chart.name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
 
250
- pie_chart = px.pie(data, names="Category", values="Values", title="Category Distribution")
251
- st.plotly_chart(pie_chart)
252
- with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
253
- pie_chart.write_image(temp_chart.name)
254
- chart_paths.append(temp_chart.name)
255
 
256
- heatmap_chart = px.density_heatmap(data, x="Category", y="Values", title="Regional Patent Density")
257
- st.plotly_chart(heatmap_chart)
258
- with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
259
- heatmap_chart.write_image(temp_chart.name)
260
- chart_paths.append(temp_chart.name)
261
 
262
- multi_line_chart = px.line(data, x="Category", y="Values", title="Trends Over Time")
263
- st.plotly_chart(multi_line_chart)
264
  with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
265
- multi_line_chart.write_image(temp_chart.name)
266
  chart_paths.append(temp_chart.name)
267
 
268
- except Exception as e:
269
- logging.error(f"Error generating visualization: {e}")
270
- st.error(f"Error generating visualization: {e}")
271
  return chart_paths
272
 
 
273
  def display_table(analyst_output):
274
  table_data = []
275
  validated_data = validate_analyst_output(analyst_output)
 
235
  def create_visualizations(analyst_output):
236
  chart_paths = []
237
  validated_data = validate_analyst_output(analyst_output)
 
 
 
 
 
238
 
239
+ if validated_data:
240
+ for item in validated_data:
241
+ category = item["Category"]
242
+ values = item["Values"]
243
+
244
+ # Handle dictionary-type data for bar charts
245
+ if isinstance(values, dict):
246
+ df = pd.DataFrame(list(values.items()), columns=["Label", "Count"])
247
+ chart = px.bar(df, x="Label", y="Count", title=f"{category} Analysis")
248
+
249
+ # Handle list-type data for pie charts
250
+ elif isinstance(values, list):
251
+ df = pd.DataFrame(values, columns=["Label"])
252
+ df["Count"] = 1 # Assign count for visualization
253
+ chart = px.pie(df, names="Label", values="Count", title=f"{category} Distribution")
254
+
255
+ # Handle text data by converting it into a simple table
256
+ elif isinstance(values, str):
257
+ st.subheader(f"{category} Insights")
258
+ st.table(pd.DataFrame({"Insights": [values]}))
259
+ continue # Skip visualization for text data
260
 
261
+ else:
262
+ st.warning(f"Unsupported data format in category: {category}")
263
+ continue
 
 
264
 
265
+ # Display the chart in Streamlit
266
+ st.plotly_chart(chart)
 
 
 
267
 
268
+ # Save the chart for PDF generation
 
269
  with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
270
+ chart.write_image(temp_chart.name)
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)