Jeremy Live
commited on
Commit
路
f504669
1
Parent(s):
0369200
upgrade graph v1
Browse files
app.py
CHANGED
@@ -568,6 +568,8 @@ async def stream_agent_response(question: str, chat_history: List[List[str]]) ->
|
|
568 |
y=y_col,
|
569 |
title=f"{y_col} por {x_col}"
|
570 |
)
|
|
|
|
|
571 |
except Exception as e:
|
572 |
logger.error(f"Error generating chart: {str(e)}", exc_info=True)
|
573 |
# Don't fail the whole request if chart generation fails
|
@@ -589,6 +591,9 @@ async def stream_agent_response(question: str, chat_history: List[List[str]]) ->
|
|
589 |
if len(pair) >= 2 and isinstance(pair[1], str) and pair[1].strip():
|
590 |
candidate_text = pair[1]
|
591 |
break
|
|
|
|
|
|
|
592 |
if candidate_text:
|
593 |
raw_lines = candidate_text.split('\n')
|
594 |
# Normalize lines: strip bullets and markdown symbols
|
@@ -613,6 +618,7 @@ async def stream_agent_response(question: str, chat_history: List[List[str]]) ->
|
|
613 |
except Exception:
|
614 |
continue
|
615 |
data.append({"label": label, "value": val})
|
|
|
616 |
if len(data) >= 2:
|
617 |
desired_type = 'pie' if any(k in q_lower for k in ["gr谩fico circular", "grafico circular", "pie", "pastel"]) else 'bar'
|
618 |
chart_fig = generate_chart(
|
@@ -622,6 +628,8 @@ async def stream_agent_response(question: str, chat_history: List[List[str]]) ->
|
|
622 |
y="value",
|
623 |
title="Distribuci贸n"
|
624 |
)
|
|
|
|
|
625 |
|
626 |
# Update the assistant's message with the response
|
627 |
assistant_message["content"] = response_text
|
@@ -644,6 +652,10 @@ async def stream_agent_response(question: str, chat_history: List[List[str]]) ->
|
|
644 |
message_content = str(assistant_message)
|
645 |
|
646 |
# Return the assistant's response and an optional interactive chart figure
|
|
|
|
|
|
|
|
|
647 |
return message_content, chart_fig
|
648 |
|
649 |
except Exception as e:
|
|
|
568 |
y=y_col,
|
569 |
title=f"{y_col} por {x_col}"
|
570 |
)
|
571 |
+
if chart_fig is not None:
|
572 |
+
logger.info(f"Chart generated from SQL table: type={desired_type}, x={x_col}, y={y_col}, rows={len(data)}")
|
573 |
except Exception as e:
|
574 |
logger.error(f"Error generating chart: {str(e)}", exc_info=True)
|
575 |
# Don't fail the whole request if chart generation fails
|
|
|
591 |
if len(pair) >= 2 and isinstance(pair[1], str) and pair[1].strip():
|
592 |
candidate_text = pair[1]
|
593 |
break
|
594 |
+
# Also consider current response_text as a data source
|
595 |
+
if not candidate_text and isinstance(response_text, str) and response_text.strip():
|
596 |
+
candidate_text = response_text
|
597 |
if candidate_text:
|
598 |
raw_lines = candidate_text.split('\n')
|
599 |
# Normalize lines: strip bullets and markdown symbols
|
|
|
618 |
except Exception:
|
619 |
continue
|
620 |
data.append({"label": label, "value": val})
|
621 |
+
logger.info(f"Fallback parse from text: extracted {len(data)} items for potential chart")
|
622 |
if len(data) >= 2:
|
623 |
desired_type = 'pie' if any(k in q_lower for k in ["gr谩fico circular", "grafico circular", "pie", "pastel"]) else 'bar'
|
624 |
chart_fig = generate_chart(
|
|
|
628 |
y="value",
|
629 |
title="Distribuci贸n"
|
630 |
)
|
631 |
+
if chart_fig is not None:
|
632 |
+
logger.info(f"Chart generated from text fallback: type={desired_type}, items={len(data)}")
|
633 |
|
634 |
# Update the assistant's message with the response
|
635 |
assistant_message["content"] = response_text
|
|
|
652 |
message_content = str(assistant_message)
|
653 |
|
654 |
# Return the assistant's response and an optional interactive chart figure
|
655 |
+
if chart_fig is None:
|
656 |
+
logger.info("No chart generated for this turn.")
|
657 |
+
else:
|
658 |
+
logger.info("Returning a chart figure to UI.")
|
659 |
return message_content, chart_fig
|
660 |
|
661 |
except Exception as e:
|