Spaces:
Sleeping
Sleeping
File size: 12,335 Bytes
3ffdb34 3fd1fbf dc6419c d74a4bd d9e880e d74a4bd 0b95002 3ffdb34 3d0183f 3ffdb34 3d0183f 3ffdb34 3d0183f 3ffdb34 3d0183f 3ffdb34 3fd1fbf 3ffdb34 bdd9f7e 3ffdb34 bdd9f7e 3ffdb34 bdd9f7e 3ffdb34 bdd9f7e 3ffdb34 bdd9f7e 6c0615e 3ffdb34 6c0615e 3ffdb34 6c0615e 056177a bdd9f7e 3ffdb34 488ea4c 3ffdb34 3fd1fbf 6c0615e 3fd1fbf 6c0615e 3fd1fbf bdd9f7e 6c0615e d74a4bd 0b95002 d74a4bd 0b95002 d74a4bd 0b95002 1a2ad31 0b95002 d74a4bd 0b95002 d74a4bd 0b95002 d74a4bd 0b95002 d74a4bd 0b95002 d74a4bd 0b95002 d74a4bd 0b95002 d74a4bd 0b95002 d74a4bd 0b95002 d74a4bd 0b95002 d74a4bd 0b95002 d74a4bd d9e880e 0b95002 d74a4bd 0b95002 d74a4bd 0b95002 d74a4bd 3ffdb34 d74a4bd d9e880e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.document_loaders import WikipediaLoader
from langchain_community.document_loaders import ArxivLoader
from langchain_community.vectorstores import SupabaseVectorStore
from langchain_core.messages import SystemMessage, HumanMessage
from langchain_core.tools import tool
from langchain.tools.retriever import create_retriever_tool
from langchain_community.document_loaders import YoutubeLoader
from langchain_community.document_loaders.youtube import TranscriptFormat
from langchain_tavily import TavilySearch
import base64
from groq import Groq
import os
import pandas as pd
import requests
from io import BytesIO
import sys
import io
import traceback
import requests
import subprocess
import tempfile
import traceback
DEFAULT_File_URL = "https://agents-course-unit4-scoring.hf.space/files/"
@tool
def add(a: int, b:int) -> int:
""" add two integer
Args:
a: first integer
b: second integer
"""
print("add")
return a + b
@tool
def substract(a: int, b:int) -> int:
""" substract two integer
Args:
a : first integer
b : second integer
"""
print("substract")
return a - b
@tool
def multiply(a: int, b: int) -> int:
"""multiply two integer
Args:
a: first integer
b: second integer
"""
print ("multiply")
return a * b
@tool
def divide(a: int, b: int) -> int:
""" divide two integer
args:
a: first integer
b: second integer
"""
print ("divide")
return a / b
@tool
def modulus(a: int, b: int) -> int:
"""
Get the modulus of two numbers.
Args:
a (int): the first number
b (int): the second number
"""
return a % b
@tool
def wikiSearch1(searchQuery:str) -> str:
""" search wikipedia to get three matching results
args:
searchQuery: the search query
"""
try:
print("wiki search")
search_results = WikipediaLoader(query=searchQuery, load_max_docs=3).load()
formatted_results = "\n\n--\n\n".join(
[
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page","")}"/>\n{doc.page_content}\n</Document>'
for doc in search_results
])
return {"wiki_results": formatted_results}
except Exception as e:
print(f"Error getting wikisearch: {str(e)}")
return f"Error getting wikisearch: {str(e)}"
@tool
def wikiSearch(query: str) -> str:
"""
Search Wikipedia and return up to 3 summaries.
Args:
query (str): The Wikipedia search query.
Returns:
str: Formatted summaries of matching pages.
"""
try:
# Load up to 3 results from Wikipedia
docs = WikipediaLoader(query=query, load_max_docs=3).load()
if not docs:
return "No Wikipedia results found."
# Format results
results = []
for doc in docs:
title = doc.metadata.get("title", "Unknown Title")
source = doc.metadata.get("source", "")
content = doc.page_content.strip()
results.append(
f"Title: {title}\nSource: {source}\n\nSummary:\n{content}"
)
return "\n\n---\n\n".join(results)
except Exception as e:
return f"Error searching Wikipedia: {str(e)}"
@tool
def arxivSearch1(searchQuery:str) -> str:
"""
search arxiv to get three matching results
args:
searchQuery: the search query
"""
try:
print("arxivsearch")
search_results = ArxivLoader(query=searchQuery, load_max_docs=3).load()
formatted_results = "\n\n--\n\n".join(
[
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page","")}"/>\n{doc.page_content}\n</Document>'
for doc in search_results
])
return {"arxiv_result": formatted_results}
except Exception as e:
print(f"Error getting arxivsearch: {str(e)}")
return f"Error getting arxivsearch: {str(e)}"
@tool
def arxivSearch(query: str) -> str:
"""
Search arXiv and return up to 3 matching paper summaries.
Args:
query (str): The search query for arXiv.
Returns:
str: Formatted search results with source links and abstracts.
"""
try:
# Load results
docs = ArxivLoader(query=query, load_max_docs=3).load()
if not docs:
return "No results found."
# Format the output
results = []
for doc in docs:
source = doc.metadata.get("source", "")
title = doc.metadata.get("Title", "")
content = doc.page_content.strip()
results.append(
f"Title: {title or 'Unknown'}\nSource: {source}\n\nAbstract:\n{content}"
)
return "\n\n---\n\n".join(results)
except Exception as e:
return f"Error searching arXiv: {str(e)}"
@tool
def webSearch(searchQuery:str) -> str:
"""
search the web using Tavily to get 2 matching results
args:
searchQuery: search query
"""
print("web_search: {searchQuery}")
#search_results = TavilySearch(max_results=3).invoke(input=searchQuery)
#search_results = TavilySearchResults(max_results=2).invoke(input=searchQuery)
tavily = TavilySearchResults(max_results=5, search_depth="advanced", include_raw_content=True)
search_results = tavily.run(searchQuery)
formatted_results = "\n\n--\n\n".join(
[
f'<Document source="{doc["url"]}" title="{doc["title"]}"/>\n{doc["content"]}\n</Document>'
for doc in search_results
]
)
return {"web_search": formatted_results}
@tool
def youtubeVideoTranscript(youtubeURL:str) -> str:
"""
Get youtube video transcript by passing in the youtube url
args:
youtubeURL: youtube url to pull out the transcript
"""
try:
print("youtube_transcript")
loader = YoutubeLoader.from_youtube_url(
youtubeURL,
add_video_info=True,
transcript_format=TranscriptFormat.CHUNKS,
chunk_size_seconds=30,
)
formatted_results = "\n\n".join(map(repr, loader.load()))
return {"Youtube transcript":formatted_results}
except Exception as e:
print(f"Error getting youtube transcript: {str(e)}")
return f"Error getting youtube transcript: {str(e)}"
@tool
def power(a: float, b: float) -> float:
"""
Get the power of two numbers.
Args:
a (float): the first number
b (float): the second number
"""
return a**b
@tool
def count_substring(substring:str, text:str) -> int:
"""
Get the number of occurences of a substring within some text. Useful for 'How many (substring) are in (text)?'
Args:
substring (str): the substring to check for.
text (str): the text to search through.
"""
return text.count(substring)
@tool
def read_image_file(taskID: str) -> dict:
"""
Reads an image file and returns a dict containing base64-encoded image data.
This can be passed to a Groq vision-enabled LLM message.
Args:
taskID (str): the task id of the question. This is use to retrieve the attachments or image
Returns:
dict: {"type": "image_url", "image_url": {"url": "data:image/<ext>;base64,<data>"}}
"""
try:
# 1. Download the image from URL
response = requests.get(DEFAULT_File_URL + taskID)
print(f"image url {DEFAULT_File_URL + taskID}")
response.raise_for_status() # ensure no HTTP error
content_type = response.headers.get("Content-Type", "")
# 2. Convert image bytes to base64
image_base64 = base64.b64encode(response.content).decode("utf-8")
return {
"type": "image_url",
"image_url": {
"url": f"data:{content_type};base64,{image_base64}"
}
}
except Exception as e:
return f"Error transcribing image: {str(e)}"
@tool
def transcribe_audio(taskID: str) -> str:
"""
Transcribes an audio file (mp3, wav, m4a, etc.) using Groq Whisper model.
Args:
taskID (str): the task id of the question. This is use to retrieve the attachments or audio
Returns:
str: Transcribed text from the audio.
"""
try:
response = requests.get(DEFAULT_File_URL + taskID)
print(f"audio file {DEFAULT_File_URL + taskID}")
response.raise_for_status() # Throw error if download failed
with open("temp_audio.mp3", "wb") as f:
f.write(response.content)
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
with open("temp_audio.mp3", "rb") as f:
transcription = client.audio.transcriptions.create(
model="whisper-large-v3",
file=f
)
return transcription.text
except Exception as e:
return f"Error transcribing audio: {str(e)}"
@tool
def read_excel_from_url(taskID: str) -> str:
"""
Downloads an Excel file from a URL and returns its contents as text or JSON.
Args:
taskID (str): the task id of the question . This is use to retrieve the attachments or excel
Returns:
str: Contents of the Excel file as text or JSON.
"""
try:
response = requests.get(DEFAULT_File_URL + taskID)
print(f"excel url {DEFAULT_File_URL + taskID}")
response.raise_for_status()
data = BytesIO(response.content)
df = pd.read_excel(data)
return df.to_json(orient="records")
except Exception as e:
return f"Error reading Excel file from URL: {str(e)}"
'''@tool
def run_python_code_from_url(taskID: str) -> str:
"""
Downloads Python code from a URL, executes it, and returns the output or errors.
Args:
taskID (str): the task id of the question . This is use to retrieve the attachments or python code
Returns:
str: Captured output or error traceback.
"""
try:
formattedURL = DEFAULT_File_URL + taskID
response = requests.get(formattedURL)
print(f"pythonurl : {formattedURL}")
response.raise_for_status()
code = response.text
old_stdout = sys.stdout
sys.stdout = mystdout = io.StringIO()
exec_globals = {}
exec(code, exec_globals)
sys.stdout = old_stdout
output = mystdout.getvalue()
if not output.strip():
output = "Code executed successfully with no output."
return output
except Exception:
sys.stdout = old_stdout
return "Error executing code:\n" + traceback.format_exc()
'''
@tool
def run_python_code_from_url(taskID: str) -> str:
"""
Downloads Python code from a URL, runs it in a separate Python process,
and returns stdout/stderr or errors.
"""
try:
formattedURL = DEFAULT_File_URL + taskID
print(f"pythonurl : {formattedURL}")
# Download the Python code
response = requests.get(formattedURL, timeout=10)
response.raise_for_status()
code = response.text
# Write code to a temporary file
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as tmp:
tmp.write(code)
tmp_path = tmp.name
# Run the code in a separate process with timeout
result = subprocess.run(
["python", tmp_path],
capture_output=True,
text=True,
timeout=5 # seconds
)
# Combine stdout and stderr
output = result.stdout.strip()
error = result.stderr.strip()
if output and error:
return f"Output:\n{output}\n\nErrors:\n{error}"
elif output:
return output
elif error:
return f"Error:\n{error}"
else:
return "Code executed successfully with no output."
except subprocess.TimeoutExpired:
return "Error: Code execution timed out."
except Exception:
return "Error executing code:\n" + traceback.format_exc()
|