Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from video_rag_tool import VideoRAGTool
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import cv2
|
| 7 |
+
import numpy as np
|
| 8 |
+
import torch
|
| 9 |
+
|
| 10 |
+
class VideoRAGApp:
|
| 11 |
+
def __init__(self):
|
| 12 |
+
self.rag_tool = VideoRAGTool()
|
| 13 |
+
self.current_video_path = None
|
| 14 |
+
self.processed = False
|
| 15 |
+
|
| 16 |
+
def process_video(self, video_file):
|
| 17 |
+
"""Process uploaded video and return status message"""
|
| 18 |
+
if video_file is None:
|
| 19 |
+
return "Please upload a video first."
|
| 20 |
+
|
| 21 |
+
# Save uploaded video to temporary file
|
| 22 |
+
temp_dir = tempfile.mkdtemp()
|
| 23 |
+
temp_path = os.path.join(temp_dir, "uploaded_video.mp4")
|
| 24 |
+
|
| 25 |
+
with open(temp_path, "wb") as f:
|
| 26 |
+
f.write(video_file)
|
| 27 |
+
|
| 28 |
+
self.current_video_path = temp_path
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
self.rag_tool.process_video(temp_path)
|
| 32 |
+
self.processed = True
|
| 33 |
+
return "Video processed successfully! You can now ask questions about the video."
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"Error processing video: {str(e)}"
|
| 36 |
+
|
| 37 |
+
def query_video(self, query_text):
|
| 38 |
+
"""Query the video and return relevant frames with descriptions"""
|
| 39 |
+
if not self.processed:
|
| 40 |
+
return "Please process a video first."
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
results = self.rag_tool.query_video(query_text, k=4)
|
| 44 |
+
|
| 45 |
+
# Extract frames for display
|
| 46 |
+
frames = []
|
| 47 |
+
captions = []
|
| 48 |
+
|
| 49 |
+
cap = cv2.VideoCapture(self.current_video_path)
|
| 50 |
+
|
| 51 |
+
for result in results:
|
| 52 |
+
frame_number = result['frame_number']
|
| 53 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
|
| 54 |
+
ret, frame = cap.read()
|
| 55 |
+
|
| 56 |
+
if ret:
|
| 57 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 58 |
+
frames.append(Image.fromarray(frame_rgb))
|
| 59 |
+
|
| 60 |
+
caption = f"Timestamp: {result['timestamp']:.2f}s\n"
|
| 61 |
+
caption += f"Relevance: {result['relevance_score']:.2f}"
|
| 62 |
+
captions.append(caption)
|
| 63 |
+
|
| 64 |
+
cap.release()
|
| 65 |
+
|
| 66 |
+
return frames, captions
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return f"Error querying video: {str(e)}"
|
| 70 |
+
|
| 71 |
+
def create_interface(self):
|
| 72 |
+
"""Create and return Gradio interface"""
|
| 73 |
+
with gr.Blocks(title="Video Chat RAG") as interface:
|
| 74 |
+
gr.Markdown("# Video Chat RAG")
|
| 75 |
+
gr.Markdown("Upload a video and ask questions about its content!")
|
| 76 |
+
|
| 77 |
+
with gr.Row():
|
| 78 |
+
video_input = gr.File(
|
| 79 |
+
label="Upload Video",
|
| 80 |
+
file_types=["video"],
|
| 81 |
+
)
|
| 82 |
+
process_button = gr.Button("Process Video")
|
| 83 |
+
|
| 84 |
+
status_output = gr.Textbox(
|
| 85 |
+
label="Status",
|
| 86 |
+
interactive=False
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
with gr.Row():
|
| 90 |
+
query_input = gr.Textbox(
|
| 91 |
+
label="Ask about the video",
|
| 92 |
+
placeholder="What's happening in the video?"
|
| 93 |
+
)
|
| 94 |
+
query_button = gr.Button("Search")
|
| 95 |
+
|
| 96 |
+
with gr.Row():
|
| 97 |
+
gallery = gr.Gallery(
|
| 98 |
+
label="Retrieved Frames",
|
| 99 |
+
show_label=True,
|
| 100 |
+
elem_id="gallery",
|
| 101 |
+
columns=[2],
|
| 102 |
+
rows=[2],
|
| 103 |
+
height="auto"
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
captions = gr.Textbox(
|
| 107 |
+
label="Frame Details",
|
| 108 |
+
interactive=False
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
# Set up event handlers
|
| 112 |
+
process_button.click(
|
| 113 |
+
fn=self.process_video,
|
| 114 |
+
inputs=[video_input],
|
| 115 |
+
outputs=[status_output]
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
query_button.click(
|
| 119 |
+
fn=self.query_video,
|
| 120 |
+
inputs=[query_input],
|
| 121 |
+
outputs=[gallery, captions]
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
return interface
|
| 125 |
+
|
| 126 |
+
# For Hugging Face Spaces deployment
|
| 127 |
+
app = VideoRAGApp()
|
| 128 |
+
interface = app.create_interface()
|
| 129 |
+
|
| 130 |
+
# Launch the app (for local testing)
|
| 131 |
+
if __name__ == "__main__":
|
| 132 |
+
interface.launch()
|