Spaces:
Running
Running
Update agent.py
Browse files
agent.py
CHANGED
@@ -1,52 +1,101 @@
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
-
import requests
|
3 |
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
def __init__(self):
|
6 |
-
|
7 |
-
self.
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from smolagents import Tool, DuckDuckGoSearchTool, GoogleSearchTool, VisitWebpageTool, WikipediaSearchTool, PythonInterpreterTool, FinalAnswerTool
|
2 |
+
from tavily import TavilyClient
|
3 |
+
from dotenv import load_dotenv
|
4 |
import os
|
|
|
5 |
|
6 |
+
load_dotenv() # Cargar variables de entorno desde .env
|
7 |
+
|
8 |
+
# Inicializar herramientas directamente en agent.py
|
9 |
+
duck_search = DuckDuckGoSearchTool()
|
10 |
+
google_search = GoogleSearchTool()
|
11 |
+
visit_page = VisitWebpageTool()
|
12 |
+
wiki_search = WikipediaSearchTool()
|
13 |
+
do_python = PythonInterpreterTool()
|
14 |
+
final_answer = FinalAnswerTool()
|
15 |
+
tavily_search = TavilyClient()
|
16 |
+
|
17 |
+
# Herramienta de transcripción de audio a texto
|
18 |
+
speech_to_text_tool = Tool.from_space("hf-audio/whisper-large-v3-turbo",
|
19 |
+
name="speech_to_text_tool",
|
20 |
+
description="""Convierte un archivo de audio a texto. Usa este comando:
|
21 |
+
'speech_to_text_tool(filename)'""",
|
22 |
+
api_name="/predict")
|
23 |
+
|
24 |
+
# Herramienta de QA visual
|
25 |
+
visual_qa_tool = Tool.from_space("sitammeur/PicQ",
|
26 |
+
name="visual_qa_tool",
|
27 |
+
description="""Responde preguntas sobre una imagen proporcionada.
|
28 |
+
Usa el comando: visual_qa_tool(question=<pregunta>, image=<nombre_de_imagen>)""",
|
29 |
+
api_name="/predict")
|
30 |
+
|
31 |
+
# Inicializar el modelo de Azure OpenAI
|
32 |
+
from smolagents import AzureOpenAIServerModel
|
33 |
+
import app_tokens
|
34 |
+
|
35 |
+
model = AzureOpenAIServerModel(
|
36 |
+
model_id=app_tokens.AZURE_OPENAI_MODEL,
|
37 |
+
azure_endpoint=app_tokens.AZURE_OPENAI_ENDPOINT,
|
38 |
+
api_key=app_tokens.AZURE_OPENAI_API_KEY,
|
39 |
+
api_version=app_tokens.OPENAI_API_VERSION
|
40 |
+
)
|
41 |
+
|
42 |
+
class BasicAgent:
|
43 |
def __init__(self):
|
44 |
+
# Agente de búsqueda en la web
|
45 |
+
self.web_agent = CodeAgent(
|
46 |
+
model=model,
|
47 |
+
tools=[visit_page, wiki_search, google_search, final_answer],
|
48 |
+
max_steps=8,
|
49 |
+
name="web_agent",
|
50 |
+
description="Este agente realiza búsquedas en la web.",
|
51 |
+
add_base_tools=True
|
52 |
+
)
|
53 |
+
|
54 |
+
# Agente de conversión de audio a texto
|
55 |
+
self.audio_agent = CodeAgent(
|
56 |
+
model=model,
|
57 |
+
tools=[speech_to_text_tool, final_answer],
|
58 |
+
max_steps=4,
|
59 |
+
name="audio_agent",
|
60 |
+
description="Este agente convierte audio a texto.",
|
61 |
+
add_base_tools=True
|
62 |
+
)
|
63 |
+
|
64 |
+
# Agente para ejecución de código Python
|
65 |
+
self.py_agent = CodeAgent(
|
66 |
+
model=model,
|
67 |
+
tools=[do_python, final_answer],
|
68 |
+
additional_authorized_imports=["json", "pandas", "numpy", "regex"],
|
69 |
+
max_steps=8,
|
70 |
+
name="python_code_agent",
|
71 |
+
description="Este agente ejecuta y valida código Python.",
|
72 |
+
add_base_tools=True
|
73 |
+
)
|
74 |
+
|
75 |
+
# Agente para análisis de imágenes
|
76 |
+
self.visual_agent = CodeAgent(
|
77 |
+
model=model,
|
78 |
+
tools=[visual_qa_tool, final_answer],
|
79 |
+
max_steps=4,
|
80 |
+
name="visual_qa_agent",
|
81 |
+
description="Este agente responde preguntas sobre imágenes.",
|
82 |
+
add_base_tools=True
|
83 |
+
)
|
84 |
+
|
85 |
+
# Agente principal que coordina otros agentes
|
86 |
+
self.manager_agent = CodeAgent(
|
87 |
+
model=model,
|
88 |
+
tools=[],
|
89 |
+
managed_agents=[self.web_agent, self.audio_agent, self.py_agent, self.visual_agent],
|
90 |
+
planning_interval=8,
|
91 |
+
verbosity_level=2,
|
92 |
+
max_steps=12,
|
93 |
+
add_base_tools=True
|
94 |
+
)
|
95 |
+
|
96 |
+
def forward(self, question: str, attachment: str = None) -> str:
|
97 |
+
if attachment:
|
98 |
+
result = self.manager_agent.run(question, additional_args={"attachment": attachment})
|
99 |
+
else:
|
100 |
+
result = self.manager_agent.run(question)
|
101 |
+
return result
|