Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from pytube import YouTube
|
|
|
|
|
|
|
| 3 |
from phi.agent import Agent
|
| 4 |
from phi.model.google import Gemini
|
| 5 |
from phi.tools.duckduckgo import DuckDuckGo
|
|
@@ -40,10 +42,10 @@ def initialize_agent():
|
|
| 40 |
# Initialize the agent
|
| 41 |
multimodal_Agent = initialize_agent()
|
| 42 |
|
| 43 |
-
# Function to get
|
| 44 |
def get_youtube_captions(youtube_url):
|
| 45 |
"""
|
| 46 |
-
Retrieves YouTube video captions using
|
| 47 |
|
| 48 |
Parameters:
|
| 49 |
- youtube_url: The URL of the YouTube video.
|
|
@@ -51,28 +53,19 @@ def get_youtube_captions(youtube_url):
|
|
| 51 |
Returns:
|
| 52 |
- The captions of the video in SRT format, or a message if captions are not available.
|
| 53 |
"""
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
if available_languages:
|
| 61 |
-
st.write(f"Available caption languages: {', '.join(available_languages)}")
|
| 62 |
-
|
| 63 |
-
# You can set a fallback language or prompt the user to select
|
| 64 |
-
language = 'en' # Change this to any available language from the list
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
return "No captions available for this video."
|
| 74 |
-
|
| 75 |
-
|
| 76 |
|
| 77 |
# YouTube video URL input
|
| 78 |
youtube_url = st.text_input(
|
|
@@ -83,13 +76,17 @@ youtube_url = st.text_input(
|
|
| 83 |
|
| 84 |
if youtube_url:
|
| 85 |
try:
|
| 86 |
-
with st.spinner("Fetching
|
| 87 |
-
# Get captions
|
| 88 |
captions = get_youtube_captions(youtube_url)
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
# User query input
|
| 95 |
user_query = st.text_area(
|
|
@@ -103,19 +100,25 @@ if youtube_url:
|
|
| 103 |
st.warning("Please enter a question or insight to analyze the video.")
|
| 104 |
else:
|
| 105 |
try:
|
| 106 |
-
with st.spinner("Analyzing
|
| 107 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
analysis_prompt = (
|
| 109 |
f"""
|
| 110 |
-
Analyze the content of the
|
| 111 |
-
Respond to the following query using
|
| 112 |
{user_query}
|
| 113 |
Provide a detailed, user-friendly, and actionable response.
|
| 114 |
"""
|
| 115 |
)
|
| 116 |
|
| 117 |
# AI agent processing
|
| 118 |
-
response = multimodal_Agent.run(analysis_prompt,
|
| 119 |
|
| 120 |
# Display the result
|
| 121 |
st.subheader("Analysis Result")
|
|
@@ -123,8 +126,7 @@ if youtube_url:
|
|
| 123 |
|
| 124 |
except Exception as error:
|
| 125 |
st.error(f"An error occurred during analysis: {error}")
|
| 126 |
-
|
| 127 |
except Exception as e:
|
| 128 |
-
st.error(f"An error occurred
|
| 129 |
else:
|
| 130 |
st.info("Enter a YouTube video link to begin analysis.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from pytube import YouTube
|
| 3 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 4 |
+
from youtube_transcript_api.formatters import SRTFormatter
|
| 5 |
from phi.agent import Agent
|
| 6 |
from phi.model.google import Gemini
|
| 7 |
from phi.tools.duckduckgo import DuckDuckGo
|
|
|
|
| 42 |
# Initialize the agent
|
| 43 |
multimodal_Agent = initialize_agent()
|
| 44 |
|
| 45 |
+
# Function to get captions using youtube-transcript-api
|
| 46 |
def get_youtube_captions(youtube_url):
|
| 47 |
"""
|
| 48 |
+
Retrieves YouTube video captions using youtube-transcript-api.
|
| 49 |
|
| 50 |
Parameters:
|
| 51 |
- youtube_url: The URL of the YouTube video.
|
|
|
|
| 53 |
Returns:
|
| 54 |
- The captions of the video in SRT format, or a message if captions are not available.
|
| 55 |
"""
|
| 56 |
+
video_id = youtube_url.split("v=")[-1].split("&")[0]
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
# Fetch transcript for the video
|
| 60 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
# Format the transcript in SRT format
|
| 63 |
+
formatter = SRTFormatter()
|
| 64 |
+
formatted_transcript = formatter.format_transcript(transcript)
|
| 65 |
+
return formatted_transcript
|
| 66 |
+
|
| 67 |
+
except Exception as e:
|
| 68 |
+
return f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# YouTube video URL input
|
| 71 |
youtube_url = st.text_input(
|
|
|
|
| 76 |
|
| 77 |
if youtube_url:
|
| 78 |
try:
|
| 79 |
+
with st.spinner("Fetching and processing the YouTube video..."):
|
| 80 |
+
# Get captions using youtube-transcript-api
|
| 81 |
captions = get_youtube_captions(youtube_url)
|
| 82 |
|
| 83 |
+
# Check if captions are available
|
| 84 |
+
if "Error" not in captions:
|
| 85 |
+
# Display captions
|
| 86 |
+
st.subheader("Video Captions:")
|
| 87 |
+
st.text(captions)
|
| 88 |
+
else:
|
| 89 |
+
st.error(captions)
|
| 90 |
|
| 91 |
# User query input
|
| 92 |
user_query = st.text_area(
|
|
|
|
| 100 |
st.warning("Please enter a question or insight to analyze the video.")
|
| 101 |
else:
|
| 102 |
try:
|
| 103 |
+
with st.spinner("Analyzing video and gathering insights..."):
|
| 104 |
+
# Upload and process the transcript (captions)
|
| 105 |
+
processed_captions = upload_file(captions)
|
| 106 |
+
while processed_captions.state.name == "PROCESSING":
|
| 107 |
+
time.sleep(1)
|
| 108 |
+
processed_captions = get_file(processed_captions.name)
|
| 109 |
+
|
| 110 |
+
# Prompt generation for analysis
|
| 111 |
analysis_prompt = (
|
| 112 |
f"""
|
| 113 |
+
Analyze the content of the uploaded YouTube video.
|
| 114 |
+
Respond to the following query using video insights and supplementary web research:
|
| 115 |
{user_query}
|
| 116 |
Provide a detailed, user-friendly, and actionable response.
|
| 117 |
"""
|
| 118 |
)
|
| 119 |
|
| 120 |
# AI agent processing
|
| 121 |
+
response = multimodal_Agent.run(analysis_prompt, videos=[processed_captions])
|
| 122 |
|
| 123 |
# Display the result
|
| 124 |
st.subheader("Analysis Result")
|
|
|
|
| 126 |
|
| 127 |
except Exception as error:
|
| 128 |
st.error(f"An error occurred during analysis: {error}")
|
|
|
|
| 129 |
except Exception as e:
|
| 130 |
+
st.error(f"An error occurred: {e}")
|
| 131 |
else:
|
| 132 |
st.info("Enter a YouTube video link to begin analysis.")
|