Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,39 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
-
import datetime
|
| 3 |
-
import requests
|
| 4 |
-
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
-
# Importante: Asumiendo que GradioUI permite un argumento de título.
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
#
|
| 11 |
duckduckgo_tool = DuckDuckGoSearchTool()
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
@tool
|
| 16 |
def get_composer_lifespan_from_web(composer_lastname: str) -> str:
|
| 17 |
-
"""
|
|
|
|
| 18 |
|
| 19 |
Args:
|
| 20 |
composer_lastname: El apellido del compositor (p. ej., 'Beethoven', 'Mozart').
|
| 21 |
-
|
| 22 |
-
INSTRUCCIÓN ESTRICTA: El Agente debe utilizar los resultados de la búsqueda en bruto para extraer el nombre, el año de nacimiento (AAAA) y el año de fallecimiento (AAAA) y generar la respuesta FINAL y ÚNICA utilizando FinalAnswerTool con el siguiente formato, incluyendo el nombre completo y los saltos de línea (\\n) entre campos:
|
| 23 |
-
|
| 24 |
-
Nombre: [Nombre Completo del Compositor]
|
| 25 |
-
Fecha de nacimiento: [AAAA]
|
| 26 |
-
Fecha de fallecimiento: [AAAA]
|
| 27 |
"""
|
| 28 |
-
|
| 29 |
-
|
| 30 |
query = f"classical composer {composer_lastname} full name birth and death year"
|
| 31 |
|
| 32 |
-
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
|
|
|
| 39 |
final_answer = FinalAnswerTool()
|
| 40 |
|
| 41 |
model = HfApiModel(
|
|
@@ -45,26 +43,24 @@ model = HfApiModel(
|
|
| 45 |
custom_role_conversions=None,
|
| 46 |
)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
with open("prompts.yaml", 'r') as stream:
|
| 51 |
prompt_templates = yaml.safe_load(stream)
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
# 2. Ajuste del GradioUI para el título:
|
| 67 |
-
custom_title = "Nacimiento y Muerte de Compositores. Introduce el apellido del compositor:"
|
| 68 |
|
| 69 |
-
#
|
|
|
|
| 70 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
|
|
|
|
|
|
|
|
|
| 2 |
import yaml
|
| 3 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 4 |
from Gradio_UI import GradioUI
|
| 5 |
|
| 6 |
+
# 1. Initialize the search tool (for internal use by our custom tool only)
|
| 7 |
duckduckgo_tool = DuckDuckGoSearchTool()
|
| 8 |
|
| 9 |
+
# 2. Define Custom Tool
|
|
|
|
| 10 |
@tool
|
| 11 |
def get_composer_lifespan_from_web(composer_lastname: str) -> str:
|
| 12 |
+
"""
|
| 13 |
+
Busca información sobre un compositor y devuelve los datos crudos para que el agente los procese.
|
| 14 |
|
| 15 |
Args:
|
| 16 |
composer_lastname: El apellido del compositor (p. ej., 'Beethoven', 'Mozart').
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
"""
|
| 18 |
+
# Usamos la herramienta de búsqueda inicializada arriba
|
|
|
|
| 19 |
query = f"classical composer {composer_lastname} full name birth and death year"
|
| 20 |
|
| 21 |
+
# Ejecutamos la búsqueda
|
| 22 |
+
search_result = duckduckgo_tool.forward(query)
|
| 23 |
|
| 24 |
+
return f"""
|
| 25 |
+
RESULTADOS DE BÚSQUEDA:
|
| 26 |
+
{search_result}
|
| 27 |
+
|
| 28 |
+
INSTRUCCIÓN OBLIGATORIA PARA EL AGENTE:
|
| 29 |
+
Usa la información de arriba para responder ÚNICAMENTE con este formato exacto:
|
| 30 |
+
|
| 31 |
+
Nombre: [Nombre Completo]
|
| 32 |
+
Fecha de nacimiento: [Año]
|
| 33 |
+
Fecha de fallecimiento: [Año]
|
| 34 |
+
"""
|
| 35 |
|
| 36 |
+
# 3. Setup Agent
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
model = HfApiModel(
|
|
|
|
| 43 |
custom_role_conversions=None,
|
| 44 |
)
|
| 45 |
|
| 46 |
+
# Load prompts
|
|
|
|
| 47 |
with open("prompts.yaml", 'r') as stream:
|
| 48 |
prompt_templates = yaml.safe_load(stream)
|
| 49 |
|
| 50 |
+
agent = CodeAgent(
|
| 51 |
+
model=model,
|
| 52 |
+
# CRITICAL FIX: Removed 'duckduckgo_tool' from this list.
|
| 53 |
+
# Now the agent MUST use 'get_composer_lifespan_from_web'.
|
| 54 |
+
tools=[final_answer, get_composer_lifespan_from_web],
|
| 55 |
+
max_steps=6,
|
| 56 |
+
verbosity_level=1,
|
| 57 |
+
grammar=None,
|
| 58 |
+
planning_interval=None,
|
| 59 |
+
name=None,
|
| 60 |
+
description=None,
|
| 61 |
+
prompt_templates=prompt_templates
|
| 62 |
+
)
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
# 4. Launch UI
|
| 65 |
+
# Removed 'title' argument to prevent the previous crash
|
| 66 |
GradioUI(agent).launch()
|