|
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) |
|
|
|
|
|
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") |
|
|
|
|
|
@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() |
|
|
|
|
|
video_url = st.text_input( |
|
"Enter a YouTube video URL", |
|
help="Paste a YouTube video link to extract the transcript and generate a summary" |
|
) |
|
|
|
|
|
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") |
|
|
|
|
|
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 |
|
|
|
|
|
if video_url: |
|
try: |
|
|
|
video_id = extract_video_id(video_url) |
|
|
|
|
|
transcript = get_transcript(video_id) |
|
|
|
|
|
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..."): |
|
|
|
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. |
|
""" |
|
) |
|
|
|
|
|
response = multimodal_Agent.run(analysis_prompt) |
|
|
|
|
|
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.") |
|
|