Download tools.py from Hugalino/agents_course_final_assignment: direct link, hf CLI and curl.
- Browser
- Download file 3.7 kB
-
https://huggingface.co/spaces/Hugalino/agents_course_final_assignment/resolve/main/tools.py
- Command line
-
hf download hf://spaces/Hugalino/agents_course_final_assignment/tools.py
-
curl -L -o tools.py https://huggingface.co/spaces/Hugalino/agents_course_final_assignment/resolve/main/tools.py
3.7 kB
| from smolagents import (DuckDuckGoSearchTool, GoogleSearchTool, | |
| WikipediaSearchTool, VisitWebpageTool, SpeechToTextTool) | |
| from smolagents import Tool | |
| import requests | |
| # Define the FileDownloadTool | |
| 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() | |
| # Save the file to the specified file name | |
| 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}" | |
| # Initialize the download file tool | |
| file_download_tool = FileDownloadTool() | |
| read_text_file_tool = ReadTextFileTool() | |
| # Initialize the DuckDuckGo search tool | |
| search_tool = DuckDuckGoSearchTool(max_results=10) | |
| #Initialize the GoogleSearch tool | |
| google_search_tool = GoogleSearchTool() | |
| google_search_tool.name = "google_search" | |
| # # Initialize Wiki search tool | |
| # wiki_search_tool = WikipediaSearchTool( | |
| # language="en", | |
| # content_type="text" | |
| # ) | |
| # Initialize VisitWebpageTool | |
| visit_web_tool = VisitWebpageTool() | |
| # Initialize SpeechToTextTool | |
| speech_to_text_tool = SpeechToTextTool() |