Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,15 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 2 |
from phi.agent import Agent
|
| 3 |
from phi.model.google import Gemini
|
| 4 |
from phi.tools.duckduckgo import DuckDuckGo
|
| 5 |
from google.generativeai import upload_file, get_file
|
| 6 |
import google.generativeai as genai
|
| 7 |
import time
|
| 8 |
-
from pathlib import Path
|
| 9 |
-
import tempfile
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
import os
|
| 12 |
-
from pytube import YouTube # Import pytube for downloading YouTube videos
|
| 13 |
|
| 14 |
load_dotenv()
|
| 15 |
|
|
@@ -19,15 +19,15 @@ if API_KEY:
|
|
| 19 |
|
| 20 |
# Page configuration
|
| 21 |
st.set_page_config(
|
| 22 |
-
page_title="
|
| 23 |
page_icon="π₯",
|
| 24 |
layout="wide"
|
| 25 |
)
|
| 26 |
|
| 27 |
-
st.title("
|
| 28 |
st.header("Powered by Gemini 2.0 Flash Exp")
|
| 29 |
|
| 30 |
-
|
| 31 |
@st.cache_resource
|
| 32 |
def initialize_agent():
|
| 33 |
return Agent(
|
|
@@ -37,69 +37,76 @@ def initialize_agent():
|
|
| 37 |
markdown=True,
|
| 38 |
)
|
| 39 |
|
| 40 |
-
# Initialize the agent
|
| 41 |
multimodal_Agent = initialize_agent()
|
| 42 |
|
| 43 |
-
# YouTube
|
| 44 |
video_url = st.text_input(
|
| 45 |
-
"Enter a YouTube video URL",
|
| 46 |
-
help="Paste a YouTube video link
|
| 47 |
)
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
if video_url:
|
| 50 |
try:
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
st.video(video_path, format="video/mp4", start_time=0)
|
| 61 |
|
| 62 |
user_query = st.text_area(
|
| 63 |
-
"What insights
|
| 64 |
-
placeholder="Ask anything about the
|
| 65 |
-
help="Provide specific questions or insights you want from the
|
| 66 |
)
|
| 67 |
|
| 68 |
-
if st.button("π
|
| 69 |
if not user_query:
|
| 70 |
-
st.warning("Please enter a
|
| 71 |
else:
|
| 72 |
try:
|
| 73 |
-
with st.spinner("Processing
|
| 74 |
-
# Upload and process the video file
|
| 75 |
-
processed_video = upload_file(video_path)
|
| 76 |
-
while processed_video.state.name == "PROCESSING":
|
| 77 |
-
time.sleep(1)
|
| 78 |
-
processed_video = get_file(processed_video.name)
|
| 79 |
-
|
| 80 |
# Prompt generation for analysis
|
| 81 |
analysis_prompt = (
|
| 82 |
f"""
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
{user_query}
|
| 86 |
-
|
|
|
|
| 87 |
"""
|
| 88 |
)
|
| 89 |
|
| 90 |
# AI agent processing
|
| 91 |
-
response = multimodal_Agent.run(analysis_prompt
|
| 92 |
|
| 93 |
# Display the result
|
| 94 |
-
st.subheader("
|
| 95 |
st.markdown(response.content)
|
| 96 |
|
| 97 |
except Exception as error:
|
| 98 |
st.error(f"An error occurred during analysis: {error}")
|
| 99 |
-
finally:
|
| 100 |
-
# Clean up temporary video file
|
| 101 |
-
Path(video_path).unlink(missing_ok=True)
|
| 102 |
except Exception as e:
|
| 103 |
-
st.error(f"Error
|
| 104 |
else:
|
| 105 |
-
st.info("Paste a YouTube video URL to begin analysis.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import re
|
| 3 |
+
import requests
|
| 4 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 5 |
from phi.agent import Agent
|
| 6 |
from phi.model.google import Gemini
|
| 7 |
from phi.tools.duckduckgo import DuckDuckGo
|
| 8 |
from google.generativeai import upload_file, get_file
|
| 9 |
import google.generativeai as genai
|
| 10 |
import time
|
|
|
|
|
|
|
| 11 |
from dotenv import load_dotenv
|
| 12 |
import os
|
|
|
|
| 13 |
|
| 14 |
load_dotenv()
|
| 15 |
|
|
|
|
| 19 |
|
| 20 |
# Page configuration
|
| 21 |
st.set_page_config(
|
| 22 |
+
page_title="Video Summarizer with Transcript Extraction",
|
| 23 |
page_icon="π₯",
|
| 24 |
layout="wide"
|
| 25 |
)
|
| 26 |
|
| 27 |
+
st.title("Video AI Summarizer with Transcript π₯π€π¬")
|
| 28 |
st.header("Powered by Gemini 2.0 Flash Exp")
|
| 29 |
|
| 30 |
+
# Initialize the agent
|
| 31 |
@st.cache_resource
|
| 32 |
def initialize_agent():
|
| 33 |
return Agent(
|
|
|
|
| 37 |
markdown=True,
|
| 38 |
)
|
| 39 |
|
|
|
|
| 40 |
multimodal_Agent = initialize_agent()
|
| 41 |
|
| 42 |
+
# YouTube URL input
|
| 43 |
video_url = st.text_input(
|
| 44 |
+
"Enter a YouTube video URL",
|
| 45 |
+
help="Paste a YouTube video link to extract the transcript and generate a summary"
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# Function to extract YouTube video ID from URL
|
| 49 |
+
def extract_video_id(url):
|
| 50 |
+
match = re.search(r"v=([a-zA-Z0-9_-]+)", url)
|
| 51 |
+
if match:
|
| 52 |
+
return match.group(1)
|
| 53 |
+
else:
|
| 54 |
+
raise ValueError("Invalid YouTube URL")
|
| 55 |
+
|
| 56 |
+
# Function to get the transcript using the YouTubeTranscriptApi
|
| 57 |
+
def get_transcript(video_id):
|
| 58 |
+
transcript_raw = YouTubeTranscriptApi.get_transcript(video_id, languages=['en', 'es', 'ko'])
|
| 59 |
+
transcript_full = ' '.join([i['text'] for i in transcript_raw])
|
| 60 |
+
return transcript_full
|
| 61 |
+
|
| 62 |
+
# Process the YouTube URL
|
| 63 |
if video_url:
|
| 64 |
try:
|
| 65 |
+
# Extract video ID from the URL
|
| 66 |
+
video_id = extract_video_id(video_url)
|
| 67 |
+
|
| 68 |
+
# Get the transcript for the video
|
| 69 |
+
transcript = get_transcript(video_id)
|
| 70 |
+
|
| 71 |
+
# Display the transcript
|
| 72 |
+
st.subheader("Video Transcript")
|
| 73 |
+
st.text_area("Transcript", transcript, height=200)
|
|
|
|
| 74 |
|
| 75 |
user_query = st.text_area(
|
| 76 |
+
"What insights or summary would you like from the transcript?",
|
| 77 |
+
placeholder="Ask anything about the transcript or request a summary.",
|
| 78 |
+
help="Provide specific questions or insights you want from the transcript."
|
| 79 |
)
|
| 80 |
|
| 81 |
+
if st.button("π Generate Summary", key="generate_summary_button"):
|
| 82 |
if not user_query:
|
| 83 |
+
st.warning("Please enter a query or request a summary of the transcript.")
|
| 84 |
else:
|
| 85 |
try:
|
| 86 |
+
with st.spinner("Processing transcript and generating summary..."):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
# Prompt generation for analysis
|
| 88 |
analysis_prompt = (
|
| 89 |
f"""
|
| 90 |
+
Here is the transcript of the video:
|
| 91 |
+
{transcript}
|
| 92 |
+
|
| 93 |
+
The user has requested the following insight/summary:
|
| 94 |
{user_query}
|
| 95 |
+
|
| 96 |
+
Please provide a detailed, user-friendly, and actionable summary based on the provided transcript.
|
| 97 |
"""
|
| 98 |
)
|
| 99 |
|
| 100 |
# AI agent processing
|
| 101 |
+
response = multimodal_Agent.run(analysis_prompt)
|
| 102 |
|
| 103 |
# Display the result
|
| 104 |
+
st.subheader("Summary Result")
|
| 105 |
st.markdown(response.content)
|
| 106 |
|
| 107 |
except Exception as error:
|
| 108 |
st.error(f"An error occurred during analysis: {error}")
|
|
|
|
|
|
|
|
|
|
| 109 |
except Exception as e:
|
| 110 |
+
st.error(f"Error processing the YouTube video: {e}")
|
| 111 |
else:
|
| 112 |
+
st.info("Paste a YouTube video URL to begin transcript extraction and analysis.")
|