Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,199 +4,73 @@ import cv2
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
import spaces
|
| 7 |
-
import
|
| 8 |
-
import
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
import warnings
|
| 11 |
warnings.filterwarnings("ignore")
|
| 12 |
|
| 13 |
# Global variables
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
text_model = None
|
| 17 |
-
text_tokenizer = None
|
| 18 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 19 |
model_loaded = False
|
| 20 |
|
| 21 |
@spaces.GPU
|
| 22 |
-
def
|
| 23 |
-
"""Load
|
| 24 |
-
global
|
| 25 |
|
| 26 |
try:
|
| 27 |
-
print("π Loading
|
| 28 |
|
| 29 |
-
|
| 30 |
-
print("Loading BLIP vision model...")
|
| 31 |
-
vision_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 32 |
-
vision_model = BlipForConditionalGeneration.from_pretrained(
|
| 33 |
-
"Salesforce/blip-image-captioning-large",
|
| 34 |
-
torch_dtype=torch.float16,
|
| 35 |
-
device_map="auto"
|
| 36 |
-
)
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
"microsoft/DialoGPT-medium",
|
| 43 |
-
torch_dtype=torch.float16,
|
| 44 |
-
device_map="auto"
|
| 45 |
)
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
model_loaded = True
|
| 52 |
-
success_msg = "β
|
| 53 |
print(success_msg)
|
| 54 |
return success_msg
|
| 55 |
|
| 56 |
except Exception as e:
|
| 57 |
model_loaded = False
|
| 58 |
-
error_msg = f"β Failed to load
|
| 59 |
print(error_msg)
|
| 60 |
return error_msg
|
| 61 |
|
| 62 |
-
def extract_key_frames(video_path, max_frames=8):
|
| 63 |
-
"""Extract key frames from video"""
|
| 64 |
-
try:
|
| 65 |
-
cap = cv2.VideoCapture(video_path)
|
| 66 |
-
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 67 |
-
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 68 |
-
duration = total_frames / fps if fps > 0 else 0
|
| 69 |
-
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 70 |
-
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 71 |
-
|
| 72 |
-
if total_frames == 0:
|
| 73 |
-
return [], None
|
| 74 |
-
|
| 75 |
-
# Get evenly spaced frames
|
| 76 |
-
frame_indices = np.linspace(0, total_frames-1, min(max_frames, total_frames), dtype=int)
|
| 77 |
-
frames = []
|
| 78 |
-
timestamps = []
|
| 79 |
-
|
| 80 |
-
for frame_idx in frame_indices:
|
| 81 |
-
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)
|
| 82 |
-
ret, frame = cap.read()
|
| 83 |
-
if ret:
|
| 84 |
-
# Convert BGR to RGB
|
| 85 |
-
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 86 |
-
|
| 87 |
-
# Resize if too large
|
| 88 |
-
if max(width, height) > 512:
|
| 89 |
-
scale = 512 / max(width, height)
|
| 90 |
-
new_width = int(width * scale)
|
| 91 |
-
new_height = int(height * scale)
|
| 92 |
-
frame_rgb = cv2.resize(frame_rgb, (new_width, new_height))
|
| 93 |
-
|
| 94 |
-
frames.append(Image.fromarray(frame_rgb))
|
| 95 |
-
timestamp = frame_idx / fps if fps > 0 else frame_idx
|
| 96 |
-
timestamps.append(timestamp)
|
| 97 |
-
|
| 98 |
-
cap.release()
|
| 99 |
-
|
| 100 |
-
video_info = {
|
| 101 |
-
"duration": duration,
|
| 102 |
-
"fps": fps,
|
| 103 |
-
"total_frames": total_frames,
|
| 104 |
-
"resolution": f"{width}x{height}",
|
| 105 |
-
"extracted_frames": len(frames)
|
| 106 |
-
}
|
| 107 |
-
|
| 108 |
-
return frames, video_info, timestamps
|
| 109 |
-
|
| 110 |
-
except Exception as e:
|
| 111 |
-
print(f"Error extracting frames: {e}")
|
| 112 |
-
return [], None, []
|
| 113 |
-
|
| 114 |
@spaces.GPU
|
| 115 |
-
def
|
| 116 |
-
"""Analyze
|
| 117 |
-
try:
|
| 118 |
-
if custom_question:
|
| 119 |
-
# Use BLIP for visual question answering
|
| 120 |
-
inputs = vision_processor(frame, custom_question, return_tensors="pt").to(device)
|
| 121 |
-
else:
|
| 122 |
-
# Use BLIP for image captioning
|
| 123 |
-
inputs = vision_processor(frame, return_tensors="pt").to(device)
|
| 124 |
-
|
| 125 |
-
with torch.no_grad():
|
| 126 |
-
if custom_question:
|
| 127 |
-
output_ids = vision_model.generate(**inputs, max_new_tokens=100)
|
| 128 |
-
else:
|
| 129 |
-
output_ids = vision_model.generate(**inputs, max_new_tokens=50)
|
| 130 |
-
|
| 131 |
-
caption = vision_processor.decode(output_ids[0], skip_special_tokens=True)
|
| 132 |
-
return caption
|
| 133 |
-
|
| 134 |
-
except Exception as e:
|
| 135 |
-
return f"Error analyzing frame: {str(e)}"
|
| 136 |
-
|
| 137 |
-
def synthesize_video_analysis(frame_descriptions, question, video_info):
|
| 138 |
-
"""Create comprehensive video analysis from frame descriptions"""
|
| 139 |
-
|
| 140 |
-
# Combine all frame descriptions
|
| 141 |
-
all_descriptions = " ".join(frame_descriptions)
|
| 142 |
-
|
| 143 |
-
# Create analysis based on question type
|
| 144 |
-
question_lower = question.lower()
|
| 145 |
-
|
| 146 |
-
analysis = f"""π₯ **AI Video Analysis**
|
| 147 |
-
|
| 148 |
-
β **Your Question:** {question}
|
| 149 |
-
|
| 150 |
-
π€ **Detailed Analysis:**
|
| 151 |
-
|
| 152 |
-
"""
|
| 153 |
-
|
| 154 |
-
if any(word in question_lower for word in ['what', 'happening', 'describe', 'see']):
|
| 155 |
-
analysis += f"Based on my analysis of {len(frame_descriptions)} key frames from the video:\n\n"
|
| 156 |
-
|
| 157 |
-
for i, desc in enumerate(frame_descriptions):
|
| 158 |
-
timestamp = i * (video_info['duration'] / len(frame_descriptions))
|
| 159 |
-
analysis += f"β’ **At {timestamp:.1f}s:** {desc}\n"
|
| 160 |
-
|
| 161 |
-
analysis += f"\n**Overall Summary:** This {video_info['duration']:.1f}-second video shows {all_descriptions.lower()}. "
|
| 162 |
-
|
| 163 |
-
# Add contextual insights
|
| 164 |
-
if len(set(frame_descriptions)) < len(frame_descriptions) * 0.3:
|
| 165 |
-
analysis += "The scene appears relatively static with consistent elements throughout."
|
| 166 |
-
else:
|
| 167 |
-
analysis += "The video shows dynamic content with changing scenes and activities."
|
| 168 |
-
|
| 169 |
-
elif any(word in question_lower for word in ['people', 'person', 'human', 'who']):
|
| 170 |
-
people_mentions = [desc for desc in frame_descriptions if any(word in desc.lower() for word in ['person', 'people', 'man', 'woman', 'child', 'human'])]
|
| 171 |
-
if people_mentions:
|
| 172 |
-
analysis += f"**People in the video:** {' '.join(people_mentions)}\n\n"
|
| 173 |
-
else:
|
| 174 |
-
analysis += "**People analysis:** No clear human figures were detected in the analyzed frames.\n\n"
|
| 175 |
-
|
| 176 |
-
elif any(word in question_lower for word in ['object', 'item', 'thing']):
|
| 177 |
-
analysis += "**Objects and items visible:**\n"
|
| 178 |
-
for desc in frame_descriptions:
|
| 179 |
-
analysis += f"β’ {desc}\n"
|
| 180 |
-
|
| 181 |
-
elif any(word in question_lower for word in ['setting', 'location', 'place', 'where']):
|
| 182 |
-
analysis += "**Setting and location analysis:**\n"
|
| 183 |
-
analysis += f"Based on the visual elements: {all_descriptions}\n\n"
|
| 184 |
-
|
| 185 |
-
elif any(word in question_lower for word in ['mood', 'emotion', 'feeling', 'atmosphere']):
|
| 186 |
-
analysis += "**Mood and atmosphere:**\n"
|
| 187 |
-
analysis += f"The visual elements suggest: {all_descriptions}\n\n"
|
| 188 |
-
|
| 189 |
-
else:
|
| 190 |
-
# General analysis
|
| 191 |
-
analysis += f"**Frame-by-frame analysis:**\n"
|
| 192 |
-
for i, desc in enumerate(frame_descriptions):
|
| 193 |
-
analysis += f"{i+1}. {desc}\n"
|
| 194 |
-
|
| 195 |
-
return analysis
|
| 196 |
-
|
| 197 |
-
@spaces.GPU
|
| 198 |
-
def analyze_video_with_ai(video_file, question, progress=gr.Progress()):
|
| 199 |
-
"""Main video analysis function"""
|
| 200 |
|
| 201 |
if video_file is None:
|
| 202 |
return "β Please upload a video file first."
|
|
@@ -204,81 +78,145 @@ def analyze_video_with_ai(video_file, question, progress=gr.Progress()):
|
|
| 204 |
if not question.strip():
|
| 205 |
return "β Please enter a question about the video."
|
| 206 |
|
| 207 |
-
if not model_loaded:
|
| 208 |
-
return "β
|
| 209 |
|
| 210 |
try:
|
| 211 |
-
progress(0.1, desc="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
-
|
| 214 |
-
frames, video_info, timestamps = extract_key_frames(video_file, max_frames=8)
|
| 215 |
|
| 216 |
-
|
| 217 |
-
|
|
|
|
| 218 |
|
| 219 |
-
|
|
|
|
| 220 |
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
else:
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
|
| 235 |
-
|
|
|
|
| 236 |
|
| 237 |
-
#
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
-
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
β’ Frame Rate: {video_info['fps']:.1f} FPS
|
| 246 |
-
β’ Total Frames: {video_info['total_frames']:,}
|
| 247 |
-
β’ Analyzed Frames: {video_info['extracted_frames']}
|
| 248 |
-
β’ Resolution: {video_info['resolution']}
|
| 249 |
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
"""
|
| 252 |
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
return analysis
|
| 256 |
|
| 257 |
except Exception as e:
|
| 258 |
-
error_msg = f"β Error during analysis: {str(e)}"
|
| 259 |
print(error_msg)
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
def create_interface():
|
| 263 |
"""Create the Gradio interface"""
|
| 264 |
|
| 265 |
-
with gr.Blocks(title="AI
|
| 266 |
-
gr.Markdown("# π₯
|
| 267 |
-
gr.Markdown("Upload videos and get detailed AI-powered analysis using
|
| 268 |
|
| 269 |
# Model loading section
|
| 270 |
with gr.Row():
|
| 271 |
with gr.Column(scale=3):
|
| 272 |
model_status = gr.Textbox(
|
| 273 |
label="π€ Model Status",
|
| 274 |
-
value="
|
| 275 |
interactive=False,
|
| 276 |
lines=2
|
| 277 |
)
|
| 278 |
with gr.Column(scale=1):
|
| 279 |
-
load_btn = gr.Button("π Load
|
| 280 |
|
| 281 |
-
load_btn.click(
|
| 282 |
|
| 283 |
gr.Markdown("---")
|
| 284 |
|
|
@@ -295,7 +233,7 @@ def create_interface():
|
|
| 295 |
lines=3,
|
| 296 |
max_lines=5
|
| 297 |
)
|
| 298 |
-
analyze_btn = gr.Button("π Analyze Video with
|
| 299 |
|
| 300 |
with gr.Column(scale=1):
|
| 301 |
output = gr.Textbox(
|
|
@@ -329,7 +267,7 @@ def create_interface():
|
|
| 329 |
|
| 330 |
# Connect the analyze button
|
| 331 |
analyze_btn.click(
|
| 332 |
-
|
| 333 |
inputs=[video_input, question_input],
|
| 334 |
outputs=output,
|
| 335 |
show_progress=True
|
|
@@ -338,16 +276,15 @@ def create_interface():
|
|
| 338 |
gr.Markdown("---")
|
| 339 |
gr.Markdown("""
|
| 340 |
### π Instructions:
|
| 341 |
-
1. **First:** Click "Load
|
| 342 |
-
2. **Then:** Upload your video file (works with
|
| 343 |
3. **Ask:** Type your question about the video content
|
| 344 |
-
4. **Analyze:** Click "Analyze Video with
|
| 345 |
|
| 346 |
-
π‘ **
|
| 347 |
-
-
|
| 348 |
-
-
|
| 349 |
-
-
|
| 350 |
-
- Works reliably with standard video formats
|
| 351 |
""")
|
| 352 |
|
| 353 |
return demo
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
import spaces
|
| 7 |
+
import tempfile
|
| 8 |
+
import os
|
| 9 |
+
import subprocess
|
| 10 |
+
import sys
|
| 11 |
+
|
| 12 |
+
# Install dependencies if needed
|
| 13 |
+
def install_dependencies():
|
| 14 |
+
"""Install required packages for VideoLLaMA3"""
|
| 15 |
+
packages = ["decord", "imageio", "imageio-ffmpeg"]
|
| 16 |
+
for package in packages:
|
| 17 |
+
try:
|
| 18 |
+
__import__(package.replace("-", "_"))
|
| 19 |
+
except ImportError:
|
| 20 |
+
print(f"Installing {package}...")
|
| 21 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", package, "--quiet"])
|
| 22 |
+
|
| 23 |
+
# Install dependencies on startup
|
| 24 |
+
install_dependencies()
|
| 25 |
+
|
| 26 |
+
from transformers import AutoModelForCausalLM, AutoProcessor
|
| 27 |
import warnings
|
| 28 |
warnings.filterwarnings("ignore")
|
| 29 |
|
| 30 |
# Global variables
|
| 31 |
+
model = None
|
| 32 |
+
processor = None
|
|
|
|
|
|
|
| 33 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 34 |
model_loaded = False
|
| 35 |
|
| 36 |
@spaces.GPU
|
| 37 |
+
def load_videollama3_model():
|
| 38 |
+
"""Load VideoLLaMA3 model"""
|
| 39 |
+
global model, processor, model_loaded
|
| 40 |
|
| 41 |
try:
|
| 42 |
+
print("π Loading VideoLLaMA3-7B model...")
|
| 43 |
|
| 44 |
+
model_name = "DAMO-NLP-SG/VideoLLaMA3-7B"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
print("Loading processor...")
|
| 47 |
+
processor = AutoProcessor.from_pretrained(
|
| 48 |
+
model_name,
|
| 49 |
+
trust_remote_code=True
|
|
|
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
|
| 52 |
+
print("Loading VideoLLaMA3 model (this may take several minutes)...")
|
| 53 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 54 |
+
model_name,
|
| 55 |
+
trust_remote_code=True,
|
| 56 |
+
device_map="auto",
|
| 57 |
+
torch_dtype=torch.bfloat16,
|
| 58 |
+
)
|
| 59 |
|
| 60 |
model_loaded = True
|
| 61 |
+
success_msg = "β
VideoLLaMA3-7B model loaded successfully! You can now analyze videos with AI."
|
| 62 |
print(success_msg)
|
| 63 |
return success_msg
|
| 64 |
|
| 65 |
except Exception as e:
|
| 66 |
model_loaded = False
|
| 67 |
+
error_msg = f"β Failed to load VideoLLaMA3: {str(e)}"
|
| 68 |
print(error_msg)
|
| 69 |
return error_msg
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
@spaces.GPU
|
| 72 |
+
def analyze_video_with_videollama3(video_file, question, progress=gr.Progress()):
|
| 73 |
+
"""Analyze video using VideoLLaMA3"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
if video_file is None:
|
| 76 |
return "β Please upload a video file first."
|
|
|
|
| 78 |
if not question.strip():
|
| 79 |
return "β Please enter a question about the video."
|
| 80 |
|
| 81 |
+
if not model_loaded or model is None or processor is None:
|
| 82 |
+
return "β VideoLLaMA3 model is not loaded. Please click 'Load VideoLLaMA3 Model' first and wait for completion."
|
| 83 |
|
| 84 |
try:
|
| 85 |
+
progress(0.1, desc="Preparing video for analysis...")
|
| 86 |
+
|
| 87 |
+
# Create the conversation in the format VideoLLaMA3 expects
|
| 88 |
+
conversation = [
|
| 89 |
+
{"role": "system", "content": "You are a helpful assistant that can analyze videos."},
|
| 90 |
+
{
|
| 91 |
+
"role": "user",
|
| 92 |
+
"content": [
|
| 93 |
+
{"type": "video", "video": {"video_path": video_file, "fps": 1, "max_frames": 64}},
|
| 94 |
+
{"type": "text", "text": question}
|
| 95 |
+
]
|
| 96 |
+
}
|
| 97 |
+
]
|
| 98 |
|
| 99 |
+
progress(0.3, desc="Processing video with VideoLLaMA3...")
|
|
|
|
| 100 |
|
| 101 |
+
# Process the conversation
|
| 102 |
+
inputs = processor(conversation=conversation, return_tensors="pt")
|
| 103 |
+
inputs = {k: v.to(device) if isinstance(v, torch.Tensor) else v for k, v in inputs.items()}
|
| 104 |
|
| 105 |
+
if "pixel_values" in inputs:
|
| 106 |
+
inputs["pixel_values"] = inputs["pixel_values"].to(torch.bfloat16)
|
| 107 |
|
| 108 |
+
progress(0.7, desc="Generating AI response...")
|
| 109 |
+
|
| 110 |
+
# Generate response
|
| 111 |
+
with torch.no_grad():
|
| 112 |
+
output_ids = model.generate(
|
| 113 |
+
**inputs,
|
| 114 |
+
max_new_tokens=512,
|
| 115 |
+
temperature=0.7,
|
| 116 |
+
do_sample=True,
|
| 117 |
+
pad_token_id=processor.tokenizer.eos_token_id,
|
| 118 |
+
eos_token_id=processor.tokenizer.eos_token_id
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
progress(0.9, desc="Processing response...")
|
| 122 |
+
|
| 123 |
+
# Decode response
|
| 124 |
+
response = processor.batch_decode(output_ids, skip_special_tokens=True)[0]
|
| 125 |
+
|
| 126 |
+
# Extract assistant response
|
| 127 |
+
if "assistant" in response.lower():
|
| 128 |
+
ai_response = response.split("assistant")[-1].strip()
|
| 129 |
+
elif "user:" in response.lower():
|
| 130 |
+
parts = response.split("user:")
|
| 131 |
+
if len(parts) > 1:
|
| 132 |
+
ai_response = parts[-1].strip()
|
| 133 |
else:
|
| 134 |
+
ai_response = response.strip()
|
| 135 |
+
else:
|
| 136 |
+
ai_response = response.strip()
|
| 137 |
|
| 138 |
+
# Clean up the response
|
| 139 |
+
ai_response = ai_response.replace("</s>", "").strip()
|
| 140 |
|
| 141 |
+
# Get video info for technical details
|
| 142 |
+
cap = cv2.VideoCapture(video_file)
|
| 143 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 144 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 145 |
+
duration = total_frames / fps if fps > 0 else 0
|
| 146 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 147 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 148 |
+
cap.release()
|
| 149 |
|
| 150 |
+
progress(1.0, desc="Analysis complete!")
|
| 151 |
+
|
| 152 |
+
# Format the final response
|
| 153 |
+
formatted_response = f"""π₯ **VideoLLaMA3 AI Video Analysis**
|
| 154 |
+
|
| 155 |
+
β **Your Question:**
|
| 156 |
+
{question}
|
| 157 |
|
| 158 |
+
π€ **AI Analysis:**
|
| 159 |
+
{ai_response}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
+
π **Video Information:**
|
| 162 |
+
β’ Duration: {duration:.1f} seconds
|
| 163 |
+
β’ Frame Rate: {fps:.1f} FPS
|
| 164 |
+
β’ Total Frames: {total_frames:,}
|
| 165 |
+
β’ Resolution: {width}x{height}
|
| 166 |
+
|
| 167 |
+
β‘ **Powered by:** VideoLLaMA3-7B (Multimodal AI)
|
| 168 |
"""
|
| 169 |
|
| 170 |
+
return formatted_response
|
|
|
|
|
|
|
| 171 |
|
| 172 |
except Exception as e:
|
| 173 |
+
error_msg = f"β Error during VideoLLaMA3 analysis: {str(e)}"
|
| 174 |
print(error_msg)
|
| 175 |
+
|
| 176 |
+
# Fallback: Basic video analysis if VideoLLaMA3 fails
|
| 177 |
+
try:
|
| 178 |
+
cap = cv2.VideoCapture(video_file)
|
| 179 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 180 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 181 |
+
duration = total_frames / fps if fps > 0 else 0
|
| 182 |
+
cap.release()
|
| 183 |
+
|
| 184 |
+
fallback_response = f"""β VideoLLaMA3 analysis failed, but here's what I can tell you:
|
| 185 |
+
|
| 186 |
+
**Video Technical Info:**
|
| 187 |
+
β’ Duration: {duration:.1f} seconds
|
| 188 |
+
β’ Frame Rate: {fps:.1f} FPS
|
| 189 |
+
β’ Total Frames: {total_frames:,}
|
| 190 |
+
|
| 191 |
+
**Error:** {str(e)}
|
| 192 |
+
|
| 193 |
+
**Suggestion:** Try reloading the model or using a shorter video file.
|
| 194 |
+
"""
|
| 195 |
+
return fallback_response
|
| 196 |
+
|
| 197 |
+
except:
|
| 198 |
+
return error_msg
|
| 199 |
|
| 200 |
def create_interface():
|
| 201 |
"""Create the Gradio interface"""
|
| 202 |
|
| 203 |
+
with gr.Blocks(title="VideoLLaMA3 AI Analyzer", theme=gr.themes.Soft()) as demo:
|
| 204 |
+
gr.Markdown("# π₯ VideoLLaMA3 Video Analysis Tool")
|
| 205 |
+
gr.Markdown("Upload videos and get detailed AI-powered analysis using VideoLLaMA3-7B!")
|
| 206 |
|
| 207 |
# Model loading section
|
| 208 |
with gr.Row():
|
| 209 |
with gr.Column(scale=3):
|
| 210 |
model_status = gr.Textbox(
|
| 211 |
label="π€ Model Status",
|
| 212 |
+
value="Model not loaded - Click the button to load VideoLLaMA3-7B β",
|
| 213 |
interactive=False,
|
| 214 |
lines=2
|
| 215 |
)
|
| 216 |
with gr.Column(scale=1):
|
| 217 |
+
load_btn = gr.Button("π Load VideoLLaMA3 Model", variant="primary", size="lg")
|
| 218 |
|
| 219 |
+
load_btn.click(load_videollama3_model, outputs=model_status)
|
| 220 |
|
| 221 |
gr.Markdown("---")
|
| 222 |
|
|
|
|
| 233 |
lines=3,
|
| 234 |
max_lines=5
|
| 235 |
)
|
| 236 |
+
analyze_btn = gr.Button("π Analyze Video with VideoLLaMA3", variant="primary", size="lg")
|
| 237 |
|
| 238 |
with gr.Column(scale=1):
|
| 239 |
output = gr.Textbox(
|
|
|
|
| 267 |
|
| 268 |
# Connect the analyze button
|
| 269 |
analyze_btn.click(
|
| 270 |
+
analyze_video_with_videollama3,
|
| 271 |
inputs=[video_input, question_input],
|
| 272 |
outputs=output,
|
| 273 |
show_progress=True
|
|
|
|
| 276 |
gr.Markdown("---")
|
| 277 |
gr.Markdown("""
|
| 278 |
### π Instructions:
|
| 279 |
+
1. **First:** Click "Load VideoLLaMA3 Model" and wait for it to complete (~5-10 minutes)
|
| 280 |
+
2. **Then:** Upload your video file (works best with videos under 2 minutes)
|
| 281 |
3. **Ask:** Type your question about the video content
|
| 282 |
+
4. **Analyze:** Click "Analyze Video with VideoLLaMA3" to get detailed insights
|
| 283 |
|
| 284 |
+
π‘ **Tips:**
|
| 285 |
+
- Keep videos under 2 minutes for best performance
|
| 286 |
+
- Ask specific, detailed questions for better results
|
| 287 |
+
- The model will analyze up to 64 frames from your video
|
|
|
|
| 288 |
""")
|
| 289 |
|
| 290 |
return demo
|