Update app.py
Browse files
app.py
CHANGED
|
@@ -4,66 +4,67 @@ import requests
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
@tool
|
| 12 |
-
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
| 13 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
-
Args:
|
| 16 |
-
arg1: the first argument
|
| 17 |
-
arg2: the second argument
|
| 18 |
-
"""
|
| 19 |
-
return "What magic will you build ?"
|
| 20 |
|
| 21 |
@tool
|
| 22 |
-
def
|
| 23 |
-
"""
|
|
|
|
| 24 |
Args:
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
"""
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
|
|
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
-
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 40 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 41 |
-
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.5,
|
| 45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
# Import tool from Hub
|
| 51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 52 |
|
| 53 |
with open("prompts.yaml", 'r') as stream:
|
| 54 |
prompt_templates = yaml.safe_load(stream)
|
| 55 |
-
|
| 56 |
-
agent = CodeAgent(
|
| 57 |
-
model=model,
|
| 58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 59 |
-
max_steps=6,
|
| 60 |
-
verbosity_level=1,
|
| 61 |
-
grammar=None,
|
| 62 |
-
planning_interval=None,
|
| 63 |
-
name=None,
|
| 64 |
-
description=None,
|
| 65 |
-
prompt_templates=prompt_templates
|
| 66 |
-
)
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
GradioUI
|
|
|
|
|
|
| 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 |
+
# Inicialización de la herramienta de búsqueda
|
| 11 |
+
duckduckgo_tool = DuckDuckGoSearchTool()
|
| 12 |
|
| 13 |
+
# --- Herramientas personalizadas (Solo queda la de compositores) ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
@tool
|
| 16 |
+
def get_composer_lifespan_from_web(composer_lastname: str) -> str:
|
| 17 |
+
"""Busca el nombre completo, el año de nacimiento y el año de fallecimiento de un compositor de música clásica.
|
| 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 |
+
global duckduckgo_tool
|
| 29 |
+
|
| 30 |
+
query = f"classical composer {composer_lastname} full name birth and death year"
|
| 31 |
+
|
| 32 |
+
search_result = duckduckgo_tool(query=query)
|
| 33 |
+
|
| 34 |
+
# La clave está en la interpretación del Agente de la instrucción estricta en el docstring.
|
| 35 |
+
return f"Consulta de búsqueda: '{query}'. \n\nResultado de búsqueda en bruto:\n\n{search_result}"
|
| 36 |
|
| 37 |
+
# --- Inicialización del Agente y Gradio ---
|
| 38 |
|
| 39 |
final_answer = FinalAnswerTool()
|
| 40 |
|
|
|
|
|
|
|
|
|
|
| 41 |
model = HfApiModel(
|
| 42 |
+
max_tokens=2096,
|
| 43 |
+
temperature=0.5,
|
| 44 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 45 |
+
custom_role_conversions=None,
|
| 46 |
)
|
| 47 |
|
|
|
|
|
|
|
| 48 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 49 |
|
| 50 |
with open("prompts.yaml", 'r') as stream:
|
| 51 |
prompt_templates = yaml.safe_load(stream)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
agent = CodeAgent(
|
| 54 |
+
model=model,
|
| 55 |
+
# La lista de herramientas ahora solo incluye las esenciales y la de compositores
|
| 56 |
+
tools=[final_answer, duckduckgo_tool, get_composer_lifespan_from_web],
|
| 57 |
+
max_steps=6,
|
| 58 |
+
verbosity_level=1,
|
| 59 |
+
grammar=None,
|
| 60 |
+
planning_interval=None,
|
| 61 |
+
name=None,
|
| 62 |
+
description=None,
|
| 63 |
+
prompt_templates=prompt_templates
|
| 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 |
+
# Si GradioUI no soporta directamente el argumento 'title', tendrás que modificar el archivo Gradio_UI.py.
|
| 70 |
+
GradioUI(agent, title=custom_title).launch()
|