JPM34 commited on
Commit
6c9d559
·
1 Parent(s): c49b8ba

Update tools

Browse files
Files changed (3) hide show
  1. tools_audio.py +2 -0
  2. tools_doc.py +63 -27
  3. 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(url: str, filename: Optional[str] = None) -> str:
41
- """
42
- Download a file from a URL and save it to a temporary location.
43
-
44
  Args:
45
- url: The URL to download from
46
- filename: Optional filename, will generate one based on URL if not provided
47
-
48
  Returns:
49
- Path to the downloaded file
50
  """
 
51
  try:
52
- # Parse URL to get filename if not provided
53
- if not filename:
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
- filename = f"downloaded_{uuid.uuid4().hex[:8]}"
61
 
62
- # Create temporary file
63
- temp_dir = tempfile.gettempdir()
64
- filepath = os.path.join(temp_dir, filename)
 
 
 
 
 
65
 
66
- # Download the file
67
- response = requests.get(url, stream=True)
68
- response.raise_for_status()
69
 
70
- # Save the file
71
- with open(filepath, "wb") as f:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  for chunk in response.iter_content(chunk_size=8192):
73
  f.write(chunk)
74
 
75
- return f"File downloaded to {filepath}. You can now process this file."
 
 
 
 
 
 
 
 
 
 
76
  except Exception as e:
77
- return f"Error downloading file: {str(e)}"
 
 
 
 
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