File size: 3,700 Bytes
05016d1
 
79691da
 
 
 
41f673f
79691da
 
 
 
 
 
 
 
 
 
 
 
 
64729d2
79691da
 
 
 
 
 
 
355fc13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79691da
ba55a3a
 
 
 
 
 
 
 
 
 
 
 
 
35dbd23
 
 
ba55a3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79691da
 
4ad2468
ba55a3a
 
4ad2468
d34ba41
 
 
a01c28c
ef639b8
 
eb403e1
 
 
 
 
898ce40
 
05016d1
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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()