Spaces:
Runtime error
Runtime error
Update helper.py
Browse files
helper.py
CHANGED
|
@@ -96,7 +96,7 @@ def get_travily_api_search_tool(tavily_api_key: str) -> Tool:
|
|
| 96 |
import requests
|
| 97 |
from langchain.tools import Tool
|
| 98 |
import os
|
| 99 |
-
|
| 100 |
def download_file_from_url(url: str, local_filename: str = None) -> str | None:
|
| 101 |
"""
|
| 102 |
Downloads a file from a given URL and saves it locally.
|
|
@@ -169,7 +169,60 @@ file_download_tool = Tool(
|
|
| 169 |
""",
|
| 170 |
func=download_file_from_url,
|
| 171 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
import speech_recognition as sr
|
| 175 |
from pydub import AudioSegment
|
|
|
|
| 96 |
import requests
|
| 97 |
from langchain.tools import Tool
|
| 98 |
import os
|
| 99 |
+
'''
|
| 100 |
def download_file_from_url(url: str, local_filename: str = None) -> str | None:
|
| 101 |
"""
|
| 102 |
Downloads a file from a given URL and saves it locally.
|
|
|
|
| 169 |
""",
|
| 170 |
func=download_file_from_url,
|
| 171 |
)
|
| 172 |
+
'''
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
def download_and_save_file(args: dict) -> str:
|
| 176 |
+
"""
|
| 177 |
+
Downloads a file from a given URL and saves it to a specified local filename.
|
| 178 |
+
Useful for downloading binary files like spreadsheets, images, or archives that
|
| 179 |
+
need to be processed by other tools locally (e.g., python_repl).
|
| 180 |
+
|
| 181 |
+
**Input:** A JSON string with 'url' and 'local_filename' keys.
|
| 182 |
+
Example: {"url": "https://example.com/data.xlsx", "local_filename": "data.xlsx"}
|
| 183 |
+
|
| 184 |
+
**Output:** Returns a success message with the local filename, or an error message.
|
| 185 |
+
"""
|
| 186 |
+
try:
|
| 187 |
+
# Ensure the input is parsed if it comes as a string (common from LLMs)
|
| 188 |
+
if isinstance(args, str):
|
| 189 |
+
import json
|
| 190 |
+
args = json.loads(args)
|
| 191 |
+
|
| 192 |
+
url = args.get("url")
|
| 193 |
+
local_filename = args.get("local_filename")
|
| 194 |
+
|
| 195 |
+
if not url or not local_filename:
|
| 196 |
+
return "Error: Both 'url' and 'local_filename' must be provided."
|
| 197 |
+
|
| 198 |
+
response = requests.get(url, stream=True, timeout=30)
|
| 199 |
+
response.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx)
|
| 200 |
+
|
| 201 |
+
# Ensure the directory exists if local_filename includes a path
|
| 202 |
+
os.makedirs(os.path.dirname(local_filename) or '.', exist_ok=True)
|
| 203 |
|
| 204 |
+
with open(local_filename, 'wb') as f:
|
| 205 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 206 |
+
f.write(chunk)
|
| 207 |
+
|
| 208 |
+
return f"File downloaded successfully to {local_filename}"
|
| 209 |
+
|
| 210 |
+
except requests.exceptions.RequestException as e:
|
| 211 |
+
return f"Error downloading file from {url}: {e}"
|
| 212 |
+
except Exception as e:
|
| 213 |
+
return f"An unexpected error occurred: {e}"
|
| 214 |
+
|
| 215 |
+
# Add this tool to your list of tools
|
| 216 |
+
file_saver_tool = Tool(
|
| 217 |
+
name="file_saver",
|
| 218 |
+
description="""
|
| 219 |
+
Downloads a file from a URL and saves it to a specified local filename.
|
| 220 |
+
Input must be a JSON string with 'url' and 'local_filename' keys.
|
| 221 |
+
Use this for downloading binary files like Excel sheets (.xlsx), images, or PDFs.
|
| 222 |
+
Example: '{"url": "https://example.com/data.xlsx", "local_filename": "my_data.xlsx"}'
|
| 223 |
+
""",
|
| 224 |
+
func=download_and_save_file,
|
| 225 |
+
)
|
| 226 |
|
| 227 |
import speech_recognition as sr
|
| 228 |
from pydub import AudioSegment
|