Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
@@ -1,63 +1,17 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
import my_tokens
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
)
|
12 |
-
|
13 |
-
class CustomAgent:
|
14 |
-
def __init__(self):
|
15 |
-
# Definir agentes de trabajo con sus herramientas
|
16 |
-
self.web_agent = CodeAgent(
|
17 |
-
model=model,
|
18 |
-
tools=[agent_tools.google_search, agent_tools.wiki_search, agent_tools.visit_page, agent_tools.final_answer],
|
19 |
-
max_steps=8,
|
20 |
-
name="web_agent",
|
21 |
-
description="Este agente maneja búsquedas web."
|
22 |
-
)
|
23 |
-
|
24 |
-
self.audio_agent = CodeAgent(
|
25 |
-
model=model,
|
26 |
-
tools=[agent_tools.speech_to_text_tool, agent_tools.final_answer],
|
27 |
-
max_steps=4,
|
28 |
-
name="audio_agent",
|
29 |
-
description="Este agente convierte audio a texto."
|
30 |
-
)
|
31 |
-
|
32 |
-
self.py_agent = CodeAgent(
|
33 |
-
model=model,
|
34 |
-
tools=[agent_tools.do_python, agent_tools.final_answer],
|
35 |
-
additional_authorized_imports=["json", "pandas", "numpy", "regex"],
|
36 |
-
max_steps=8,
|
37 |
-
name="python_code_agent",
|
38 |
-
description="Este agente ejecuta y valida código Python."
|
39 |
-
)
|
40 |
-
|
41 |
-
self.visual_agent = CodeAgent(
|
42 |
-
model=model,
|
43 |
-
tools=[agent_tools.visual_qa_tool, agent_tools.final_answer],
|
44 |
-
max_steps=4,
|
45 |
-
name="visual_qa_agent",
|
46 |
-
description="Este agente responde preguntas sobre imágenes."
|
47 |
-
)
|
48 |
-
|
49 |
-
self.manager_agent = CodeAgent(
|
50 |
-
model=model,
|
51 |
-
tools=[],
|
52 |
-
managed_agents=[self.web_agent, self.audio_agent, self.py_agent, self.visual_agent],
|
53 |
-
planning_interval=8,
|
54 |
-
verbosity_level=2,
|
55 |
-
max_steps=12,
|
56 |
)
|
|
|
57 |
|
58 |
-
def
|
59 |
-
|
60 |
-
|
61 |
-
else:
|
62 |
-
result = self.manager_agent.run(question)
|
63 |
-
return result
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer, pipeline
|
2 |
+
import torch
|
|
|
3 |
|
4 |
+
class HuggingFaceAgent:
|
5 |
+
def __init__(self, model_id, access_token):
|
6 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_id, token=access_token)
|
7 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
8 |
+
model_id,
|
9 |
+
token=access_token,
|
10 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
11 |
+
device_map="auto"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
)
|
13 |
+
self.pipeline = pipeline("text-generation", model=self.model, tokenizer=self.tokenizer)
|
14 |
|
15 |
+
def responder(self, prompt):
|
16 |
+
respuesta = self.pipeline(prompt, max_new_tokens=256, do_sample=True, temperature=0.7)[0]["generated_text"]
|
17 |
+
return respuesta[len(prompt):].strip()
|
|
|
|
|
|