Jeremy Live commited on
Commit
0444129
1 Parent(s): 5c04851
Files changed (1) hide show
  1. app.py +106 -75
app.py CHANGED
@@ -432,8 +432,21 @@ def execute_sql_query(query, db_connection):
432
  lines.append("| " + " | ".join(values) + " |")
433
  return "\n".join(lines)
434
  except Exception:
435
- # Formato simple si algo falla
436
- return "\n".join([str(row) for row in rows])
 
 
 
 
 
 
 
 
 
 
 
 
 
437
 
438
  except Exception as e:
439
  return f"Error ejecutando la consulta: {str(e)}"
@@ -465,6 +478,55 @@ def parse_markdown_table(markdown_text: str) -> Optional[List[Dict[str, Any]]]:
465
  except Exception:
466
  return None
467
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
468
  def generate_plot(data, x_col, y_col, title, x_label, y_label):
469
  """Generate a plot from data and return the file path."""
470
  plt.figure(figsize=(10, 6))
@@ -588,79 +650,48 @@ async def stream_agent_response(question: str, chat_history: List[List[str]]) ->
588
 
589
  logger.info(f"Extracted response text: {response_text[:200]}...")
590
 
591
- # Check if the response contains an SQL query and it truly looks like SQL
592
- sql_query = extract_sql_query(response_text)
593
- if sql_query and looks_like_sql(sql_query):
594
- logger.info(f"Detected SQL query: {sql_query}")
595
- db_connection, _ = setup_database_connection()
596
- if db_connection:
597
- query_result = execute_sql_query(sanitize_sql_query_text(sql_query), db_connection)
598
-
599
- # Add the query and its result to the response
600
- response_text += f"\n\n### 馃攳 Resultado de la consulta:\n```sql\n{sql_query}\n```\n\n{query_result}"
601
-
602
- # Try to generate an interactive chart if the result is tabular
603
- try:
604
- if isinstance(query_result, str) and '|' in query_result and '---' in query_result:
605
- # Convert markdown table to DataFrame
606
-
607
- # Clean up the markdown table
608
- lines = [line.strip() for line in query_result.split('\n')
609
- if line.strip() and '---' not in line and '|' in line]
610
- if len(lines) > 1: # At least header + 1 data row
611
- # Get column names from the first line
612
- columns = [col.strip() for col in lines[0].split('|')[1:-1]]
613
- # Get data rows
614
- data = []
615
- for line in lines[1:]:
616
- values = [val.strip() for val in line.split('|')[1:-1]]
617
- if len(values) == len(columns):
618
- data.append(dict(zip(columns, values)))
619
-
620
- if data and len(columns) >= 2:
621
- # Determine chart type from user's question (supports pie chart)
622
- q_lower = question.lower()
623
- if any(k in q_lower for k in ["gr谩fico circular", "grafico circular", "pie", "pastel"]):
624
- desired_type = 'pie'
625
- elif any(k in q_lower for k in ["l铆nea", "linea", "line"]):
626
- desired_type = 'line'
627
- elif any(k in q_lower for k in ["dispersi贸n", "dispersion", "scatter"]):
628
- desired_type = 'scatter'
629
- elif any(k in q_lower for k in ["histograma", "histogram"]):
630
- desired_type = 'histogram'
631
- else:
632
- desired_type = 'bar'
633
-
634
- # Choose x/y columns (assume first is category, second numeric)
635
- x_col = columns[0]
636
- # pick first numeric column different to x
637
- y_col = None
638
- for col in columns[1:]:
639
- try:
640
- pd.to_numeric(data[0][col])
641
- y_col = col
642
- break
643
- except Exception:
644
- continue
645
- if y_col:
646
- chart_fig = generate_chart(
647
- data=data,
648
- chart_type=desired_type,
649
- x=x_col,
650
- y=y_col,
651
- title=f"{y_col} por {x_col}"
652
- )
653
- if chart_fig is not None:
654
- logger.info(f"Chart generated from SQL table: type={desired_type}, x={x_col}, y={y_col}, rows={len(data)}")
655
- chart_state = {"data": data, "x_col": x_col, "y_col": y_col, "title": f"{y_col} por {x_col}", "chart_type": desired_type}
656
- except Exception as e:
657
- logger.error(f"Error generating chart: {str(e)}", exc_info=True)
658
- # Don't fail the whole request if chart generation fails
659
- response_text += "\n\n鈿狅笍 No se pudo generar la visualizaci贸n de los datos."
660
- else:
661
- response_text += "\n\n鈿狅笍 No se pudo conectar a la base de datos para ejecutar la consulta."
662
- elif sql_query and not looks_like_sql(sql_query):
663
- logger.info("Detected code block but it does not look like SQL; skipping execution.")
664
 
665
  # If we still have no chart but the user clearly wants one,
666
  # try a second pass to get ONLY a SQL query from the agent and execute it.
 
432
  lines.append("| " + " | ".join(values) + " |")
433
  return "\n".join(lines)
434
  except Exception:
435
+ # Formato markdown b谩sico incluso si no hay metadatos de columnas
436
+ try:
437
+ num_cols = len(rows[0]) if rows and hasattr(rows[0], "__len__") else 0
438
+ headers = [f"col{i+1}" for i in range(num_cols)] or ["value"]
439
+ lines = []
440
+ header_line = "| " + " | ".join(headers) + " |"
441
+ sep_line = "|" + "|".join([" --- " for _ in headers]) + "|"
442
+ lines.append(header_line)
443
+ lines.append(sep_line)
444
+ for r in rows:
445
+ vals = [str(v) for v in (list(r) if hasattr(r, "__iter__") and not isinstance(r, (str, bytes)) else [r])]
446
+ lines.append("| " + " | ".join(vals) + " |")
447
+ return "\n".join(lines)
448
+ except Exception:
449
+ return "\n".join([str(row) for row in rows])
450
 
451
  except Exception as e:
452
  return f"Error ejecutando la consulta: {str(e)}"
 
478
  except Exception:
479
  return None
480
 
481
+ def parse_label_value_pairs(text: str) -> List[Dict[str, Any]]:
482
+ """Extract generic label-number pairs from free text.
483
+
484
+ Supports formats like:
485
+ - LABEL: 123
486
+ - LABEL (123)
487
+ - ('LABEL', 123)
488
+ Returns a list of {label, value}.
489
+ """
490
+ if not isinstance(text, str) or not text.strip():
491
+ return []
492
+ pairs: List[Dict[str, Any]] = []
493
+ lines = [l.strip() for l in text.split('\n') if l.strip()]
494
+ # 1) Tuple-like: ('LABEL', 123)
495
+ tuple_re = re.compile(r"\(\s*['\"]?\s*([^'\",()]+?)\s*['\"]?\s*,\s*([0-9][0-9.,]*)\s*\)")
496
+ # 2) Colon separated: LABEL: 123
497
+ colon_re = re.compile(r"^(.+?):\s*([0-9][0-9.,]*)$")
498
+ # 3) Parenthesis after label: LABEL (123)
499
+ paren_re = re.compile(r"^(.+?)\s*\(\s*([0-9][0-9.,]*)\s*\)$")
500
+ for l in lines:
501
+ m = tuple_re.search(l)
502
+ if m:
503
+ label = re.sub(r"[*_`]+", "", m.group(1)).strip()
504
+ try:
505
+ value = float(m.group(2).replace(',', ''))
506
+ pairs.append({"label": label, "value": value})
507
+ continue
508
+ except Exception:
509
+ pass
510
+ m = colon_re.match(l)
511
+ if m:
512
+ label = re.sub(r"[*_`]+", "", m.group(1)).strip()
513
+ try:
514
+ value = float(m.group(2).replace(',', ''))
515
+ pairs.append({"label": label, "value": value})
516
+ continue
517
+ except Exception:
518
+ pass
519
+ m = paren_re.match(l)
520
+ if m:
521
+ label = re.sub(r"[*_`]+", "", m.group(1)).strip()
522
+ try:
523
+ value = float(m.group(2).replace(',', ''))
524
+ pairs.append({"label": label, "value": value})
525
+ continue
526
+ except Exception:
527
+ pass
528
+ return pairs
529
+
530
  def generate_plot(data, x_col, y_col, title, x_label, y_label):
531
  """Generate a plot from data and return the file path."""
532
  plt.figure(figsize=(10, 6))
 
650
 
651
  logger.info(f"Extracted response text: {response_text[:200]}...")
652
 
653
+ # 1) Try to parse label/number pairs from the agent's answer directly
654
+ parsed_pairs = parse_label_value_pairs(response_text)
655
+ q_lower = question.lower()
656
+ desired_type = 'pie' if any(k in q_lower for k in ["gr谩fico circular", "grafico circular", "pie", "pastel"]) else 'bar'
657
+ if chart_fig is None and len(parsed_pairs) >= 2:
658
+ chart_fig = generate_chart(parsed_pairs, desired_type, 'label', 'value', 'Distribuci贸n')
659
+ if chart_fig is not None:
660
+ chart_state = {"data": parsed_pairs, "x_col": "label", "y_col": "value", "title": "Distribuci贸n", "chart_type": desired_type}
661
+
662
+ # 2) If not, execute any SQL present in the response once and chart it
663
+ if chart_fig is None:
664
+ sql_query = extract_sql_query(response_text)
665
+ if sql_query and looks_like_sql(sql_query):
666
+ logger.info(f"Detected SQL query: {sql_query}")
667
+ db_connection, _ = setup_database_connection()
668
+ if db_connection:
669
+ query_result = execute_sql_query(sanitize_sql_query_text(sql_query), db_connection)
670
+ response_text += f"\n\n### 馃攳 Resultado de la consulta:\n```sql\n{sql_query}\n```\n\n{query_result}"
671
+ data_list = parse_markdown_table(query_result) if isinstance(query_result, str) else None
672
+ if data_list:
673
+ columns = list(data_list[0].keys())
674
+ x_col = columns[0]
675
+ y_col = None
676
+ for col in columns[1:]:
677
+ try:
678
+ pd.to_numeric(data_list[0][col])
679
+ y_col = col
680
+ break
681
+ except Exception:
682
+ continue
683
+ if y_col:
684
+ chart_fig = generate_chart(data_list, desired_type, x_col, y_col, f"{y_col} por {x_col}")
685
+ if chart_fig is not None:
686
+ chart_state = {"data": data_list, "x_col": x_col, "y_col": y_col, "title": f"{y_col} por {x_col}", "chart_type": desired_type}
687
+ else:
688
+ tuples = parse_label_value_pairs(str(query_result))
689
+ if len(tuples) >= 2:
690
+ chart_fig = generate_chart(tuples, desired_type, 'label', 'value', 'Distribuci贸n')
691
+ if chart_fig is not None:
692
+ chart_state = {"data": tuples, "x_col": "label", "y_col": "value", "title": "Distribuci贸n", "chart_type": desired_type}
693
+ else:
694
+ response_text += "\n\n鈿狅笍 No se pudo conectar a la base de datos para ejecutar la consulta."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
695
 
696
  # If we still have no chart but the user clearly wants one,
697
  # try a second pass to get ONLY a SQL query from the agent and execute it.