TommasoBB commited on
Commit
b1194ab
·
verified ·
1 Parent(s): 32c95ee

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +38 -28
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
- file_url = f"{self.api_url}/files/{task_id}"
21
- print(f"FileReaderTool downloading from: {file_url}")
22
- try:
23
- resp = requests.get(file_url, timeout=30)
24
- resp.raise_for_status()
25
- content = resp.text
26
- print(f"FileReaderTool downloaded {len(content)} chars.")
27
- return content
28
- except requests.exceptions.RequestException as e:
29
- error_msg = f"Failed to download file for task {task_id}: {e}"
30
- print(error_msg)
31
- return error_msg
 
 
 
 
 
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
- file_url = f"{self.api_url}/files/{task_id}"
45
- print(f"ImageReaderTool downloading from: {file_url}")
46
- try:
47
- resp = requests.get(file_url, timeout=30)
48
- resp.raise_for_status()
49
- # Detect content type for the base64 data URI
50
- content_type = resp.headers.get("Content-Type", "image/png")
51
- image_b64 = base64.b64encode(resp.content).decode("utf-8")
52
- print(f"ImageReaderTool downloaded image ({len(resp.content)} bytes, {content_type}).")
53
- return f"data:{content_type};base64,{image_b64}"
54
- except requests.exceptions.RequestException as e:
55
- error_msg = f"Failed to download image for task {task_id}: {e}"
56
- print(error_msg)
57
- return error_msg
 
 
 
 
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