Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,71 @@ from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel, Visit
|
|
8 |
# --- Constants ---
|
9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
# --- Basic Agent Definition ---
|
12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
13 |
# class BasicAgent:
|
@@ -21,17 +86,37 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
21 |
class BasicAgent:
|
22 |
def __init__(self):
|
23 |
|
24 |
-
self.
|
25 |
-
model_id="
|
26 |
api_base="https://api.siliconflow.cn/v1/",
|
27 |
api_key=os.getenv('MODEL_TOKEN'),
|
28 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
self.tools = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
self.agent = CodeAgent(
|
33 |
tools=self.tools,
|
34 |
-
model=self.
|
|
|
|
|
35 |
max_steps=20
|
36 |
)
|
37 |
print("BasicAgent initialized.")
|
|
|
8 |
# --- Constants ---
|
9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
10 |
|
11 |
+
class AttachmentDownloadTool(Tool):
|
12 |
+
name = "attachment-downloader"
|
13 |
+
description = "Downloads the file associated with the given task_id. If it does not exist, return None. input: task_id。output: attachment files or None"
|
14 |
+
inputs = {
|
15 |
+
"task_id": {
|
16 |
+
"type": "str",
|
17 |
+
"description": "task_id that needs to download attachment files."
|
18 |
+
}
|
19 |
+
}
|
20 |
+
output_type = File
|
21 |
+
|
22 |
+
def forward(self, task_id):
|
23 |
+
download_url = f"{api_url}/files/"
|
24 |
+
|
25 |
+
try:
|
26 |
+
response = requests.get(download_url + task_id, stream=True, timeout=15)
|
27 |
+
if response.status_code != 200:
|
28 |
+
return None
|
29 |
+
file_obj = io.BytesIO(response.content)
|
30 |
+
file_obj.seek(0)
|
31 |
+
return file_obj
|
32 |
+
except Exception as e:
|
33 |
+
return None
|
34 |
+
|
35 |
+
class ImageCaptionTool(Tool):
|
36 |
+
name = "image-captioner"
|
37 |
+
description = "Identify the content of the input image and describe it in natural language. Input: image. Output: description text."
|
38 |
+
inputs = {
|
39 |
+
"image": {
|
40 |
+
"type": "image",
|
41 |
+
"description": "Images that need to be identified and described"
|
42 |
+
}
|
43 |
+
}
|
44 |
+
output_type = str
|
45 |
+
|
46 |
+
def setup(self):
|
47 |
+
self.model = OpenAIServerModel(
|
48 |
+
model_id="Qwen/Qwen2.5-VL-32B-Instruct",
|
49 |
+
api_base="https://api.siliconflow.cn/v1/",
|
50 |
+
api_key=os.getenv('MODEL_TOKEN'),
|
51 |
+
)
|
52 |
+
|
53 |
+
def forward(self, image):
|
54 |
+
prompt = "Please describe the content of this picture in detail."
|
55 |
+
return self.model(prompt, images=[image])
|
56 |
+
|
57 |
+
class AudioToTextTool(Tool):
|
58 |
+
name = "audio-to-text"
|
59 |
+
description = "Convert the input audio content to text. Input: audio. Output: text."
|
60 |
+
inputs = {
|
61 |
+
"audio": {
|
62 |
+
"type": "audio",
|
63 |
+
"description": "The audio file that needs to be transcribed"
|
64 |
+
}
|
65 |
+
}
|
66 |
+
output_type = str
|
67 |
+
|
68 |
+
def setup(self):
|
69 |
+
# 使用 HuggingFace Hub 上的 Whisper 大模型
|
70 |
+
self.model = HfApiModel(model_id="openai/whisper-large-v3") # 或其他支持音频转写的模型
|
71 |
+
|
72 |
+
def forward(self, audio):
|
73 |
+
prompt = "Please transcribe this audio content into text."
|
74 |
+
return self.model(prompt, audios=[audio])
|
75 |
+
|
76 |
# --- Basic Agent Definition ---
|
77 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
78 |
# class BasicAgent:
|
|
|
86 |
class BasicAgent:
|
87 |
def __init__(self):
|
88 |
|
89 |
+
self.think_model = OpenAIServerModel(
|
90 |
+
model_id="THUDM/GLM-Z1-32B-0414",
|
91 |
api_base="https://api.siliconflow.cn/v1/",
|
92 |
api_key=os.getenv('MODEL_TOKEN'),
|
93 |
)
|
94 |
+
self.base_model = OpenAIServerModel(
|
95 |
+
model_id="THUDM/GLM-4-32B-0414",
|
96 |
+
api_base="https://api.siliconflow.cn/v1/",
|
97 |
+
api_key=os.getenv('MODEL_TOKEN'),
|
98 |
+
)
|
99 |
+
# self.vision_model = OpenAIServerModel(
|
100 |
+
# model_id="Qwen/Qwen2.5-VL-32B-Instruct",
|
101 |
+
# api_base="https://api.siliconflow.cn/v1/",
|
102 |
+
# api_key=os.getenv('MODEL_TOKEN'),
|
103 |
+
# )
|
104 |
|
105 |
+
self.tools = [AttachmentDownloadTool, ImageCaptionTool, AudioToTextTool]
|
106 |
+
|
107 |
+
web_agent = ToolCallingAgent(
|
108 |
+
tools=[DuckDuckGoSearchTool(), VisitWebpageTool()],
|
109 |
+
model=self.base_model,
|
110 |
+
max_steps=10,
|
111 |
+
name="web_search_agent",
|
112 |
+
description="Runs web searches for you.",
|
113 |
+
)
|
114 |
|
115 |
self.agent = CodeAgent(
|
116 |
tools=self.tools,
|
117 |
+
model=self.think_model,
|
118 |
+
managed_agents=[web_agent,],
|
119 |
+
additional_authorized_imports=["time", "numpy", "pandas"],
|
120 |
max_steps=20
|
121 |
)
|
122 |
print("BasicAgent initialized.")
|