Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,6 @@
|
|
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
|
8 |
-
from google.generativeai import upload_file, get_file
|
9 |
-
import google.generativeai as genai
|
10 |
-
|
11 |
-
import time
|
12 |
-
from pathlib import Path
|
13 |
-
from dotenv import load_dotenv
|
14 |
-
import os
|
15 |
-
import tempfile
|
16 |
-
|
17 |
-
# Load environment variables
|
18 |
-
load_dotenv()
|
19 |
-
API_KEY = os.getenv("GOOGLE_API_KEY")
|
20 |
-
if API_KEY:
|
21 |
-
genai.configure(api_key=API_KEY)
|
22 |
-
|
23 |
-
# Page configuration
|
24 |
-
st.set_page_config(
|
25 |
-
page_title="YouTube Video Summarizer",
|
26 |
-
page_icon="π₯",
|
27 |
-
layout="wide"
|
28 |
-
)
|
29 |
-
|
30 |
-
st.title("Phidata YouTube Video Summarizer Agent π₯π€π¬")
|
31 |
-
st.header("Powered by Gemini 2.0 Flash Exp")
|
32 |
-
|
33 |
-
@st.cache_resource
|
34 |
-
def initialize_agent():
|
35 |
-
return Agent(
|
36 |
-
name="YouTube Video Summarizer",
|
37 |
-
model=Gemini(id="gemini-2.0-flash-exp"),
|
38 |
-
tools=[DuckDuckGo()],
|
39 |
-
markdown=True,
|
40 |
-
)
|
41 |
-
|
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):
|
@@ -92,44 +52,6 @@ if youtube_url:
|
|
92 |
else:
|
93 |
st.error(captions)
|
94 |
|
95 |
-
# User query input
|
96 |
-
user_query = st.text_area(
|
97 |
-
"What insights are you seeking from the video?",
|
98 |
-
placeholder="Ask anything about the video content. The AI agent will analyze and gather additional context if needed.",
|
99 |
-
help="Provide specific questions or insights you want from the video."
|
100 |
-
)
|
101 |
-
|
102 |
-
if st.button("π Analyze Video", key="analyze_video_button"):
|
103 |
-
if not user_query:
|
104 |
-
st.warning("Please enter a question or insight to analyze the video.")
|
105 |
-
else:
|
106 |
-
try:
|
107 |
-
with st.spinner("Analyzing video and gathering insights..."):
|
108 |
-
# Upload and process the transcript (captions)
|
109 |
-
processed_captions = upload_file(captions)
|
110 |
-
while processed_captions.state.name == "PROCESSING":
|
111 |
-
time.sleep(1)
|
112 |
-
processed_captions = get_file(processed_captions.name)
|
113 |
-
|
114 |
-
# Prompt generation for analysis
|
115 |
-
analysis_prompt = (
|
116 |
-
f"""
|
117 |
-
Analyze the content of the uploaded YouTube video.
|
118 |
-
Respond to the following query using video insights and supplementary web research:
|
119 |
-
{user_query}
|
120 |
-
Provide a detailed, user-friendly, and actionable response.
|
121 |
-
"""
|
122 |
-
)
|
123 |
-
|
124 |
-
# AI agent processing
|
125 |
-
response = multimodal_Agent.run(analysis_prompt, videos=[processed_captions])
|
126 |
-
|
127 |
-
# Display the result
|
128 |
-
st.subheader("Analysis Result")
|
129 |
-
st.markdown(response.content)
|
130 |
-
|
131 |
-
except Exception as error:
|
132 |
-
st.error(f"An error occurred during analysis: {error}")
|
133 |
except Exception as e:
|
134 |
st.error(f"An error occurred: {e}")
|
135 |
else:
|
|
|
1 |
import streamlit as st
|
|
|
2 |
from youtube_transcript_api import YouTubeTranscriptApi
|
3 |
from youtube_transcript_api.formatters import SRTFormatter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# Function to get captions using youtube-transcript-api
|
6 |
def get_youtube_captions(youtube_url):
|
|
|
52 |
else:
|
53 |
st.error(captions)
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
except Exception as e:
|
56 |
st.error(f"An error occurred: {e}")
|
57 |
else:
|