-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
165 lines (90 loc) · 3.75 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import streamlit as st
from phi.agent import Agent
from phi.model.google import Gemini
from phi.tools.duckduckgo import DuckDuckGo
from google.generativeai import upload_file, get_file
import google.generativeai as genai
import os
import time
from pathlib import Path
import tempfile
st.set_page_config(
page_title='VidWise',
page_icon='🎥',
layout='wide'
)
st.title('VidWise 🎥')
st.header('Powered by Google Gemini 1.5 Flash and PhiData')
if 'GOOGLE_API_KEY' not in st.session_state:
st.session_state.GOOGLE_API_KEY = None
api_key_input = st.text_input(
"Enter your Google API Key:",
type="password",
help="This key will be used to access Google Gemini APIs."
)
if st.button("Set API Key"):
if api_key_input:
try:
os.environ["GOOGLE_API_KEY"] = api_key_input
st.session_state.GOOGLE_API_KEY = api_key_input
genai.configure(api_key=api_key_input)
st.cache_data.clear()
st.success("API Key set successfully!")
except Exception as e:
st.error(f"Failed to configure API Key: {e}")
else:
st.warning('No Gemini API key found. Please set your API key in order to proceed further')
if st.session_state.GOOGLE_API_KEY:
@st.cache_resource
def initialize_agent():
return Agent(
name='Video AI Analyzer',
model=Gemini(id='gemini-1.5-flash'),
tools=[DuckDuckGo()],
markdown=True
)
multimodal_agent = initialize_agent()
video_file_upload = st.file_uploader(
'Upload your video',
type=['mp4', 'mov', 'avi'],
help='Upload your video for analysis'
)
if video_file_upload:
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video:
temp_video.write(video_file_upload.read())
video_path = temp_video.name
st.video(video_path, format='video/mp4', start_time=0)
user_question = st.text_area(
'What insights are you seeking from this video?',
placeholder='Ask anything about the video content.',
help='Provide specific content or insights you want from the video.'
)
if st.button('🔍 Analyze Video', key='analyze_video_button'):
if not user_question:
st.warning('Please enter your question')
else:
try:
with st.spinner('Processing video and gathering insights...'):
processed_video = upload_file(video_path)
while processed_video.state.name == 'PROCESSING':
time.sleep(1)
processed_video = get_file(processed_video.name)
analysis_prompt = (
f"""
Analyze the uploaded video for content and context.
Respond to the following query using video insights and supplementary web research:
{user_question}
Provide a detailed, user-friendly, and actionable response.
"""
)
res = multimodal_agent.run(analysis_prompt, videos=[processed_video])
st.subheader("Analysis Result")
st.markdown(res.content)
except Exception as error:
st.error(f"An error occurred during analysis: {error}")
finally:
Path(video_path).unlink(missing_ok=True)
else:
st.info("Upload a video file to begin analysis.")
else:
st.info("Please set your Google API Key to proceed.")