TommasoBB commited on
Commit
9eb4567
·
verified ·
1 Parent(s): 15dae30

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +14 -26
tools.py CHANGED
@@ -1,28 +1,24 @@
1
  import base64
2
  import requests
3
- from smolagents import DuckDuckGoSearchTool
4
- from smolagents import Tool
 
 
 
 
5
 
6
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
7
 
8
  # Tool to download and read file attachments (text) from the scoring API
9
- class FileReaderTool(Tool):
 
10
  name = "file_reader"
11
- description = "Downloads and reads a text file attachment from the scoring API given a task_id. Returns the file content as text. Use this tool when the question references an attached text file (e.g. .txt, .csv, .json)."
12
- inputs = {
13
- "task_id": {
14
- "type": "string",
15
- "description": "The task_id of the question whose file attachment to download."
16
- }
17
- }
18
- output_type = "string"
19
 
20
  def __init__(self, api_url: str = DEFAULT_API_URL):
21
- super().__init__()
22
  self.api_url = api_url
23
  print("FileReaderTool initialized.")
24
 
25
- def forward(self, task_id: str) -> str:
26
  file_url = f"{self.api_url}/files/{task_id}"
27
  print(f"FileReaderTool downloading from: {file_url}")
28
  try:
@@ -38,23 +34,15 @@ class FileReaderTool(Tool):
38
 
39
 
40
  # Tool to download image attachments and return them as base64
41
- class ImageReaderTool(Tool):
 
42
  name = "image_reader"
43
- description = "Downloads an image file attachment from the scoring API given a task_id. Returns the image as a base64-encoded string. Use this tool when the question references an attached image file (e.g. .png, .jpg, .jpeg, .gif, .webp)."
44
- inputs = {
45
- "task_id": {
46
- "type": "string",
47
- "description": "The task_id of the question whose image attachment to download."
48
- }
49
- }
50
- output_type = "string"
51
 
52
  def __init__(self, api_url: str = DEFAULT_API_URL):
53
- super().__init__()
54
  self.api_url = api_url
55
  print("ImageReaderTool initialized.")
56
 
57
- def forward(self, task_id: str) -> str:
58
  file_url = f"{self.api_url}/files/{task_id}"
59
  print(f"ImageReaderTool downloading from: {file_url}")
60
  try:
@@ -85,9 +73,9 @@ class WebSearchTool( DuckDuckGoSearchTool):
85
  def __init__(self):
86
  super().__init__()
87
  print("WebSearchTool initialized.")
88
- def __call__(self, query: str) -> str:
89
  print(f"WebSearchTool received query (first 50 chars): {query[:50]}...")
90
- result = super().__call__(query)
91
  print(f"WebSearchTool returning result (first 100 chars): {result[:100]}...")
92
  return result
93
 
 
1
  import base64
2
  import requests
3
+ try:
4
+ from smolagents import DuckDuckGoSearchTool
5
+ from smolagents import Tool
6
+ except ImportError:
7
+ DuckDuckGoSearchTool = object
8
+ Tool = object
9
 
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
  # Tool to download and read file attachments (text) from the scoring API
13
+ # Plain class (no Tool inheritance) — called directly, not via CodeAgent
14
+ class FileReaderTool:
15
  name = "file_reader"
 
 
 
 
 
 
 
 
16
 
17
  def __init__(self, api_url: str = DEFAULT_API_URL):
 
18
  self.api_url = api_url
19
  print("FileReaderTool initialized.")
20
 
21
+ def __call__(self, task_id: str) -> str:
22
  file_url = f"{self.api_url}/files/{task_id}"
23
  print(f"FileReaderTool downloading from: {file_url}")
24
  try:
 
34
 
35
 
36
  # Tool to download image attachments and return them as base64
37
+ # Plain class (no Tool inheritance) — called directly, not via CodeAgent
38
+ class ImageReaderTool:
39
  name = "image_reader"
 
 
 
 
 
 
 
 
40
 
41
  def __init__(self, api_url: str = DEFAULT_API_URL):
 
42
  self.api_url = api_url
43
  print("ImageReaderTool initialized.")
44
 
45
+ def __call__(self, task_id: str) -> str:
46
  file_url = f"{self.api_url}/files/{task_id}"
47
  print(f"ImageReaderTool downloading from: {file_url}")
48
  try:
 
73
  def __init__(self):
74
  super().__init__()
75
  print("WebSearchTool initialized.")
76
+ def forward(self, query: str) -> str:
77
  print(f"WebSearchTool received query (first 50 chars): {query[:50]}...")
78
+ result = super().forward(query)
79
  print(f"WebSearchTool returning result (first 100 chars): {result[:100]}...")
80
  return result
81