Update gaia_tools.py
Browse files- gaia_tools.py +95 -4
gaia_tools.py
CHANGED
|
@@ -1,5 +1,9 @@
|
|
| 1 |
-
|
| 2 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
@tool
|
| 5 |
def ReverseTextTool(text: str) -> str:
|
|
@@ -12,7 +16,6 @@ def ReverseTextTool(text: str) -> str:
|
|
| 12 |
"""
|
| 13 |
return text[::-1]
|
| 14 |
|
| 15 |
-
|
| 16 |
@tool
|
| 17 |
def RunPythonFileTool(file_path: str) -> str:
|
| 18 |
"""
|
|
@@ -31,7 +34,6 @@ def RunPythonFileTool(file_path: str) -> str:
|
|
| 31 |
except Exception as e:
|
| 32 |
return f"Execution failed: {e}"
|
| 33 |
|
| 34 |
-
|
| 35 |
@tool
|
| 36 |
def download_server(url: str, save_path: str) -> str:
|
| 37 |
"""
|
|
@@ -49,4 +51,93 @@ def download_server(url: str, save_path: str) -> str:
|
|
| 49 |
f.write(response.content)
|
| 50 |
return f"File downloaded to {save_path}"
|
| 51 |
except Exception as e:
|
| 52 |
-
return f"Failed to download: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import requests
|
| 3 |
+
import wikipedia
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from smolagents import PythonInterpreterTool, tool
|
| 6 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 7 |
|
| 8 |
@tool
|
| 9 |
def ReverseTextTool(text: str) -> str:
|
|
|
|
| 16 |
"""
|
| 17 |
return text[::-1]
|
| 18 |
|
|
|
|
| 19 |
@tool
|
| 20 |
def RunPythonFileTool(file_path: str) -> str:
|
| 21 |
"""
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
return f"Execution failed: {e}"
|
| 36 |
|
|
|
|
| 37 |
@tool
|
| 38 |
def download_server(url: str, save_path: str) -> str:
|
| 39 |
"""
|
|
|
|
| 51 |
f.write(response.content)
|
| 52 |
return f"File downloaded to {save_path}"
|
| 53 |
except Exception as e:
|
| 54 |
+
return f"Failed to download: {e}"
|
| 55 |
+
|
| 56 |
+
@tool
|
| 57 |
+
def WikipediaSearchTool(query: str) -> str:
|
| 58 |
+
"""
|
| 59 |
+
Performs a search on Wikipedia and returns a summary of the result.
|
| 60 |
+
Args:
|
| 61 |
+
query: The search term.
|
| 62 |
+
Returns:
|
| 63 |
+
A string containing the summary of the search result or an error message.
|
| 64 |
+
"""
|
| 65 |
+
try:
|
| 66 |
+
summary = wikipedia.summary(query, sentences=3)
|
| 67 |
+
return summary
|
| 68 |
+
except wikipedia.exceptions.PageError:
|
| 69 |
+
return f"Error: No Wikipedia page found for '{query}'."
|
| 70 |
+
except wikipedia.exceptions.DisambiguationError as e:
|
| 71 |
+
return f"Error: Multiple results found for '{query}'. Try a more specific query. Options: {e.options}"
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return f"An unexpected error occurred: {e}"
|
| 74 |
+
|
| 75 |
+
@tool
|
| 76 |
+
def YouTubeVideoAnalysisTool(video_id: str, keyword: str) -> str:
|
| 77 |
+
"""
|
| 78 |
+
Fetches the transcript of a YouTube video by its ID and performs a keyword search.
|
| 79 |
+
Args:
|
| 80 |
+
video_id: The ID of the YouTube video.
|
| 81 |
+
keyword: The keyword to search for in the transcript.
|
| 82 |
+
Returns:
|
| 83 |
+
A string indicating if the keyword was found and providing a snippet, or an error message.
|
| 84 |
+
"""
|
| 85 |
+
try:
|
| 86 |
+
transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
|
| 87 |
+
full_transcript = " ".join([d['text'] for d in transcript_list])
|
| 88 |
+
|
| 89 |
+
if keyword.lower() in full_transcript.lower():
|
| 90 |
+
index = full_transcript.lower().find(keyword.lower())
|
| 91 |
+
start = max(0, index - 50)
|
| 92 |
+
end = min(len(full_transcript), index + len(keyword) + 50)
|
| 93 |
+
snippet = full_transcript[start:end]
|
| 94 |
+
return f"Keyword '{keyword}' found in the video. Snippet: '...{snippet}...'."
|
| 95 |
+
else:
|
| 96 |
+
return f"Keyword '{keyword}' not found in the video transcript."
|
| 97 |
+
except Exception as e:
|
| 98 |
+
return f"An error occurred while fetching the YouTube transcript: {e}"
|
| 99 |
+
|
| 100 |
+
@tool
|
| 101 |
+
def ExcelFileParserTool(file_path: str, query: str = "get_headers") -> str:
|
| 102 |
+
"""
|
| 103 |
+
Reads and queries data from an Excel file at a given path.
|
| 104 |
+
|
| 105 |
+
Args:
|
| 106 |
+
file_path (str): The path to the Excel file (.xlsx, .xls).
|
| 107 |
+
query (str): The type of query to perform. Defaults to 'get_headers'.
|
| 108 |
+
Other options include 'find_value:column_name:value_to_find'.
|
| 109 |
+
|
| 110 |
+
Returns:
|
| 111 |
+
str: A string with the result of the query or an error message.
|
| 112 |
+
"""
|
| 113 |
+
try:
|
| 114 |
+
df = pd.read_excel(file_path)
|
| 115 |
+
|
| 116 |
+
if query == "get_headers":
|
| 117 |
+
return f"Headers found: {', '.join(df.columns.tolist())}"
|
| 118 |
+
|
| 119 |
+
elif query.startswith("find_value:"):
|
| 120 |
+
parts = query.split(":")
|
| 121 |
+
if len(parts) != 3:
|
| 122 |
+
return "Error: Invalid query format. Use 'find_value:column_name:value_to_find'."
|
| 123 |
+
|
| 124 |
+
column_name = parts[1]
|
| 125 |
+
value_to_find = parts[2]
|
| 126 |
+
|
| 127 |
+
if column_name not in df.columns:
|
| 128 |
+
return f"Error: Column '{column_name}' not found."
|
| 129 |
+
|
| 130 |
+
found_rows = df[df[column_name].astype(str).str.contains(value_to_find, case=False, na=False)]
|
| 131 |
+
|
| 132 |
+
if not found_rows.empty:
|
| 133 |
+
return f"Value '{value_to_find}' found in column '{column_name}'. Found in the following rows:\n{found_rows.to_string()}"
|
| 134 |
+
else:
|
| 135 |
+
return f"Value '{value_to_find}' not found in column '{column_name}'."
|
| 136 |
+
|
| 137 |
+
else:
|
| 138 |
+
return "Error: Unknown query type. Supported queries: 'get_headers', 'find_value:column_name:value_to_find'."
|
| 139 |
+
|
| 140 |
+
except FileNotFoundError:
|
| 141 |
+
return f"Error: File not found at '{file_path}'."
|
| 142 |
+
except Exception as e:
|
| 143 |
+
return f"An unexpected error occurred: {e}"
|