stevafernandes commited on
Commit
76b9ad5
Β·
verified Β·
1 Parent(s): cc81245

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -55
app.py CHANGED
@@ -11,38 +11,54 @@ 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 "AIzaSyA8TTu9s6fJDG9RlMwOyHFxg270xLgpiyE"
15
 
16
  # VideoProcessor class
17
  class VideoProcessor:
18
  def __init__(self, api_key):
19
  genai.configure(api_key=api_key)
20
- self.model = genai.GenerativeModel("gemini-2.0-flash")
21
 
22
  def upload_video(self, video_path, display_name="uploaded_video"):
23
- return genai.upload_file(path=video_path, display_name=display_name)
 
 
 
24
 
25
  def wait_for_processing(self, video_file):
26
- while video_file.state.name == "PROCESSING":
 
 
27
  time.sleep(2)
28
  video_file = genai.get_file(video_file.name)
 
29
  if video_file.state.name == "FAILED":
30
  raise RuntimeError("Video processing failed")
 
 
31
  return video_file
32
 
33
  def chat_with_video(self, video_file, prompt):
34
- response = self.model.generate_content([video_file, prompt])
35
- return response.text
36
-
37
- # Initialize session state
38
- for key in ["video_processor", "video_file", "video_name", "messages"]:
39
- if key not in st.session_state:
40
- st.session_state[key] = None if key != "messages" else []
 
 
 
 
 
 
 
 
41
 
42
  # Main app function
43
  def main():
44
  st.set_page_config(page_title="Video Retrieval-Augmented Generation", page_icon="🎬", layout="wide")
45
- st.header("Video Retrieval-Augmented Generation - Gemini 2.0")
46
  st.markdown("---")
47
 
48
  # Step 1: API Key input
@@ -58,59 +74,92 @@ def main():
58
  uploaded_file = st.file_uploader("Upload a video", type=['mp4', 'mov', 'avi', 'mkv', 'webm'])
59
 
60
  if uploaded_file:
61
- if mimetypes.guess_type(uploaded_file.name)[0].startswith("video"):
 
 
62
  file_size = len(uploaded_file.getvalue()) / (1024**2)
63
- st.info(f"Size: {file_size:.2f} MB")
64
-
 
 
 
 
 
 
 
 
 
 
 
 
65
  if st.session_state.video_name != uploaded_file.name:
66
- st.session_state.video_processor = VideoProcessor(api_key)
67
- with tempfile.NamedTemporaryFile(delete=False, suffix=Path(uploaded_file.name).suffix) as tmp:
68
- tmp.write(uploaded_file.getvalue())
69
- tmp_path = tmp.name
70
-
71
- with st.spinner("Uploading and processing..."):
72
- video_file = st.session_state.video_processor.upload_video(tmp_path, uploaded_file.name)
73
- processed_file = st.session_state.video_processor.wait_for_processing(video_file)
74
- st.session_state.video_file = processed_file
75
- st.session_state.video_name = uploaded_file.name
76
- st.session_state.messages.clear()
77
- st.success("βœ… Video processed")
78
-
79
- os.unlink(tmp_path)
80
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  st.video(uploaded_file.getvalue())
82
  else:
83
- st.error("Not a valid video file")
84
-
85
- if st.button("Reset Chat"):
86
- st.session_state.messages.clear()
87
-
88
- if st.button("Reset All"):
89
- st.session_state.clear()
 
 
 
 
 
 
 
90
 
91
  # Step 3: Chat about Video
92
  st.subheader("Step 3: Chat with your video")
93
 
94
  if st.session_state.video_file:
 
95
  for msg in st.session_state.messages:
96
  with st.chat_message(msg["role"]):
97
  st.markdown(msg["content"])
98
 
99
- user_question = st.chat_input("Ask a question about the video...")
100
- if user_question:
101
- st.session_state.messages.append({"role": "user", "content": user_question})
102
- with st.chat_message("user"):
103
- st.markdown(user_question)
104
-
105
- with st.chat_message("assistant"):
106
- placeholder = st.empty()
107
- with st.spinner("Generating response..."):
108
- response = st.session_state.video_processor.chat_with_video(st.session_state.video_file, user_question)
109
-
110
- placeholder.markdown(response)
111
- st.session_state.messages.append({"role": "assistant", "content": response})
112
- else:
113
- st.info("Please upload a video in step 2 to start chatting.")
114
-
115
- if __name__ == "__main__":
116
- main()
 
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 "AIzaSyA8TTu9s6fJDG9RlMwOyHFxg270xLgpiyE" # Warning: Hardcoded key
15
 
16
  # VideoProcessor class
17
  class VideoProcessor:
18
  def __init__(self, api_key):
19
  genai.configure(api_key=api_key)
20
+ self.model = genai.GenerativeModel("gemini-2.0-flash-exp")
21
 
22
  def upload_video(self, video_path, display_name="uploaded_video"):
23
+ try:
24
+ return genai.upload_file(path=video_path, display_name=display_name)
25
+ except Exception as e:
26
+ raise RuntimeError(f"Failed to upload video: {str(e)}")
27
 
28
  def wait_for_processing(self, video_file):
29
+ max_attempts = 60 # Maximum wait time: 2 minutes
30
+ attempts = 0
31
+ while video_file.state.name == "PROCESSING" and attempts < max_attempts:
32
  time.sleep(2)
33
  video_file = genai.get_file(video_file.name)
34
+ attempts += 1
35
  if video_file.state.name == "FAILED":
36
  raise RuntimeError("Video processing failed")
37
+ if attempts >= max_attempts:
38
+ raise RuntimeError("Video processing timeout")
39
  return video_file
40
 
41
  def chat_with_video(self, video_file, prompt):
42
+ try:
43
+ response = self.model.generate_content([video_file, prompt])
44
+ return response.text
45
+ except Exception as e:
46
+ return f"Error generating response: {str(e)}"
47
+
48
+ # Initialize session state properly
49
+ if "messages" not in st.session_state:
50
+ st.session_state.messages = []
51
+ if "video_processor" not in st.session_state:
52
+ st.session_state.video_processor = None
53
+ 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():
60
  st.set_page_config(page_title="Video Retrieval-Augmented Generation", page_icon="🎬", layout="wide")
61
+ st.header("🎬 Video Retrieval-Augmented Generation - Gemini 2.0")
62
  st.markdown("---")
63
 
64
  # Step 1: API Key input
 
74
  uploaded_file = st.file_uploader("Upload a video", type=['mp4', 'mov', 'avi', 'mkv', 'webm'])
75
 
76
  if uploaded_file:
77
+ # Validate video file
78
+ mime_type = mimetypes.guess_type(uploaded_file.name)[0]
79
+ if mime_type and mime_type.startswith("video"):
80
  file_size = len(uploaded_file.getvalue()) / (1024**2)
81
+
82
+ # Display file info
83
+ col1, col2 = st.columns(2)
84
+ with col1:
85
+ st.info(f"πŸ“ File: {uploaded_file.name}")
86
+ with col2:
87
+ st.info(f"πŸ’Ύ Size: {file_size:.2f} MB")
88
+
89
+ # Check file size limit (200MB for Gemini)
90
+ if file_size > 200:
91
+ st.error("❌ File too large! Maximum size is 200MB")
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("πŸ“€ Uploading and processing video..."):
109
+ progress_bar = st.progress(0)
110
+ progress_bar.progress(25, text="Uploading video...")
111
+
112
+ video_file = st.session_state.video_processor.upload_video(tmp_path, uploaded_file.name)
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="Complete!")
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("βœ… Video processed successfully!")
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"❌ Error processing video: {str(e)}")
129
+ st.session_state.video_file = None
130
+ st.session_state.video_name = None
131
+
132
+ finally:
133
+ # Clean up temporary file
134
+ if tmp_path and os.path.exists(tmp_path):
135
+ os.unlink(tmp_path)
136
+
137
+ # Display video player
138
  st.video(uploaded_file.getvalue())
139
  else:
140
+ st.error("❌ Please upload a valid video file")
141
+
142
+ # Control buttons
143
+ col1, col2 = st.columns(2)
144
+ with col1:
145
+ if st.button("πŸ”„ Reset Chat", disabled=not st.session_state.messages):
146
+ st.session_state.messages = []
147
+ st.rerun()
148
+
149
+ with col2:
150
+ if st.button("πŸ—‘οΈ Reset All", disabled=not st.session_state.video_file):
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: Chat with your video")
157
 
158
  if st.session_state.video_file:
159
+ # Display chat history
160
  for msg in st.session_state.messages:
161
  with st.chat_message(msg["role"]):
162
  st.markdown(msg["content"])
163
 
164
+ # Chat input
165
+ user_question = st.chat_input("Ask a question ab