Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -6,6 +6,7 @@ except ImportError:
|
|
| 6 |
Tool = object
|
| 7 |
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 9 |
|
| 10 |
# Tool to download and read file attachments (text) from the scoring API
|
| 11 |
# Plain class (no Tool inheritance) — called directly, not via CodeAgent
|
|
@@ -16,19 +17,24 @@ class FileReaderTool:
|
|
| 16 |
self.api_url = api_url
|
| 17 |
print("FileReaderTool initialized.")
|
| 18 |
|
| 19 |
-
def __call__(self, task_id: str) -> str:
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
# Tool to download image attachments and return them as base64
|
|
@@ -40,21 +46,25 @@ class ImageReaderTool:
|
|
| 40 |
self.api_url = api_url
|
| 41 |
print("ImageReaderTool initialized.")
|
| 42 |
|
| 43 |
-
def __call__(self, task_id: str) -> str:
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
|
| 60 |
#search web tool — uses ddgs directly to avoid smolagents DuckDuckGoSearchTool package check
|
|
|
|
| 6 |
Tool = object
|
| 7 |
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
+
HF_DATASET_BASE = "https://huggingface.co/datasets/gaia-benchmark/GAIA/resolve/main/2023/test"
|
| 10 |
|
| 11 |
# Tool to download and read file attachments (text) from the scoring API
|
| 12 |
# Plain class (no Tool inheritance) — called directly, not via CodeAgent
|
|
|
|
| 17 |
self.api_url = api_url
|
| 18 |
print("FileReaderTool initialized.")
|
| 19 |
|
| 20 |
+
def __call__(self, task_id: str, file_name: str = "") -> str:
|
| 21 |
+
# Try HF dataset URL first (actual file location), then scoring API fallback
|
| 22 |
+
urls_to_try = []
|
| 23 |
+
if file_name:
|
| 24 |
+
urls_to_try.append(f"{HF_DATASET_BASE}/{file_name}")
|
| 25 |
+
urls_to_try.append(f"{self.api_url}/files/{task_id}")
|
| 26 |
+
|
| 27 |
+
for url in urls_to_try:
|
| 28 |
+
print(f"FileReaderTool trying: {url}")
|
| 29 |
+
try:
|
| 30 |
+
resp = requests.get(url, timeout=30)
|
| 31 |
+
if resp.status_code == 200:
|
| 32 |
+
content = resp.text
|
| 33 |
+
print(f"FileReaderTool downloaded {len(content)} chars.")
|
| 34 |
+
return content
|
| 35 |
+
except requests.exceptions.RequestException:
|
| 36 |
+
continue
|
| 37 |
+
return f"Failed to download file for task {task_id}"
|
| 38 |
|
| 39 |
|
| 40 |
# Tool to download image attachments and return them as base64
|
|
|
|
| 46 |
self.api_url = api_url
|
| 47 |
print("ImageReaderTool initialized.")
|
| 48 |
|
| 49 |
+
def __call__(self, task_id: str, file_name: str = "") -> str:
|
| 50 |
+
# Try HF dataset URL first (actual file location), then scoring API fallback
|
| 51 |
+
urls_to_try = []
|
| 52 |
+
if file_name:
|
| 53 |
+
urls_to_try.append(f"{HF_DATASET_BASE}/{file_name}")
|
| 54 |
+
urls_to_try.append(f"{self.api_url}/files/{task_id}")
|
| 55 |
+
|
| 56 |
+
for url in urls_to_try:
|
| 57 |
+
print(f"ImageReaderTool trying: {url}")
|
| 58 |
+
try:
|
| 59 |
+
resp = requests.get(url, timeout=30)
|
| 60 |
+
if resp.status_code == 200:
|
| 61 |
+
content_type = resp.headers.get("Content-Type", "image/png")
|
| 62 |
+
image_b64 = base64.b64encode(resp.content).decode("utf-8")
|
| 63 |
+
print(f"ImageReaderTool downloaded image ({len(resp.content)} bytes, {content_type}).")
|
| 64 |
+
return f"data:{content_type};base64,{image_b64}"
|
| 65 |
+
except requests.exceptions.RequestException:
|
| 66 |
+
continue
|
| 67 |
+
return f"Failed to download image for task {task_id}"
|
| 68 |
|
| 69 |
|
| 70 |
#search web tool — uses ddgs directly to avoid smolagents DuckDuckGoSearchTool package check
|