llamasrock commited on
Commit
46fbc41
·
verified ·
1 Parent(s): c8b3448

Update app.py

Browse files

updated the YouTube tool

Files changed (1) hide show
  1. app.py +27 -17
app.py CHANGED
@@ -14,7 +14,11 @@ from langgraph.prebuilt import tools_condition
14
  from langgraph.prebuilt import ToolNode
15
  import base64
16
  from google.ai.generativelanguage_v1beta.types import Tool as GenAITool
17
- from google.ai.generativelanguage_v1beta.types import FileData
 
 
 
 
18
 
19
  # (Keep Constants as is)
20
  # --- Constants ---
@@ -117,26 +121,32 @@ def open_image(task_id: str) -> str:
117
 
118
  return base64.b64encode(image_file.read()).decode("utf-8")
119
 
120
- def open_youtube_video(url: str) -> HumanMessage:
121
  '''
122
- Opens a video file from the given URL.
123
  Args:
124
  url: The URL of the video file.
 
125
  Returns:
126
- HumanMessage instructions for the video file.
127
  '''
128
- video = FileData(url=url)
129
-
130
- return HumanMessage(
131
- content=[
132
- {"type": "text", "text": "Watch the video and answer the question."},
133
- {
134
- "type": "media",
135
- "data": video,
136
- "mime_type": "video/mp4",
137
- },
138
- ]
139
- )
 
 
 
 
 
140
 
141
  def google_search(query: str) -> str:
142
  '''
@@ -184,7 +194,7 @@ class BasicAgent:
184
  If you are asked to transcribe an audio file, use the 'transcribe_audio' tool.
185
  If you are asked to run a Python code, use the 'python_code' tool.
186
  If you are asked to open an image, use the 'open_image' tool.
187
- If you are asked to open a YouTube video, use the 'open_video' tool.
188
  If the question requires a web search because your internal knowledge doesn't have the information, use the 'google_search' tool.
189
  ''')
190
 
 
14
  from langgraph.prebuilt import ToolNode
15
  import base64
16
  from google.ai.generativelanguage_v1beta.types import Tool as GenAITool
17
+ from google import genai
18
+ from google.genai import types
19
+ import os
20
+
21
+ GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
22
 
23
  # (Keep Constants as is)
24
  # --- Constants ---
 
121
 
122
  return base64.b64encode(image_file.read()).decode("utf-8")
123
 
124
+ def open_youtube_video(url: str, query:str) -> str:
125
  '''
126
+ Answers a question about a video from the given URL.
127
  Args:
128
  url: The URL of the video file.
129
+ query: The question to be answered about the video.
130
  Returns:
131
+ Answer to the question about the video.
132
  '''
133
+
134
+ client = genai.Client(api_key=GOOGLE_API_KEY)
135
+
136
+ response = client.models.generate_content(
137
+ model='models/gemini-2.0-flash',
138
+ contents=types.Content(
139
+ parts=[
140
+ types.Part(
141
+ file_data=types.FileData(file_uri=url)
142
+ ),
143
+ types.Part(text=f'''{query} YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated
144
+ list of numbers and/or strings.''')
145
+ ]
146
+ )
147
+ )
148
+
149
+ return response.text
150
 
151
  def google_search(query: str) -> str:
152
  '''
 
194
  If you are asked to transcribe an audio file, use the 'transcribe_audio' tool.
195
  If you are asked to run a Python code, use the 'python_code' tool.
196
  If you are asked to open an image, use the 'open_image' tool.
197
+ If you were given a link with www.youtube.com, use the 'open_youtube_video' tool.
198
  If the question requires a web search because your internal knowledge doesn't have the information, use the 'google_search' tool.
199
  ''')
200