| import os |
| import requests |
| import wikipedia |
| import pandas as pd |
| from smolagents import PythonInterpreterTool, tool |
| from youtube_transcript_api import YouTubeTranscriptApi |
|
|
| @tool |
| def ReverseTextTool(text: str) -> str: |
| """ |
| Reverses a text string character by character. |
| Args: |
| text (str): The text to reverse |
| Returns: |
| str: The reversed text |
| """ |
| return text[::-1] |
|
|
| @tool |
| def RunPythonFileTool(file_path: str) -> str: |
| """ |
| Executes a Python script loaded from the specified path using the PythonInterpreterTool. |
| Args: |
| file_path (str): The full path to the python (.py) file containing the Python code. |
| Returns: |
| str: The output produced by the code execution, or an error message if it fails. |
| """ |
| try: |
| with open(file_path, "r") as f: |
| code = f.read() |
| interpreter = PythonInterpreterTool() |
| result = interpreter.run({"code": code}) |
| return result.get("output", "No output returned.") |
| except Exception as e: |
| return f"Execution failed: {e}" |
|
|
| @tool |
| def download_server(url: str, save_path: str) -> str: |
| """ |
| Downloads a file from a URL and saves it to the given path. |
| Args: |
| url (str): The URL from which to download the file. |
| save_path (str): The local file path where the downloaded file will be saved. |
| Returns: |
| str: A message indicating the result of the download operation. |
| """ |
| try: |
| response = requests.get(url, timeout=30) |
| response.raise_for_status() |
| with open(save_path, "wb") as f: |
| f.write(response.content) |
| return f"File downloaded to {save_path}" |
| except Exception as e: |
| return f"Failed to download: {e}" |
|
|
| @tool |
| def WikipediaSearchTool(query: str) -> str: |
| """ |
| Performs a search on Wikipedia and returns a summary of the result. |
| Args: |
| query: The search term. |
| Returns: |
| A string containing the summary of the search result or an error message. |
| """ |
| try: |
| summary = wikipedia.summary(query, sentences=3) |
| return summary |
| except wikipedia.exceptions.PageError: |
| return f"Error: No Wikipedia page found for '{query}'." |
| except wikipedia.exceptions.DisambiguationError as e: |
| return f"Error: Multiple results found for '{query}'. Try a more specific query. Options: {e.options}" |
| except Exception as e: |
| return f"An unexpected error occurred: {e}" |
|
|
| @tool |
| def YouTubeVideoAnalysisTool(video_id: str, keyword: str) -> str: |
| """ |
| Fetches the transcript of a YouTube video by its ID and performs a keyword search. |
| Args: |
| video_id: The ID of the YouTube video. |
| keyword: The keyword to search for in the transcript. |
| Returns: |
| A string indicating if the keyword was found and providing a snippet, or an error message. |
| """ |
| try: |
| transcript_list = YouTubeTranscriptApi.get_transcript(video_id) |
| full_transcript = " ".join([d['text'] for d in transcript_list]) |
| |
| if keyword.lower() in full_transcript.lower(): |
| index = full_transcript.lower().find(keyword.lower()) |
| start = max(0, index - 50) |
| end = min(len(full_transcript), index + len(keyword) + 50) |
| snippet = full_transcript[start:end] |
| return f"Keyword '{keyword}' found in the video. Snippet: '...{snippet}...'." |
| else: |
| return f"Keyword '{keyword}' not found in the video transcript." |
| except Exception as e: |
| return f"An error occurred while fetching the YouTube transcript: {e}" |
|
|
| @tool |
| def ExcelFileParserTool(file_path: str, query: str = "get_headers") -> str: |
| """ |
| Reads and queries data from an Excel file at a given path. |
| |
| Args: |
| file_path (str): The path to the Excel file (.xlsx, .xls). |
| query (str): The type of query to perform. Defaults to 'get_headers'. |
| Other options include 'find_value:column_name:value_to_find'. |
| |
| Returns: |
| str: A string with the result of the query or an error message. |
| """ |
| try: |
| df = pd.read_excel(file_path) |
| |
| if query == "get_headers": |
| return f"Headers found: {', '.join(df.columns.tolist())}" |
| |
| elif query.startswith("find_value:"): |
| parts = query.split(":") |
| if len(parts) != 3: |
| return "Error: Invalid query format. Use 'find_value:column_name:value_to_find'." |
| |
| column_name = parts[1] |
| value_to_find = parts[2] |
| |
| if column_name not in df.columns: |
| return f"Error: Column '{column_name}' not found." |
| |
| found_rows = df[df[column_name].astype(str).str.contains(value_to_find, case=False, na=False)] |
| |
| if not found_rows.empty: |
| return f"Value '{value_to_find}' found in column '{column_name}'. Found in the following rows:\n{found_rows.to_string()}" |
| else: |
| return f"Value '{value_to_find}' not found in column '{column_name}'." |
| |
| else: |
| return "Error: Unknown query type. Supported queries: 'get_headers', 'find_value:column_name:value_to_find'." |
| |
| except FileNotFoundError: |
| return f"Error: File not found at '{file_path}'." |
| except Exception as e: |
| return f"An unexpected error occurred: {e}" |