Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from yt_dlp import YoutubeDL
|
3 |
+
from phi.agent import Agent
|
4 |
+
from phi.model.google import Gemini
|
5 |
+
from phi.tools.duckduckgo import DuckDuckGo
|
6 |
+
from google.generativeai import upload_file, get_file
|
7 |
+
import google.generativeai as genai
|
8 |
+
|
9 |
+
import time
|
10 |
+
from pathlib import Path
|
11 |
+
from dotenv import load_dotenv
|
12 |
+
import os
|
13 |
+
import tempfile
|
14 |
+
|
15 |
+
# Load environment variables
|
16 |
+
load_dotenv()
|
17 |
+
API_KEY = os.getenv("GOOGLE_API_KEY")
|
18 |
+
if API_KEY:
|
19 |
+
genai.configure(api_key=API_KEY)
|
20 |
+
|
21 |
+
# Page configuration
|
22 |
+
st.set_page_config(
|
23 |
+
page_title="YouTube Video Summarizer",
|
24 |
+
page_icon="π₯",
|
25 |
+
layout="wide"
|
26 |
+
)
|
27 |
+
|
28 |
+
st.title("Phidata YouTube Video Summarizer Agent π₯π€π¬")
|
29 |
+
st.header("Powered by Gemini 2.0 Flash Exp")
|
30 |
+
|
31 |
+
@st.cache_resource
|
32 |
+
def initialize_agent():
|
33 |
+
return Agent(
|
34 |
+
name="YouTube Video Summarizer",
|
35 |
+
model=Gemini(id="gemini-2.0-flash-exp"),
|
36 |
+
tools=[DuckDuckGo()],
|
37 |
+
markdown=True,
|
38 |
+
)
|
39 |
+
|
40 |
+
# Initialize the agent
|
41 |
+
multimodal_Agent = initialize_agent()
|
42 |
+
|
43 |
+
# Function to download YouTube video using yt-dlp
|
44 |
+
def download_youtube_video(youtube_url):
|
45 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
|
46 |
+
ydl_opts = {
|
47 |
+
'format': 'bestvideo+bestaudio/best',
|
48 |
+
'outtmpl': temp_video.name,
|
49 |
+
'quiet': True,
|
50 |
+
}
|
51 |
+
with YoutubeDL(ydl_opts) as ydl:
|
52 |
+
ydl.download([youtube_url])
|
53 |
+
return temp_video.name
|
54 |
+
|
55 |
+
# YouTube video URL input
|
56 |
+
youtube_url = st.text_input(
|
57 |
+
"Enter the YouTube video link",
|
58 |
+
placeholder="Paste the YouTube video URL here",
|
59 |
+
help="Provide the link to the YouTube video you want to summarize."
|
60 |
+
)
|
61 |
+
|
62 |
+
if youtube_url:
|
63 |
+
try:
|
64 |
+
with st.spinner("Downloading and processing the YouTube video..."):
|
65 |
+
# Download video using yt-dlp
|
66 |
+
video_path = download_youtube_video(youtube_url)
|
67 |
+
|
68 |
+
# Display video
|
69 |
+
st.video(video_path, format="video/mp4", start_time=0)
|
70 |
+
|
71 |
+
# User query input
|
72 |
+
user_query = st.text_area(
|
73 |
+
"What insights are you seeking from the video?",
|
74 |
+
placeholder="Ask anything about the video content. The AI agent will analyze and gather additional context if needed.",
|
75 |
+
help="Provide specific questions or insights you want from the video."
|
76 |
+
)
|
77 |
+
|
78 |
+
if st.button("π Analyze Video", key="analyze_video_button"):
|
79 |
+
if not user_query:
|
80 |
+
st.warning("Please enter a question or insight to analyze the video.")
|
81 |
+
else:
|
82 |
+
try:
|
83 |
+
with st.spinner("Analyzing video and gathering insights..."):
|
84 |
+
# Upload and process video file
|
85 |
+
processed_video = upload_file(video_path)
|
86 |
+
while processed_video.state.name == "PROCESSING":
|
87 |
+
time.sleep(1)
|
88 |
+
processed_video = get_file(processed_video.name)
|
89 |
+
|
90 |
+
# Prompt generation for analysis
|
91 |
+
analysis_prompt = (
|
92 |
+
f"""
|
93 |
+
Analyze the content of the uploaded YouTube video.
|
94 |
+
Respond to the following query using video insights and supplementary web research:
|
95 |
+
{user_query}
|
96 |
+
Provide a detailed, user-friendly, and actionable response.
|
97 |
+
"""
|
98 |
+
)
|
99 |
+
|
100 |
+
# AI agent processing
|
101 |
+
response = multimodal_Agent.run(analysis_prompt, videos=[processed_video])
|
102 |
+
|
103 |
+
# Display the result
|
104 |
+
st.subheader("Analysis Result")
|
105 |
+
st.markdown(response.content)
|
106 |
+
|
107 |
+
except Exception as error:
|
108 |
+
st.error(f"An error occurred during analysis: {error}")
|
109 |
+
finally:
|
110 |
+
# Clean up temporary video file
|
111 |
+
Path(video_path).unlink(missing_ok=True)
|
112 |
+
except Exception as e:
|
113 |
+
st.error(f"An error occurred while downloading the video: {e}")
|
114 |
+
else:
|
115 |
+
st.info("Enter a YouTube video link to begin analysis.")
|