File size: 3,760 Bytes
eaa4026 f0b9c7c e1c36f1 eaa4026 e1c36f1 f0b9c7c e1c36f1 f0b9c7c e1c36f1 85acead f0b9c7c e1c36f1 2569351 f0b9c7c e1c36f1 f0b9c7c eaa4026 f0b9c7c e1c36f1 eaa4026 f0b9c7c eaa4026 e1c36f1 f0b9c7c e1c36f1 f0b9c7c e1c36f1 f0b9c7c 17ce9b6 e1c36f1 f0b9c7c e1c36f1 f0b9c7c e1c36f1 f0b9c7c e1c36f1 f0b9c7c e1c36f1 f0b9c7c e1c36f1 eaa4026 e1c36f1 eaa4026 f0b9c7c eaa4026 f0b9c7c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
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.")
|