|
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_dotenv() |
|
API_KEY = os.getenv("GOOGLE_API_KEY") |
|
if API_KEY: |
|
genai.configure(api_key=API_KEY) |
|
|
|
|
|
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, |
|
) |
|
|
|
|
|
multimodal_Agent = initialize_agent() |
|
|
|
|
|
def get_youtube_captions(youtube_url): |
|
""" |
|
Retrieves YouTube video captions using pytube. |
|
|
|
Parameters: |
|
- youtube_url: The URL of the YouTube video. |
|
|
|
Returns: |
|
- The captions of the video in SRT format, or a message if captions are not available. |
|
""" |
|
yt = YouTube(youtube_url) |
|
|
|
|
|
caption_tracks = yt.caption_tracks |
|
available_languages = [track.language_code for track in caption_tracks] |
|
|
|
if available_languages: |
|
st.write(f"Available caption languages: {', '.join(available_languages)}") |
|
|
|
|
|
language = 'en' |
|
|
|
|
|
if language in yt.captions: |
|
caption = yt.captions[language] |
|
return caption.generate_srt_captions() |
|
else: |
|
return f"Captions not available in the selected language ({language})." |
|
else: |
|
return "No captions available for this video." |
|
|
|
|
|
|
|
|
|
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("Fetching captions from the YouTube video..."): |
|
|
|
captions = get_youtube_captions(youtube_url) |
|
|
|
|
|
st.subheader("Video Captions (SRT format)") |
|
st.text(captions) |
|
|
|
|
|
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 captions and gathering insights..."): |
|
|
|
analysis_prompt = ( |
|
f""" |
|
Analyze the content of the following YouTube video captions. |
|
Respond to the following query using the video captions and supplementary web research: |
|
{user_query} |
|
Provide a detailed, user-friendly, and actionable response. |
|
""" |
|
) |
|
|
|
|
|
response = multimodal_Agent.run(analysis_prompt, video_captions=captions) |
|
|
|
|
|
st.subheader("Analysis Result") |
|
st.markdown(response.content) |
|
|
|
except Exception as error: |
|
st.error(f"An error occurred during analysis: {error}") |
|
|
|
except Exception as e: |
|
st.error(f"An error occurred while fetching captions: {e}") |
|
else: |
|
st.info("Enter a YouTube video link to begin analysis.") |
|
|