Spaces:
Build error
Build error
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
import cmath
|
| 2 |
import pytesseract
|
| 3 |
from PIL import Image
|
|
@@ -118,6 +120,39 @@ def get_text_from_image(image_path: str) -> str:
|
|
| 118 |
return f'There is not module installed: {str(e)}'
|
| 119 |
except Exception as e:
|
| 120 |
return f"Error extracting text from image: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
alltools = [
|
| 123 |
wiki_search,
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
import cmath
|
| 4 |
import pytesseract
|
| 5 |
from PIL import Image
|
|
|
|
| 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 |
+
"""
|
| 127 |
+
Download a file from a URL and save it to a temporary location.
|
| 128 |
+
Args:
|
| 129 |
+
url (str): the URL of the file to download.
|
| 130 |
+
filename (str, optional): the name of the file. If not provided, a random name file will be created.
|
| 131 |
+
"""
|
| 132 |
+
try:
|
| 133 |
+
# Parse URL to get filename if not provided
|
| 134 |
+
if not filename:
|
| 135 |
+
path = urlparse(url).path
|
| 136 |
+
filename = os.path.basename(path)
|
| 137 |
+
if not filename:
|
| 138 |
+
filename = f"downloaded_{uuid.uuid4().hex[:8]}"
|
| 139 |
+
|
| 140 |
+
# Create temporary file
|
| 141 |
+
temp_dir = tempfile.gettempdir()
|
| 142 |
+
filepath = os.path.join(temp_dir, filename)
|
| 143 |
+
|
| 144 |
+
# Download the file
|
| 145 |
+
response = requests.get(url, stream=True)
|
| 146 |
+
response.raise_for_status()
|
| 147 |
+
|
| 148 |
+
# Save the file
|
| 149 |
+
with open(filepath, "wb") as f:
|
| 150 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 151 |
+
f.write(chunk)
|
| 152 |
+
|
| 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,
|