Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -22,7 +22,6 @@ load_dotenv()
|
|
| 22 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 23 |
|
| 24 |
|
| 25 |
-
@tool
|
| 26 |
def duckduck_websearch(query: str) -> str:
|
| 27 |
"""
|
| 28 |
Performs a web search using the given query, downloads the content of two relevant web pages,
|
|
@@ -54,7 +53,6 @@ def duckduck_websearch(query: str) -> str:
|
|
| 54 |
return cleaned_text
|
| 55 |
|
| 56 |
|
| 57 |
-
@tool
|
| 58 |
def serper_websearch(query: str) -> str:
|
| 59 |
"""
|
| 60 |
Performs a web search using the given query with SERPER Search Engine
|
|
@@ -69,7 +67,6 @@ def serper_websearch(query: str) -> str:
|
|
| 69 |
results = search.run(query)
|
| 70 |
return results
|
| 71 |
|
| 72 |
-
@tool
|
| 73 |
def visit_webpage(url: str) -> str:
|
| 74 |
"""
|
| 75 |
Fetches raw HTML content of a web page.
|
|
@@ -86,7 +83,6 @@ def visit_webpage(url: str) -> str:
|
|
| 86 |
except Exception as e:
|
| 87 |
return f"[ERROR fetching {url}]: {str(e)}"
|
| 88 |
|
| 89 |
-
@tool
|
| 90 |
def wiki_search(query: str) -> str:
|
| 91 |
"""
|
| 92 |
Searches for a Wikipedia articles using the provided query and returns the content of the corresponding Wikipedia pages.
|
|
@@ -102,7 +98,6 @@ def wiki_search(query: str) -> str:
|
|
| 102 |
combined_text = "\n\n".join(doc.page_content for doc in docs)
|
| 103 |
return combined_text
|
| 104 |
|
| 105 |
-
@tool
|
| 106 |
def youtube_viewer(youtube_url: str, question: str) -> str:
|
| 107 |
"""
|
| 108 |
Analyzes a YouTube video from the provided URL and returns an answer
|
|
@@ -130,7 +125,6 @@ def youtube_viewer(youtube_url: str, question: str) -> str:
|
|
| 130 |
)
|
| 131 |
return response.text
|
| 132 |
|
| 133 |
-
@tool
|
| 134 |
def text_splitter(text: str) -> List[str]:
|
| 135 |
"""
|
| 136 |
Splits text into chunks using LangChain's CharacterTextSplitter.
|
|
@@ -144,7 +138,6 @@ def text_splitter(text: str) -> List[str]:
|
|
| 144 |
splitter = CharacterTextSplitter(chunk_size=450, chunk_overlap=10)
|
| 145 |
return splitter.split_text(text)
|
| 146 |
|
| 147 |
-
@tool
|
| 148 |
def read_file(task_id: str) -> str:
|
| 149 |
"""
|
| 150 |
First download the file, then read its content
|
|
@@ -162,7 +155,6 @@ def read_file(task_id: str) -> str:
|
|
| 162 |
with open('temp') as f:
|
| 163 |
return f.read()
|
| 164 |
|
| 165 |
-
@tool
|
| 166 |
def excel_read(task_id: str) -> str:
|
| 167 |
"""
|
| 168 |
First download the excel file, then read its content
|
|
@@ -192,7 +184,6 @@ def excel_read(task_id: str) -> str:
|
|
| 192 |
except Exception as e:
|
| 193 |
return f"Error analyzing Excel file: {str(e)}"
|
| 194 |
|
| 195 |
-
@tool
|
| 196 |
def csv_read(task_id: str) -> str:
|
| 197 |
"""
|
| 198 |
First download the csv file, then read its content
|
|
@@ -223,7 +214,6 @@ def csv_read(task_id: str) -> str:
|
|
| 223 |
return f"Error analyzing CSV file: {str(e)}"
|
| 224 |
|
| 225 |
|
| 226 |
-
@tool
|
| 227 |
def mp3_listen(task_id: str) -> str:
|
| 228 |
"""
|
| 229 |
First download the mp3 file, then listen to it
|
|
@@ -244,7 +234,6 @@ def mp3_listen(task_id: str) -> str:
|
|
| 244 |
return "\n".join(contents)
|
| 245 |
|
| 246 |
|
| 247 |
-
@tool
|
| 248 |
def image_caption(dir: str) -> str:
|
| 249 |
"""
|
| 250 |
Understand the content of the provided image
|
|
@@ -260,7 +249,6 @@ def image_caption(dir: str) -> str:
|
|
| 260 |
return metadata[0].page_content
|
| 261 |
|
| 262 |
|
| 263 |
-
@tool
|
| 264 |
def run_python(code: str):
|
| 265 |
""" Run the given python code
|
| 266 |
|
|
@@ -269,7 +257,6 @@ def run_python(code: str):
|
|
| 269 |
"""
|
| 270 |
return PythonREPLTool().run(code)
|
| 271 |
|
| 272 |
-
@tool
|
| 273 |
def multiply(a: float, b: float) -> float:
|
| 274 |
"""
|
| 275 |
Multiply two numbers.
|
|
@@ -283,7 +270,6 @@ def multiply(a: float, b: float) -> float:
|
|
| 283 |
"""
|
| 284 |
return a * b
|
| 285 |
|
| 286 |
-
@tool
|
| 287 |
def add(a: float, b: float) -> float:
|
| 288 |
"""
|
| 289 |
Add two numbers.
|
|
@@ -297,7 +283,6 @@ def add(a: float, b: float) -> float:
|
|
| 297 |
"""
|
| 298 |
return a + b
|
| 299 |
|
| 300 |
-
@tool
|
| 301 |
def subtract(a: float, b: float) -> float:
|
| 302 |
"""
|
| 303 |
Subtract two numbers.
|
|
@@ -311,7 +296,6 @@ def subtract(a: float, b: float) -> float:
|
|
| 311 |
"""
|
| 312 |
return a - b
|
| 313 |
|
| 314 |
-
@tool
|
| 315 |
def divide(a: float, b: float) -> float:
|
| 316 |
"""Divide two numbers.
|
| 317 |
|
|
|
|
| 22 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 23 |
|
| 24 |
|
|
|
|
| 25 |
def duckduck_websearch(query: str) -> str:
|
| 26 |
"""
|
| 27 |
Performs a web search using the given query, downloads the content of two relevant web pages,
|
|
|
|
| 53 |
return cleaned_text
|
| 54 |
|
| 55 |
|
|
|
|
| 56 |
def serper_websearch(query: str) -> str:
|
| 57 |
"""
|
| 58 |
Performs a web search using the given query with SERPER Search Engine
|
|
|
|
| 67 |
results = search.run(query)
|
| 68 |
return results
|
| 69 |
|
|
|
|
| 70 |
def visit_webpage(url: str) -> str:
|
| 71 |
"""
|
| 72 |
Fetches raw HTML content of a web page.
|
|
|
|
| 83 |
except Exception as e:
|
| 84 |
return f"[ERROR fetching {url}]: {str(e)}"
|
| 85 |
|
|
|
|
| 86 |
def wiki_search(query: str) -> str:
|
| 87 |
"""
|
| 88 |
Searches for a Wikipedia articles using the provided query and returns the content of the corresponding Wikipedia pages.
|
|
|
|
| 98 |
combined_text = "\n\n".join(doc.page_content for doc in docs)
|
| 99 |
return combined_text
|
| 100 |
|
|
|
|
| 101 |
def youtube_viewer(youtube_url: str, question: str) -> str:
|
| 102 |
"""
|
| 103 |
Analyzes a YouTube video from the provided URL and returns an answer
|
|
|
|
| 125 |
)
|
| 126 |
return response.text
|
| 127 |
|
|
|
|
| 128 |
def text_splitter(text: str) -> List[str]:
|
| 129 |
"""
|
| 130 |
Splits text into chunks using LangChain's CharacterTextSplitter.
|
|
|
|
| 138 |
splitter = CharacterTextSplitter(chunk_size=450, chunk_overlap=10)
|
| 139 |
return splitter.split_text(text)
|
| 140 |
|
|
|
|
| 141 |
def read_file(task_id: str) -> str:
|
| 142 |
"""
|
| 143 |
First download the file, then read its content
|
|
|
|
| 155 |
with open('temp') as f:
|
| 156 |
return f.read()
|
| 157 |
|
|
|
|
| 158 |
def excel_read(task_id: str) -> str:
|
| 159 |
"""
|
| 160 |
First download the excel file, then read its content
|
|
|
|
| 184 |
except Exception as e:
|
| 185 |
return f"Error analyzing Excel file: {str(e)}"
|
| 186 |
|
|
|
|
| 187 |
def csv_read(task_id: str) -> str:
|
| 188 |
"""
|
| 189 |
First download the csv file, then read its content
|
|
|
|
| 214 |
return f"Error analyzing CSV file: {str(e)}"
|
| 215 |
|
| 216 |
|
|
|
|
| 217 |
def mp3_listen(task_id: str) -> str:
|
| 218 |
"""
|
| 219 |
First download the mp3 file, then listen to it
|
|
|
|
| 234 |
return "\n".join(contents)
|
| 235 |
|
| 236 |
|
|
|
|
| 237 |
def image_caption(dir: str) -> str:
|
| 238 |
"""
|
| 239 |
Understand the content of the provided image
|
|
|
|
| 249 |
return metadata[0].page_content
|
| 250 |
|
| 251 |
|
|
|
|
| 252 |
def run_python(code: str):
|
| 253 |
""" Run the given python code
|
| 254 |
|
|
|
|
| 257 |
"""
|
| 258 |
return PythonREPLTool().run(code)
|
| 259 |
|
|
|
|
| 260 |
def multiply(a: float, b: float) -> float:
|
| 261 |
"""
|
| 262 |
Multiply two numbers.
|
|
|
|
| 270 |
"""
|
| 271 |
return a * b
|
| 272 |
|
|
|
|
| 273 |
def add(a: float, b: float) -> float:
|
| 274 |
"""
|
| 275 |
Add two numbers.
|
|
|
|
| 283 |
"""
|
| 284 |
return a + b
|
| 285 |
|
|
|
|
| 286 |
def subtract(a: float, b: float) -> float:
|
| 287 |
"""
|
| 288 |
Subtract two numbers.
|
|
|
|
| 296 |
"""
|
| 297 |
return a - b
|
| 298 |
|
|
|
|
| 299 |
def divide(a: float, b: float) -> float:
|
| 300 |
"""Divide two numbers.
|
| 301 |
|