Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ import logging
|
|
| 10 |
import gradio as gr
|
| 11 |
import tempfile
|
| 12 |
import os
|
|
|
|
| 13 |
|
| 14 |
class VideoRAGTool:
|
| 15 |
def __init__(self, model_name: str = "openai/clip-vit-base-patch32"):
|
|
@@ -61,6 +62,9 @@ class VideoRAGTool:
|
|
| 61 |
|
| 62 |
cap.release()
|
| 63 |
|
|
|
|
|
|
|
|
|
|
| 64 |
features_array = np.vstack(features_list)
|
| 65 |
self.frame_index = faiss.IndexFlatL2(features_array.shape[1])
|
| 66 |
self.frame_index.add(features_array)
|
|
@@ -92,25 +96,34 @@ class VideoRAGApp:
|
|
| 92 |
self.rag_tool = VideoRAGTool()
|
| 93 |
self.current_video_path = None
|
| 94 |
self.processed = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
def process_video(self, video_file):
|
| 97 |
"""Process uploaded video and return status message"""
|
| 98 |
-
if video_file is None:
|
| 99 |
-
return "Please upload a video first."
|
| 100 |
-
|
| 101 |
-
temp_dir = tempfile.mkdtemp()
|
| 102 |
-
temp_path = os.path.join(temp_dir, "uploaded_video.mp4")
|
| 103 |
-
|
| 104 |
-
with open(temp_path, "wb") as f:
|
| 105 |
-
f.write(video_file)
|
| 106 |
-
|
| 107 |
-
self.current_video_path = temp_path
|
| 108 |
-
|
| 109 |
try:
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
self.processed = True
|
| 112 |
return "Video processed successfully! You can now ask questions about the video."
|
|
|
|
| 113 |
except Exception as e:
|
|
|
|
| 114 |
return f"Error processing video: {str(e)}"
|
| 115 |
|
| 116 |
def query_video(self, query_text):
|
|
|
|
| 10 |
import gradio as gr
|
| 11 |
import tempfile
|
| 12 |
import os
|
| 13 |
+
import shutil
|
| 14 |
|
| 15 |
class VideoRAGTool:
|
| 16 |
def __init__(self, model_name: str = "openai/clip-vit-base-patch32"):
|
|
|
|
| 62 |
|
| 63 |
cap.release()
|
| 64 |
|
| 65 |
+
if not features_list:
|
| 66 |
+
raise ValueError("No frames were processed from the video")
|
| 67 |
+
|
| 68 |
features_array = np.vstack(features_list)
|
| 69 |
self.frame_index = faiss.IndexFlatL2(features_array.shape[1])
|
| 70 |
self.frame_index.add(features_array)
|
|
|
|
| 96 |
self.rag_tool = VideoRAGTool()
|
| 97 |
self.current_video_path = None
|
| 98 |
self.processed = False
|
| 99 |
+
self.temp_dir = tempfile.mkdtemp()
|
| 100 |
+
|
| 101 |
+
def __del__(self):
|
| 102 |
+
"""Cleanup temporary files on deletion"""
|
| 103 |
+
if hasattr(self, 'temp_dir') and os.path.exists(self.temp_dir):
|
| 104 |
+
shutil.rmtree(self.temp_dir, ignore_errors=True)
|
| 105 |
|
| 106 |
def process_video(self, video_file):
|
| 107 |
"""Process uploaded video and return status message"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
try:
|
| 109 |
+
if video_file is None:
|
| 110 |
+
return "Please upload a video first."
|
| 111 |
+
|
| 112 |
+
# video_file is now a file path provided by Gradio
|
| 113 |
+
video_path = video_file.name
|
| 114 |
+
|
| 115 |
+
# Create a copy in our temp directory
|
| 116 |
+
temp_video_path = os.path.join(self.temp_dir, "current_video.mp4")
|
| 117 |
+
shutil.copy2(video_path, temp_video_path)
|
| 118 |
+
|
| 119 |
+
self.current_video_path = temp_video_path
|
| 120 |
+
|
| 121 |
+
self.rag_tool.process_video(self.current_video_path)
|
| 122 |
self.processed = True
|
| 123 |
return "Video processed successfully! You can now ask questions about the video."
|
| 124 |
+
|
| 125 |
except Exception as e:
|
| 126 |
+
self.processed = False
|
| 127 |
return f"Error processing video: {str(e)}"
|
| 128 |
|
| 129 |
def query_video(self, query_text):
|