Aya1610 commited on
Commit
ef2d522
·
verified ·
1 Parent(s): 1562427

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +66 -1
agent.py CHANGED
@@ -28,6 +28,71 @@ openai_api_key = os.getenv("OPENAI_API_KEY") # Replace with your actual key
28
  search_tool = DuckDuckGoSearchResults(max_results=3)
29
  wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper(top_k_results=3))
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  # Image Description Tool (using GPT-4 Vision)
32
  @tool
33
  def describe_image(image_url: str) -> str:
@@ -108,7 +173,7 @@ def convert_currency(amount: float, from_currency: str, to_currency: str) -> flo
108
  # ---------------------
109
 
110
  # Available tools
111
- tools = [search_tool, wikipedia, describe_image, calculate, convert_date, convert_currency]
112
  tools_as_openai = [convert_to_openai_tool(t) for t in tools]
113
 
114
  # Agent State Definition
 
28
  search_tool = DuckDuckGoSearchResults(max_results=3)
29
  wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper(top_k_results=3))
30
 
31
+ #video analysis
32
+ @tool
33
+ def analyze_video(video_url: str, question: str) -> str:
34
+ """
35
+ Analyze a video to answer a specific question about its content.
36
+ Supports YouTube videos. Provide both the video URL and the question.
37
+
38
+ Example:
39
+ analyze_video(
40
+ "https://www.youtube.com/watch?v=1htKBjuUWec",
41
+ "What does Teal'c say in response to the question 'Isn't that hot?'"
42
+ )
43
+ """
44
+ try:
45
+ # Create a temporary directory for processing
46
+ with tempfile.TemporaryDirectory() as tmp_dir:
47
+ # Download audio from YouTube
48
+ ydl_opts = {
49
+ 'format': 'bestaudio/best',
50
+ 'outtmpl': os.path.join(tmp_dir, 'audio.%(ext)s'),
51
+ 'postprocessors': [{
52
+ 'key': 'FFmpegExtractAudio',
53
+ 'preferredcodec': 'mp3',
54
+ 'preferredquality': '192',
55
+ }],
56
+ 'quiet': True,
57
+ }
58
+
59
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
60
+ ydl.download([video_url])
61
+ audio_file = os.path.join(tmp_dir, 'audio.mp3')
62
+
63
+ # If the file wasn't created with .mp3 extension, find it
64
+ if not os.path.exists(audio_file):
65
+ for f in os.listdir(tmp_dir):
66
+ if f.startswith('audio.'):
67
+ audio_file = os.path.join(tmp_dir, f)
68
+ break
69
+
70
+ # Transcribe audio using OpenAI Whisper
71
+ client = OpenAI()
72
+ with open(audio_file, 'rb') as f:
73
+ transcript = client.audio.transcriptions.create(
74
+ model="whisper-large-v3",
75
+ file=f,
76
+ response_format="text"
77
+ )
78
+
79
+ # Use GPT to answer the question based on transcript
80
+ response = client.chat.completions.create(
81
+ model="gpt-4-turbo",
82
+ messages=[
83
+ {"role": "system", "content": "Answer the question based on the video transcript below. Be precise and quote directly when possible."},
84
+ {"role": "user", "content": f"Question: {question}\n\nTranscript:\n{transcript}"}
85
+ ],
86
+ max_tokens=300
87
+ )
88
+
89
+ return response.choices[0].message.content
90
+
91
+ except Exception as e:
92
+ return f"Error analyzing video: {str(e)}"
93
+
94
+
95
+
96
  # Image Description Tool (using GPT-4 Vision)
97
  @tool
98
  def describe_image(image_url: str) -> str:
 
173
  # ---------------------
174
 
175
  # Available tools
176
+ tools = [search_tool, wikipedia, analyze_video, describe_image, calculate, convert_date, convert_currency]
177
  tools_as_openai = [convert_to_openai_tool(t) for t in tools]
178
 
179
  # Agent State Definition