Aya1610 commited on
Commit
c4a61a8
·
verified ·
1 Parent(s): 58d4b08

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +39 -57
agent.py CHANGED
@@ -3,6 +3,7 @@ import os
3
  import operator
4
  import json
5
  import re
 
6
  from typing import TypedDict, Annotated, Sequence, Union
7
  from langgraph.graph import StateGraph, END
8
  from langchain_community.tools import DuckDuckGoSearchResults
@@ -30,68 +31,49 @@ 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
 
3
  import operator
4
  import json
5
  import re
6
+ from youtube_transcript_api import YouTubeTranscriptApi
7
  from typing import TypedDict, Annotated, Sequence, Union
8
  from langgraph.graph import StateGraph, END
9
  from langchain_community.tools import DuckDuckGoSearchResults
 
31
 
32
  #video analysis
33
  @tool
34
+ def analyze_youtube_video(youtube_link: str, question: str) -> str:
35
  """
36
+ Analyze a YouTube video to answer a specific question about its content.
37
+ Returns the answer to the question based on the video's transcript.
38
 
39
+ Args:
40
+ youtube_link: URL of the YouTube video
41
+ question: Specific question about the video content
42
+
43
+ Returns:
44
+ Answer to the question or error message
45
  """
46
+ # Extract video ID from various YouTube URL formats
47
+ def extract_video_id(url):
48
+ regex = r"(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^\"&?\/\s]{11})"
49
+ match = re.search(regex, url)
50
+ return match.group(1) if match else None
51
+
52
  try:
53
+ # Get video ID
54
+ video_id = extract_video_id(youtube_link)
55
+ if not video_id:
56
+ return "Error: Invalid YouTube URL format"
57
+
58
+ # Get transcript
59
+ transcript = YouTubeTranscriptApi.get_transcript(video_id)
60
+ transcript_text = " ".join([entry['text'] for entry in transcript])
61
+
62
+ # Use OpenAI to answer the question based on transcript
63
+ client = OpenAI()
64
+ response = client.chat.completions.create(
65
+ model="gpt-4-turbo",
66
+ messages=[
67
+ {"role": "system", "content": "Answer the user's question based EXCLUSIVELY on the video transcript below. Be precise and quote directly when possible."},
68
+ {"role": "user", "content": f"Question: {question}\n\nTranscript:\n{transcript_text}"}
69
+ ],
70
+ max_tokens=300
71
+ )
72
+
73
+ return response.choices[0].message.content
74
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  except Exception as e:
76
+ return f"Error: {str(e)}"
 
 
77
 
78
  # Image Description Tool (using GPT-4 Vision)
79
  @tool