Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,282 +1,93 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
import
|
| 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 |
-
#
|
| 13 |
-
|
| 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 |
-
#
|
| 24 |
-
|
| 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 with the correct implementation"""
|
| 39 |
-
global model, processor, model_loaded
|
| 40 |
-
|
| 41 |
try:
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
model_path = "DAMO-NLP-SG/VideoLLaMA3-7B"
|
| 45 |
-
|
| 46 |
-
print("Loading processor...")
|
| 47 |
-
processor = AutoProcessor.from_pretrained(
|
| 48 |
-
model_path,
|
| 49 |
-
trust_remote_code=True
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
print("Loading VideoLLaMA3 model (this may take several minutes)...")
|
| 53 |
model = AutoModelForCausalLM.from_pretrained(
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
device_map=
|
| 57 |
-
|
| 58 |
)
|
| 59 |
-
|
| 60 |
-
model_loaded = True
|
| 61 |
-
success_msg = "β
VideoLLaMA3-7B model loaded successfully! Ready for video analysis."
|
| 62 |
-
print(success_msg)
|
| 63 |
-
return success_msg
|
| 64 |
-
|
| 65 |
except Exception as e:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
|
| 75 |
if video_file is None:
|
| 76 |
-
return "
|
| 77 |
|
| 78 |
if not question.strip():
|
| 79 |
-
return "
|
| 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 |
-
|
| 86 |
-
|
| 87 |
-
# Create the exact conversation format from VideoLLaMA3 official implementation
|
| 88 |
-
conversation = [
|
| 89 |
-
{"role": "system", "content": "You are a helpful assistant."},
|
| 90 |
-
{
|
| 91 |
-
"role": "user",
|
| 92 |
-
"content": [
|
| 93 |
-
{"type": "video", "video": {"video_path": video_file, "fps": 1, "max_frames": 128}},
|
| 94 |
-
{"type": "text", "text": question}
|
| 95 |
-
]
|
| 96 |
-
}
|
| 97 |
-
]
|
| 98 |
-
|
| 99 |
-
progress(0.3, desc="Processing with VideoLLaMA3...")
|
| 100 |
-
|
| 101 |
-
# Use the EXACT processor call from official VideoLLaMA3 code
|
| 102 |
-
inputs = processor(
|
| 103 |
-
conversation=conversation,
|
| 104 |
-
add_system_prompt=True,
|
| 105 |
-
add_generation_prompt=True,
|
| 106 |
-
return_tensors="pt"
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
# Move inputs to device
|
| 110 |
-
inputs = {k: v.to(device) if isinstance(v, torch.Tensor) else v for k, v in inputs.items()}
|
| 111 |
-
|
| 112 |
-
if "pixel_values" in inputs:
|
| 113 |
-
inputs["pixel_values"] = inputs["pixel_values"].to(torch.bfloat16)
|
| 114 |
-
|
| 115 |
-
progress(0.7, desc="Generating VideoLLaMA3 response...")
|
| 116 |
-
|
| 117 |
-
# Generate response with VideoLLaMA3
|
| 118 |
-
with torch.no_grad():
|
| 119 |
-
output_ids = model.generate(
|
| 120 |
-
**inputs,
|
| 121 |
-
max_new_tokens=512,
|
| 122 |
-
do_sample=True,
|
| 123 |
-
temperature=0.1,
|
| 124 |
-
use_cache=True,
|
| 125 |
-
pad_token_id=processor.tokenizer.eos_token_id,
|
| 126 |
-
eos_token_id=processor.tokenizer.eos_token_id
|
| 127 |
-
)
|
| 128 |
-
|
| 129 |
-
progress(0.9, desc="Processing VideoLLaMA3 response...")
|
| 130 |
-
|
| 131 |
-
# Decode the response
|
| 132 |
-
response = processor.batch_decode(output_ids, skip_special_tokens=True)[0]
|
| 133 |
-
|
| 134 |
-
# Extract assistant response - VideoLLaMA3 specific parsing
|
| 135 |
-
if "assistant" in response.lower():
|
| 136 |
-
ai_response = response.split("assistant")[-1].strip()
|
| 137 |
-
elif "<|im_start|>assistant" in response:
|
| 138 |
-
ai_response = response.split("<|im_start|>assistant")[-1].strip()
|
| 139 |
-
else:
|
| 140 |
-
# Fallback: extract everything after the user's question
|
| 141 |
-
parts = response.split(question)
|
| 142 |
-
if len(parts) > 1:
|
| 143 |
-
ai_response = parts[-1].strip()
|
| 144 |
-
else:
|
| 145 |
-
ai_response = response.strip()
|
| 146 |
|
| 147 |
-
#
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
# Get video metadata
|
| 151 |
-
cap = cv2.VideoCapture(video_file)
|
| 152 |
-
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 153 |
-
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 154 |
-
duration = total_frames / fps if fps > 0 else 0
|
| 155 |
-
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 156 |
-
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 157 |
-
cap.release()
|
| 158 |
-
|
| 159 |
-
progress(1.0, desc="VideoLLaMA3 analysis complete!")
|
| 160 |
-
|
| 161 |
-
# Format response
|
| 162 |
-
formatted_response = f"""π₯ **VideoLLaMA3 AI Video Analysis**
|
| 163 |
-
|
| 164 |
-
β **Your Question:**
|
| 165 |
-
{question}
|
| 166 |
-
|
| 167 |
-
π€ **VideoLLaMA3 Response:**
|
| 168 |
-
{ai_response}
|
| 169 |
-
|
| 170 |
-
π **Video Details:**
|
| 171 |
-
β’ Duration: {duration:.1f} seconds
|
| 172 |
-
β’ Resolution: {width}x{height}
|
| 173 |
-
β’ Frame Rate: {fps:.1f} FPS
|
| 174 |
-
β’ Total Frames: {total_frames:,}
|
| 175 |
-
β’ Analyzed with: Up to 128 frames at 1 FPS
|
| 176 |
-
|
| 177 |
-
β‘ **Powered by:** VideoLLaMA3-7B (Official Implementation)
|
| 178 |
-
"""
|
| 179 |
-
|
| 180 |
-
return formatted_response
|
| 181 |
|
| 182 |
except Exception as e:
|
| 183 |
-
|
| 184 |
-
print(f"Full error: {e}")
|
| 185 |
-
return error_msg
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
-
with gr.
|
| 191 |
-
gr.
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
label="β Ask VideoLLaMA3 about the video",
|
| 219 |
-
placeholder="What is happening in this video? Describe it in detail.",
|
| 220 |
-
lines=3,
|
| 221 |
-
max_lines=5
|
| 222 |
-
)
|
| 223 |
-
analyze_btn = gr.Button("π Analyze with VideoLLaMA3", variant="primary", size="lg")
|
| 224 |
-
|
| 225 |
-
with gr.Column(scale=1):
|
| 226 |
-
output = gr.Textbox(
|
| 227 |
-
label="π― VideoLLaMA3 Analysis Results",
|
| 228 |
-
lines=25,
|
| 229 |
-
max_lines=30,
|
| 230 |
-
show_copy_button=True
|
| 231 |
-
)
|
| 232 |
-
|
| 233 |
-
# Example questions
|
| 234 |
-
gr.Markdown("### π‘ Example Questions for VideoLLaMA3:")
|
| 235 |
-
|
| 236 |
-
example_questions = [
|
| 237 |
-
"What is happening in this video? Describe the scene in detail.",
|
| 238 |
-
"Who are the people in this video and what are they doing?",
|
| 239 |
-
"Describe the setting, location, and environment shown.",
|
| 240 |
-
"What objects can you identify in this video?",
|
| 241 |
-
"What is the mood or atmosphere of this video?",
|
| 242 |
-
"Can you summarize the key events chronologically?"
|
| 243 |
-
]
|
| 244 |
-
|
| 245 |
-
with gr.Row():
|
| 246 |
-
for i in range(0, len(example_questions), 2):
|
| 247 |
-
with gr.Column():
|
| 248 |
-
if i < len(example_questions):
|
| 249 |
-
btn1 = gr.Button(example_questions[i], size="sm")
|
| 250 |
-
btn1.click(lambda x=example_questions[i]: x, outputs=question_input)
|
| 251 |
-
if i+1 < len(example_questions):
|
| 252 |
-
btn2 = gr.Button(example_questions[i+1], size="sm")
|
| 253 |
-
btn2.click(lambda x=example_questions[i+1]: x, outputs=question_input)
|
| 254 |
-
|
| 255 |
-
# Connect analyze button
|
| 256 |
-
analyze_btn.click(
|
| 257 |
-
analyze_video_with_videollama3,
|
| 258 |
-
inputs=[video_input, question_input],
|
| 259 |
-
outputs=output,
|
| 260 |
-
show_progress=True
|
| 261 |
-
)
|
| 262 |
-
|
| 263 |
-
gr.Markdown("---")
|
| 264 |
-
gr.Markdown("""
|
| 265 |
-
### π How to Use VideoLLaMA3:
|
| 266 |
-
1. **Load Model:** Click "Load VideoLLaMA3" and wait (~10 minutes for first load)
|
| 267 |
-
2. **Upload Video:** Choose your video file (works best under 2 minutes)
|
| 268 |
-
3. **Ask Question:** Type what you want to know about the video
|
| 269 |
-
4. **Analyze:** Click "Analyze with VideoLLaMA3" for AI-powered insights
|
| 270 |
-
|
| 271 |
-
### π§ Technical Details:
|
| 272 |
-
- **Model:** VideoLLaMA3-7B (Official DAMO-NLP-SG implementation)
|
| 273 |
-
- **Analysis:** Processes up to 128 frames at 1 FPS sampling
|
| 274 |
-
- **Capabilities:** Video understanding, object detection, scene description, temporal reasoning
|
| 275 |
-
- **Best Performance:** Videos under 2 minutes, clear visuals, specific questions
|
| 276 |
-
""")
|
| 277 |
|
| 278 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
|
|
|
|
| 280 |
if __name__ == "__main__":
|
| 281 |
-
demo = create_interface()
|
| 282 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# HERE IS WHERE THE MODEL NAME GOES β¬οΈ
|
| 6 |
+
model_name = "DAMO-NLP-SG/VideoRefer-VideoLLaMA3-7B"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Load the model function
|
| 9 |
+
def load_model():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
+
# Use the model name here β¬οΈ
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
model_name, # And here β¬οΈ
|
| 15 |
+
torch_dtype=torch.float16,
|
| 16 |
+
device_map="auto",
|
| 17 |
+
trust_remote_code=True # May be needed for some models
|
| 18 |
)
|
| 19 |
+
return tokenizer, model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
except Exception as e:
|
| 21 |
+
return None, None
|
| 22 |
+
|
| 23 |
+
# Initialize model (this happens when the Space starts)
|
| 24 |
+
print(f"Loading model: {model_name}") # And you can use it here β¬οΈ
|
| 25 |
+
tokenizer, model = load_model()
|
| 26 |
|
| 27 |
+
def process_video_question(video_file, question):
|
| 28 |
+
"""Process video and answer questions about it"""
|
| 29 |
+
if model is None:
|
| 30 |
+
return "Sorry, the model failed to load. Please try again later."
|
| 31 |
|
| 32 |
if video_file is None:
|
| 33 |
+
return "Please upload a video file first."
|
| 34 |
|
| 35 |
if not question.strip():
|
| 36 |
+
return "Please enter a question about the video."
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
try:
|
| 39 |
+
# Your video processing logic would go here
|
| 40 |
+
# This is a placeholder - you'll need to implement the actual VideoLLaMA3 pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# For now, just return a simple response
|
| 43 |
+
response = f"I received your video and question: '{question}'. Video processing with {model_name} would happen here."
|
| 44 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
+
return f"Error processing video: {str(e)}"
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# Create the Gradio interface
|
| 50 |
+
with gr.Blocks(title="VideoLLaMA3 Demo") as demo:
|
| 51 |
+
gr.Markdown("# π₯ VideoLLaMA3 Interactive Demo")
|
| 52 |
+
gr.Markdown(f"**Model:** `{model_name}`") # Display the model name β¬οΈ
|
| 53 |
+
gr.Markdown("Upload a video and ask questions about its content!")
|
| 54 |
|
| 55 |
+
with gr.Row():
|
| 56 |
+
with gr.Column(scale=1):
|
| 57 |
+
video_input = gr.Video(
|
| 58 |
+
label="πΉ Upload Video",
|
| 59 |
+
height=300
|
| 60 |
+
)
|
| 61 |
+
question_input = gr.Textbox(
|
| 62 |
+
label="β Ask a question about the video",
|
| 63 |
+
placeholder="What is happening in this video?",
|
| 64 |
+
lines=2
|
| 65 |
+
)
|
| 66 |
+
submit_btn = gr.Button("π Analyze Video", variant="primary")
|
| 67 |
+
|
| 68 |
+
with gr.Column(scale=1):
|
| 69 |
+
output_text = gr.Textbox(
|
| 70 |
+
label="π€ AI Response",
|
| 71 |
+
lines=10,
|
| 72 |
+
placeholder="The AI response will appear here..."
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
# Examples section
|
| 76 |
+
gr.Markdown("### π‘ Example Questions:")
|
| 77 |
+
gr.Markdown("""
|
| 78 |
+
- "What objects can you see in this video?"
|
| 79 |
+
- "Describe the main action happening"
|
| 80 |
+
- "What is the setting or location?"
|
| 81 |
+
- "How many people are in the video?"
|
| 82 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
# Connect the button to the function
|
| 85 |
+
submit_btn.click(
|
| 86 |
+
fn=process_video_question,
|
| 87 |
+
inputs=[video_input, question_input],
|
| 88 |
+
outputs=output_text
|
| 89 |
+
)
|
| 90 |
|
| 91 |
+
# Launch the app
|
| 92 |
if __name__ == "__main__":
|
|
|
|
| 93 |
demo.launch()
|