Jeremy Live
commited on
Commit
·
26e76ca
1
Parent(s):
fa719ce
m1
Browse files
app.py
CHANGED
@@ -389,6 +389,33 @@ def execute_sql_query(query, db_connection):
|
|
389 |
except Exception as e:
|
390 |
return f"Error ejecutando la consulta: {str(e)}"
|
391 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
392 |
def generate_plot(data, x_col, y_col, title, x_label, y_label):
|
393 |
"""Generate a plot from data and return the file path."""
|
394 |
plt.figure(figsize=(10, 6))
|
@@ -606,24 +633,18 @@ async def stream_agent_response(question: str, chat_history: List[List[str]]) ->
|
|
606 |
db_connection, _ = setup_database_connection()
|
607 |
if db_connection:
|
608 |
query_result = execute_sql_query(sql_query2, db_connection)
|
609 |
-
#
|
610 |
-
|
611 |
-
|
612 |
-
|
613 |
-
|
614 |
-
|
615 |
-
|
616 |
-
|
617 |
-
pass
|
618 |
-
# As a fallback, don't rely on text table; just skip charting here
|
619 |
-
if data is not None and hasattr(data, "empty") and not data.empty:
|
620 |
-
# Heuristics: choose first column as x and second as y if numeric
|
621 |
-
x_col = data.columns[0]
|
622 |
-
# pick first numeric column different to x
|
623 |
y_col = None
|
624 |
-
for col in
|
625 |
try:
|
626 |
-
pd.to_numeric(
|
627 |
y_col = col
|
628 |
break
|
629 |
except Exception:
|
@@ -631,15 +652,15 @@ async def stream_agent_response(question: str, chat_history: List[List[str]]) ->
|
|
631 |
if y_col:
|
632 |
desired_type = 'pie' if any(k in q_lower for k in ["gráfico circular", "grafico circular", "pie", "pastel"]) else 'bar'
|
633 |
chart_fig = generate_chart(
|
634 |
-
data=
|
635 |
chart_type=desired_type,
|
636 |
x=x_col,
|
637 |
y=y_col,
|
638 |
title=f"{y_col} por {x_col}"
|
639 |
)
|
640 |
if chart_fig is not None:
|
641 |
-
logger.info("Chart generated from second-pass SQL execution.")
|
642 |
-
chart_state = {"data":
|
643 |
else:
|
644 |
logger.info("No DB connection on second pass; skipping.")
|
645 |
except Exception as e:
|
@@ -662,7 +683,8 @@ async def stream_agent_response(question: str, chat_history: List[List[str]]) ->
|
|
662 |
if not candidate_text and isinstance(response_text, str) and response_text.strip():
|
663 |
candidate_text = response_text
|
664 |
if candidate_text:
|
665 |
-
|
|
|
666 |
# Normalize lines: strip bullets and markdown symbols
|
667 |
norm_lines = []
|
668 |
for l in raw_lines:
|
|
|
389 |
except Exception as e:
|
390 |
return f"Error ejecutando la consulta: {str(e)}"
|
391 |
|
392 |
+
def parse_markdown_table(markdown_text: str) -> Optional[List[Dict[str, Any]]]:
|
393 |
+
"""Parse a GitHub-style markdown table string to a list of dicts.
|
394 |
+
|
395 |
+
Returns None if parsing fails or no rows are found.
|
396 |
+
"""
|
397 |
+
if not isinstance(markdown_text, str):
|
398 |
+
return None
|
399 |
+
try:
|
400 |
+
# Keep only table lines with pipes and drop the separator row
|
401 |
+
lines = [
|
402 |
+
line.strip() for line in markdown_text.split('\n')
|
403 |
+
if line.strip() and '|' in line and set(line.strip()) != {'-','|',':',' '}
|
404 |
+
]
|
405 |
+
if len(lines) < 2:
|
406 |
+
return None
|
407 |
+
# First line is header
|
408 |
+
columns = [col.strip() for col in lines[0].split('|')[1:-1]]
|
409 |
+
data: List[Dict[str, Any]] = []
|
410 |
+
for line in lines[1:]:
|
411 |
+
values = [val.strip() for val in line.split('|')[1:-1]]
|
412 |
+
if len(values) != len(columns):
|
413 |
+
continue
|
414 |
+
data.append(dict(zip(columns, values)))
|
415 |
+
return data if data else None
|
416 |
+
except Exception:
|
417 |
+
return None
|
418 |
+
|
419 |
def generate_plot(data, x_col, y_col, title, x_label, y_label):
|
420 |
"""Generate a plot from data and return the file path."""
|
421 |
plt.figure(figsize=(10, 6))
|
|
|
633 |
db_connection, _ = setup_database_connection()
|
634 |
if db_connection:
|
635 |
query_result = execute_sql_query(sql_query2, db_connection)
|
636 |
+
# Append query and result to response_text for transparency
|
637 |
+
response_text += f"\n\n### 🔍 Resultado de la consulta (2ª pasada):\n```sql\n{sql_query2}\n```\n\n{query_result}"
|
638 |
+
# Try robust markdown table parse
|
639 |
+
data_list = parse_markdown_table(query_result) if isinstance(query_result, str) else None
|
640 |
+
if data_list:
|
641 |
+
# Infer columns
|
642 |
+
columns = list(data_list[0].keys())
|
643 |
+
x_col = columns[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
644 |
y_col = None
|
645 |
+
for col in columns[1:]:
|
646 |
try:
|
647 |
+
pd.to_numeric(data_list[0][col])
|
648 |
y_col = col
|
649 |
break
|
650 |
except Exception:
|
|
|
652 |
if y_col:
|
653 |
desired_type = 'pie' if any(k in q_lower for k in ["gráfico circular", "grafico circular", "pie", "pastel"]) else 'bar'
|
654 |
chart_fig = generate_chart(
|
655 |
+
data=data_list,
|
656 |
chart_type=desired_type,
|
657 |
x=x_col,
|
658 |
y=y_col,
|
659 |
title=f"{y_col} por {x_col}"
|
660 |
)
|
661 |
if chart_fig is not None:
|
662 |
+
logger.info("Chart generated from second-pass SQL execution (markdown parse).")
|
663 |
+
chart_state = {"data": data_list, "x_col": x_col, "y_col": y_col, "title": f"{y_col} por {x_col}", "chart_type": desired_type}
|
664 |
else:
|
665 |
logger.info("No DB connection on second pass; skipping.")
|
666 |
except Exception as e:
|
|
|
683 |
if not candidate_text and isinstance(response_text, str) and response_text.strip():
|
684 |
candidate_text = response_text
|
685 |
if candidate_text:
|
686 |
+
# Support colon-number pairs even when they are in one paragraph
|
687 |
+
raw_lines = re.split(r"\n|,\s+(?=[^,]+:\s*\d)" , candidate_text)
|
688 |
# Normalize lines: strip bullets and markdown symbols
|
689 |
norm_lines = []
|
690 |
for l in raw_lines:
|