Spaces:
Build error
Build error
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -3,8 +3,12 @@ import requests
|
|
| 3 |
import cmath
|
| 4 |
import pytesseract
|
| 5 |
from PIL import Image
|
| 6 |
-
from
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
## MATH TOOLS
|
| 10 |
@tool
|
|
@@ -52,7 +56,6 @@ def divide(a: float, b: float) -> float:
|
|
| 52 |
raise ValueError("Cannot divided by zero.")
|
| 53 |
return a / b
|
| 54 |
|
| 55 |
-
|
| 56 |
@tool
|
| 57 |
def modulus(a: int, b: int) -> int:
|
| 58 |
"""
|
|
@@ -87,40 +90,8 @@ def square_root(a: float) -> float | complex:
|
|
| 87 |
return cmath.sqrt(a)
|
| 88 |
|
| 89 |
|
| 90 |
-
##
|
| 91 |
-
|
| 92 |
-
def wiki_search(query: str) -> str:
|
| 93 |
-
"""Search Wikipedia for a query and return maximum 2 results.
|
| 94 |
-
Args:
|
| 95 |
-
query: The search query."""
|
| 96 |
-
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 97 |
-
formatted_search_docs = "\n\n---\n\n".join(
|
| 98 |
-
[
|
| 99 |
-
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 100 |
-
for doc in search_docs
|
| 101 |
-
]
|
| 102 |
-
)
|
| 103 |
-
return {"wiki_results": formatted_search_docs}
|
| 104 |
-
|
| 105 |
-
## OCR
|
| 106 |
-
@tool
|
| 107 |
-
def get_text_from_image(image_path: str) -> str:
|
| 108 |
-
"""
|
| 109 |
-
Extract text from an image using OCR pytesseract.
|
| 110 |
-
Args:
|
| 111 |
-
image_path (str): the path to the image file.
|
| 112 |
-
"""
|
| 113 |
-
try:
|
| 114 |
-
# Open the image
|
| 115 |
-
image = Image.open(image_path)
|
| 116 |
-
# Extract text from the image
|
| 117 |
-
text = pytesseract.image_to_string(image)
|
| 118 |
-
return f"Extracted text from image:\n\n{text}"
|
| 119 |
-
except ModuleNotFoundError as e:
|
| 120 |
-
return f'There is not module installed: {str(e)}'
|
| 121 |
-
except Exception as e:
|
| 122 |
-
return f"Error extracting text from image: {str(e)}"
|
| 123 |
-
## FILE TOOLS
|
| 124 |
@tool
|
| 125 |
def download_file_from_url(url: str, filename: Optional[str] = None) -> str:
|
| 126 |
"""
|
|
@@ -153,15 +124,3 @@ def download_file_from_url(url: str, filename: Optional[str] = None) -> str:
|
|
| 153 |
return f"File downloaded to {filepath}. You can read this file to process its contents."
|
| 154 |
except Exception as e:
|
| 155 |
return f"Error downloading file: {str(e)}"
|
| 156 |
-
|
| 157 |
-
alltools = [
|
| 158 |
-
wiki_search,
|
| 159 |
-
get_text_from_image,
|
| 160 |
-
multiply,
|
| 161 |
-
add,
|
| 162 |
-
subtract,
|
| 163 |
-
divide,
|
| 164 |
-
modulus,
|
| 165 |
-
power,
|
| 166 |
-
square_root
|
| 167 |
-
]
|
|
|
|
| 3 |
import cmath
|
| 4 |
import pytesseract
|
| 5 |
from PIL import Image
|
| 6 |
+
from smolagents import tool, FinalAnswerTool, DuckDuckGoSearchTool, ToolCollection
|
| 7 |
+
|
| 8 |
+
DuckDuckGoSearchTool = DuckDuckGoSearchTool()
|
| 9 |
+
FinalAnswer = FinalAnswer()
|
| 10 |
+
|
| 11 |
+
image_tool_collection = ToolCollection.from_hub("huggingface-tools/diffusion-tools-6630bb19a942c2306a2cdb6f")
|
| 12 |
|
| 13 |
## MATH TOOLS
|
| 14 |
@tool
|
|
|
|
| 56 |
raise ValueError("Cannot divided by zero.")
|
| 57 |
return a / b
|
| 58 |
|
|
|
|
| 59 |
@tool
|
| 60 |
def modulus(a: int, b: int) -> int:
|
| 61 |
"""
|
|
|
|
| 90 |
return cmath.sqrt(a)
|
| 91 |
|
| 92 |
|
| 93 |
+
## IO TOOLS
|
| 94 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
@tool
|
| 96 |
def download_file_from_url(url: str, filename: Optional[str] = None) -> str:
|
| 97 |
"""
|
|
|
|
| 124 |
return f"File downloaded to {filepath}. You can read this file to process its contents."
|
| 125 |
except Exception as e:
|
| 126 |
return f"Error downloading file: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|