thatting commited on
Commit
4ed60bf
·
verified ·
1 Parent(s): 6dc12c4

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +74 -0
tools.py CHANGED
@@ -21,6 +21,80 @@ def tool_reverse_string(text: str) -> str:
21
  """
22
  return text[::-1]
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  #class FinalAnswerTool(Tool):
25
  # name = "final_answer"
26
  # description = "Provides a final answer to the given problem."
 
21
  """
22
  return text[::-1]
23
 
24
+
25
+ @tool
26
+ def tool_read_files(filepath: str) -> str:
27
+ """
28
+ Downloads a .py or .xlsx file from a remote URL and returns its contents as plain text.
29
+ Raises a recoverable exception if the file does not end with .py or .xlsx.
30
+ Args:
31
+ filepath: The path to the Python (.py) or Excel (.xlsx) file.
32
+ """
33
+ root_url = "https://agents-course-unit4-scoring.hf.space/files/"
34
+ # Strip the file extension from the url before downloading
35
+ import os
36
+ base, ext = os.path.splitext(filepath)
37
+ url = root_url + base
38
+
39
+ if filepath.endswith('.py'):
40
+ response = requests.get(url)
41
+ if response.status_code != 200:
42
+ raise Exception(f"Recoverable: Failed to download file from {url}")
43
+ return response.text
44
+
45
+ elif filepath.endswith('.xlsx'):
46
+ response = requests.get(url)
47
+ if response.status_code != 200:
48
+ raise Exception(f"Recoverable: Failed to download file from {url}")
49
+
50
+ wb = openpyxl.load_workbook(io.BytesIO(response.content), data_only=True)
51
+ result = []
52
+ for sheet in wb.worksheets:
53
+ result.append(f"# Sheet: {sheet.title}")
54
+ for row in sheet.iter_rows(values_only=True):
55
+ result.append(','.join([str(cell) if cell is not None else '' for cell in row]))
56
+ return '\n'.join(result)
57
+
58
+ else:
59
+ raise Exception("Recoverable: Only .py and .xlsx files can be read with this tool.")
60
+
61
+
62
+ @tool
63
+ def tool_download_image(filepath: str) -> str:
64
+ """
65
+ Downloads an image file (.png, .jpg, .jpeg) from a remote URL and returns useful information about the image.
66
+ This includes the image URL and basic metadata like dimensions and format.
67
+ Raises a recoverable exception if the file is not a supported image type.
68
+ Args:
69
+ filepath: The path to the image file.
70
+ """
71
+ root_url = "https://agents-course-unit4-scoring.hf.space/files/"
72
+ base, ext = os.path.splitext(filepath)
73
+ url = root_url + base
74
+
75
+ if ext.lower() in ['.png', '.jpg', '.jpeg']:
76
+ response = requests.get(url)
77
+ if response.status_code != 200:
78
+ raise Exception(f"Recoverable: Failed to download image from {url}")
79
+
80
+ # Get image metadata using Pillow
81
+ try:
82
+
83
+ img = Image.open(io.BytesIO(response.content))
84
+ width, height = img.size
85
+ format = img.format
86
+ mode = img.mode
87
+
88
+ # Return useful information about the image
89
+ return f"Image URL: {url}\nFormat: {format}\nDimensions: {width}x{height}\nMode: {mode}"
90
+ except ImportError:
91
+ # Fallback if PIL is not available
92
+ content_type = response.headers.get('Content-Type', 'unknown')
93
+ content_length = response.headers.get('Content-Length', 'unknown')
94
+ return f"Content-Type: {content_type}\nSize: {content_length} bytes"
95
+ else:
96
+ raise Exception("Recoverable: Only .png, .jpg, and .jpeg files can be processed with this tool.")
97
+
98
  #class FinalAnswerTool(Tool):
99
  # name = "final_answer"
100
  # description = "Provides a final answer to the given problem."