ekkasilina's picture
added reverse tools
7e2c9ef
from youtube_transcript_api import YouTubeTranscriptApi
from smolagents import tool
@tool
def analyze_image(question: str, image_url: str) -> str:
"""
Analyze an image using OpenAI's API.
Args:
question (str): The question to ask about the image. eg. "What is in this image?"
image_url (str): The URL of the image to analyze.
"""
client = OpenAI()
response = client.responses.create(
model="gpt-4o-mini",
input=[
{
"role": "user",
"content": [
{ "type": "input_text", "text": f"{question}" },
{
"type": "input_image",
"image_url": f"{image_url}",
}
]
}
]
)
return response
@tool
# a function to get youtube transcript from video id
def get_youtube_transcript(video_id: str) -> str:
"""
Fetches the transcript of a YouTube video given its video ID.
Args:
video_id (str): The ID of the YouTube video. Pass in the video ID, NOT the video URL. For a video with the URL https://www.youtube.com/watch?v=12345 the ID is 12345.
Returns:
str: The transcript of the YouTube video. as a single string with each line separated by a newline character.
"""
# Initialize the YouTubeTranscriptApi
ytt_api = YouTubeTranscriptApi()
fetched_transcript = ytt_api.fetch(video_id)
raw_data = fetched_transcript.to_raw_data()
# raw data is in the form of [{ 'text': 'Hey there', 'start': 0.0, 'duration': 1.54 }, { 'text': 'how are you',, 'start': 1.54, 'duration': 4.16 }, ... ] we will return ony the text element as lines
transcript = "\n".join([item['text'] for item in raw_data])
return transcript
@tool
def reverse_text(question: str) -> str:
"""
Reverse a string if it appears to be written backward.
Args:
question (str): The question that seemed to be written backward"
"""
if question.strip()[0] in "?.!":
reversed_candidate = question[::-1]
return reversed_candidate
return "Text seems normal."