Commit ·
a2a7f98
1
Parent(s): 47daeed
added reverse tools
Browse filesFiles changed (2)
- app.py+2-48
- hf_agent_course/tools.pyAdded+59
4 unmodified lines56789101111 unmodified lines2324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737425 unmodified lines1001011021031041051064 unmodified linesimport pandas as pdfrom smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool, toolfrom smolagents import OpenAIServerModelfrom youtube_transcript_api import YouTubeTranscriptApi# (Keep Constants as is)11 unmodified linesprint(f"Agent returning fixed answer: {fixed_answer}")return fixed_answer@tooldef 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 iddef 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 YouTubeTranscriptApiytt_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 linestranscript = "\n".join([item['text'] for item in raw_data])return transcriptdef run_and_submit_all( profile: gr.OAuthProfile | None):"""25 unmodified linessearch_tool = DuckDuckGoSearchTool()my_small_agent = CodeAgent(model=openai_41mini_model,tools=[search_tool, analyze_image, get_youtube_transcript],name="web_agent",description="Use search engine to find webpages related to a subject and get the page content",#additional_authorized_imports=["pandas", "numpy","bs4"],4 unmodified lines56789101111 unmodified lines23242526272825 unmodified lines545556575859604 unmodified linesimport pandas as pdfrom smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool, toolfrom smolagents import OpenAIServerModelfrom tools import analyze_image, get_youtube_transcript, reverse_text# (Keep Constants as is)11 unmodified linesprint(f"Agent returning fixed answer: {fixed_answer}")return fixed_answerdef run_and_submit_all( profile: gr.OAuthProfile | None):"""25 unmodified linessearch_tool = DuckDuckGoSearchTool()my_small_agent = CodeAgent(model=openai_41mini_model,tools=[search_tool, analyze_image, get_youtube_transcript, reverse_text],name="web_agent",description="Use search engine to find webpages related to a subject and get the page content",#additional_authorized_imports=["pandas", "numpy","bs4"],
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859from youtube_transcript_api import YouTubeTranscriptApifrom smolagents import tool
@tooldef 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 iddef 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
@tooldef 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" """ reversed_candidate = question[::-1] if question.strip() and question.strip()[0] in "?.!": return reversed_candidate return "Text seems normal."app.py+2-48
hf_agent_course/tools.pyAdded+59