Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -258,69 +258,6 @@ async def search_tavily(
|
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
-
##### MCP AGENT #####
|
| 262 |
-
sse_url = "https://pharma-ia-gradio-mcp-server.hf.space/gradio_api/mcp/sse"
|
| 263 |
-
|
| 264 |
-
# Crear instancia del agente (con llm definido)
|
| 265 |
-
mcp_agent = MCPAgent(sse_url, HF_TOKEN, llm)
|
| 266 |
-
|
| 267 |
-
# Historial de chat
|
| 268 |
-
chat_history = []
|
| 269 |
-
|
| 270 |
-
def format_tool_call(tool_name, arguments):
|
| 271 |
-
message = f"🔧 **Llamando a herramienta**: `{tool_name}`\n```json\n{arguments}\n```"
|
| 272 |
-
print(message) # Solo imprime en consola
|
| 273 |
-
return "" # Devuelve cadena vacía para no mostrar en chat
|
| 274 |
-
|
| 275 |
-
def format_tool_result(tool_name, result):
|
| 276 |
-
message = f"✅ **Resultado de `{tool_name}`**\n```\n{result}\n```"
|
| 277 |
-
print(message) # Solo imprime en consola
|
| 278 |
-
return "" # Devuelve cadena vacía para no mostrar en chat
|
| 279 |
-
|
| 280 |
-
async def chat_with_agent(message, history):
|
| 281 |
-
history = history or []
|
| 282 |
-
|
| 283 |
-
# Agregar mensaje del usuario y marcador de "escribiendo"
|
| 284 |
-
history.append((message, None)) # None activa el indicador de typing
|
| 285 |
-
yield history
|
| 286 |
-
|
| 287 |
-
# Variables para seguimiento
|
| 288 |
-
processing_message = None
|
| 289 |
-
|
| 290 |
-
try:
|
| 291 |
-
# Procesar con el agente
|
| 292 |
-
full_response = ""
|
| 293 |
-
async for event in mcp_agent.stream_response(message):
|
| 294 |
-
if event["type"] == "tool_call":
|
| 295 |
-
format_tool_call(event["tool_name"], event["arguments"])
|
| 296 |
-
|
| 297 |
-
elif event["type"] == "tool_result":
|
| 298 |
-
format_tool_result(event["tool_name"], event["result"])
|
| 299 |
-
|
| 300 |
-
elif event["type"] == "final_response":
|
| 301 |
-
full_response = event["content"]
|
| 302 |
-
|
| 303 |
-
# Actualizar indicador de actividad periódicamente
|
| 304 |
-
if not processing_message or len(history[-1][1] or "") > len(processing_message):
|
| 305 |
-
processing_message = "✍️ Generando respuesta..."
|
| 306 |
-
history[-1] = (history[-1][0], processing_message)
|
| 307 |
-
yield history
|
| 308 |
-
|
| 309 |
-
# Mostrar respuesta final
|
| 310 |
-
if full_response:
|
| 311 |
-
history[-1] = (history[-1][0], full_response)
|
| 312 |
-
yield history
|
| 313 |
-
|
| 314 |
-
except Exception as e:
|
| 315 |
-
history[-1] = (history[-1][0], f"⚠️ Error: {str(e)}")
|
| 316 |
-
yield history
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
# Configuración de la interfaz Gradio
|
| 324 |
with gr.Blocks(title="Herramientas MCP", theme=gr.themes.Base()) as tools_tab:
|
| 325 |
with gr.Accordion("Búsqueda Académica", open=False):
|
| 326 |
arxiv_interface = gr.Interface(
|
|
@@ -359,55 +296,11 @@ with gr.Blocks(title="Herramientas MCP", theme=gr.themes.Base()) as tools_tab:
|
|
| 359 |
description="Realiza búsquedas en web usando la API de Tavily.",
|
| 360 |
api_name="_search_tavily"
|
| 361 |
)
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
# Creamos el Agente MCP
|
| 366 |
-
with gr.Blocks(title="Agente MCP", theme=gr.themes.Base()) as agent_tab:
|
| 367 |
-
# Chat principal
|
| 368 |
-
chatbot = gr.Chatbot(
|
| 369 |
-
label="Chat con el Agente MCP",
|
| 370 |
-
height=600,
|
| 371 |
-
bubble_full_width=True,
|
| 372 |
-
render_markdown=True,
|
| 373 |
-
show_label=False
|
| 374 |
-
)
|
| 375 |
-
|
| 376 |
-
# Barra de entrada
|
| 377 |
-
with gr.Row():
|
| 378 |
-
msg = gr.Textbox(
|
| 379 |
-
placeholder="Escribe tu mensaje aquí...",
|
| 380 |
-
container=False,
|
| 381 |
-
scale=9,
|
| 382 |
-
autofocus=True
|
| 383 |
-
)
|
| 384 |
-
btn = gr.Button("Enviar", scale=1)
|
| 385 |
-
|
| 386 |
-
clear = gr.ClearButton([msg, chatbot], value="Limpiar conversación")
|
| 387 |
-
|
| 388 |
-
# Inicialización del agente (sin mostrar herramientas)
|
| 389 |
-
async def init_agent():
|
| 390 |
-
await mcp_agent.initialize()
|
| 391 |
-
return gr.Info("Agente listo para conversar")
|
| 392 |
-
|
| 393 |
-
# Manejo de interacciones
|
| 394 |
-
msg.submit(
|
| 395 |
-
chat_with_agent,
|
| 396 |
-
inputs=[msg, chatbot],
|
| 397 |
-
outputs=[chatbot]
|
| 398 |
-
).then(lambda: "", None, [msg])
|
| 399 |
-
|
| 400 |
-
btn.click(
|
| 401 |
-
chat_with_agent,
|
| 402 |
-
inputs=[msg, chatbot],
|
| 403 |
-
outputs=[chatbot]
|
| 404 |
-
).then(lambda: "", None, [msg])
|
| 405 |
|
| 406 |
-
|
| 407 |
-
# Creamos la interfaz con las dos pestañas principales
|
| 408 |
demo = gr.TabbedInterface(
|
| 409 |
-
[
|
| 410 |
-
["
|
| 411 |
)
|
| 412 |
|
| 413 |
-
demo.launch(
|
|
|
|
| 258 |
|
| 259 |
|
| 260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
with gr.Blocks(title="Herramientas MCP", theme=gr.themes.Base()) as tools_tab:
|
| 262 |
with gr.Accordion("Búsqueda Académica", open=False):
|
| 263 |
arxiv_interface = gr.Interface(
|
|
|
|
| 296 |
description="Realiza búsquedas en web usando la API de Tavily.",
|
| 297 |
api_name="_search_tavily"
|
| 298 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
|
| 300 |
+
# Creamos la interfaz solo con las herramientas
|
|
|
|
| 301 |
demo = gr.TabbedInterface(
|
| 302 |
+
[tools_tab],
|
| 303 |
+
["Tools MCP"]
|
| 304 |
)
|
| 305 |
|
| 306 |
+
demo.launch()
|