Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,27 @@
|
|
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 phi.tools.youtube_tools import YouTubeTools
|
|
|
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 |
|
13 |
# Load environment variables
|
14 |
load_dotenv()
|
15 |
|
16 |
-
#
|
17 |
API_KEY = os.getenv("GOOGLE_API_KEY")
|
18 |
-
if
|
19 |
-
|
20 |
-
st.stop()
|
21 |
|
22 |
-
|
|
|
23 |
|
24 |
# Page configuration
|
25 |
st.set_page_config(
|
@@ -29,162 +31,156 @@ st.set_page_config(
|
|
29 |
)
|
30 |
|
31 |
st.title("Phidata Video AI Summarizer Agent π₯π€π¬")
|
32 |
-
st.header("Powered by Gemini 2.0 Flash Exp")
|
33 |
-
|
34 |
-
# Initialize agents
|
35 |
-
@st.cache_resource
|
36 |
-
def initialize_multimodal_agent():
|
37 |
-
return Agent(
|
38 |
-
name="Video AI Summarizer",
|
39 |
-
model=Gemini(id="gemini-2.0-flash-exp"),
|
40 |
-
tools=[DuckDuckGo(), YouTubeTools()],
|
41 |
-
markdown=True,
|
42 |
-
)
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
return Agent(
|
47 |
-
name="YouTube Summarizer",
|
48 |
-
model=Gemini(id="gemini-2.0-flash-exp"),
|
49 |
-
tools=[YouTubeTools()],
|
50 |
-
get_youtube_video_captions = True,
|
51 |
-
show_tool_calls=True,
|
52 |
-
description="You are a YouTube agent. Obtain the captions of a YouTube video and answer questions.",
|
53 |
-
markdown=True,
|
54 |
|
55 |
-
|
|
|
|
|
|
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
Provide a structured, detailed response with clear sections and actionable insights.
|
71 |
-
"""
|
72 |
-
|
73 |
-
# Create tabs for different input methods
|
74 |
-
video_tab, youtube_tab = st.tabs(["Upload Video", "YouTube URL"])
|
75 |
-
|
76 |
-
# Upload Video Tab
|
77 |
-
with video_tab:
|
78 |
-
upload_query = st.text_area(
|
79 |
-
"What insights are you seeking from the uploaded video?",
|
80 |
-
placeholder="Ask anything about the video content. The AI agent will analyze and gather additional context if needed.",
|
81 |
-
help="Provide specific questions or insights you want from the uploaded video.",
|
82 |
-
key="upload_query"
|
83 |
-
)
|
84 |
-
|
85 |
video_file = st.file_uploader(
|
86 |
-
"Upload a video file",
|
87 |
-
type=['mp4', 'mov', 'avi'],
|
88 |
-
help="Upload a video for AI analysis"
|
89 |
)
|
90 |
|
91 |
if video_file:
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
st.warning("Please enter a question or insight to analyze the video.")
|
97 |
else:
|
98 |
try:
|
99 |
with st.spinner("Processing video and gathering insights..."):
|
100 |
-
#
|
101 |
-
|
102 |
-
temp_video.write(video_file.getvalue())
|
103 |
-
video_path = temp_video.name
|
104 |
-
|
105 |
-
# Upload and process video
|
106 |
-
processed_video = genai.upload_file(video_path)
|
107 |
-
|
108 |
-
# Wait for processing with timeout
|
109 |
-
timeout = 60 # 60 seconds timeout
|
110 |
-
start_time = time.time()
|
111 |
while processed_video.state.name == "PROCESSING":
|
112 |
-
if time.time() - start_time > timeout:
|
113 |
-
raise TimeoutError("Video processing timed out")
|
114 |
time.sleep(1)
|
115 |
-
processed_video =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
|
117 |
-
#
|
118 |
-
analysis_prompt = generate_analysis_prompt(upload_query, "uploaded video")
|
119 |
response = multimodal_Agent.run(analysis_prompt, videos=[processed_video])
|
120 |
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
st.markdown(response.content)
|
125 |
|
126 |
-
except TimeoutError:
|
127 |
-
st.error("Video processing timed out. Please try with a shorter video.")
|
128 |
except Exception as error:
|
129 |
-
st.error(f"An error occurred: {
|
130 |
finally:
|
131 |
-
# Clean up temporary file
|
132 |
Path(video_path).unlink(missing_ok=True)
|
133 |
else:
|
134 |
-
st.info("
|
135 |
-
|
136 |
-
# YouTube
|
137 |
-
with
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
youtube_prompt = generate_analysis_prompt(youtube_query, youtube_url)
|
156 |
-
|
157 |
-
# Use the YouTube-specific agent to process the video
|
158 |
-
response = youtube_agent.run(youtube_prompt, markdown=True)
|
159 |
-
|
160 |
-
# Display results
|
161 |
-
st.success("Analysis complete!")
|
162 |
-
st.subheader("Analysis Results")
|
163 |
-
st.markdown(response.content)
|
164 |
-
|
165 |
-
except Exception as error:
|
166 |
-
st.error(f"An error occurred: {str(error)}")
|
167 |
else:
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from phi.agent import Agent
|
3 |
from phi.model.google import Gemini
|
4 |
+
from phi.model.groq import Groq
|
5 |
from phi.tools.duckduckgo import DuckDuckGo
|
6 |
from phi.tools.youtube_tools import YouTubeTools
|
7 |
+
from google.generativeai import upload_file, get_file
|
8 |
import google.generativeai as genai
|
9 |
+
from dotenv import load_dotenv
|
10 |
import time
|
11 |
from pathlib import Path
|
12 |
import tempfile
|
|
|
13 |
import os
|
14 |
|
15 |
# Load environment variables
|
16 |
load_dotenv()
|
17 |
|
18 |
+
# Google API Key
|
19 |
API_KEY = os.getenv("GOOGLE_API_KEY")
|
20 |
+
if API_KEY:
|
21 |
+
genai.configure(api_key=API_KEY)
|
|
|
22 |
|
23 |
+
# Groq API Key
|
24 |
+
groq_api_key = os.getenv("Groq_Api_key")
|
25 |
|
26 |
# Page configuration
|
27 |
st.set_page_config(
|
|
|
31 |
)
|
32 |
|
33 |
st.title("Phidata Video AI Summarizer Agent π₯π€π¬")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
# Tabs for video upload and YouTube summarization
|
36 |
+
tab1, tab2 = st.tabs(["π₯ Video Upload", "π YouTube Summarizer"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
# Tab 1: Video Upload and Analysis
|
39 |
+
with tab1:
|
40 |
+
st.header("π₯ Analyze Uploaded Video")
|
41 |
+
st.subheader("Powered by Gemini 2.0 Flash Exp")
|
42 |
|
43 |
+
@st.cache_resource
|
44 |
+
def initialize_multimodal_agent():
|
45 |
+
return Agent(
|
46 |
+
name="Video AI Summarizer",
|
47 |
+
model=Gemini(id="gemini-2.0-flash-exp"),
|
48 |
+
tools=[DuckDuckGo()],
|
49 |
+
markdown=True,
|
50 |
+
)
|
51 |
+
|
52 |
+
multimodal_Agent = initialize_multimodal_agent()
|
53 |
+
|
54 |
+
# File uploader
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
video_file = st.file_uploader(
|
56 |
+
"Upload a video file", type=['mp4', 'mov', 'avi'], help="Upload a video for AI analysis"
|
|
|
|
|
57 |
)
|
58 |
|
59 |
if video_file:
|
60 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video:
|
61 |
+
temp_video.write(video_file.read())
|
62 |
+
video_path = temp_video.name
|
63 |
+
|
64 |
+
st.video(video_path, format="video/mp4", start_time=0)
|
65 |
+
|
66 |
+
user_query = st.text_area(
|
67 |
+
"What insights are you seeking from the video?",
|
68 |
+
placeholder="Ask anything about the video content. The AI agent will analyze and gather additional context if needed.",
|
69 |
+
help="Provide specific questions or insights you want from the video."
|
70 |
+
)
|
71 |
+
|
72 |
+
if st.button("π Analyze Video", key="analyze_video_button"):
|
73 |
+
if not user_query:
|
74 |
st.warning("Please enter a question or insight to analyze the video.")
|
75 |
else:
|
76 |
try:
|
77 |
with st.spinner("Processing video and gathering insights..."):
|
78 |
+
# Upload and process video file
|
79 |
+
processed_video = upload_file(video_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
while processed_video.state.name == "PROCESSING":
|
|
|
|
|
81 |
time.sleep(1)
|
82 |
+
processed_video = get_file(processed_video.name)
|
83 |
+
|
84 |
+
# Prompt generation for analysis
|
85 |
+
analysis_prompt = (
|
86 |
+
f"""
|
87 |
+
Analyze the uploaded video for content and context.
|
88 |
+
Respond to the following query using video insights and supplementary web research:
|
89 |
+
{user_query}
|
90 |
+
|
91 |
+
Provide a detailed, user-friendly, and actionable response.
|
92 |
+
"""
|
93 |
+
)
|
94 |
|
95 |
+
# AI agent processing
|
|
|
96 |
response = multimodal_Agent.run(analysis_prompt, videos=[processed_video])
|
97 |
|
98 |
+
# Display the result
|
99 |
+
st.subheader("Analysis Result")
|
100 |
+
st.markdown(response.content)
|
|
|
101 |
|
|
|
|
|
102 |
except Exception as error:
|
103 |
+
st.error(f"An error occurred during analysis: {error}")
|
104 |
finally:
|
105 |
+
# Clean up temporary video file
|
106 |
Path(video_path).unlink(missing_ok=True)
|
107 |
else:
|
108 |
+
st.info("Upload a video file to begin analysis.")
|
109 |
+
|
110 |
+
# Tab 2: YouTube Video Summarizer with Search Results
|
111 |
+
with tab2:
|
112 |
+
st.header("π Summarize and Analyze YouTube Videos with Search Results")
|
113 |
+
|
114 |
+
@st.cache_resource
|
115 |
+
def initialize_youtube_agent():
|
116 |
+
return Agent(
|
117 |
+
tools=[YouTubeTools(), DuckDuckGo()],
|
118 |
+
model=Groq(id="llama-3.3-70b-versatile", api_key=groq_api_key),
|
119 |
+
show_tool_calls=True,
|
120 |
+
description=(
|
121 |
+
"You are a YouTube agent. Obtain the captions of a YouTube video and answer questions"
|
122 |
+
"You are a YouTube analysis agent. Your task is to summarize videos, "
|
123 |
+
"identify key points, provide insights, and gather relevant search results for additional context."
|
124 |
+
),
|
125 |
+
)
|
126 |
+
|
127 |
+
if not groq_api_key:
|
128 |
+
st.error("Groq API key not found. Ensure it is set in the .env file.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
else:
|
130 |
+
youtube_agent = initialize_youtube_agent()
|
131 |
+
|
132 |
+
video_url = st.text_input("Enter YouTube video URL:")
|
133 |
+
|
134 |
+
user_query = st.text_area(
|
135 |
+
"What specific insights or analysis are you looking for?",
|
136 |
+
placeholder="For example: 'Summarize the video content, analyze the speaker's tone, and provide related key takeaways.'",
|
137 |
+
help="Provide specific instructions for a more detailed analysis of the YouTube video."
|
138 |
+
)
|
139 |
+
|
140 |
+
if st.button("Generate Analysis with Search Results", key="analyze_youtube_search_button"):
|
141 |
+
if not video_url.strip():
|
142 |
+
st.warning("Please enter a valid YouTube video URL.")
|
143 |
+
elif not user_query.strip():
|
144 |
+
st.warning("Please provide specific instructions for analysis.")
|
145 |
+
else:
|
146 |
+
with st.spinner("Processing video and generating insights..."):
|
147 |
+
try:
|
148 |
+
# Enhanced Prompt for Tab 2
|
149 |
+
enhanced_prompt = (
|
150 |
+
f"""
|
151 |
+
Analyze the YouTube video available at: {video_url}
|
152 |
+
|
153 |
+
Your task:
|
154 |
+
- Watch and analyze the video content.
|
155 |
+
- Summarize the main ideas, arguments, or points presented in the video.
|
156 |
+
- Identify key takeaways, such as statistics, quotes, or important insights.
|
157 |
+
- Analyze the speaker's tone, purpose, and the intended audience for the video.
|
158 |
+
- Highlight any actionable insights, lessons, or suggestions the video provides.
|
159 |
+
|
160 |
+
In addition to summarizing the video, perform a web search to gather supplementary information about the video's topic.
|
161 |
+
For example:
|
162 |
+
- Provide links to relevant blogs, articles, or news stories.
|
163 |
+
- Highlight related insights or key trends from your search.
|
164 |
+
- Suggest further reading materials for users who wish to dive deeper into the topic.
|
165 |
+
|
166 |
+
Final Output:
|
167 |
+
- Present the analysis in a concise, user-friendly format.
|
168 |
+
- Structure the output clearly with headings for each section, such as 'Summary', 'Key Insights', 'Search Results', etc.
|
169 |
+
- If any supplementary information conflicts with the video content, mention it explicitly.
|
170 |
+
|
171 |
+
User Query/Focus:
|
172 |
+
{user_query}
|
173 |
+
"""
|
174 |
+
)
|
175 |
+
|
176 |
+
# AI agent generates response for analysis and search
|
177 |
+
response = youtube_agent.print_response(enhanced_prompt, markdown=True)
|
178 |
+
|
179 |
+
# Display the results
|
180 |
+
st.subheader("Video Analysis and Summary")
|
181 |
+
st.markdown(response)
|
182 |
+
|
183 |
+
except Exception as e:
|
184 |
+
st.error(f"An error occurred: {e}")
|
185 |
+
|
186 |
+
|