Spaces:
Runtime error
Runtime error
Upload tools.py
Browse files
tools.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.tools import tool
|
| 2 |
+
from langchain_experimental.utilities import PythonREPL
|
| 3 |
+
from langchain_community.tools.ddg_search import DuckDuckGoSearchRun
|
| 4 |
+
from langchain_community.tools.wikipedia.tool import WikipediaQueryRun
|
| 5 |
+
from langchain_community.utilities.wikipedia import WikipediaAPIWrapper
|
| 6 |
+
from smolagents import VisitWebpageTool, PythonInterpreterTool, SpeechToTextTool, Tool
|
| 7 |
+
from langchain_community.tools.google_serper.tool import GoogleSerperAPIWrapper
|
| 8 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
duckduckgo = DuckDuckGoSearchRun()
|
| 14 |
+
python_repl = PythonREPL()
|
| 15 |
+
wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
|
| 16 |
+
# Instantiate smolagents tools
|
| 17 |
+
visit_webpage_tool = VisitWebpageTool()
|
| 18 |
+
python_interpreter_tool = PythonInterpreterTool()
|
| 19 |
+
speech_to_text_tool = SpeechToTextTool()
|
| 20 |
+
search_api = GoogleSerperAPIWrapper()
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
transcript_tool = Tool.from_space(
|
| 24 |
+
"maguid28/TranscriptTool",
|
| 25 |
+
name="TranscriptTool",
|
| 26 |
+
description="""
|
| 27 |
+
A smolagent tool for transcribing audio and video files into text. This tool utilises Whisper for transcription
|
| 28 |
+
and ffmpeg for media conversion, enabling agents to process multimedia inputs into text. The tool supports robust
|
| 29 |
+
file handling, including format conversion to WAV and dynamic device selection for optimal performance.
|
| 30 |
+
"""
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
@tool
|
| 34 |
+
def python_repl_tool(code: str) -> str:
|
| 35 |
+
"""
|
| 36 |
+
Execute Python code in a REPL environment and return the output as a string.
|
| 37 |
+
Args:
|
| 38 |
+
code (str): The Python code to execute in the REPL.
|
| 39 |
+
Returns:
|
| 40 |
+
str: The output of the executed code from the REPL.
|
| 41 |
+
"""
|
| 42 |
+
return python_repl.run(code)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@tool
|
| 46 |
+
def visit_webpage(url: str) -> str:
|
| 47 |
+
"""
|
| 48 |
+
Visit a webpage and return its content as a string.
|
| 49 |
+
Args:
|
| 50 |
+
url (str): The URL of the webpage to visit.
|
| 51 |
+
Returns:
|
| 52 |
+
str: The content of the webpage.
|
| 53 |
+
"""
|
| 54 |
+
return visit_webpage_tool(url=url)
|
| 55 |
+
|
| 56 |
+
@tool
|
| 57 |
+
def python_interpreter(code: str) -> str:
|
| 58 |
+
"""
|
| 59 |
+
Execute Python code and return the output as a string.
|
| 60 |
+
Args:
|
| 61 |
+
code (str): The Python code to execute.
|
| 62 |
+
Returns:
|
| 63 |
+
str: The output of the executed code.
|
| 64 |
+
"""
|
| 65 |
+
return python_interpreter_tool(code=code)
|
| 66 |
+
|
| 67 |
+
@tool
|
| 68 |
+
def speech_to_text(audio_path: str) -> str:
|
| 69 |
+
"""
|
| 70 |
+
Convert speech in an audio file to text.
|
| 71 |
+
Args:
|
| 72 |
+
audio_path (str): The path to the audio file.
|
| 73 |
+
Returns:
|
| 74 |
+
str: The transcribed text from the audio.
|
| 75 |
+
"""
|
| 76 |
+
return speech_to_text_tool(audio=audio_path)
|
| 77 |
+
|
| 78 |
+
@tool
|
| 79 |
+
def read_file_contents(file_path: str) -> str:
|
| 80 |
+
"""
|
| 81 |
+
Read and return the contents of a file as a string.
|
| 82 |
+
Args:
|
| 83 |
+
file_path (str): The path to the file to read.
|
| 84 |
+
Returns:
|
| 85 |
+
str: The contents of the file, or an error message if the file cannot be read.
|
| 86 |
+
"""
|
| 87 |
+
try:
|
| 88 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 89 |
+
return f.read()
|
| 90 |
+
except Exception as e:
|
| 91 |
+
return f"Error reading file: {e}"
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
@tool
|
| 95 |
+
def google_serper_search(query: str) -> str:
|
| 96 |
+
"""
|
| 97 |
+
Search the web using Google Serper API and return the results as a string.
|
| 98 |
+
Args:
|
| 99 |
+
query (str): The search query string.
|
| 100 |
+
Returns:
|
| 101 |
+
str: The search results from Google Serper API.
|
| 102 |
+
"""
|
| 103 |
+
return search_api.run(query)
|
| 104 |
+
|
| 105 |
+
@tool
|
| 106 |
+
def transcript_tool_langchain(audio_path: str) -> str:
|
| 107 |
+
"""
|
| 108 |
+
Transcribe an audio or video file to text using the TranscriptTool from smolagents.
|
| 109 |
+
Args:
|
| 110 |
+
audio_path (str): The path to the audio or video file.
|
| 111 |
+
Returns:
|
| 112 |
+
str: The transcribed text from the file.
|
| 113 |
+
Raises:
|
| 114 |
+
FileNotFoundError: If the file does not exist at the given path.
|
| 115 |
+
"""
|
| 116 |
+
import os
|
| 117 |
+
if not os.path.exists(audio_path):
|
| 118 |
+
raise FileNotFoundError(f"File not found: {audio_path}")
|
| 119 |
+
return transcript_tool(audio_path)
|
| 120 |
+
|
| 121 |
+
@tool
|
| 122 |
+
def youtube_transcript_tool(youtube_url_or_id: str) -> str:
|
| 123 |
+
"""
|
| 124 |
+
Fetch the transcript of a YouTube video using its URL or video ID.
|
| 125 |
+
Args:
|
| 126 |
+
youtube_url_or_id (str): The YouTube video URL or video ID.
|
| 127 |
+
Returns:
|
| 128 |
+
str: The transcript of the video, or an error message if not available.
|
| 129 |
+
"""
|
| 130 |
+
import re
|
| 131 |
+
# Extract video ID from URL if needed
|
| 132 |
+
video_id = youtube_url_or_id
|
| 133 |
+
match = re.search(r"(?:v=|youtu.be/)([\w-]{11})", youtube_url_or_id)
|
| 134 |
+
if match:
|
| 135 |
+
video_id = match.group(1)
|
| 136 |
+
try:
|
| 137 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 138 |
+
return " ".join([entry['text'] for entry in transcript])
|
| 139 |
+
except Exception as e:
|
| 140 |
+
return f"Error fetching transcript: {e}"
|