Spaces:
Running
Running
Update agent.py
Browse files
agent.py
CHANGED
@@ -116,48 +116,56 @@ class VisitWebpageTool(Tool):
|
|
116 |
except RequestException as e:
|
117 |
return f"Error fetching the webpage: {str(e)}"
|
118 |
except Exception as e:
|
119 |
-
return
|
|
|
120 |
|
121 |
def __init__(self, *args, **kwargs):
|
122 |
self.is_initialized = False
|
123 |
|
124 |
class DownloadTaskAttachmentTool(Tool):
|
125 |
name = "download_file"
|
126 |
-
description = "Downloads the file attached to the task ID"
|
127 |
inputs = {'task_id': {'type': 'string', 'description': 'The task id to download attachment from.'}}
|
128 |
output_type = "string"
|
129 |
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
Returns the file path where the file is saved locally.
|
135 |
-
"""
|
136 |
-
file_url = f"{DEFAULT_API_URL}/files/{task_id}"
|
137 |
-
local_file_path = f"downloads/{task_id}.file"
|
138 |
|
|
|
|
|
139 |
print(f"Downloading file for task ID {task_id} from {file_url}...")
|
140 |
try:
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
response = requests.get(file_url, stream=True, timeout=15)
|
146 |
response.raise_for_status()
|
147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
os.makedirs("downloads", exist_ok=True)
|
149 |
with open(local_file_path, "wb") as file:
|
150 |
for chunk in response.iter_content(chunk_size=8192):
|
151 |
file.write(chunk)
|
152 |
-
|
153 |
print(f"File downloaded successfully: {local_file_path}")
|
154 |
return local_file_path
|
155 |
except requests.exceptions.RequestException as e:
|
156 |
-
|
157 |
-
raise
|
158 |
-
|
159 |
-
def __init__(self, *args, **kwargs):
|
160 |
-
self.is_initialized = False
|
161 |
|
162 |
@tool
|
163 |
def SpeechToTextTool(audio_path: str) -> str:
|
|
|
116 |
except RequestException as e:
|
117 |
return f"Error fetching the webpage: {str(e)}"
|
118 |
except Exception as e:
|
119 |
+
return
|
120 |
+
f"An unexpected error occurred: {str(e)}"
|
121 |
|
122 |
def __init__(self, *args, **kwargs):
|
123 |
self.is_initialized = False
|
124 |
|
125 |
class DownloadTaskAttachmentTool(Tool):
|
126 |
name = "download_file"
|
127 |
+
description = "Downloads the file attached to the task ID and returns the local file path. Supports Excel (.xlsx), image (.png), and audio (.mp3) files."
|
128 |
inputs = {'task_id': {'type': 'string', 'description': 'The task id to download attachment from.'}}
|
129 |
output_type = "string"
|
130 |
|
131 |
+
def __init__(self, rate_limiter: Optional[Limiter] = None, default_api_url: str = "https://agents-course-unit4-scoring.hf.space", *args, **kwargs):
|
132 |
+
self.is_initialized = False
|
133 |
+
self.rate_limiter = rate_limiter
|
134 |
+
self.default_api_url = default_api_url
|
|
|
|
|
|
|
|
|
135 |
|
136 |
+
async def forward(self, task_id: str) -> str:
|
137 |
+
file_url = f"{self.default_api_url}/files/{task_id}"
|
138 |
print(f"Downloading file for task ID {task_id} from {file_url}...")
|
139 |
try:
|
140 |
+
if self.rate_limiter:
|
141 |
+
while not self.rate_limiter.consume(1):
|
142 |
+
print(f"Rate limit reached for downloading file for task {task_id}. Waiting...")
|
143 |
+
await asyncio.sleep(60 / 15) # Assuming 15 RPM
|
144 |
response = requests.get(file_url, stream=True, timeout=15)
|
145 |
response.raise_for_status()
|
146 |
+
|
147 |
+
# Determine file extension based on Content-Type
|
148 |
+
content_type = response.headers.get('Content-Type', '').lower()
|
149 |
+
if 'image/png' in content_type:
|
150 |
+
extension = '.png'
|
151 |
+
elif 'image/jpeg' in content_type:
|
152 |
+
extension = '.jpg'
|
153 |
+
elif 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' in content_type:
|
154 |
+
extension = '.xlsx'
|
155 |
+
elif 'audio/mpeg' in content_type:
|
156 |
+
extension = '.mp3'
|
157 |
+
else:
|
158 |
+
return f"Error: Unsupported file type {content_type} for task {task_id}"
|
159 |
+
|
160 |
+
local_file_path = f"downloads/{task_id}{extension}"
|
161 |
os.makedirs("downloads", exist_ok=True)
|
162 |
with open(local_file_path, "wb") as file:
|
163 |
for chunk in response.iter_content(chunk_size=8192):
|
164 |
file.write(chunk)
|
|
|
165 |
print(f"File downloaded successfully: {local_file_path}")
|
166 |
return local_file_path
|
167 |
except requests.exceptions.RequestException as e:
|
168 |
+
return f"Error downloading file for task {task_id}: {str(e)}"
|
|
|
|
|
|
|
|
|
169 |
|
170 |
@tool
|
171 |
def SpeechToTextTool(audio_path: str) -> str:
|