Spaces:
Runtime error
Runtime error
Update my_tools.py
Browse files- my_tools.py +58 -38
my_tools.py
CHANGED
|
@@ -1,56 +1,76 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
| 3 |
import math
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import wikipedia
|
| 5 |
|
| 6 |
-
# ---
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
results.append(f"{r['title']}: {r['body']}")
|
| 13 |
-
return "\n".join(results) if results else "Sin resultados"
|
| 14 |
-
except Exception as e:
|
| 15 |
-
return f"Error en búsqueda web: {e}"
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
)
|
| 22 |
|
| 23 |
-
|
| 24 |
-
def wikipedia_lookup(query: str) -> str:
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
return summary
|
| 28 |
except Exception as e:
|
| 29 |
-
return f"
|
| 30 |
|
| 31 |
-
wikipedia_tool =
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
)
|
| 36 |
|
| 37 |
-
|
| 38 |
-
def calculator(expression: str) -> str:
|
| 39 |
try:
|
| 40 |
-
result = eval(
|
| 41 |
return str(result)
|
| 42 |
except Exception as e:
|
| 43 |
return f"Error de cálculo: {e}"
|
| 44 |
|
| 45 |
-
calculator_tool =
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
import inspect
|
| 5 |
+
import pandas as pd
|
| 6 |
import math
|
| 7 |
+
|
| 8 |
+
# --- AGENTE LLAMAINDEX IMPORTADO ---
|
| 9 |
+
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
| 10 |
+
from llama_index.core.agent import ReActAgent
|
| 11 |
+
from llama_index.core.tools import FunctionTool
|
| 12 |
+
from duckduckgo_search import DDGS
|
| 13 |
import wikipedia
|
| 14 |
|
| 15 |
+
# --- Configuración del LLM ---
|
| 16 |
+
HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN", "")
|
| 17 |
+
llm = HuggingFaceInferenceAPI(
|
| 18 |
+
model_name="HuggingFaceH4/zephyr-7b-beta",
|
| 19 |
+
token=HF_TOKEN if HF_TOKEN else None
|
| 20 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# --- Herramientas del agente ---
|
| 23 |
+
def buscar_web(query: str) -> str:
|
| 24 |
+
with DDGS() as ddgs:
|
| 25 |
+
results = list(ddgs.text(query, region='es-es', safesearch='moderate', timelimit='y', max_results=3))
|
| 26 |
+
if results:
|
| 27 |
+
return "\n".join([f"{r['title']}: {r['body']}" for r in results])
|
| 28 |
+
return "No se encontraron resultados."
|
| 29 |
+
|
| 30 |
+
search_tool = FunctionTool.from_defaults(
|
| 31 |
+
fn=buscar_web,
|
| 32 |
+
name="web_search",
|
| 33 |
+
description="Busca en la web utilizando DuckDuckGo."
|
| 34 |
)
|
| 35 |
|
| 36 |
+
def get_wikipedia_summary(query: str) -> str:
|
|
|
|
| 37 |
try:
|
| 38 |
+
return wikipedia.summary(query, sentences=3, auto_suggest=False)
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
+
return f"Error al buscar en Wikipedia: {e}"
|
| 41 |
|
| 42 |
+
wikipedia_tool = FunctionTool.from_defaults(
|
| 43 |
+
fn=get_wikipedia_summary,
|
| 44 |
+
name="wikipedia_lookup",
|
| 45 |
+
description="Busca un resumen breve de un tema en Wikipedia."
|
| 46 |
)
|
| 47 |
|
| 48 |
+
def calcular_expresion(expr: str) -> str:
|
|
|
|
| 49 |
try:
|
| 50 |
+
result = eval(expr, {"__builtins__": {}}, math.__dict__)
|
| 51 |
return str(result)
|
| 52 |
except Exception as e:
|
| 53 |
return f"Error de cálculo: {e}"
|
| 54 |
|
| 55 |
+
calculator_tool = FunctionTool.from_defaults(
|
| 56 |
+
fn=calcular_expresion,
|
| 57 |
+
name="calculadora",
|
| 58 |
+
description="Resuelve expresiones matemáticas, incluyendo funciones trigonométricas, logaritmos, potencias, etc."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# --- Agente LlamaIndex ---
|
| 62 |
+
alfred_agent = ReActAgent.from_tools(
|
| 63 |
+
tools=[search_tool, wikipedia_tool, calculator_tool],
|
| 64 |
+
llm=llm,
|
| 65 |
+
verbose=False
|
| 66 |
)
|
| 67 |
|
| 68 |
+
def basic_agent_response(question):
|
| 69 |
+
response = alfred_agent.query(question)
|
| 70 |
+
return str(response)
|
| 71 |
+
|
| 72 |
+
# --- Constants ---
|
| 73 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 74 |
+
|
| 75 |
+
# --- Función de evaluación y envío ---
|
| 76 |
+
# (sin cambios, continúa igual...)
|