Spaces:
Runtime error
Runtime error
Update agent.py
Browse files
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
|
| 34 |
"""
|
| 35 |
-
Analyze a video to answer a specific question about its content.
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
try:
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 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
|
| 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
|