Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115import streamlit as stfrom yt_dlp import YoutubeDLfrom phi.agent import Agentfrom phi.model.google import Geminifrom phi.tools.duckduckgo import DuckDuckGofrom google.generativeai import upload_file, get_fileimport google.generativeai as genai
import timefrom pathlib import Pathfrom dotenv import load_dotenvimport osimport tempfile
# Load environment variablesload_dotenv()API_KEY = os.getenv("GOOGLE_API_KEY")if API_KEY: genai.configure(api_key=API_KEY)
# Page configurationst.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_resourcedef initialize_agent(): return Agent( name="YouTube Video Summarizer", model=Gemini(id="gemini-2.0-flash-exp"), tools=[DuckDuckGo()], markdown=True, )
# Initialize the agentmultimodal_Agent = initialize_agent()
# Function to download YouTube video using yt-dlpdef download_youtube_video(youtube_url): with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video: ydl_opts = { 'format': 'bestvideo+bestaudio/best', 'outtmpl': temp_video.name, 'quiet': True, } with YoutubeDL(ydl_opts) as ydl: ydl.download([youtube_url]) return temp_video.name
# YouTube video URL inputyoutube_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 yt-dlp 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.")app.pyAdded+115