stevafernandes commited on
Commit
ff90e76
Β·
verified Β·
1 Parent(s): 7f12de3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -96
app.py CHANGED
@@ -6,102 +6,91 @@ import time
6
  import mimetypes
7
  from pathlib import Path
8
 
9
- # Hard-coded Gemini API key
10
  GEMINI_API_KEY = "AIzaSyDCMPwXHagWqYTQB3HL7FceHEmKUv3v4wc"
11
  genai.configure(api_key=GEMINI_API_KEY)
12
 
13
- def main():
14
- # Set Streamlit page configuration
15
- st.set_page_config(
16
- page_title="Video RAG with Gemini",
17
- page_icon="🎬",
18
- layout="wide"
19
- )
20
-
21
- class VideoProcessor:
22
- def __init__(self):
23
- self.model = genai.GenerativeModel("gemini-2.0-flash")
24
-
25
- def upload_video(self, video_path, display_name="uploaded_video"):
26
- return genai.upload_file(path=video_path, display_name=display_name)
27
-
28
- def wait_for_processing(self, video_file):
29
- while video_file.state.name == "PROCESSING":
30
- time.sleep(2)
31
- video_file = genai.get_file(video_file.name)
32
- if video_file.state.name == "FAILED":
33
- raise RuntimeError("Video processing failed")
34
- return video_file
35
-
36
- def chat_with_video(self, video_file, prompt):
37
- response = self.model.generate_content([video_file, prompt])
38
- return response.text
39
-
40
- # Initialize session state
41
- for key in ["video_processor", "video_file", "video_name", "messages"]:
42
- if key not in st.session_state:
43
- st.session_state[key] = None if key != "messages" else []
44
-
45
- # Sidebar UI
46
- with st.sidebar:
47
- st.header("πŸ“Ή Upload Video")
48
- uploaded_file = st.file_uploader("Choose video", type=['mp4', 'mov', 'avi', 'mkv', 'webm'])
49
-
50
- if uploaded_file:
51
- if mimetypes.guess_type(uploaded_file.name)[0].startswith("video"):
52
- file_size = len(uploaded_file.getvalue()) / (1024**2)
53
- st.info(f"Size: {file_size:.2f} MB")
54
-
55
- if st.session_state.video_name != uploaded_file.name:
56
- st.session_state.video_processor = VideoProcessor()
57
- with tempfile.NamedTemporaryFile(delete=False, suffix=Path(uploaded_file.name).suffix) as tmp:
58
- tmp.write(uploaded_file.getvalue())
59
- tmp_path = tmp.name
60
-
61
- with st.spinner("Uploading and processing..."):
62
- video_file = st.session_state.video_processor.upload_video(tmp_path, uploaded_file.name)
63
- processed_file = st.session_state.video_processor.wait_for_processing(video_file)
64
- st.session_state.video_file = processed_file
65
- st.session_state.video_name = uploaded_file.name
66
- st.session_state.messages.clear()
67
- st.success("βœ… Video processed")
68
-
69
- os.unlink(tmp_path)
70
-
71
- st.video(uploaded_file.getvalue())
72
- else:
73
- st.error("Not a valid video file")
74
-
75
- if st.button("Reset Chat"):
76
- st.session_state.messages.clear()
77
-
78
- if st.button("Reset All"):
79
- st.session_state.clear()
80
-
81
- # Main Chat UI
82
- st.title("🎬 Video RAG with Gemini")
83
-
84
- if not st.session_state.video_file:
85
- st.info("πŸ‘ˆ Upload a video to start chatting")
86
- else:
87
- for msg in st.session_state.messages:
88
- with st.chat_message(msg["role"]):
89
- st.markdown(msg["content"])
90
-
91
- prompt = st.chat_input("Ask about the video...")
92
-
93
- if prompt:
94
- st.session_state.messages.append({"role": "user", "content": prompt})
95
- with st.chat_message("user"):
96
- st.markdown(prompt)
97
-
98
- with st.chat_message("assistant"):
99
- placeholder = st.empty()
100
- with st.spinner("Thinking..."):
101
- response = st.session_state.video_processor.chat_with_video(st.session_state.video_file, prompt)
102
-
103
- placeholder.markdown(response)
104
- st.session_state.messages.append({"role": "assistant", "content": response})
105
-
106
- if __name__ == "__main__":
107
- main()
 
6
  import mimetypes
7
  from pathlib import Path
8
 
9
+ # Gemini API key (hardcoded for demo purposes)
10
  GEMINI_API_KEY = "AIzaSyDCMPwXHagWqYTQB3HL7FceHEmKUv3v4wc"
11
  genai.configure(api_key=GEMINI_API_KEY)
12
 
13
+ st.set_page_config(page_title="Video RAG with Gemini", page_icon="🎬", layout="wide")
14
+
15
+ class VideoProcessor:
16
+ def __init__(self):
17
+ self.model = genai.GenerativeModel("gemini-2.0-flash")
18
+
19
+ def upload_video(self, video_path, display_name="uploaded_video"):
20
+ return genai.upload_file(path=video_path, display_name=display_name)
21
+
22
+ def wait_for_processing(self, video_file):
23
+ while video_file.state.name == "PROCESSING":
24
+ time.sleep(2)
25
+ video_file = genai.get_file(video_file.name)
26
+ if video_file.state.name == "FAILED":
27
+ raise RuntimeError("Video processing failed")
28
+ return video_file
29
+
30
+ def chat_with_video(self, video_file, prompt):
31
+ response = self.model.generate_content([video_file, prompt])
32
+ return response.text
33
+
34
+ # Initialize session state
35
+ for key in ["video_processor", "video_file", "video_name", "messages"]:
36
+ if key not in st.session_state:
37
+ st.session_state[key] = None if key != "messages" else []
38
+
39
+ with st.sidebar:
40
+ st.header("πŸ“Ή Upload Video")
41
+ uploaded_file = st.file_uploader("Choose video", type=['mp4', 'mov', 'avi', 'mkv', 'webm'])
42
+
43
+ if uploaded_file:
44
+ if mimetypes.guess_type(uploaded_file.name)[0].startswith("video"):
45
+ file_size = len(uploaded_file.getvalue()) / (1024**2)
46
+ st.info(f"Size: {file_size:.2f} MB")
47
+
48
+ if st.session_state.video_name != uploaded_file.name:
49
+ st.session_state.video_processor = VideoProcessor()
50
+ with tempfile.NamedTemporaryFile(delete=False, suffix=Path(uploaded_file.name).suffix) as tmp:
51
+ tmp.write(uploaded_file.getvalue())
52
+ tmp_path = tmp.name
53
+
54
+ with st.spinner("Uploading and processing..."):
55
+ video_file = st.session_state.video_processor.upload_video(tmp_path, uploaded_file.name)
56
+ processed_file = st.session_state.video_processor.wait_for_processing(video_file)
57
+ st.session_state.video_file = processed_file
58
+ st.session_state.video_name = uploaded_file.name
59
+ st.session_state.messages.clear()
60
+ st.success("βœ… Video processed")
61
+
62
+ os.unlink(tmp_path)
63
+
64
+ st.video(uploaded_file.getvalue())
65
+ else:
66
+ st.error("Not a valid video file")
67
+
68
+ if st.button("Reset Chat"):
69
+ st.session_state.messages.clear()
70
+
71
+ if st.button("Reset All"):
72
+ st.session_state.clear()
73
+
74
+ st.title("🎬 Video RAG with Gemini")
75
+
76
+ if not st.session_state.video_file:
77
+ st.info("πŸ‘ˆ Upload a video to start chatting")
78
+ else:
79
+ for msg in st.session_state.messages:
80
+ with st.chat_message(msg["role"]):
81
+ st.markdown(msg["content"])
82
+
83
+ prompt = st.chat_input("Ask about the video...")
84
+
85
+ if prompt:
86
+ st.session_state.messages.append({"role": "user", "content": prompt})
87
+ with st.chat_message("user"):
88
+ st.markdown(prompt)
89
+
90
+ with st.chat_message("assistant"):
91
+ placeholder = st.empty()
92
+ with st.spinner("Thinking..."):
93
+ response = st.session_state.video_processor.chat_with_video(st.session_state.video_file, prompt)
94
+
95
+ placeholder.markdown(response)
96
+ st.session_state.messages.append({"role": "assistant", "content": response})