testing / app.py
Manasa1's picture
Update app.py
841092b verified
raw
history blame
4.63 kB
import streamlit as st
from pytube import YouTube
from phi.agent import Agent
from phi.model.google import Gemini
from phi.tools.duckduckgo import DuckDuckGo
from google.generativeai import upload_file, get_file
import google.generativeai as genai
import time
from pathlib import Path
from dotenv import load_dotenv
import os
import tempfile
# Load environment variables
load_dotenv()
API_KEY = os.getenv("GOOGLE_API_KEY")
if API_KEY:
genai.configure(api_key=API_KEY)
# Page configuration
st.set_page_config(
page_title="YouTube Video Summarizer",
page_icon="πŸŽ₯",
layout="wide"
)
st.title("Phidata YouTube Video Summarizer Agent πŸŽ₯πŸŽ€πŸ–¬")
st.header("Powered by Gemini 2.0 Flash Exp")
@st.cache_resource
def initialize_agent():
return Agent(
name="YouTube Video Summarizer",
model=Gemini(id="gemini-2.0-flash-exp"),
tools=[DuckDuckGo()],
markdown=True,
)
# Initialize the agent
multimodal_Agent = initialize_agent()
# Function to download YouTube video using pytube
def download_youtube_video(youtube_url):
"""
Downloads a YouTube video using pytube.
Parameters:
- youtube_url: The URL of the YouTube video.
Returns:
- The path to the downloaded video file.
"""
# Initialize YouTube object using pytube
yt = YouTube(youtube_url)
# Get the highest resolution stream available
video_stream = yt.streams.get_highest_resolution()
# Temporary file for the video
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
temp_video_path = temp_video.name
try:
# Download the video to the temporary file
video_stream.download(output_path=temp_video_path)
return temp_video_path # Return the video file path
except Exception as e:
raise RuntimeError(f"An error occurred while downloading the video: {str(e)}")
# YouTube video URL input
youtube_url = st.text_input(
"Enter the YouTube video link",
placeholder="Paste the YouTube video URL here",
help="Provide the link to the YouTube video you want to summarize."
)
if youtube_url:
try:
with st.spinner("Downloading and processing the YouTube video..."):
# Download video using pytube
video_path = download_youtube_video(youtube_url)
# Display video
st.video(video_path, format="video/mp4", start_time=0)
# User query input
user_query = st.text_area(
"What insights are you seeking from the video?",
placeholder="Ask anything about the video content. The AI agent will analyze and gather additional context if needed.",
help="Provide specific questions or insights you want from the video."
)
if st.button("πŸ” Analyze Video", key="analyze_video_button"):
if not user_query:
st.warning("Please enter a question or insight to analyze the video.")
else:
try:
with st.spinner("Analyzing video and gathering insights..."):
# Upload and process video file
processed_video = upload_file(video_path)
while processed_video.state.name == "PROCESSING":
time.sleep(1)
processed_video = get_file(processed_video.name)
# Prompt generation for analysis
analysis_prompt = (
f"""
Analyze the content of the uploaded YouTube video.
Respond to the following query using video insights and supplementary web research:
{user_query}
Provide a detailed, user-friendly, and actionable response.
"""
)
# AI agent processing
response = multimodal_Agent.run(analysis_prompt, videos=[processed_video])
# Display the result
st.subheader("Analysis Result")
st.markdown(response.content)
except Exception as error:
st.error(f"An error occurred during analysis: {error}")
finally:
# Clean up temporary video file
Path(video_path).unlink(missing_ok=True)
except Exception as e:
st.error(f"An error occurred while downloading the video: {e}")
else:
st.info("Enter a YouTube video link to begin analysis.")