stevafernandes commited on
Commit
397509e
·
verified ·
1 Parent(s): 88b9dd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -68
app.py CHANGED
@@ -6,16 +6,17 @@ import time
6
  import mimetypes
7
  from pathlib import Path
8
 
9
- # Step 1: Enter API Key
10
- st.sidebar.header("Step 1: Enter Gemini API Key")
11
- user_api_key = st.sidebar.text_input("Enter your Gemini API key (optional):", type="password")
12
-
13
- # Gemini API key configuration
14
- GEMINI_API_KEY = user_api_key if user_api_key else "AIzaSyDCMPwXHagWqYTQB3HL7FceHEmKUv3v4wc"
15
- genai.configure(api_key=GEMINI_API_KEY)
16
-
17
  class VideoProcessor:
18
- def __init__(self):
 
19
  self.model = genai.GenerativeModel("gemini-2.0-flash")
20
 
21
  def upload_video(self, video_path, display_name="uploaded_video"):
@@ -38,64 +39,76 @@ for key in ["video_processor", "video_file", "video_name", "messages"]:
38
  if key not in st.session_state:
39
  st.session_state[key] = None if key != "messages" else []
40
 
41
- st.title("🎬 Video Retrieval-Augmented Generation - Gemini 2.0")
42
-
43
- # Step 2: Upload Video
44
- st.header("Step 2: Upload Video")
45
- uploaded_file = st.file_uploader("Choose a video file", type=['mp4', 'mov', 'avi', 'mkv', 'webm'])
46
-
47
- if uploaded_file:
48
- if mimetypes.guess_type(uploaded_file.name)[0].startswith("video"):
49
- file_size = len(uploaded_file.getvalue()) / (1024**2)
50
- st.info(f"Size: {file_size:.2f} MB")
51
-
52
- if st.session_state.video_name != uploaded_file.name:
53
- st.session_state.video_processor = VideoProcessor()
54
- with tempfile.NamedTemporaryFile(delete=False, suffix=Path(uploaded_file.name).suffix) as tmp:
55
- tmp.write(uploaded_file.getvalue())
56
- tmp_path = tmp.name
57
-
58
- with st.spinner("Uploading and processing..."):
59
- video_file = st.session_state.video_processor.upload_video(tmp_path, uploaded_file.name)
60
- processed_file = st.session_state.video_processor.wait_for_processing(video_file)
61
- st.session_state.video_file = processed_file
62
- st.session_state.video_name = uploaded_file.name
63
- st.session_state.messages.clear()
64
- st.success("✅ Video processed")
65
-
66
- os.unlink(tmp_path)
67
-
68
- st.video(uploaded_file.getvalue())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  else:
70
- st.error("Not a valid video file")
71
-
72
- if st.button("Reset Chat"):
73
- st.session_state.messages.clear()
74
-
75
- if st.button("Reset All"):
76
- st.session_state.clear()
77
-
78
- # Step 3: Chatting about the Video
79
- if st.session_state.video_file:
80
- st.header("Step 3: Chat with Video")
81
-
82
- for msg in st.session_state.messages:
83
- with st.chat_message(msg["role"]):
84
- st.markdown(msg["content"])
85
-
86
- prompt = st.chat_input("Ask about the video...")
87
-
88
- if prompt:
89
- st.session_state.messages.append({"role": "user", "content": prompt})
90
- with st.chat_message("user"):
91
- st.markdown(prompt)
92
-
93
- with st.chat_message("assistant"):
94
- placeholder = st.empty()
95
- with st.spinner("Thinking..."):
96
- response = st.session_state.video_processor.chat_with_video(st.session_state.video_file, prompt)
97
 
98
- placeholder.markdown(response)
99
- st.session_state.messages.append({"role": "assistant", "content": response})
100
- else:
101
- st.info("👈 Please upload a video to start chatting.")
 
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.sidebar.text_input("Enter your Gemini API key", type="password")
14
+ return GOOGLE_API_KEY or "AIzaSyDCMPwXHagWqYTQB3HL7FceHEmKUv3v4wc"
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"):
 
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("Retrieval-Augmented Generation - Gemini 2.0")
46
+ st.markdown("---")
47
+
48
+ api_key = get_api_key()
49
+
50
+ if not api_key:
51
+ st.sidebar.error("Please enter your API key to proceed.")
52
+ st.stop()
53
+
54
+ # Step 2: Upload Video
55
+ st.subheader("Step 2: Upload your video file")
56
+ uploaded_file = st.file_uploader("Upload a video", type=['mp4', 'mov', 'avi', 'mkv', 'webm'])
57
+
58
+ if uploaded_file:
59
+ if mimetypes.guess_type(uploaded_file.name)[0].startswith("video"):
60
+ file_size = len(uploaded_file.getvalue()) / (1024**2)
61
+ st.info(f"Size: {file_size:.2f} MB")
62
+
63
+ if st.session_state.video_name != uploaded_file.name:
64
+ st.session_state.video_processor = VideoProcessor(api_key)
65
+ with tempfile.NamedTemporaryFile(delete=False, suffix=Path(uploaded_file.name).suffix) as tmp:
66
+ tmp.write(uploaded_file.getvalue())
67
+ tmp_path = tmp.name
68
+
69
+ with st.spinner("Uploading and processing..."):
70
+ video_file = st.session_state.video_processor.upload_video(tmp_path, uploaded_file.name)
71
+ processed_file = st.session_state.video_processor.wait_for_processing(video_file)
72
+ st.session_state.video_file = processed_file
73
+ st.session_state.video_name = uploaded_file.name
74
+ st.session_state.messages.clear()
75
+ st.success("✅ Video processed")
76
+
77
+ os.unlink(tmp_path)
78
+
79
+ st.video(uploaded_file.getvalue())
80
+ else:
81
+ st.error("Not a valid video file")
82
+
83
+ if st.button("Reset Chat"):
84
+ st.session_state.messages.clear()
85
+
86
+ if st.button("Reset All"):
87
+ st.session_state.clear()
88
+
89
+ # Step 3: Chat about Video
90
+ st.subheader("Step 3: Chat with your video")
91
+
92
+ if st.session_state.video_file:
93
+ for msg in st.session_state.messages:
94
+ with st.chat_message(msg["role"]):
95
+ st.markdown(msg["content"])
96
+
97
+ user_question = st.chat_input("Ask a question about the video...")
98
+ if user_question:
99
+ st.session_state.messages.append({"role": "user", "content": user_question})
100
+ with st.chat_message("user"):
101
+ st.markdown(user_question)
102
+
103
+ with st.chat_message("assistant"):
104
+ placeholder = st.empty()
105
+ with st.spinner("Generating response..."):
106
+ response = st.session_state.video_processor.chat_with_video(st.session_state.video_file, user_question)
107
+
108
+ placeholder.markdown(response)
109
+ st.session_state.messages.append({"role": "assistant", "content": response})
110
  else:
111
+ st.info("👈 Please upload a video to start chatting.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
+ if __name__ == "__main__":
114
+ main()