ekkasilina commited on
Commit
a2a7f98
·
1 Parent(s): 47daeed

added reverse tools

Browse files
Files changed (2) hide show
  1. app.py +2 -48
  2. hf_agent_course/tools.py +59 -0
app.py CHANGED
@@ -5,7 +5,7 @@ import inspect
5
  import pandas as pd
6
  from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool, tool
7
  from smolagents import OpenAIServerModel
8
- from youtube_transcript_api import YouTubeTranscriptApi
9
 
10
 
11
  # (Keep Constants as is)
@@ -23,52 +23,6 @@ class BasicAgent:
23
  print(f"Agent returning fixed answer: {fixed_answer}")
24
  return fixed_answer
25
 
26
-
27
- @tool
28
- def analyze_image(question: str, image_url: str) -> str:
29
- """
30
- Analyze an image using OpenAI's API.
31
- Args:
32
- question (str): The question to ask about the image. eg. "What is in this image?"
33
- image_url (str): The URL of the image to analyze.
34
- """
35
- client = OpenAI()
36
-
37
- response = client.responses.create(
38
- model="gpt-4o-mini",
39
- input=[
40
- {
41
- "role": "user",
42
- "content": [
43
- { "type": "input_text", "text": f"{question}" },
44
- {
45
- "type": "input_image",
46
- "image_url": f"{image_url}",
47
- }
48
- ]
49
- }
50
- ]
51
- )
52
-
53
- return response
54
-
55
- @tool
56
- # a function to get youtube transcript from video id
57
- def get_youtube_transcript(video_id: str) -> str:
58
- """
59
- Fetches the transcript of a YouTube video given its video ID.
60
- Args:
61
- 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.
62
- Returns:
63
- str: The transcript of the YouTube video. as a single string with each line separated by a newline character.
64
- """
65
- # Initialize the YouTubeTranscriptApi
66
- ytt_api = YouTubeTranscriptApi()
67
- fetched_transcript = ytt_api.fetch(video_id)
68
- raw_data = fetched_transcript.to_raw_data()
69
- # 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
70
- transcript = "\n".join([item['text'] for item in raw_data])
71
- return transcript
72
 
73
  def run_and_submit_all( profile: gr.OAuthProfile | None):
74
  """
@@ -100,7 +54,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
100
  search_tool = DuckDuckGoSearchTool()
101
  my_small_agent = CodeAgent(
102
  model=openai_41mini_model,
103
- tools=[search_tool, analyze_image, get_youtube_transcript],
104
  name="web_agent",
105
  description="Use search engine to find webpages related to a subject and get the page content",
106
  #additional_authorized_imports=["pandas", "numpy","bs4"],
 
5
  import pandas as pd
6
  from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool, tool
7
  from smolagents import OpenAIServerModel
8
+ from tools import analyze_image, get_youtube_transcript, reverse_text
9
 
10
 
11
  # (Keep Constants as is)
 
23
  print(f"Agent returning fixed answer: {fixed_answer}")
24
  return fixed_answer
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  def run_and_submit_all( profile: gr.OAuthProfile | None):
28
  """
 
54
  search_tool = DuckDuckGoSearchTool()
55
  my_small_agent = CodeAgent(
56
  model=openai_41mini_model,
57
+ tools=[search_tool, analyze_image, get_youtube_transcript, reverse_text],
58
  name="web_agent",
59
  description="Use search engine to find webpages related to a subject and get the page content",
60
  #additional_authorized_imports=["pandas", "numpy","bs4"],
hf_agent_course/tools.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from youtube_transcript_api import YouTubeTranscriptApi
2
+ from smolagents import tool
3
+
4
+ @tool
5
+ def analyze_image(question: str, image_url: str) -> str:
6
+ """
7
+ Analyze an image using OpenAI's API.
8
+ Args:
9
+ question (str): The question to ask about the image. eg. "What is in this image?"
10
+ image_url (str): The URL of the image to analyze.
11
+ """
12
+ client = OpenAI()
13
+ response = client.responses.create(
14
+ model="gpt-4o-mini",
15
+ input=[
16
+ {
17
+ "role": "user",
18
+ "content": [
19
+ { "type": "input_text", "text": f"{question}" },
20
+ {
21
+ "type": "input_image",
22
+ "image_url": f"{image_url}",
23
+ }
24
+ ]
25
+ }
26
+ ]
27
+ )
28
+
29
+ return response
30
+
31
+ @tool
32
+ # a function to get youtube transcript from video id
33
+ def get_youtube_transcript(video_id: str) -> str:
34
+ """
35
+ Fetches the transcript of a YouTube video given its video ID.
36
+ Args:
37
+ 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.
38
+ Returns:
39
+ str: The transcript of the YouTube video. as a single string with each line separated by a newline character.
40
+ """
41
+ # Initialize the YouTubeTranscriptApi
42
+ ytt_api = YouTubeTranscriptApi()
43
+ fetched_transcript = ytt_api.fetch(video_id)
44
+ raw_data = fetched_transcript.to_raw_data()
45
+ # 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
46
+ transcript = "\n".join([item['text'] for item in raw_data])
47
+ return transcript
48
+
49
+ @tool
50
+ def reverse_text(question: str) -> str:
51
+ """
52
+ Reverse a string if it appears to be written backward.
53
+ Args:
54
+ question (str): The question that seemed to be written backward"
55
+ """
56
+ reversed_candidate = question[::-1]
57
+ if question.strip() and question.strip()[0] in "?.!":
58
+ return reversed_candidate
59
+ return "Text seems normal."