Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -97,13 +97,13 @@ space_clients: Dict[str, Client] = {}
|
|
| 97 |
|
| 98 |
|
| 99 |
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
arxiv_tool = ArxivToolSpec(max_results=5).to_tool_list()[0]
|
| 106 |
-
arxiv_tool.return_direct = True
|
| 107 |
|
| 108 |
async def search_arxiv(
|
| 109 |
query: str,
|
|
@@ -262,6 +262,104 @@ async def search_tavily(
|
|
| 262 |
|
| 263 |
|
| 264 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
|
| 266 |
|
| 267 |
|
|
|
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
+
##### ARXIV INSTANCE #####
|
| 101 |
+
arxiv_tool = ArxivToolSpec(max_results=5).to_tool_list()[0]
|
| 102 |
+
arxiv_tool.return_direct = True
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
+
##### MCP TOOLS #####
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
async def search_arxiv(
|
| 109 |
query: str,
|
|
|
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
+
async def list_spaces_names() -> dict:
|
| 266 |
+
"""
|
| 267 |
+
Devuelve una lista simplificada con los nombres y descripciones de todos los spaces disponibles.
|
| 268 |
+
|
| 269 |
+
Returns:
|
| 270 |
+
dict: {
|
| 271 |
+
"status": "success"|"error",
|
| 272 |
+
"spaces": list[dict{"name": str, "description": str}],
|
| 273 |
+
"count": int
|
| 274 |
+
}
|
| 275 |
+
"""
|
| 276 |
+
try:
|
| 277 |
+
spaces_list = [
|
| 278 |
+
{"name": space["name"], "description": space["description"]}
|
| 279 |
+
for space in spaces_metadata["spaces"]
|
| 280 |
+
]
|
| 281 |
+
|
| 282 |
+
return {
|
| 283 |
+
"status": "success",
|
| 284 |
+
"spaces": spaces_list,
|
| 285 |
+
"count": len(spaces_list)
|
| 286 |
+
}
|
| 287 |
+
except Exception as e:
|
| 288 |
+
return {
|
| 289 |
+
"status": "error",
|
| 290 |
+
"message": f"Error al obtener la lista de spaces: {str(e)}",
|
| 291 |
+
"spaces": [],
|
| 292 |
+
"count": 0
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
async def query_context(
|
| 297 |
+
space_name: str,
|
| 298 |
+
message_text: str,
|
| 299 |
+
api_name: str = "/get_context_only"
|
| 300 |
+
) -> dict:
|
| 301 |
+
"""
|
| 302 |
+
Esta herramienta obtiene SOLO el contexto relevante para una consulta desde un Space especializado.
|
| 303 |
+
|
| 304 |
+
Parámetros:
|
| 305 |
+
space_name (str): Nombre exacto del Space a consultar (debe existir en 'list_spaces_names')
|
| 306 |
+
message_text (str): Texto de la consulta/pregunta para generar el contexto
|
| 307 |
+
api_name (str, optional): Endpoint de la API a usar (siempre "/get_context_only")
|
| 308 |
+
|
| 309 |
+
Retorna:
|
| 310 |
+
dict: {
|
| 311 |
+
"status": "success"|"error",
|
| 312 |
+
"space": str, # Nombre del Space consultado
|
| 313 |
+
"query": str, # Texto de la consulta enviada
|
| 314 |
+
"context": str, # Contexto formateado obtenido
|
| 315 |
+
"url": str # URL del Space
|
| 316 |
+
}
|
| 317 |
+
"""
|
| 318 |
+
|
| 319 |
+
try:
|
| 320 |
+
space_info = next((s for s in spaces_metadata["spaces"] if s["name"] == space_name), None)
|
| 321 |
+
|
| 322 |
+
if not space_info:
|
| 323 |
+
logger.warning(f"Space no encontrado: {space_name}")
|
| 324 |
+
return {
|
| 325 |
+
"status": "error",
|
| 326 |
+
"message": f"Space '{space_name}' no encontrado",
|
| 327 |
+
"available_spaces": [s["name"] for s in spaces_metadata["spaces"]]
|
| 328 |
+
}
|
| 329 |
+
|
| 330 |
+
logger.info(f"Obteniendo contexto del space: {space_name}")
|
| 331 |
+
|
| 332 |
+
if space_name not in space_clients:
|
| 333 |
+
logger.debug(f"Creando nuevo cliente para space: {space_name}")
|
| 334 |
+
space_clients[space_name] = Client(space_info["url"], hf_token=HF_TOKEN)
|
| 335 |
+
|
| 336 |
+
client = space_clients[space_name]
|
| 337 |
+
|
| 338 |
+
logger.info(f"Enviando consulta para contexto a space {space_name}")
|
| 339 |
+
context = client.predict(
|
| 340 |
+
message=message_text,
|
| 341 |
+
api_name=api_name
|
| 342 |
+
)
|
| 343 |
+
|
| 344 |
+
logger.debug(f"Contexto obtenido (longitud: {len(context) if context else 0})")
|
| 345 |
+
|
| 346 |
+
return {
|
| 347 |
+
"status": "success",
|
| 348 |
+
"space": space_name,
|
| 349 |
+
"query": message_text,
|
| 350 |
+
"context": context,
|
| 351 |
+
"url": space_info["url"]
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
except Exception as e:
|
| 355 |
+
logger.error(f"Error en query_context: {str(e)}", exc_info=True)
|
| 356 |
+
return {
|
| 357 |
+
"status": "error",
|
| 358 |
+
"space": space_name,
|
| 359 |
+
"query": message_text,
|
| 360 |
+
"error": str(e),
|
| 361 |
+
"url": space_info.get("url", "") if space_info else ""
|
| 362 |
+
}
|
| 363 |
|
| 364 |
|
| 365 |
|