Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,37 @@
|
|
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,
|
6 |
import google.generativeai as genai
|
7 |
|
8 |
-
from moviepy.editor import VideoFileClip
|
9 |
import time
|
10 |
from pathlib import Path
|
|
|
11 |
import tempfile
|
|
|
12 |
from dotenv import load_dotenv
|
|
|
|
|
13 |
import os
|
14 |
|
15 |
-
|
16 |
-
load_dotenv()
|
17 |
-
API_KEY = os.getenv("GOOGLE_API_KEY")
|
18 |
if API_KEY:
|
19 |
genai.configure(api_key=API_KEY)
|
20 |
|
21 |
-
#
|
22 |
st.set_page_config(
|
23 |
-
page_title="Multimodal AI Agent
|
24 |
page_icon="π₯",
|
25 |
-
layout="wide"
|
26 |
)
|
27 |
|
28 |
st.title("Phidata Video AI Summarizer Agent π₯π€π¬")
|
29 |
st.header("Powered by Gemini 2.0 Flash Exp")
|
30 |
|
|
|
31 |
@st.cache_resource
|
32 |
def initialize_agent():
|
33 |
-
"""Initialize the multimodal AI agent."""
|
34 |
return Agent(
|
35 |
name="Video AI Summarizer",
|
36 |
model=Gemini(id="gemini-2.0-flash-exp"),
|
@@ -38,8 +39,8 @@ def initialize_agent():
|
|
38 |
markdown=True,
|
39 |
)
|
40 |
|
41 |
-
|
42 |
-
|
43 |
|
44 |
# File uploader
|
45 |
video_file = st.file_uploader(
|
@@ -47,39 +48,31 @@ video_file = st.file_uploader(
|
|
47 |
)
|
48 |
|
49 |
if video_file:
|
50 |
-
# Temporary storage for uploaded video
|
51 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video:
|
52 |
temp_video.write(video_file.read())
|
53 |
video_path = temp_video.name
|
54 |
|
55 |
-
# Display the uploaded video
|
56 |
st.video(video_path, format="video/mp4", start_time=0)
|
57 |
|
58 |
-
# Display video duration
|
59 |
-
clip = VideoFileClip(video_path)
|
60 |
-
st.info(f"Video Duration: {clip.duration:.2f} seconds")
|
61 |
-
|
62 |
-
# Text area for user queries
|
63 |
user_query = st.text_area(
|
64 |
"What insights are you seeking from the video?",
|
65 |
placeholder="Ask anything about the video content. The AI agent will analyze and gather additional context if needed.",
|
66 |
help="Provide specific questions or insights you want from the video."
|
67 |
)
|
68 |
|
69 |
-
# Analysis button
|
70 |
if st.button("π Analyze Video", key="analyze_video_button"):
|
71 |
if not user_query:
|
72 |
st.warning("Please enter a question or insight to analyze the video.")
|
73 |
else:
|
74 |
try:
|
75 |
with st.spinner("Processing video and gathering insights..."):
|
76 |
-
# Upload and process
|
77 |
processed_video = upload_file(video_path)
|
78 |
while processed_video.state.name == "PROCESSING":
|
79 |
time.sleep(1)
|
80 |
processed_video = get_file(processed_video.name)
|
81 |
|
82 |
-
#
|
83 |
analysis_prompt = (
|
84 |
f"""
|
85 |
Analyze the uploaded video for content and context.
|
@@ -90,8 +83,8 @@ if video_file:
|
|
90 |
"""
|
91 |
)
|
92 |
|
93 |
-
# AI agent
|
94 |
-
response =
|
95 |
|
96 |
# Display the result
|
97 |
st.subheader("Analysis Result")
|
@@ -102,40 +95,19 @@ if video_file:
|
|
102 |
finally:
|
103 |
# Clean up temporary video file
|
104 |
Path(video_path).unlink(missing_ok=True)
|
105 |
-
|
106 |
-
# Additional features
|
107 |
-
if st.checkbox("Show Video Frames", help="Preview frames from the video."):
|
108 |
-
frame_rate = st.slider("Select Frame Rate (frames per second)", 1, 10, 1)
|
109 |
-
for i in range(0, int(clip.duration), frame_rate):
|
110 |
-
frame = clip.get_frame(i)
|
111 |
-
st.image(frame, caption=f"Frame at {i} seconds")
|
112 |
-
|
113 |
-
if st.checkbox("Show Video Transcription", help="Generate a transcription of the video's audio."):
|
114 |
-
try:
|
115 |
-
import whisper
|
116 |
-
model = whisper.load_model("base")
|
117 |
-
transcription = model.transcribe(video_path)
|
118 |
-
st.text_area("Video Transcription", transcription['text'], height=300)
|
119 |
-
except ImportError:
|
120 |
-
st.error("The 'whisper' library is not installed. Please install it to enable transcription.")
|
121 |
-
|
122 |
else:
|
123 |
st.info("Upload a video file to begin analysis.")
|
124 |
|
125 |
-
#
|
126 |
st.markdown(
|
127 |
"""
|
128 |
<style>
|
129 |
.stTextArea textarea {
|
130 |
height: 100px;
|
131 |
}
|
132 |
-
.stButton > button {
|
133 |
-
background-color: #007ACC;
|
134 |
-
color: white;
|
135 |
-
border-radius: 5px;
|
136 |
-
}
|
137 |
</style>
|
138 |
""",
|
139 |
unsafe_allow_html=True
|
140 |
)
|
141 |
|
|
|
|
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 |
|
|
|
8 |
import time
|
9 |
from pathlib import Path
|
10 |
+
|
11 |
import tempfile
|
12 |
+
|
13 |
from dotenv import load_dotenv
|
14 |
+
load_dotenv()
|
15 |
+
|
16 |
import os
|
17 |
|
18 |
+
API_KEY=os.getenv("GOOGLE_API_KEY")
|
|
|
|
|
19 |
if API_KEY:
|
20 |
genai.configure(api_key=API_KEY)
|
21 |
|
22 |
+
# Page configuration
|
23 |
st.set_page_config(
|
24 |
+
page_title="Multimodal AI Agent- Video Summarizer",
|
25 |
page_icon="π₯",
|
26 |
+
layout="wide"
|
27 |
)
|
28 |
|
29 |
st.title("Phidata Video AI Summarizer Agent π₯π€π¬")
|
30 |
st.header("Powered by Gemini 2.0 Flash Exp")
|
31 |
|
32 |
+
|
33 |
@st.cache_resource
|
34 |
def initialize_agent():
|
|
|
35 |
return Agent(
|
36 |
name="Video AI Summarizer",
|
37 |
model=Gemini(id="gemini-2.0-flash-exp"),
|
|
|
39 |
markdown=True,
|
40 |
)
|
41 |
|
42 |
+
## Initialize the agent
|
43 |
+
multimodal_Agent=initialize_agent()
|
44 |
|
45 |
# File uploader
|
46 |
video_file = st.file_uploader(
|
|
|
48 |
)
|
49 |
|
50 |
if video_file:
|
|
|
51 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video:
|
52 |
temp_video.write(video_file.read())
|
53 |
video_path = temp_video.name
|
54 |
|
|
|
55 |
st.video(video_path, format="video/mp4", start_time=0)
|
56 |
|
|
|
|
|
|
|
|
|
|
|
57 |
user_query = st.text_area(
|
58 |
"What insights are you seeking from the video?",
|
59 |
placeholder="Ask anything about the video content. The AI agent will analyze and gather additional context if needed.",
|
60 |
help="Provide specific questions or insights you want from the video."
|
61 |
)
|
62 |
|
|
|
63 |
if st.button("π Analyze Video", key="analyze_video_button"):
|
64 |
if not user_query:
|
65 |
st.warning("Please enter a question or insight to analyze the video.")
|
66 |
else:
|
67 |
try:
|
68 |
with st.spinner("Processing video and gathering insights..."):
|
69 |
+
# Upload and process video file
|
70 |
processed_video = upload_file(video_path)
|
71 |
while processed_video.state.name == "PROCESSING":
|
72 |
time.sleep(1)
|
73 |
processed_video = get_file(processed_video.name)
|
74 |
|
75 |
+
# Prompt generation for analysis
|
76 |
analysis_prompt = (
|
77 |
f"""
|
78 |
Analyze the uploaded video for content and context.
|
|
|
83 |
"""
|
84 |
)
|
85 |
|
86 |
+
# AI agent processing
|
87 |
+
response = multimodal_Agent.run(analysis_prompt, videos=[processed_video])
|
88 |
|
89 |
# Display the result
|
90 |
st.subheader("Analysis Result")
|
|
|
95 |
finally:
|
96 |
# Clean up temporary video file
|
97 |
Path(video_path).unlink(missing_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
else:
|
99 |
st.info("Upload a video file to begin analysis.")
|
100 |
|
101 |
+
# Customize text area height
|
102 |
st.markdown(
|
103 |
"""
|
104 |
<style>
|
105 |
.stTextArea textarea {
|
106 |
height: 100px;
|
107 |
}
|
|
|
|
|
|
|
|
|
|
|
108 |
</style>
|
109 |
""",
|
110 |
unsafe_allow_html=True
|
111 |
)
|
112 |
|
113 |
+
|