Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
249 |
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
pie_chart.write_image(temp_chart.name)
|
254 |
-
chart_paths.append(temp_chart.name)
|
255 |
|
256 |
-
|
257 |
-
st.plotly_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 |
-
|
263 |
-
st.plotly_chart(multi_line_chart)
|
264 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
|
265 |
-
|
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)
|