Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,13 +6,6 @@ import time
|
|
| 6 |
import mimetypes
|
| 7 |
from pathlib import Path
|
| 8 |
|
| 9 |
-
# --- Get API key from environment variable or user input ---
|
| 10 |
-
def get_api_key():
|
| 11 |
-
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY", "")
|
| 12 |
-
if not GOOGLE_API_KEY:
|
| 13 |
-
GOOGLE_API_KEY = st.text_input("Enter your Gemini API key", type="password")
|
| 14 |
-
return GOOGLE_API_KEY or "AIzaSyChkOeyjQb4wkjMfgROQKAkPIxP0GFpqqc" # Warning: Hardcoded key
|
| 15 |
-
|
| 16 |
# VideoProcessor class
|
| 17 |
class VideoProcessor:
|
| 18 |
def __init__(self, api_key):
|
|
@@ -54,6 +47,8 @@ if "video_file" not in st.session_state:
|
|
| 54 |
st.session_state.video_file = None
|
| 55 |
if "video_name" not in st.session_state:
|
| 56 |
st.session_state.video_name = None
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# Main app function
|
| 59 |
def main():
|
|
@@ -62,16 +57,77 @@ def main():
|
|
| 62 |
st.markdown("---")
|
| 63 |
|
| 64 |
# Step 1: API Key input
|
| 65 |
-
st.subheader("Step 1:
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
if not api_key:
|
| 69 |
-
st.error("Please enter your API key to proceed.")
|
| 70 |
st.stop()
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
# Step 2: Upload Video
|
| 73 |
-
st.subheader("Step 2: Upload
|
| 74 |
-
uploaded_file = st.file_uploader("
|
| 75 |
|
| 76 |
if uploaded_file:
|
| 77 |
# Validate video file
|
|
@@ -82,30 +138,26 @@ def main():
|
|
| 82 |
# Display file info
|
| 83 |
col1, col2 = st.columns(2)
|
| 84 |
with col1:
|
| 85 |
-
st.info(f"
|
| 86 |
with col2:
|
| 87 |
-
st.info(f"
|
| 88 |
|
| 89 |
# Check file size limit (200MB for Gemini)
|
| 90 |
if file_size > 200:
|
| 91 |
-
st.error("
|
| 92 |
st.stop()
|
| 93 |
|
| 94 |
# Process new video if different from current
|
| 95 |
if st.session_state.video_name != uploaded_file.name:
|
| 96 |
tmp_path = None
|
| 97 |
try:
|
| 98 |
-
# Initialize processor if needed
|
| 99 |
-
if not st.session_state.video_processor:
|
| 100 |
-
st.session_state.video_processor = VideoProcessor(api_key)
|
| 101 |
-
|
| 102 |
# Create temporary file
|
| 103 |
with tempfile.NamedTemporaryFile(delete=False, suffix=Path(uploaded_file.name).suffix) as tmp:
|
| 104 |
tmp.write(uploaded_file.getvalue())
|
| 105 |
tmp_path = tmp.name
|
| 106 |
|
| 107 |
# Upload and process with progress indication
|
| 108 |
-
with st.spinner("
|
| 109 |
progress_bar = st.progress(0)
|
| 110 |
progress_bar.progress(25, text="Uploading video...")
|
| 111 |
|
|
@@ -113,19 +165,19 @@ def main():
|
|
| 113 |
progress_bar.progress(50, text="Processing video...")
|
| 114 |
|
| 115 |
processed_file = st.session_state.video_processor.wait_for_processing(video_file)
|
| 116 |
-
progress_bar.progress(100, text="
|
| 117 |
|
| 118 |
# Update session state
|
| 119 |
st.session_state.video_file = processed_file
|
| 120 |
st.session_state.video_name = uploaded_file.name
|
| 121 |
st.session_state.messages = [] # Clear previous conversation
|
| 122 |
|
| 123 |
-
st.success("
|
| 124 |
time.sleep(1) # Show success message briefly
|
| 125 |
progress_bar.empty() # Clear progress bar
|
| 126 |
|
| 127 |
except Exception as e:
|
| 128 |
-
st.error(f"
|
| 129 |
st.session_state.video_file = None
|
| 130 |
st.session_state.video_name = None
|
| 131 |
|
|
@@ -137,23 +189,32 @@ def main():
|
|
| 137 |
# Display video player
|
| 138 |
st.video(uploaded_file.getvalue())
|
| 139 |
else:
|
| 140 |
-
st.error("
|
| 141 |
|
| 142 |
# Control buttons
|
| 143 |
-
col1, col2 = st.columns(
|
| 144 |
with col1:
|
| 145 |
-
if st.button("
|
| 146 |
st.session_state.messages = []
|
| 147 |
st.rerun()
|
| 148 |
|
| 149 |
with col2:
|
| 150 |
-
if st.button("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
for key in list(st.session_state.keys()):
|
| 152 |
del st.session_state[key]
|
| 153 |
st.rerun()
|
| 154 |
|
|
|
|
|
|
|
| 155 |
# Step 3: Chat about Video
|
| 156 |
-
st.subheader("Step 3:
|
| 157 |
|
| 158 |
if st.session_state.video_file:
|
| 159 |
# Display chat history
|
|
@@ -172,19 +233,19 @@ def main():
|
|
| 172 |
# Generate and display assistant response
|
| 173 |
with st.chat_message("assistant"):
|
| 174 |
placeholder = st.empty()
|
| 175 |
-
with st.spinner("
|
| 176 |
try:
|
| 177 |
response = st.session_state.video_processor.chat_with_video(
|
| 178 |
st.session_state.video_file,
|
| 179 |
user_question
|
| 180 |
)
|
| 181 |
except Exception as e:
|
| 182 |
-
response = f"
|
| 183 |
|
| 184 |
placeholder.markdown(response)
|
| 185 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 186 |
else:
|
| 187 |
-
st.info("
|
| 188 |
|
| 189 |
if __name__ == "__main__":
|
| 190 |
main()
|
|
|
|
| 6 |
import mimetypes
|
| 7 |
from pathlib import Path
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# VideoProcessor class
|
| 10 |
class VideoProcessor:
|
| 11 |
def __init__(self, api_key):
|
|
|
|
| 47 |
st.session_state.video_file = None
|
| 48 |
if "video_name" not in st.session_state:
|
| 49 |
st.session_state.video_name = None
|
| 50 |
+
if "api_key" not in st.session_state:
|
| 51 |
+
st.session_state.api_key = ""
|
| 52 |
|
| 53 |
# Main app function
|
| 54 |
def main():
|
|
|
|
| 57 |
st.markdown("---")
|
| 58 |
|
| 59 |
# Step 1: API Key input
|
| 60 |
+
st.subheader("Step 1: API Configuration")
|
| 61 |
+
|
| 62 |
+
# Check for environment variable first
|
| 63 |
+
env_api_key = os.environ.get("GOOGLE_API_KEY", "")
|
| 64 |
+
|
| 65 |
+
if env_api_key:
|
| 66 |
+
st.info("Using API key from environment variable")
|
| 67 |
+
st.session_state.api_key = env_api_key
|
| 68 |
+
api_key = env_api_key
|
| 69 |
+
else:
|
| 70 |
+
# Create columns for better layout
|
| 71 |
+
col1, col2 = st.columns([3, 1])
|
| 72 |
+
|
| 73 |
+
with col1:
|
| 74 |
+
# API key input field
|
| 75 |
+
api_key_input = st.text_input(
|
| 76 |
+
"Gemini API Key",
|
| 77 |
+
value=st.session_state.api_key,
|
| 78 |
+
type="password",
|
| 79 |
+
placeholder="AIzaSy...",
|
| 80 |
+
help="Get your API key from https://makersuite.google.com/app/apikey"
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
with col2:
|
| 84 |
+
# Save button
|
| 85 |
+
st.markdown("<br>", unsafe_allow_html=True) # Add spacing
|
| 86 |
+
if st.button("Save API Key", type="primary"):
|
| 87 |
+
if api_key_input:
|
| 88 |
+
st.session_state.api_key = api_key_input
|
| 89 |
+
st.success("API key saved successfully")
|
| 90 |
+
# Reinitialize processor with new key
|
| 91 |
+
st.session_state.video_processor = None
|
| 92 |
+
else:
|
| 93 |
+
st.error("Please enter an API key")
|
| 94 |
+
|
| 95 |
+
api_key = st.session_state.api_key
|
| 96 |
+
|
| 97 |
+
# Show instructions if no API key
|
| 98 |
+
if not api_key:
|
| 99 |
+
st.warning("Please enter your Gemini API key to continue")
|
| 100 |
+
with st.expander("How to obtain your API key"):
|
| 101 |
+
st.markdown("""
|
| 102 |
+
1. Navigate to [Google AI Studio](https://makersuite.google.com/app/apikey)
|
| 103 |
+
2. Sign in with your Google account
|
| 104 |
+
3. Click on "Get API key" or "Create API key"
|
| 105 |
+
4. Copy the generated API key
|
| 106 |
+
5. Paste it in the field above and click "Save API Key"
|
| 107 |
+
|
| 108 |
+
**Important:** Keep your API key secure and do not share it publicly.
|
| 109 |
+
""")
|
| 110 |
|
| 111 |
if not api_key:
|
| 112 |
+
st.error("API key required. Please enter your API key to proceed.")
|
| 113 |
st.stop()
|
| 114 |
|
| 115 |
+
# Initialize processor with the API key if not already initialized
|
| 116 |
+
if api_key and not st.session_state.video_processor:
|
| 117 |
+
try:
|
| 118 |
+
st.session_state.video_processor = VideoProcessor(api_key)
|
| 119 |
+
st.success("API key validated and processor initialized")
|
| 120 |
+
except Exception as e:
|
| 121 |
+
st.error(f"Invalid API key or initialization error: {str(e)}")
|
| 122 |
+
st.session_state.api_key = ""
|
| 123 |
+
st.session_state.video_processor = None
|
| 124 |
+
st.stop()
|
| 125 |
+
|
| 126 |
+
st.markdown("---")
|
| 127 |
+
|
| 128 |
# Step 2: Upload Video
|
| 129 |
+
st.subheader("Step 2: Video Upload")
|
| 130 |
+
uploaded_file = st.file_uploader("Select a video file", type=['mp4', 'mov', 'avi', 'mkv', 'webm'])
|
| 131 |
|
| 132 |
if uploaded_file:
|
| 133 |
# Validate video file
|
|
|
|
| 138 |
# Display file info
|
| 139 |
col1, col2 = st.columns(2)
|
| 140 |
with col1:
|
| 141 |
+
st.info(f"File: {uploaded_file.name}")
|
| 142 |
with col2:
|
| 143 |
+
st.info(f"Size: {file_size:.2f} MB")
|
| 144 |
|
| 145 |
# Check file size limit (200MB for Gemini)
|
| 146 |
if file_size > 200:
|
| 147 |
+
st.error("File size exceeds limit. Maximum file size is 200MB.")
|
| 148 |
st.stop()
|
| 149 |
|
| 150 |
# Process new video if different from current
|
| 151 |
if st.session_state.video_name != uploaded_file.name:
|
| 152 |
tmp_path = None
|
| 153 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
# Create temporary file
|
| 155 |
with tempfile.NamedTemporaryFile(delete=False, suffix=Path(uploaded_file.name).suffix) as tmp:
|
| 156 |
tmp.write(uploaded_file.getvalue())
|
| 157 |
tmp_path = tmp.name
|
| 158 |
|
| 159 |
# Upload and process with progress indication
|
| 160 |
+
with st.spinner("Uploading and processing video..."):
|
| 161 |
progress_bar = st.progress(0)
|
| 162 |
progress_bar.progress(25, text="Uploading video...")
|
| 163 |
|
|
|
|
| 165 |
progress_bar.progress(50, text="Processing video...")
|
| 166 |
|
| 167 |
processed_file = st.session_state.video_processor.wait_for_processing(video_file)
|
| 168 |
+
progress_bar.progress(100, text="Processing complete")
|
| 169 |
|
| 170 |
# Update session state
|
| 171 |
st.session_state.video_file = processed_file
|
| 172 |
st.session_state.video_name = uploaded_file.name
|
| 173 |
st.session_state.messages = [] # Clear previous conversation
|
| 174 |
|
| 175 |
+
st.success("Video processed successfully")
|
| 176 |
time.sleep(1) # Show success message briefly
|
| 177 |
progress_bar.empty() # Clear progress bar
|
| 178 |
|
| 179 |
except Exception as e:
|
| 180 |
+
st.error(f"Error processing video: {str(e)}")
|
| 181 |
st.session_state.video_file = None
|
| 182 |
st.session_state.video_name = None
|
| 183 |
|
|
|
|
| 189 |
# Display video player
|
| 190 |
st.video(uploaded_file.getvalue())
|
| 191 |
else:
|
| 192 |
+
st.error("Please upload a valid video file")
|
| 193 |
|
| 194 |
# Control buttons
|
| 195 |
+
col1, col2, col3 = st.columns(3)
|
| 196 |
with col1:
|
| 197 |
+
if st.button("Reset Chat", disabled=not st.session_state.messages):
|
| 198 |
st.session_state.messages = []
|
| 199 |
st.rerun()
|
| 200 |
|
| 201 |
with col2:
|
| 202 |
+
if st.button("Reset Video", disabled=not st.session_state.video_file):
|
| 203 |
+
st.session_state.video_file = None
|
| 204 |
+
st.session_state.video_name = None
|
| 205 |
+
st.session_state.messages = []
|
| 206 |
+
st.rerun()
|
| 207 |
+
|
| 208 |
+
with col3:
|
| 209 |
+
if st.button("Clear API Key"):
|
| 210 |
for key in list(st.session_state.keys()):
|
| 211 |
del st.session_state[key]
|
| 212 |
st.rerun()
|
| 213 |
|
| 214 |
+
st.markdown("---")
|
| 215 |
+
|
| 216 |
# Step 3: Chat about Video
|
| 217 |
+
st.subheader("Step 3: Video Analysis Chat")
|
| 218 |
|
| 219 |
if st.session_state.video_file:
|
| 220 |
# Display chat history
|
|
|
|
| 233 |
# Generate and display assistant response
|
| 234 |
with st.chat_message("assistant"):
|
| 235 |
placeholder = st.empty()
|
| 236 |
+
with st.spinner("Processing..."):
|
| 237 |
try:
|
| 238 |
response = st.session_state.video_processor.chat_with_video(
|
| 239 |
st.session_state.video_file,
|
| 240 |
user_question
|
| 241 |
)
|
| 242 |
except Exception as e:
|
| 243 |
+
response = f"Error: {str(e)}"
|
| 244 |
|
| 245 |
placeholder.markdown(response)
|
| 246 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 247 |
else:
|
| 248 |
+
st.info("Please upload a video in Step 2 to begin analysis.")
|
| 249 |
|
| 250 |
if __name__ == "__main__":
|
| 251 |
main()
|