Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,102 +6,91 @@ import time
|
|
| 6 |
import mimetypes
|
| 7 |
from pathlib import Path
|
| 8 |
|
| 9 |
-
#
|
| 10 |
GEMINI_API_KEY = "AIzaSyDCMPwXHagWqYTQB3HL7FceHEmKUv3v4wc"
|
| 11 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 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 |
-
st.session_state.video_processor
|
| 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 |
-
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})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|