|
|
from smolagents import (DuckDuckGoSearchTool, GoogleSearchTool, |
|
|
WikipediaSearchTool, VisitWebpageTool, SpeechToTextTool) |
|
|
from smolagents import Tool |
|
|
import requests |
|
|
|
|
|
|
|
|
class FileDownloadTool(Tool): |
|
|
name = "download_question_file_tool" |
|
|
description = """ |
|
|
This tool downloads the file associated with the question defined by the task_id input and save it as a local file |
|
|
It returns the downloaded file name""" |
|
|
|
|
|
inputs = { |
|
|
"task_id": { |
|
|
"type": "string", |
|
|
"description": "The task ID to be used to retrieve the file", |
|
|
}, |
|
|
"file_name": { |
|
|
"type": "string", |
|
|
"description": "The file name to save as", |
|
|
"nullable": True |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
output_type = "string" |
|
|
|
|
|
def forward(self, task_id: str, file_name: str=""): |
|
|
if file_name: |
|
|
print(f"Downloading file `{file_name}` for task {task_id}") |
|
|
try: |
|
|
file_url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}" |
|
|
response = requests.get(file_url, timeout=15) |
|
|
response.raise_for_status() |
|
|
|
|
|
|
|
|
with open(file_name, 'wb') as file: |
|
|
file.write(response.content) |
|
|
|
|
|
print(f"File `{file_name}` downloaded successfully.") |
|
|
return file_name |
|
|
except requests.exceptions.RequestException as e: |
|
|
print(f"Error downloading file: {e}") |
|
|
return f"Error downloading file: {e}", None |
|
|
except Exception as e: |
|
|
print(f"An unexpected error occurred during downloading file: {e}") |
|
|
return f"An unexpected error occurred during downloading file: {e}", None |
|
|
else: |
|
|
print(f"There is no file to download for task {task_id}") |
|
|
return f"There is no file to download for task {task_id}", None |
|
|
|
|
|
class ReadTextFileTool(Tool): |
|
|
name = "read_text_file_tool" |
|
|
description = """ |
|
|
This tool open and read the text file given the file name as the input |
|
|
It returns the file content as a string""" |
|
|
|
|
|
inputs = { |
|
|
"file_name": { |
|
|
"type": "string", |
|
|
"description": "The file name to read", |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
output_type = "string" |
|
|
|
|
|
def forward(self, file_name:str)->str: |
|
|
""" |
|
|
This function opens an text file and returns its content as a string. |
|
|
|
|
|
:param file_name: The name of the text file to be opened. |
|
|
:return: The content of the text file as a string. |
|
|
""" |
|
|
try: |
|
|
with open(file_name, 'r') as file: |
|
|
content = file.read() |
|
|
return content |
|
|
except FileNotFoundError: |
|
|
print(f"File `{file_name}` not found.") |
|
|
return f"File `{file_name}` not found.", None |
|
|
except Exception as e: |
|
|
print(f"An error occurred while opening the file: {e}") |
|
|
return f"An error occurred while opening the file: {e}" |
|
|
|
|
|
|
|
|
|
|
|
file_download_tool = FileDownloadTool() |
|
|
|
|
|
read_text_file_tool = ReadTextFileTool() |
|
|
|
|
|
|
|
|
search_tool = DuckDuckGoSearchTool(max_results=10) |
|
|
|
|
|
|
|
|
google_search_tool = GoogleSearchTool() |
|
|
google_search_tool.name = "google_search" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
visit_web_tool = VisitWebpageTool() |
|
|
|
|
|
|
|
|
speech_to_text_tool = SpeechToTextTool() |