Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -381,7 +381,7 @@ def validate_analyst_output(analyst_output):
|
|
| 381 |
return analyst_output
|
| 382 |
|
| 383 |
|
| 384 |
-
# Visualization
|
| 385 |
def create_visualizations(analyst_output):
|
| 386 |
chart_paths = []
|
| 387 |
validated_data = validate_analyst_output(analyst_output)
|
|
@@ -392,7 +392,7 @@ def create_visualizations(analyst_output):
|
|
| 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:
|
|
@@ -402,21 +402,27 @@ def create_visualizations(analyst_output):
|
|
| 402 |
|
| 403 |
# Handle list data for bar/pie charts
|
| 404 |
elif isinstance(values, list):
|
| 405 |
-
#
|
| 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 |
-
|
| 411 |
-
|
| 412 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 420 |
elif isinstance(values, str):
|
| 421 |
st.subheader(f"{category} Insights")
|
| 422 |
st.table(pd.DataFrame({"Insights": [values]}))
|
|
@@ -427,10 +433,10 @@ def create_visualizations(analyst_output):
|
|
| 427 |
logging.warning(f"Unsupported data format in {category}: {values}")
|
| 428 |
continue
|
| 429 |
|
| 430 |
-
# Display
|
| 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)
|
|
@@ -441,6 +447,7 @@ def create_visualizations(analyst_output):
|
|
| 441 |
|
| 442 |
return chart_paths
|
| 443 |
|
|
|
|
| 444 |
def display_table(analyst_output):
|
| 445 |
table_data = []
|
| 446 |
validated_data = validate_analyst_output(analyst_output)
|
|
@@ -451,34 +458,35 @@ def display_table(analyst_output):
|
|
| 451 |
values = item["Values"]
|
| 452 |
|
| 453 |
try:
|
| 454 |
-
# Handle dictionary data
|
| 455 |
if isinstance(values, dict):
|
| 456 |
df = pd.DataFrame(list(values.items()), columns=["Label", "Count"])
|
| 457 |
st.subheader(f"{category} (Table View)")
|
| 458 |
st.dataframe(df)
|
| 459 |
table_data.extend(df.to_dict(orient="records"))
|
| 460 |
|
| 461 |
-
# Handle list data
|
| 462 |
elif isinstance(values, list):
|
| 463 |
-
# Handle complex lists (list of dictionaries)
|
| 464 |
if all(isinstance(v, dict) for v in values):
|
|
|
|
| 465 |
df = pd.DataFrame(values)
|
| 466 |
st.subheader(f"{category} (Detailed View)")
|
| 467 |
st.dataframe(df)
|
| 468 |
table_data.extend(df.to_dict(orient="records"))
|
| 469 |
-
# Handle simple lists
|
| 470 |
else:
|
|
|
|
| 471 |
df = pd.DataFrame(values, columns=["Items"])
|
| 472 |
st.subheader(f"{category} (List View)")
|
| 473 |
st.dataframe(df)
|
| 474 |
table_data.extend(df.to_dict(orient="records"))
|
| 475 |
|
| 476 |
-
# Handle
|
| 477 |
elif isinstance(values, str):
|
| 478 |
st.subheader(f"{category} (Summary)")
|
| 479 |
st.table(pd.DataFrame({"Insights": [values]}))
|
| 480 |
table_data.append({"Category": category, "Values": values})
|
| 481 |
|
|
|
|
| 482 |
else:
|
| 483 |
st.warning(f"Unsupported data format for {category}")
|
| 484 |
logging.warning(f"Unsupported data in {category}: {values}")
|
|
@@ -489,7 +497,6 @@ def display_table(analyst_output):
|
|
| 489 |
|
| 490 |
return table_data
|
| 491 |
|
| 492 |
-
|
| 493 |
def parse_analyst_output(raw_output):
|
| 494 |
key_insights = []
|
| 495 |
data_insights = []
|
|
|
|
| 381 |
return analyst_output
|
| 382 |
|
| 383 |
|
| 384 |
+
# Visualization
|
| 385 |
def create_visualizations(analyst_output):
|
| 386 |
chart_paths = []
|
| 387 |
validated_data = validate_analyst_output(analyst_output)
|
|
|
|
| 392 |
values = item["Values"]
|
| 393 |
|
| 394 |
try:
|
| 395 |
+
# Handle dictionary data for bar/pie charts
|
| 396 |
if isinstance(values, dict):
|
| 397 |
df = pd.DataFrame(list(values.items()), columns=["Label", "Count"])
|
| 398 |
if len(df) <= 5:
|
|
|
|
| 402 |
|
| 403 |
# Handle list data for bar/pie charts
|
| 404 |
elif isinstance(values, list):
|
| 405 |
+
# Handle 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 |
+
# Optional: Generate bar chart for complex data
|
| 411 |
+
for col in df.columns:
|
| 412 |
+
if pd.api.types.is_numeric_dtype(df[col]):
|
| 413 |
+
chart = px.bar(df, x=df.index, y=col, title=f"{category} - {col} Analysis")
|
| 414 |
+
st.plotly_chart(chart)
|
| 415 |
+
break
|
| 416 |
+
continue
|
| 417 |
+
|
| 418 |
+
# Handle simple lists
|
| 419 |
else:
|
| 420 |
df = pd.DataFrame(values, columns=["Items"])
|
| 421 |
df = df["Items"].value_counts().reset_index()
|
| 422 |
df.columns = ["Label", "Count"]
|
| 423 |
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")
|
| 424 |
|
| 425 |
+
# Handle text data
|
| 426 |
elif isinstance(values, str):
|
| 427 |
st.subheader(f"{category} Insights")
|
| 428 |
st.table(pd.DataFrame({"Insights": [values]}))
|
|
|
|
| 433 |
logging.warning(f"Unsupported data format in {category}: {values}")
|
| 434 |
continue
|
| 435 |
|
| 436 |
+
# Display the chart
|
| 437 |
st.plotly_chart(chart)
|
| 438 |
|
| 439 |
+
# Save the chart for PDF export
|
| 440 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_chart:
|
| 441 |
chart.write_image(temp_chart.name)
|
| 442 |
chart_paths.append(temp_chart.name)
|
|
|
|
| 447 |
|
| 448 |
return chart_paths
|
| 449 |
|
| 450 |
+
|
| 451 |
def display_table(analyst_output):
|
| 452 |
table_data = []
|
| 453 |
validated_data = validate_analyst_output(analyst_output)
|
|
|
|
| 458 |
values = item["Values"]
|
| 459 |
|
| 460 |
try:
|
| 461 |
+
# Handle dictionary data
|
| 462 |
if isinstance(values, dict):
|
| 463 |
df = pd.DataFrame(list(values.items()), columns=["Label", "Count"])
|
| 464 |
st.subheader(f"{category} (Table View)")
|
| 465 |
st.dataframe(df)
|
| 466 |
table_data.extend(df.to_dict(orient="records"))
|
| 467 |
|
| 468 |
+
# Handle list data
|
| 469 |
elif isinstance(values, list):
|
|
|
|
| 470 |
if all(isinstance(v, dict) for v in values):
|
| 471 |
+
# Detailed View for list of dictionaries
|
| 472 |
df = pd.DataFrame(values)
|
| 473 |
st.subheader(f"{category} (Detailed View)")
|
| 474 |
st.dataframe(df)
|
| 475 |
table_data.extend(df.to_dict(orient="records"))
|
|
|
|
| 476 |
else:
|
| 477 |
+
# Simple List View
|
| 478 |
df = pd.DataFrame(values, columns=["Items"])
|
| 479 |
st.subheader(f"{category} (List View)")
|
| 480 |
st.dataframe(df)
|
| 481 |
table_data.extend(df.to_dict(orient="records"))
|
| 482 |
|
| 483 |
+
# Handle string data
|
| 484 |
elif isinstance(values, str):
|
| 485 |
st.subheader(f"{category} (Summary)")
|
| 486 |
st.table(pd.DataFrame({"Insights": [values]}))
|
| 487 |
table_data.append({"Category": category, "Values": values})
|
| 488 |
|
| 489 |
+
# Handle unsupported data types
|
| 490 |
else:
|
| 491 |
st.warning(f"Unsupported data format for {category}")
|
| 492 |
logging.warning(f"Unsupported data in {category}: {values}")
|
|
|
|
| 497 |
|
| 498 |
return table_data
|
| 499 |
|
|
|
|
| 500 |
def parse_analyst_output(raw_output):
|
| 501 |
key_insights = []
|
| 502 |
data_insights = []
|