TommasoBB commited on
Commit
f660630
·
verified ·
1 Parent(s): 00a50e0

Create tools.py

Browse files
Files changed (1) hide show
  1. tools.py +93 -0
tools.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 __call__(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:
29
+ resp = requests.get(file_url, timeout=30)
30
+ resp.raise_for_status()
31
+ content = resp.text
32
+ print(f"FileReaderTool downloaded {len(content)} chars.")
33
+ return content
34
+ except requests.exceptions.RequestException as e:
35
+ error_msg = f"Failed to download file for task {task_id}: {e}"
36
+ print(error_msg)
37
+ return error_msg
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 __call__(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:
61
+ resp = requests.get(file_url, timeout=30)
62
+ resp.raise_for_status()
63
+ # Detect content type for the base64 data URI
64
+ content_type = resp.headers.get("Content-Type", "image/png")
65
+ image_b64 = base64.b64encode(resp.content).decode("utf-8")
66
+ print(f"ImageReaderTool downloaded image ({len(resp.content)} bytes, {content_type}).")
67
+ return f"data:{content_type};base64,{image_b64}"
68
+ except requests.exceptions.RequestException as e:
69
+ error_msg = f"Failed to download image for task {task_id}: {e}"
70
+ print(error_msg)
71
+ return error_msg
72
+
73
+
74
+ #search web tool
75
+ class WebSearchTool(Tool, DuckDuckGoSearchTool):
76
+ name = "web_search"
77
+ description = "Searches the web for information based on a query string."
78
+ inputs = {
79
+ "query": {
80
+ "type": "string",
81
+ "description": "The query string to search for."
82
+ }
83
+ }
84
+ output_type = "string"
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
+