File size: 47,230 Bytes
f639c56 dc2d325 24f29f0 f639c56 1fe11cb f639c56 1fe11cb 241f37e f639c56 241f37e f639c56 713a6e6 683b6ad f639c56 d89427c 713a6e6 d89427c 713a6e6 d89427c 332a246 d89427c 332a246 713a6e6 7b28785 683b6ad 55ff70d 683b6ad d89427c 683b6ad d89427c 332a246 d89427c 332a246 d89427c fa13896 7b28785 d89427c 241f37e 332a246 683b6ad d89427c 332a246 6aebc39 332a246 d89427c 683b6ad 713a6e6 332a246 713a6e6 7b28785 713a6e6 683b6ad 713a6e6 332a246 713a6e6 7b28785 713a6e6 683b6ad 713a6e6 f639c56 713a6e6 f639c56 c22eca1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 |
import os
import sys
import re
import gradio as gr
import json
import tempfile
import base64
import io
from typing import List, Dict, Any, Optional, Tuple, Union
import logging
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
try:
from sqlalchemy import text as sa_text
except Exception:
sa_text = None
try:
# Intentar importar dependencias opcionales
from langchain_community.agent_toolkits import create_sql_agent
from langchain_community.agent_toolkits.sql.toolkit import SQLDatabaseToolkit
from langchain_community.utilities import SQLDatabase
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.agents.agent_types import AgentType
from langchain.memory import ConversationBufferWindowMemory
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
import pymysql
from dotenv import load_dotenv
DEPENDENCIES_AVAILABLE = True
except ImportError as e:
logger.warning(f"Some dependencies are not available: {e}")
DEPENDENCIES_AVAILABLE = False
# Configuración de logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Configure logging
logging.basicConfig(level=logging.INFO)
def generate_chart(data: Union[Dict, List[Dict], pd.DataFrame],
chart_type: str,
x: str,
y: str = None,
title: str = "",
x_label: str = None,
y_label: str = None):
"""
Generate an interactive Plotly figure from data.
Args:
data: The data to plot (can be a list of dicts or a pandas DataFrame)
chart_type: Type of chart to generate (bar, line, pie, scatter, histogram)
x: Column name for x-axis (names for pie)
y: Column name for y-axis (values for pie)
title: Chart title
x_label: Label for x-axis
y_label: Label for y-axis
Returns:
A Plotly Figure object (interactive) or None on error
"""
try:
# Convert data to DataFrame if it's a list of dicts
if isinstance(data, list):
df = pd.DataFrame(data)
elif isinstance(data, dict):
df = pd.DataFrame([data])
else:
df = data
if not isinstance(df, pd.DataFrame):
return None
# Generate the appropriate chart type
fig = None
if chart_type == 'bar':
fig = px.bar(df, x=x, y=y, title=title)
elif chart_type == 'line':
fig = px.line(df, x=x, y=y, title=title)
elif chart_type == 'pie':
fig = px.pie(df, names=x, values=y, title=title, hole=0)
elif chart_type == 'scatter':
fig = px.scatter(df, x=x, y=y, title=title)
elif chart_type == 'histogram':
fig = px.histogram(df, x=x, title=title)
else:
return None
# Update layout
fig.update_layout(
xaxis_title=x_label or x,
yaxis_title=y_label or (y if y != x else ''),
title=title or f"{chart_type.capitalize()} Chart of {x} vs {y}" if y else f"{chart_type.capitalize()} Chart of {x}",
template="plotly_white",
margin=dict(l=20, r=20, t=40, b=20),
height=400
)
return fig
except Exception as e:
error_msg = f"Error generating chart: {str(e)}"
logger.error(error_msg, exc_info=True)
return None
logger = logging.getLogger(__name__)
def check_environment():
"""Verifica si el entorno está configurado correctamente."""
if not DEPENDENCIES_AVAILABLE:
return False, "Missing required Python packages. Please install them with: pip install -r requirements.txt"
# Verificar si estamos en un entorno con variables de entorno
required_vars = ["DB_USER", "DB_PASSWORD", "DB_HOST", "DB_NAME", "GOOGLE_API_KEY"]
missing_vars = [var for var in required_vars if not os.getenv(var)]
if missing_vars:
return False, f"Missing required environment variables: {', '.join(missing_vars)}"
return True, "Environment is properly configured"
def setup_database_connection():
"""Intenta establecer una conexión a la base de datos."""
if not DEPENDENCIES_AVAILABLE:
return None, "Dependencies not available"
try:
load_dotenv(override=True)
# Debug: Log all environment variables (without sensitive values)
logger.info("Environment variables:")
for key, value in os.environ.items():
if any(s in key.lower() for s in ['pass', 'key', 'secret']):
logger.info(f" {key}: {'*' * 8} (hidden for security)")
else:
logger.info(f" {key}: {value}")
db_user = os.getenv("DB_USER")
db_password = os.getenv("DB_PASSWORD")
db_host = os.getenv("DB_HOST")
db_name = os.getenv("DB_NAME")
# Debug: Log database connection info (without password)
logger.info(f"Database connection attempt - Host: {db_host}, User: {db_user}, DB: {db_name}")
if not all([db_user, db_password, db_host, db_name]):
missing = [var for var, val in [
("DB_USER", db_user),
("DB_PASSWORD", "*" if db_password else ""),
("DB_HOST", db_host),
("DB_NAME", db_name)
] if not val]
logger.error(f"Missing required database configuration: {', '.join(missing)}")
return None, f"Missing database configuration: {', '.join(missing)}"
if not all([db_user, db_password, db_host, db_name]):
return None, "Missing database configuration"
logger.info(f"Connecting to database: {db_user}@{db_host}/{db_name}")
# Probar conexión
connection = pymysql.connect(
host=db_host,
user=db_user,
password=db_password,
database=db_name,
connect_timeout=5,
cursorclass=pymysql.cursors.DictCursor
)
connection.close()
# Si la conexión es exitosa, crear motor SQLAlchemy
db_uri = f"mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}"
logger.info("Database connection successful")
return SQLDatabase.from_uri(db_uri), ""
except Exception as e:
error_msg = f"Error connecting to database: {str(e)}"
logger.error(error_msg)
return None, error_msg
def initialize_llm():
"""Inicializa el modelo de lenguaje."""
if not DEPENDENCIES_AVAILABLE:
error_msg = "Dependencies not available. Make sure all required packages are installed."
logger.error(error_msg)
return None, error_msg
google_api_key = os.getenv("GOOGLE_API_KEY")
logger.info(f"GOOGLE_API_KEY found: {'Yes' if google_api_key else 'No'}")
if not google_api_key:
error_msg = "GOOGLE_API_KEY not found in environment variables. Please check your Hugging Face Space secrets."
logger.error(error_msg)
return None, error_msg
try:
logger.info("Initializing Google Generative AI...")
llm = ChatGoogleGenerativeAI(
model="gemini-2.0-flash",
temperature=0,
google_api_key=google_api_key,
convert_system_message_to_human=True # Convert system messages to human messages
)
# Test the model with a simple prompt
test_prompt = "Hello, this is a test."
logger.info(f"Testing model with prompt: {test_prompt}")
test_response = llm.invoke(test_prompt)
logger.info(f"Model test response: {str(test_response)[:100]}...") # Log first 100 chars
logger.info("Google Generative AI initialized successfully")
return llm, ""
except Exception as e:
error_msg = f"Error initializing Google Generative AI: {str(e)}"
logger.error(error_msg, exc_info=True) # Include full stack trace
return None, error_msg
def create_agent():
"""Crea el agente SQL si es posible."""
if not DEPENDENCIES_AVAILABLE:
error_msg = "Dependencies not available. Please check if all required packages are installed."
logger.error(error_msg)
return None, error_msg
logger.info("Starting agent creation process...")
def create_agent(llm, db_connection):
"""Create and return a SQL database agent with conversation memory."""
if not llm:
error_msg = "Cannot create agent: LLM is not available"
logger.error(error_msg)
return None, error_msg
if not db_connection:
error_msg = "Cannot create agent: Database connection is not available"
logger.error(error_msg)
return None, error_msg
try:
logger.info("Creating SQL agent with memory...")
# Create conversation memory
memory = ConversationBufferWindowMemory(
memory_key="chat_history",
k=5, # Keep last 5 message exchanges in memory
return_messages=True,
output_key="output"
)
# Create the database toolkit with additional configuration
toolkit = SQLDatabaseToolkit(
db=db_connection,
llm=llm
)
# Create the agent with memory and more detailed configuration
agent = create_sql_agent(
llm=llm,
toolkit=toolkit,
agent_type=AgentType.OPENAI_FUNCTIONS,
verbose=True,
handle_parsing_errors=True, # Better error handling for parsing
max_iterations=10, # Limit the number of iterations
early_stopping_method="generate", # Stop early if the agent is stuck
memory=memory, # Add memory to the agent
return_intermediate_steps=True # Important for memory to work properly
)
# Test the agent with a simple query
logger.info("Testing agent with a simple query...")
try:
test_query = "SELECT 1"
test_result = agent.run(test_query)
logger.info(f"Agent test query successful: {str(test_result)[:200]}...")
except Exception as e:
logger.warning(f"Agent test query failed (this might be expected): {str(e)}")
# Continue even if test fails, as it might be due to model limitations
logger.info("SQL agent created successfully")
return agent, ""
except Exception as e:
error_msg = f"Error creating SQL agent: {str(e)}"
logger.error(error_msg, exc_info=True)
return None, error_msg
# Inicializar el agente
logger.info("="*50)
logger.info("Starting application initialization...")
logger.info(f"Python version: {sys.version}")
logger.info(f"Current working directory: {os.getcwd()}")
logger.info(f"Files in working directory: {os.listdir()}")
# Verificar las variables de entorno
logger.info("Checking environment variables...")
for var in ["DB_USER", "DB_PASSWORD", "DB_HOST", "DB_NAME", "GOOGLE_API_KEY"]:
logger.info(f"{var}: {'*' * 8 if os.getenv(var) else 'NOT SET'}")
# Initialize components
logger.info("Initializing database connection...")
db_connection, db_error = setup_database_connection()
if db_error:
logger.error(f"Failed to initialize database: {db_error}")
logger.info("Initializing language model...")
llm, llm_error = initialize_llm()
if llm_error:
logger.error(f"Failed to initialize language model: {llm_error}")
logger.info("Initializing agent...")
agent, agent_error = create_agent(llm, db_connection)
db_connected = agent is not None
if agent:
logger.info("Agent initialized successfully")
else:
logger.error(f"Failed to initialize agent: {agent_error}")
logger.info("="*50)
def looks_like_sql(s: str) -> bool:
"""Heuristic to check if a string looks like an executable SQL statement."""
if not s:
return False
s_strip = s.strip().lstrip("-- ")
# common starters
return bool(re.match(r"^(WITH|SELECT|INSERT|UPDATE|DELETE|CREATE|ALTER|DROP|TRUNCATE)\b", s_strip, re.IGNORECASE))
def extract_sql_query(text):
"""Extrae consultas SQL del texto. Acepta solo bloques etiquetados como ```sql
o cadenas que claramente parezcan SQL. Evita ejecutar texto genérico.
"""
if not text:
return None
# Buscar TODOS los bloques en backticks y elegir los que sean 'sql'
for m in re.finditer(r"```(\w+)?\s*(.*?)```", text, re.DOTALL | re.IGNORECASE):
lang = (m.group(1) or '').lower()
body = (m.group(2) or '').strip()
if lang in {"sql", "postgresql", "mysql"} and looks_like_sql(body):
return body
# Si no hay bloques etiquetados, buscar una consulta SQL simple con palabras clave
simple = re.search(r"(WITH|SELECT|INSERT|UPDATE|DELETE|CREATE|ALTER|DROP|TRUNCATE)[\s\S]*?;", text, re.IGNORECASE)
if simple:
candidate = simple.group(0).strip()
if looks_like_sql(candidate):
return candidate
return None
def execute_sql_query(query, db_connection):
"""Ejecuta una consulta SQL y devuelve los resultados como una cadena."""
if not db_connection:
return "Error: No hay conexión a la base de datos"
try:
with db_connection._engine.connect() as connection:
# Ensure SQLAlchemy receives a SQL expression
if sa_text is not None and isinstance(query, str):
result = connection.execute(sa_text(query))
else:
result = connection.execute(query)
# Fetch data and column names
columns = list(result.keys()) if hasattr(result, "keys") else []
rows = result.fetchall()
# Convertir los resultados a un formato legible
if not rows:
return "La consulta no devolvió resultados"
# Si es un solo resultado, devolverlo directamente
try:
if len(rows) == 1 and len(rows[0]) == 1:
return str(rows[0][0])
except Exception:
pass
# Si hay múltiples filas, formatear como tabla Markdown
try:
import pandas as pd
# Convert SQLAlchemy Row objects to list of dicts using column names
if columns:
data = [
{col: val for col, val in zip(columns, tuple(row))}
for row in rows
]
df = pd.DataFrame(data)
else:
# Fallback: let pandas infer columns
df = pd.DataFrame(rows)
# Prefer Markdown output for downstream chart parsing
try:
return df.to_markdown(index=False)
except Exception:
# If optional dependency 'tabulate' is missing, build a simple Markdown table
headers = list(map(str, df.columns))
header_line = "| " + " | ".join(headers) + " |"
sep_line = "| " + " | ".join(["---"] * len(headers)) + " |"
body_lines = []
for _, r in df.iterrows():
body_lines.append("| " + " | ".join(map(lambda v: str(v), r.values)) + " |")
return "\n".join([header_line, sep_line, *body_lines])
except ImportError:
# Si pandas no está disponible, usar formato simple
return "\n".join([str(row) for row in rows])
except Exception as e:
return f"Error ejecutando la consulta: {str(e)}"
def detect_chart_preferences(question: str) -> Tuple[bool, str]:
"""Detect whether the user is asking for a chart and infer desired type.
Returns (wants_chart, chart_type) where chart_type is one of
{'bar', 'pie', 'line', 'scatter', 'histogram'}.
Defaults to 'bar' when ambiguous.
"""
try:
q = (question or "").lower()
# Broad triggers indicating any chart request
chart_triggers = [
"grafico", "gráfico", "grafica", "gráfica", "chart", "graph",
"visualizacion", "visualización", "plot", "plotly", "diagrama"
]
wants_chart = any(k in q for k in chart_triggers)
# Specific type hints
if any(k in q for k in ["pastel", "pie", "circular", "donut", "dona", "anillo"]):
return wants_chart or True, "pie"
if any(k in q for k in ["linea", "línea", "line", "tendencia"]):
return wants_chart or True, "line"
if any(k in q for k in ["dispersión", "dispersion", "scatter", "puntos"]):
return wants_chart or True, "scatter"
if any(k in q for k in ["histograma", "histogram"]):
return wants_chart or True, "histogram"
if any(k in q for k in ["barra", "barras", "columnas", "column"]):
return wants_chart or True, "bar"
# Default
return wants_chart, "bar"
except Exception:
return False, "bar"
def generate_plot(data, x_col, y_col, title, x_label, y_label):
"""Generate a plot from data and return the file path."""
plt.figure(figsize=(10, 6))
plt.bar(data[x_col], data[y_col])
plt.title(title)
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.xticks(rotation=45)
plt.tight_layout()
# Save to a temporary file
temp_dir = tempfile.mkdtemp()
plot_path = os.path.join(temp_dir, "plot.png")
plt.savefig(plot_path)
plt.close()
return plot_path
def convert_to_messages_format(chat_history):
"""Convert chat history to the format expected by Gradio 5.x"""
if not chat_history:
return []
messages = []
# If the first element is a list, assume it's in the old format
if isinstance(chat_history[0], list):
for msg in chat_history:
if isinstance(msg, list) and len(msg) == 2:
# Format: [user_msg, bot_msg]
user_msg, bot_msg = msg
if user_msg:
messages.append({"role": "user", "content": user_msg})
if bot_msg:
messages.append({"role": "assistant", "content": bot_msg})
else:
# Assume it's already in the correct format or can be used as is
for msg in chat_history:
if isinstance(msg, dict) and "role" in msg and "content" in msg:
messages.append(msg)
elif isinstance(msg, str):
# If it's a string, assume it's a user message
messages.append({"role": "user", "content": msg})
return messages
async def stream_agent_response(question: str, chat_history: List[List[str]]) -> Tuple[str, Optional["go.Figure"]]:
"""Procesa la pregunta del usuario y devuelve la respuesta del agente con memoria de conversación."""
global agent # Make sure we can modify the agent's memory
# Initialize response
response_text = ""
chart_fig = None
messages = []
# Add previous chat history in the correct format for the agent
for msg_pair in chat_history:
if len(msg_pair) >= 1 and msg_pair[0]: # User message
messages.append(HumanMessage(content=msg_pair[0]))
if len(msg_pair) >= 2 and msg_pair[1]: # Assistant message
messages.append(AIMessage(content=msg_pair[1]))
# Add current user's question
user_message = HumanMessage(content=question)
messages.append(user_message)
if not agent:
error_msg = (
"## ⚠️ Error: Agente no inicializado\n\n"
"No se pudo inicializar el agente de base de datos. Por favor, verifica que:\n"
"1. Todas las variables de entorno estén configuradas correctamente\n"
"2. La base de datos esté accesible\n"
f"3. El modelo de lenguaje esté disponible\n\n"
f"Error: {agent_error}"
)
return error_msg, None
# Update the agent's memory with the full conversation history
try:
# Rebuild agent memory from chat history pairs
if hasattr(agent, 'memory') and agent.memory is not None:
agent.memory.clear()
for i in range(0, len(messages)-1, 2): # (user, assistant)
if i+1 < len(messages):
agent.memory.save_context(
{"input": messages[i].content},
{"output": messages[i+1].content}
)
except Exception as e:
logger.error(f"Error updating agent memory: {str(e)}", exc_info=True)
try:
# Add empty assistant message that will be updated
assistant_message = {"role": "assistant", "content": ""}
messages.append(assistant_message)
# Execute the agent with proper error handling
try:
# Let the agent use its memory; don't pass raw chat_history
response = await agent.ainvoke({"input": question})
logger.info(f"Agent response type: {type(response)}")
logger.info(f"Agent response content: {str(response)[:500]}...")
# Handle different response formats
if hasattr(response, 'output') and response.output:
response_text = response.output
elif isinstance(response, str):
response_text = response
elif hasattr(response, 'get') and callable(response.get) and 'output' in response:
response_text = response['output']
else:
response_text = str(response)
# logger.info(f"Extracted response text: {response_text[:200]}...")
# # Check if the response contains an SQL query and it truly looks like SQL
# sql_query = extract_sql_query(response_text)
# if sql_query and looks_like_sql(sql_query):
# logger.info(f"Detected SQL query: {sql_query}")
# # Execute the query and update the response
# db_connection, _ = setup_database_connection()
# if db_connection:
# query_result = execute_sql_query(sql_query, db_connection)
# # Add the query and its result to the response
# response_text += f"\n\n### 🔍 Resultado de la consulta:\n```sql\n{sql_query}\n```\n\n{query_result}"
# # Try to generate an interactive chart if the result is tabular
# try:
# if isinstance(query_result, str) and '|' in query_result and '---' in query_result:
# # Convert markdown table to DataFrame
# # Clean up the markdown table
# lines = [line.strip() for line in query_result.split('\n')
# if line.strip() and '---' not in line and '|' in line]
# if len(lines) > 1: # At least header + 1 data row
# # Get column names from the first line
# columns = [col.strip() for col in lines[0].split('|')[1:-1]]
# # Get data rows
# data = []
# for line in lines[1:]:
# values = [val.strip() for val in line.split('|')[1:-1]]
# if len(values) == len(columns):
# data.append(dict(zip(columns, values)))
# if data and len(columns) >= 2:
# # Determine chart type from user's question
# _, desired_type = detect_chart_preferences(question)
# # Choose x/y columns (assume first is category, second numeric)
# x_col = columns[0]
# y_col = columns[1]
# # Coerce numeric values for y
# for row in data:
# try:
# row[y_col] = float(re.sub(r"[^0-9.\-]", "", str(row[y_col])))
# except Exception:
# pass
# chart_fig = generate_chart(
# data=data,
# chart_type=desired_type,
# x=x_col,
# y=y_col,
# title=f"{y_col} por {x_col}"
# )
# if chart_fig is not None:
# logger.info(f"Chart generated from SQL table: type={desired_type}, x={x_col}, y={y_col}, rows={len(data)}")
# except Exception as e:
# logger.error(f"Error generating chart: {str(e)}", exc_info=True)
# # Don't fail the whole request if chart generation fails
# response_text += "\n\n⚠️ No se pudo generar la visualización de los datos."
# else:
# response_text += "\n\n⚠️ No se pudo conectar a la base de datos para ejecutar la consulta."
# elif sql_query and not looks_like_sql(sql_query):
# logger.info("Detected code block but it does not look like SQL; skipping execution.")
# If we still have no chart but the user clearly wants one,
# try a second pass to get ONLY a SQL query from the agent and execute it.
if chart_fig is None:
wants_chart, default_type = detect_chart_preferences(question)
if wants_chart:
try:
logger.info("Second pass: asking agent for ONLY SQL query in fenced block.")
sql_only_prompt = (
"Devuelve SOLO la consulta SQL en un bloque ```sql``` para responder a: "
f"{question}. No incluyas explicación ni texto adicional."
)
sql_only_resp = await agent.ainvoke({"input": sql_only_prompt})
sql_only_text = str(sql_only_resp)
sql_query2 = extract_sql_query(sql_only_text)
if sql_query2 and looks_like_sql(sql_query2):
logger.info(f"Second pass SQL detected: {sql_query2}")
db_connection, _ = setup_database_connection()
if db_connection:
query_result = execute_sql_query(sql_query2, db_connection)
# Try to parse table-like text into DataFrame if possible
data = None
if isinstance(query_result, str):
try:
import pandas as pd
df = pd.read_csv(io.StringIO(query_result), sep="|")
data = df
except Exception:
pass
# As a fallback, don't rely on text table; just skip charting here
if data is not None and hasattr(data, "empty") and not data.empty:
# Heuristics: choose first column as x and second as y if numeric
x_col = data.columns[0]
# pick first numeric column different to x
y_col = None
for col in data.columns[1:]:
try:
pd.to_numeric(data[col])
y_col = col
break
except Exception:
continue
if y_col:
desired_type = default_type
chart_fig = generate_chart(
data=data,
chart_type=desired_type,
x=x_col,
y=y_col,
title=f"{y_col} por {x_col}"
)
if chart_fig is not None:
logger.info("Chart generated from second-pass SQL execution.")
else:
logger.info("No DB connection on second pass; skipping.")
except Exception as e:
logger.error(f"Second-pass SQL synthesis failed: {e}")
# Fallback: if user asked for a chart and we didn't get SQL or chart yet,
# parse the most recent assistant text for lines like "LABEL: NUMBER" (bulleted or plain).
if chart_fig is None:
wants_chart, desired_type = detect_chart_preferences(question)
if wants_chart:
# Find the most recent assistant message with usable numeric pairs
candidate_text = ""
if chat_history:
for pair in reversed(chat_history):
if len(pair) >= 2 and isinstance(pair[1], str) and pair[1].strip():
candidate_text = pair[1]
break
# Also consider current response_text as a data source
if not candidate_text and isinstance(response_text, str) and response_text.strip():
candidate_text = response_text
if candidate_text:
raw_lines = candidate_text.split('\n')
# Normalize lines: strip bullets and markdown symbols
norm_lines = []
for l in raw_lines:
s = l.strip()
if not s:
continue
s = s.lstrip("•*-\t ")
# Remove surrounding markdown emphasis from labels later
norm_lines.append(s)
data = []
for l in norm_lines:
# Accept patterns like "**LABEL**: 123" or "LABEL: 1,234"
m = re.match(r"^(.+?):\s*([0-9][0-9.,]*)$", l)
if m:
label = m.group(1).strip()
# Strip common markdown emphasis
label = re.sub(r"[*_`]+", "", label).strip()
try:
val = float(m.group(2).replace(',', ''))
except Exception:
continue
data.append({"label": label, "value": val})
logger.info(f"Fallback parse from text: extracted {len(data)} items for potential chart")
if len(data) >= 2:
chart_fig = generate_chart(
data=data,
chart_type=desired_type,
x="label",
y="value",
title="Distribución"
)
if chart_fig is not None:
logger.info(f"Chart generated from text fallback: type={desired_type}, items={len(data)}")
# Update the assistant's message with the response
assistant_message["content"] = response_text
except Exception as e:
error_msg = f"Error al ejecutar el agente: {str(e)}"
logger.error(error_msg, exc_info=True)
assistant_message["content"] = f"## ❌ Error\n\n{error_msg}"
# Return the message in the correct format for Gradio Chatbot
# Format: list of tuples where each tuple is (user_msg, bot_msg)
# For a single response, we return [(None, message)]
message_content = ""
if isinstance(assistant_message, dict) and "content" in assistant_message:
message_content = assistant_message["content"]
elif isinstance(assistant_message, str):
message_content = assistant_message
else:
message_content = str(assistant_message)
# Return the assistant's response and an optional interactive chart figure
if chart_fig is None:
logger.info("No chart generated for this turn.")
else:
logger.info("Returning a chart figure to UI.")
return message_content, chart_fig
except Exception as e:
error_msg = f"## ❌ Error\n\nOcurrió un error al procesar tu solicitud:\n\n```\n{str(e)}\n```"
logger.error(f"Error in stream_agent_response: {str(e)}", exc_info=True)
# Return error message and no chart
return error_msg, None
# Custom CSS for the app
custom_css = """
.gradio-container {
max-width: 1200px !important;
margin: 0 auto !important;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
}
#chatbot {
min-height: 500px;
border: 1px solid #e0e0e0;
border-radius: 8px;
margin-bottom: 20px;
padding: 20px;
background-color: #f9f9f9;
}
.user-message, .bot-message {
padding: 12px 16px;
border-radius: 18px;
margin: 8px 0;
max-width: 80%;
line-height: 1.5;
}
.user-message {
background-color: #007bff;
color: white;
margin-left: auto;
border-bottom-right-radius: 4px;
}
.bot-message {
background-color: #f1f1f1;
color: #333;
margin-right: auto;
border-bottom-left-radius: 4px;
}
#question-input textarea {
min-height: 50px !important;
border-radius: 8px !important;
padding: 12px !important;
font-size: 16px !important;
}
#send-button {
height: 100%;
background-color: #007bff !important;
color: white !important;
border: none !important;
border-radius: 8px !important;
font-weight: 500 !important;
transition: background-color 0.2s !important;
}
#send-button:hover {
background-color: #0056b3 !important;
}
.status-message {
text-align: center;
color: #666;
font-style: italic;
margin: 10px 0;
}
"""
def create_ui():
"""Crea y devuelve los componentes de la interfaz de usuario de Gradio."""
# Verificar el estado del entorno
env_ok, env_message = check_environment()
# Crear el tema personalizado
theme = gr.themes.Soft(
primary_hue="blue",
secondary_hue="indigo",
neutral_hue="slate"
)
with gr.Blocks(
css=custom_css,
title="Asistente de Base de Datos SQL",
theme=theme
) as demo:
# Encabezado
gr.Markdown("""
# 🤖 Asistente de Base de Datos SQL
Haz preguntas en lenguaje natural sobre tu base de datos y obtén resultados de consultas SQL.
""")
# Mensaje de estado
if not env_ok:
gr.Warning("⚠️ " + env_message)
# Create the chat interface
with gr.Row():
chatbot = gr.Chatbot(
value=[],
elem_id="chatbot",
type="messages", # migrate to messages format to avoid deprecation
avatar_images=(
None,
(os.path.join(os.path.dirname(__file__), "logo.svg")),
),
height=600,
render_markdown=True, # Enable markdown rendering
show_label=False,
show_share_button=False,
container=True,
layout="panel" # Better layout for messages
)
# Chart display area (interactive Plotly figure)
# In Gradio 5, gr.Plot accepts a plotly.graph_objects.Figure
chart_display = gr.Plot(
label="📊 Visualización",
)
# Input area
with gr.Row():
question_input = gr.Textbox(
label="",
placeholder="Escribe tu pregunta aquí...",
container=False,
scale=5,
min_width=300,
max_lines=3,
autofocus=True,
elem_id="question-input"
)
submit_button = gr.Button(
"Enviar",
variant="primary",
min_width=100,
scale=1,
elem_id="send-button"
)
# System status
with gr.Accordion("ℹ️ Estado del sistema", open=not env_ok):
if not DEPENDENCIES_AVAILABLE:
gr.Markdown("""
## ❌ Dependencias faltantes
Para ejecutar esta aplicación localmente, necesitas instalar las dependencias:
```bash
pip install -r requirements.txt
```
""")
else:
if not agent:
gr.Markdown(f"""
## ⚠️ Configuración incompleta
No se pudo inicializar el agente de base de datos. Por favor, verifica que:
1. Todas las variables de entorno estén configuradas correctamente
2. La base de datos esté accesible
3. La API de Google Gemini esté configurada
**Error:** {agent_error if agent_error else 'No se pudo determinar el error'}
### Configuración local
Crea un archivo `.env` en la raíz del proyecto con las siguientes variables:
```
DB_USER=tu_usuario
DB_PASSWORD=tu_contraseña
DB_HOST=tu_servidor
DB_NAME=tu_base_de_datos
GOOGLE_API_KEY=tu_api_key_de_google
```
""")
else:
if os.getenv('SPACE_ID'):
# Modo demo en Hugging Face Spaces
gr.Markdown("""
## 🚀 Modo Demo
Esta es una demostración del asistente de base de datos SQL. Para usar la versión completa con conexión a base de datos:
1. Clona este espacio en tu cuenta de Hugging Face
2. Configura las variables de entorno en la configuración del espacio:
- `DB_USER`: Tu usuario de base de datos
- `DB_PASSWORD`: Tu contraseña de base de datos
- `DB_HOST`: La dirección del servidor de base de datos
- `DB_NAME`: El nombre de la base de datos
- `GOOGLE_API_KEY`: Tu clave de API de Google Gemini
**Nota:** Actualmente estás en modo de solo demostración.
""")
else:
gr.Markdown("""
## ✅ Sistema listo
El asistente está listo para responder tus preguntas sobre la base de datos.
""")
# Hidden component for streaming output
streaming_output_display = gr.Textbox(visible=False)
return demo, chatbot, chart_display, question_input, submit_button, streaming_output_display
def create_application():
"""Create and configure the Gradio application."""
# Create the UI components
demo, chatbot, chart_display, question_input, submit_button, streaming_output_display = create_ui()
def user_message(user_input: str, chat_history: List[Dict[str, str]]) -> Tuple[str, List[Dict[str, str]]]:
"""Add user message to chat history (messages format) and clear input."""
if not user_input.strip():
return "", chat_history
logger.info(f"User message: {user_input}")
if chat_history is None:
chat_history = []
# Append user message in messages format
chat_history.append({"role": "user", "content": user_input})
return "", chat_history
async def bot_response(chat_history: List[Dict[str, str]]) -> Tuple[List[Dict[str, str]], Optional[go.Figure]]:
"""Generate bot response for messages-format chat history and return optional chart figure."""
if not chat_history:
return chat_history, None
# Ensure last message is a user turn awaiting assistant reply
last = chat_history[-1]
if not isinstance(last, dict) or last.get("role") != "user" or not last.get("content"):
return chat_history, None
try:
question = last["content"]
logger.info(f"Processing question: {question}")
# Convert prior messages to pair history for stream_agent_response()
pair_history: List[List[str]] = []
i = 0
while i < len(chat_history) - 1:
m1 = chat_history[i]
m2 = chat_history[i + 1] if i + 1 < len(chat_history) else None
if (
isinstance(m1, dict)
and m1.get("role") == "user"
and isinstance(m2, dict)
and m2.get("role") == "assistant"
):
pair_history.append([m1.get("content", ""), m2.get("content", "")])
i += 2
else:
i += 1
# Call the agent for this new user question
assistant_message, chart_fig = await stream_agent_response(question, pair_history)
# Append assistant message back into messages history
chat_history.append({"role": "assistant", "content": assistant_message})
# If user asked for a chart but none was produced, try to build one
# from the latest assistant text using the same fallback logic.
if chart_fig is None:
wants_chart, desired_type = detect_chart_preferences(question)
if wants_chart and isinstance(assistant_message, str):
candidate_text = assistant_message
raw_lines = candidate_text.split('\n')
norm_lines = []
for l in raw_lines:
s = l.strip().lstrip("•*\t -")
if s:
norm_lines.append(s)
data = []
for l in norm_lines:
m = re.match(r"^(.+?):\s*([0-9][0-9.,]*)$", l)
if m:
label = re.sub(r"[*_`]+", "", m.group(1)).strip()
try:
val = float(m.group(2).replace(',', ''))
except Exception:
continue
data.append({"label": label, "value": val})
if len(data) >= 2:
chart_fig = generate_chart(
data=data,
chart_type=desired_type,
x="label",
y="value",
title="Distribución"
)
logger.info("Response generation complete")
return chat_history, chart_fig
except Exception as e:
error_msg = f"## ❌ Error\n\nError al procesar la solicitud:\n\n```\n{str(e)}\n```"
logger.error(error_msg, exc_info=True)
# Ensure we add an assistant error message for the UI
chat_history.append({"role": "assistant", "content": error_msg})
return chat_history, None
# Event handlers
with demo:
# Handle form submission
msg_submit = question_input.submit(
fn=user_message,
inputs=[question_input, chatbot],
outputs=[question_input, chatbot],
queue=True
).then(
fn=bot_response,
inputs=[chatbot],
outputs=[chatbot, chart_display],
api_name="ask"
)
# Handle button click
btn_click = submit_button.click(
fn=user_message,
inputs=[question_input, chatbot],
outputs=[question_input, chatbot],
queue=True
).then(
fn=bot_response,
inputs=[chatbot],
outputs=[chatbot, chart_display]
)
return demo
# Create the application
demo = create_application()
# Configuración para Hugging Face Spaces
def get_app():
"""Obtiene la instancia de la aplicación Gradio para Hugging Face Spaces."""
# Verificar si estamos en un entorno de Hugging Face Spaces
if os.getenv('SPACE_ID'):
# Configuración específica para Spaces
demo.title = "🤖 Asistente de Base de Datos SQL (Demo)"
demo.description = """
Este es un demo del asistente de base de datos SQL.
Para usar la versión completa con conexión a base de datos, clona este espacio y configura las variables de entorno.
"""
return demo
# Para desarrollo local
if __name__ == "__main__":
# Configuración para desarrollo local - versión simplificada para Gradio 5.x
demo.launch(
server_name="0.0.0.0",
server_port=7860,
debug=True,
share=False
)
|