Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,16 +6,14 @@ import time
|
|
| 6 |
import mimetypes
|
| 7 |
from pathlib import Path
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 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 |
-
#st.set_page_config(page_title="Video-Based Retrieval-Augmented Generation", page_icon="🎬", layout="wide")
|
| 18 |
-
|
| 19 |
class VideoProcessor:
|
| 20 |
def __init__(self):
|
| 21 |
self.model = genai.GenerativeModel("gemini-2.0-flash")
|
|
@@ -40,46 +38,47 @@ for key in ["video_processor", "video_file", "video_name", "messages"]:
|
|
| 40 |
if key not in st.session_state:
|
| 41 |
st.session_state[key] = None if key != "messages" else []
|
| 42 |
|
| 43 |
-
|
| 44 |
-
st.header("📹 Upload Video")
|
| 45 |
-
uploaded_file = st.file_uploader("Choose video", type=['mp4', 'mov', 'avi', 'mkv', 'webm'])
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
st.info(f"Size: {file_size:.2f} MB")
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
tmp_path = tmp.name
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
st.session_state.messages.clear()
|
| 64 |
-
st.success("✅ Video processed")
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
else:
|
| 70 |
-
st.error("Not a valid video file")
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
|
| 78 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
if not st.session_state.video_file:
|
| 81 |
-
st.info("👈 Upload a video to start chatting")
|
| 82 |
-
else:
|
| 83 |
for msg in st.session_state.messages:
|
| 84 |
with st.chat_message(msg["role"]):
|
| 85 |
st.markdown(msg["content"])
|
|
@@ -98,3 +97,5 @@ else:
|
|
| 98 |
|
| 99 |
placeholder.markdown(response)
|
| 100 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
|
|
|
|
|
|
| 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")
|
|
|
|
| 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"])
|
|
|
|
| 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.")
|