Update tools.py
Browse files
tools.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
from smolagents import (
|
2 |
DuckDuckGoSearchTool,
|
3 |
PythonInterpreterTool,
|
@@ -7,9 +9,62 @@ from smolagents import (
|
|
7 |
WikipediaSearchTool,
|
8 |
)
|
9 |
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
PythonInterpreterTool(),
|
13 |
WikipediaSearchTool(),
|
14 |
VisitWebpageTool(),
|
15 |
-
|
|
|
|
|
|
|
|
1 |
+
from typing import Any, List
|
2 |
+
|
3 |
from smolagents import (
|
4 |
DuckDuckGoSearchTool,
|
5 |
PythonInterpreterTool,
|
|
|
9 |
WikipediaSearchTool,
|
10 |
)
|
11 |
|
12 |
+
import pytesseract
|
13 |
+
from PIL import Image
|
14 |
+
|
15 |
+
class ReadFileTool(Tool):
|
16 |
+
"""
|
17 |
+
Tool to read a file and return its content.
|
18 |
+
Args:
|
19 |
+
file_path (str): Path to the file to read.
|
20 |
+
Returns:
|
21 |
+
str: Content of the file or error message.
|
22 |
+
"""
|
23 |
+
|
24 |
+
name = "read_file"
|
25 |
+
description = "Reads a file and returns its content"
|
26 |
+
inputs = {
|
27 |
+
"file_path": {"type": "string", "description": "Path to the file to read"},
|
28 |
+
}
|
29 |
+
output_type = "string"
|
30 |
+
|
31 |
+
def forward(self, file_path: str) -> str:
|
32 |
+
try:
|
33 |
+
with open(file_path, "r") as file:
|
34 |
+
return file.read()
|
35 |
+
except Exception as e:
|
36 |
+
return f"Error reading file: {str(e)}"
|
37 |
+
|
38 |
+
|
39 |
+
class ExtractTextFromImageTool(Tool):
|
40 |
+
name = "extract_text_from_image"
|
41 |
+
description = "Extracts text from an image using pytesseract"
|
42 |
+
inputs = {
|
43 |
+
"image_path": {"type": "string", "description": "Path to the image file"},
|
44 |
+
}
|
45 |
+
output_type = "string"
|
46 |
+
|
47 |
+
def forward(self, image_path: str) -> str:
|
48 |
+
try:
|
49 |
+
image = Image.open(image_path)
|
50 |
+
text = pytesseract.image_to_string(image)
|
51 |
+
return text
|
52 |
+
except Exception as e:
|
53 |
+
return f"Error extracting text from image: {str(e)}"
|
54 |
+
|
55 |
+
|
56 |
+
def get_tools() -> List[Tool]:
|
57 |
+
"""
|
58 |
+
Returns a list of available tools for the agent.
|
59 |
+
Returns:
|
60 |
+
List[Tool]: List of initialized tool instances.
|
61 |
+
"""
|
62 |
+
tools = [
|
63 |
+
DuckDuckGoSearchTool(),
|
64 |
PythonInterpreterTool(),
|
65 |
WikipediaSearchTool(),
|
66 |
VisitWebpageTool(),
|
67 |
+
ReadFileTool(),
|
68 |
+
ExtractTextFromImageTool(),
|
69 |
+
]
|
70 |
+
return tools
|