Update tools
Browse files- tools_audio.py +2 -0
- tools_doc.py +63 -27
- tools_video.py +1 -0
tools_audio.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
# Audio Transcription Tool
|
|
|
|
|
|
|
| 2 |
from google import genai
|
| 3 |
from google.genai import types
|
| 4 |
from langchain_core.tools import tool
|
|
|
|
| 1 |
# Audio Transcription Tool
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
from google import genai
|
| 5 |
from google.genai import types
|
| 6 |
from langchain_core.tools import tool
|
tools_doc.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import requests
|
| 3 |
import tempfile
|
|
|
|
| 4 |
|
| 5 |
from langchain_core.tools import tool
|
| 6 |
|
| 7 |
# from smolagents import tool
|
| 8 |
-
from typing import Optional
|
| 9 |
from urllib.parse import urlparse
|
| 10 |
|
| 11 |
|
|
@@ -36,45 +38,79 @@ def save_and_read_file(content: str, filename: Optional[str] = None) -> str:
|
|
| 36 |
return f"File saved to {filepath}. You can read this file to process its contents."
|
| 37 |
|
| 38 |
|
|
|
|
| 39 |
@tool
|
| 40 |
-
def download_file_from_url(
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
Args:
|
| 45 |
-
url:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
Returns:
|
| 49 |
-
|
| 50 |
"""
|
|
|
|
| 51 |
try:
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
path = urlparse(url).path
|
| 55 |
-
filename = os.path.basename(path)
|
| 56 |
-
if not filename:
|
| 57 |
-
# Generate a random name if we couldn't extract one
|
| 58 |
-
import uuid
|
| 59 |
|
| 60 |
-
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
for chunk in response.iter_content(chunk_size=8192):
|
| 73 |
f.write(chunk)
|
| 74 |
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
except Exception as e:
|
| 77 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
|
| 80 |
@tool
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
import requests
|
| 4 |
import tempfile
|
| 5 |
+
import uuid
|
| 6 |
|
| 7 |
from langchain_core.tools import tool
|
| 8 |
|
| 9 |
# from smolagents import tool
|
| 10 |
+
from typing import Optional, Dict, Union
|
| 11 |
from urllib.parse import urlparse
|
| 12 |
|
| 13 |
|
|
|
|
| 38 |
return f"File saved to {filepath}. You can read this file to process its contents."
|
| 39 |
|
| 40 |
|
| 41 |
+
# File Download Tool
|
| 42 |
@tool
|
| 43 |
+
def download_file_from_url(
|
| 44 |
+
url: str, directory: str
|
| 45 |
+
) -> Dict[str, Union[str, None]]:
|
| 46 |
+
"""Downloads a file from a URL and saves it to a directory.
|
| 47 |
Args:
|
| 48 |
+
url (str): the URL to download the file from.
|
| 49 |
+
directory (str): the directory to save the file to.
|
|
|
|
| 50 |
Returns:
|
| 51 |
+
Dict[str, Union[str, None]]: A dictionary containing the file type and path.
|
| 52 |
"""
|
| 53 |
+
|
| 54 |
try:
|
| 55 |
+
response = requests.get(url, stream=True, timeout=10)
|
| 56 |
+
response.raise_for_status()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
content_type = response.headers.get("content-type", "").lower()
|
| 59 |
|
| 60 |
+
# Try to get filename from headers
|
| 61 |
+
filename = None
|
| 62 |
+
cd = response.headers.get("content-disposition", "")
|
| 63 |
+
match = re.search(r"filename\*=UTF-8\'\'(.+)", cd) or re.search(
|
| 64 |
+
r'filename="?([^"]+)"?', cd
|
| 65 |
+
)
|
| 66 |
+
if match:
|
| 67 |
+
filename = match.group(1)
|
| 68 |
|
| 69 |
+
# If not in headers, try URL
|
| 70 |
+
if not filename:
|
| 71 |
+
filename = os.path.basename(url.split("?")[0])
|
| 72 |
|
| 73 |
+
# Fallback to generated filename
|
| 74 |
+
if not filename:
|
| 75 |
+
extension = {
|
| 76 |
+
"image/jpeg": ".jpg",
|
| 77 |
+
"image/png": ".png",
|
| 78 |
+
"image/gif": ".gif",
|
| 79 |
+
"audio/wav": ".wav",
|
| 80 |
+
"audio/mpeg": ".mp3",
|
| 81 |
+
"video/mp4": ".mp4",
|
| 82 |
+
"text/plain": ".txt",
|
| 83 |
+
"text/csv": ".csv",
|
| 84 |
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": ".xlsx",
|
| 85 |
+
"application/vnd.ms-excel": ".xls",
|
| 86 |
+
"application/octet-stream": ".bin",
|
| 87 |
+
}.get(content_type, ".bin")
|
| 88 |
+
filename = f"downloaded_{uuid.uuid4().hex[:8]}{extension}"
|
| 89 |
+
|
| 90 |
+
os.makedirs(directory, exist_ok=True)
|
| 91 |
+
file_path = os.path.join(directory, filename)
|
| 92 |
+
|
| 93 |
+
with open(file_path, "wb") as f:
|
| 94 |
for chunk in response.iter_content(chunk_size=8192):
|
| 95 |
f.write(chunk)
|
| 96 |
|
| 97 |
+
# shutil.copy(file_path, os.getcwd())
|
| 98 |
+
|
| 99 |
+
if os.path.exists(file_path) and os.path.getsize(file_path) > 0:
|
| 100 |
+
return {"type": content_type, "path": file_path}
|
| 101 |
+
else:
|
| 102 |
+
return {
|
| 103 |
+
"type": "error",
|
| 104 |
+
"path": None,
|
| 105 |
+
"error": "Failed to save file",
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
except Exception as e:
|
| 109 |
+
return {
|
| 110 |
+
"type": "error",
|
| 111 |
+
"path": None,
|
| 112 |
+
"error": f"Error downloading file: {str(e)}",
|
| 113 |
+
}
|
| 114 |
|
| 115 |
|
| 116 |
@tool
|
tools_video.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import imageio
|
| 2 |
import os
|
| 3 |
import re
|
|
|
|
| 4 |
import yt_dlp
|
| 5 |
|
| 6 |
from datetime import timedelta
|
|
|
|
| 1 |
import imageio
|
| 2 |
import os
|
| 3 |
import re
|
| 4 |
+
import tempfile
|
| 5 |
import yt_dlp
|
| 6 |
|
| 7 |
from datetime import timedelta
|