Spaces:
Running
Running
File size: 13,096 Bytes
27ea68e ad40800 12c2f26 ad40800 f6ecd68 ad40800 f6ecd68 8439dff 1c586d4 ad40800 f6ecd68 ad40800 11dc663 ad40800 11dc663 ad40800 11dc663 ad40800 11dc663 ad40800 319eac2 fa9810e 319eac2 fa9810e ad40800 46a6817 27ea68e 46a6817 27ea68e 46a6817 27ea68e 46a6817 bc3a4c2 f6ecd68 8c4faf2 f6ecd68 70b37bc f6ecd68 70b37bc f6ecd68 70b37bc 61a6edd 70b37bc f6ecd68 70b37bc 61a6edd 70b37bc 61a6edd 70b37bc 61a6edd f6ecd68 bc3a4c2 46a6817 ba36b5d e313929 46a6817 ee99541 e313929 46a6817 ee99541 e313929 67212e1 e313929 9663e57 e313929 8be85ec 4be8302 8c4faf2 8be85ec 8c4faf2 f6ecd68 8c4faf2 f6ecd68 8c4faf2 18618de 8c4faf2 f6ecd68 8c4faf2 f6ecd68 8c4faf2 f6ecd68 8c4faf2 f6ecd68 e313929 f6ecd68 e313929 27ea68e | 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 | import gradio as gr
from llama_index.core import VectorStoreIndex
from llama_index.core import (
StorageContext,
load_index_from_storage,
)
from llama_index.tools.arxiv import ArxivToolSpec
from llama_index.core import Settings
from llama_index.llms.azure_openai import AzureOpenAI
from llama_index.embeddings.azure_openai import AzureOpenAIEmbedding
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.openai import OpenAIEmbedding
from typing import Optional, List, Dict, Any
from pathlib import Path
import aiohttp
import json
import os
from mcp_agent import MCPAgent
import asyncio
from gradio_client import Client, handle_file
HF_TOKEN = os.environ.get('HF_TOKEN')
##### LLM #####
openai_api_key = os.environ.get('OPENAI_API_KEY')
llm = OpenAI(
model="gpt-4.1",
api_key=openai_api_key,
)
embed_model = OpenAIEmbedding(
model="text-embedding-ada-002",
api_key=openai_api_key,
)
Settings.llm = llm
Settings.embed_model = embed_model
##### END LLM #####
##### LOAD RETRIEVERS #####
DOCUMENTS_BASE_PATH = "./"
RETRIEVERS_JSON_PATH = Path("./retrievers.json")
# Cargar metadatos
def load_retrievers_metadata():
try:
with open(RETRIEVERS_JSON_PATH, 'r', encoding='utf-8') as f:
return json.load(f)
except Exception as e:
print(f"Error cargando retrievers.json: {str(e)}")
print(f"Detalles del error: {traceback.format_exc()}") # Necesitarías importar traceback
return {}
retrievers_metadata = load_retrievers_metadata()
SOURCES = {source: f"{source.lower()}/" for source in retrievers_metadata.keys()}
# Cargar índices
indices: Dict[str, VectorStoreIndex] = {}
for source, rel_path in SOURCES.items():
full_path = os.path.join(DOCUMENTS_BASE_PATH, rel_path)
if not os.path.exists(full_path):
print(f"Advertencia: No se encontró la ruta para {source}")
continue
for root, dirs, files in os.walk(full_path):
if "storage_nodes" in dirs:
try:
storage_path = os.path.join(root, "storage_nodes")
storage_context = StorageContext.from_defaults(persist_dir=storage_path)
index_name = os.path.basename(root)
indices[index_name] = load_index_from_storage(storage_context) #, index_id="vector_index"
print(f"Índice cargado correctamente: {index_name}")
except Exception as e:
print(f"Error cargando índice {index_name}: {str(e)}")
print(f"Detalles del error: {traceback.format_exc()}")
##### ARXIV INSTANCE #####
arxiv_tool = ArxivToolSpec(max_results=5).to_tool_list()[0]
arxiv_tool.return_direct = True
##### MCP TOOLS #####
async def search_arxiv(
query: str,
max_results: int = 5
) -> Dict[str, Any]:
"""
Busca artículos académicos en ArXiv.
Args:
query: Términos de búsqueda (ej. "deep learning")
max_results: Número máximo de resultados (1-10, default 5)
Returns:
Dict: Resultados de la búsqueda con metadatos de los papers
"""
try:
# Configurar máximo de resultados
max_results = min(max(1, max_results), 10)
arxiv_tool.metadata.max_results = max_results
# Ejecutar búsqueda y obtener resultados
tool_output = arxiv_tool(query=query)
# Procesar documentos
papers = []
for doc in tool_output.raw_output: # Acceder correctamente a los documentos
content = doc.text_resource.text.split('\n')
papers.append({
'title': content[0].split(': ')[1] if ': ' in content[0] else content[0],
'abstract': '\n'.join(content[1:]).strip(),
'pdf_url': content[0].split(': ')[0].replace('http://', 'https://'),
'arxiv_id': content[0].split(': ')[0].split('/')[-1].replace('v1', '')
})
return {
'papers': papers,
'count': len(papers),
'query': query,
'status': 'success'
}
except Exception as e:
return {
'papers': [],
'count': 0,
'query': query,
'status': 'error',
'error': str(e)
}
async def list_retrievers(source: str = None) -> dict:
"""
Devuelve la lista de retrievers disponibles.
Si se especifica una source y existe, filtra por ella; si no existe, devuelve todas.
Args:
source (str, optional): Fuente para filtrar. Si no existe, se ignorará. Defaults to None.
Returns:
dict: {
"retrievers": Lista de retrievers (filtrados o completos),
"count": Número total,
"status": "success"|"error",
"source_requested": source, # Muestra lo que se solicitó
"source_used": "all"|source # Muestra lo que realmente se usó
}
"""
try:
available = []
source_exists = source in retrievers_metadata if source else False
for current_source, indexes in retrievers_metadata.items():
# Solo filtrar si el source existe, sino mostrar todo
if source_exists and current_source != source:
continue
for index_name, metadata in indexes.items():
available.append({
"name": index_name,
"source": current_source,
"title": metadata.get("title", ""),
"description": metadata.get("description", "")
})
return {
"retrievers": available,
"count": len(available),
"status": "success",
"source_requested": source,
"source_used": source if source_exists else "all"
}
except Exception as e:
return {
"retrievers": [],
"count": 0,
"status": "error",
"error": str(e),
"source_requested": source,
"source_used": "none"
}
async def search_tavily(
query: str,
days: int = 7,
max_results: int = 1,
include_answer: bool = False
) -> dict:
"""Perform a web search using the Tavily API.
Args:
query: Search query string (required)
days: Restrict search to last N days (default: 7)
max_results: Maximum results to return (default: 1)
include_answer: Include a direct answer only when requested by the user (default: False)
Returns:
dict: Search results from Tavily
"""
# Obtener la API key de las variables de entorno
tavily_api_key = os.environ.get('TAVILY_API_KEY')
if not tavily_api_key:
raise ValueError("TAVILY_API_KEY environment variable not set")
headers = {
"Authorization": f"Bearer {tavily_api_key}",
"Content-Type": "application/json"
}
payload = {
"query": query,
"search_depth": "basic",
"max_results": max_results,
"days": days if days else None,
"include_answer": include_answer
}
try:
async with aiohttp.ClientSession() as session:
async with session.post(
"https://api.tavily.com/search",
headers=headers,
json=payload
) as response:
response.raise_for_status()
result = await response.json()
return result
except Exception as e:
return {
"error": str(e),
"status": "failed",
"query": query
}
##### MCP AGENT #####
sse_url = "https://pharma-ia-gradio-mcp-server.hf.space/gradio_api/mcp/sse"
# Crear instancia del agente (con llm definido)
mcp_agent = MCPAgent(sse_url, HF_TOKEN, llm)
# Historial de chat
chat_history = []
def format_tool_call(tool_name, arguments):
message = f"🔧 **Llamando a herramienta**: `{tool_name}`\n```json\n{arguments}\n```"
print(message) # Solo imprime en consola
return "" # Devuelve cadena vacía para no mostrar en chat
def format_tool_result(tool_name, result):
message = f"✅ **Resultado de `{tool_name}`**\n```\n{result}\n```"
print(message) # Solo imprime en consola
return "" # Devuelve cadena vacía para no mostrar en chat
async def chat_with_agent(message, history):
history = history or []
# Agregar mensaje del usuario y marcador de "escribiendo"
history.append((message, None)) # None activa el indicador de typing
yield history
# Variables para seguimiento
processing_message = None
try:
# Procesar con el agente
full_response = ""
async for event in mcp_agent.stream_response(message):
if event["type"] == "tool_call":
format_tool_call(event["tool_name"], event["arguments"])
elif event["type"] == "tool_result":
format_tool_result(event["tool_name"], event["result"])
elif event["type"] == "final_response":
full_response = event["content"]
# Actualizar indicador de actividad periódicamente
if not processing_message or len(history[-1][1] or "") > len(processing_message):
processing_message = "✍️ Generando respuesta..."
history[-1] = (history[-1][0], processing_message)
yield history
# Mostrar respuesta final
if full_response:
history[-1] = (history[-1][0], full_response)
yield history
except Exception as e:
history[-1] = (history[-1][0], f"⚠️ Error: {str(e)}")
yield history
# Configuración de la interfaz Gradio
with gr.Blocks(title="Herramientas MCP", theme=gr.themes.Base()) as tools_tab:
with gr.Accordion("Búsqueda Académica", open=False):
arxiv_interface = gr.Interface(
fn=search_arxiv,
inputs=[
gr.Textbox(label="Términos de búsqueda", placeholder="Ej: deep learning"),
gr.Slider(1, 10, value=5, step=1, label="Número máximo de resultados")
],
outputs=gr.JSON(label="Resultados de búsqueda"),
title="Búsqueda en ArXiv",
description="Busca artículos académicos en ArXiv por palabras clave.",
api_name="_search_arxiv"
)
with gr.Accordion("Retrievers", open=False):
retrievers_interface = gr.Interface(
fn=list_retrievers,
inputs=gr.Textbox(label="Fuente (opcional)", placeholder="Dejar vacío para listar todos"),
outputs=gr.JSON(label="Lista de retrievers"),
title="Lista de Retrievers",
description="Muestra los retrievers disponibles, opcionalmente filtrados por fuente.",
api_name="_list_retrievers"
)
with gr.Accordion("Búsqueda Web", open=False):
tavily_interface = gr.Interface(
fn=search_tavily,
inputs=[
gr.Textbox(label="Consulta de búsqueda", placeholder="Ej: últimas noticias sobre IA"),
gr.Slider(1, 30, value=7, step=1, label="Últimos N días (0 para sin límite)"),
gr.Slider(1, 10, value=1, step=1, label="Máximo de resultados"),
gr.Checkbox(label="Incluir respuesta directa", value=False)
],
outputs=gr.JSON(label="Resultados de Tavily"),
title="Búsqueda Web (Tavily)",
description="Realiza búsquedas en web usando la API de Tavily.",
api_name="_search_tavily"
)
# Creamos el Agente MCP
with gr.Blocks(title="Agente MCP", theme=gr.themes.Base()) as agent_tab:
# Chat principal
chatbot = gr.Chatbot(
label="Chat con el Agente MCP",
height=600,
bubble_full_width=True,
render_markdown=True,
show_label=False
)
# Barra de entrada
with gr.Row():
msg = gr.Textbox(
placeholder="Escribe tu mensaje aquí...",
container=False,
scale=9,
autofocus=True
)
btn = gr.Button("Enviar", scale=1)
clear = gr.ClearButton([msg, chatbot], value="Limpiar conversación")
# Inicialización del agente (sin mostrar herramientas)
async def init_agent():
await mcp_agent.initialize()
return gr.Info("Agente listo para conversar")
# Manejo de interacciones
msg.submit(
chat_with_agent,
inputs=[msg, chatbot],
outputs=[chatbot]
).then(lambda: "", None, [msg])
btn.click(
chat_with_agent,
inputs=[msg, chatbot],
outputs=[chatbot]
).then(lambda: "", None, [msg])
# Creamos la interfaz con las dos pestañas principales
demo = gr.TabbedInterface(
[agent_tab, tools_tab],
["Agente MCP", "Tools MCP"]
)
demo.launch(mcp_server=True) |