import streamlit as st import re import requests from youtube_transcript_api import YouTubeTranscriptApi 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 dotenv import load_dotenv import os 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="Video Summarizer with Transcript Extraction", page_icon="🎥", layout="wide" ) st.title("Video AI Summarizer with Transcript 🎥🎤🖬") st.header("Powered by Gemini 2.0 Flash Exp") # Initialize the agent @st.cache_resource def initialize_agent(): return Agent( name="Video AI Summarizer", model=Gemini(id="gemini-2.0-flash-exp"), tools=[DuckDuckGo()], markdown=True, ) multimodal_Agent = initialize_agent() # YouTube URL input video_url = st.text_input( "Enter a YouTube video URL", help="Paste a YouTube video link to extract the transcript and generate a summary" ) # Function to extract YouTube video ID from URL def extract_video_id(url): match = re.search(r"v=([a-zA-Z0-9_-]+)", url) if match: return match.group(1) else: raise ValueError("Invalid YouTube URL") # Function to get the transcript using the YouTubeTranscriptApi def get_transcript(video_id): transcript_raw = YouTubeTranscriptApi.get_transcript(video_id, languages=['en', 'es', 'ko']) transcript_full = ' '.join([i['text'] for i in transcript_raw]) return transcript_full # Process the YouTube URL if video_url: try: # Extract video ID from the URL video_id = extract_video_id(video_url) # Get the transcript for the video transcript = get_transcript(video_id) # Display the transcript st.subheader("Video Transcript") st.text_area("Transcript", transcript, height=200) user_query = st.text_area( "What insights or summary would you like from the transcript?", placeholder="Ask anything about the transcript or request a summary.", help="Provide specific questions or insights you want from the transcript." ) if st.button("🔍 Generate Summary", key="generate_summary_button"): if not user_query: st.warning("Please enter a query or request a summary of the transcript.") else: try: with st.spinner("Processing transcript and generating summary..."): # Prompt generation for analysis analysis_prompt = ( f""" Here is the transcript of the video: {transcript} The user has requested the following insight/summary: {user_query} Please provide a detailed, user-friendly, and actionable summary based on the provided transcript. """ ) # AI agent processing response = multimodal_Agent.run(analysis_prompt) # Display the result st.subheader("Summary 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"Error processing the YouTube video: {e}") else: st.info("Paste a YouTube video URL to begin transcript extraction and analysis.")