Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,50 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import
|
| 4 |
|
| 5 |
-
#
|
| 6 |
model_name = "DAMO-NLP-SG/VideoRefer-VideoLLaMA3-7B"
|
| 7 |
|
| 8 |
-
#
|
|
|
|
|
|
|
|
|
|
| 9 |
def load_model():
|
|
|
|
| 10 |
try:
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
-
model_name,
|
| 15 |
-
|
| 16 |
device_map="auto",
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
except Exception as e:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
tokenizer, model = load_model()
|
| 26 |
|
| 27 |
def process_video_question(video_file, question):
|
| 28 |
-
"""Process video and answer questions about it"""
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
if video_file is None:
|
| 33 |
return "Please upload a video file first."
|
|
@@ -36,20 +53,84 @@ def process_video_question(video_file, question):
|
|
| 36 |
return "Please enter a question about the video."
|
| 37 |
|
| 38 |
try:
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
return response
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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}`")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
gr.Markdown("Upload a video and ask questions about its content!")
|
| 54 |
|
| 55 |
with gr.Row():
|
|
@@ -61,25 +142,29 @@ with gr.Blocks(title="VideoLLaMA3 Demo") as demo:
|
|
| 61 |
question_input = gr.Textbox(
|
| 62 |
label="β Ask a question about the video",
|
| 63 |
placeholder="What is happening in this video?",
|
| 64 |
-
lines=
|
| 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=
|
| 72 |
-
placeholder="The AI response will appear here..."
|
|
|
|
| 73 |
)
|
| 74 |
|
| 75 |
# Examples section
|
| 76 |
-
gr.
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
# Connect the button to the function
|
| 85 |
submit_btn.click(
|
|
@@ -87,6 +172,13 @@ with gr.Blocks(title="VideoLLaMA3 Demo") as demo:
|
|
| 87 |
inputs=[video_input, question_input],
|
| 88 |
outputs=output_text
|
| 89 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
# Launch the app
|
| 92 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoProcessor
|
| 4 |
|
| 5 |
+
# Model name
|
| 6 |
model_name = "DAMO-NLP-SG/VideoRefer-VideoLLaMA3-7B"
|
| 7 |
|
| 8 |
+
# Global variables for model and processor
|
| 9 |
+
model = None
|
| 10 |
+
processor = None
|
| 11 |
+
|
| 12 |
def load_model():
|
| 13 |
+
global model, processor
|
| 14 |
try:
|
| 15 |
+
print("Loading VideoLLaMA3 model...")
|
| 16 |
+
print("This may take several minutes on first load...")
|
| 17 |
+
|
| 18 |
+
# Load model with correct parameters based on official documentation
|
| 19 |
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
+
model_name,
|
| 21 |
+
trust_remote_code=True,
|
| 22 |
device_map="auto",
|
| 23 |
+
torch_dtype=torch.bfloat16, # Changed from float16 to bfloat16
|
| 24 |
+
attn_implementation="flash_attention_2", # Added for better performance
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Load processor (not tokenizer)
|
| 28 |
+
processor = AutoProcessor.from_pretrained(
|
| 29 |
+
model_name,
|
| 30 |
+
trust_remote_code=True
|
| 31 |
)
|
| 32 |
+
|
| 33 |
+
print("Model and processor loaded successfully!")
|
| 34 |
+
return True
|
| 35 |
+
|
| 36 |
except Exception as e:
|
| 37 |
+
print(f"Error loading model: {e}")
|
| 38 |
+
import traceback
|
| 39 |
+
traceback.print_exc()
|
| 40 |
+
return False
|
|
|
|
| 41 |
|
| 42 |
def process_video_question(video_file, question):
|
| 43 |
+
"""Process video and answer questions about it using VideoLLaMA3"""
|
| 44 |
+
global model, processor
|
| 45 |
+
|
| 46 |
+
if model is None or processor is None:
|
| 47 |
+
return "Model is not loaded. Please wait for the model to initialize or check the logs for errors."
|
| 48 |
|
| 49 |
if video_file is None:
|
| 50 |
return "Please upload a video file first."
|
|
|
|
| 53 |
return "Please enter a question about the video."
|
| 54 |
|
| 55 |
try:
|
| 56 |
+
print(f"Processing video: {video_file}")
|
| 57 |
+
print(f"Question: {question}")
|
| 58 |
+
|
| 59 |
+
# Prepare conversation in the format expected by VideoLLaMA3
|
| 60 |
+
conversation = [
|
| 61 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 62 |
+
{
|
| 63 |
+
"role": "user",
|
| 64 |
+
"content": [
|
| 65 |
+
{
|
| 66 |
+
"type": "video",
|
| 67 |
+
"video": {
|
| 68 |
+
"video_path": video_file,
|
| 69 |
+
"fps": 1,
|
| 70 |
+
"max_frames": 128
|
| 71 |
+
}
|
| 72 |
+
},
|
| 73 |
+
{"type": "text", "text": question}
|
| 74 |
+
]
|
| 75 |
+
}
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
+
# Process the conversation
|
| 79 |
+
inputs = processor(conversation=conversation, return_tensors="pt")
|
| 80 |
+
|
| 81 |
+
# Move inputs to GPU if available
|
| 82 |
+
inputs = {k: v.cuda() if isinstance(v, torch.Tensor) else v for k, v in inputs.items()}
|
| 83 |
+
|
| 84 |
+
# Convert pixel values to bfloat16 if present
|
| 85 |
+
if "pixel_values" in inputs:
|
| 86 |
+
inputs["pixel_values"] = inputs["pixel_values"].to(torch.bfloat16)
|
| 87 |
+
|
| 88 |
+
# Generate response
|
| 89 |
+
print("Generating response...")
|
| 90 |
+
with torch.no_grad():
|
| 91 |
+
output_ids = model.generate(
|
| 92 |
+
**inputs,
|
| 93 |
+
max_new_tokens=512,
|
| 94 |
+
do_sample=True,
|
| 95 |
+
temperature=0.7,
|
| 96 |
+
top_p=0.9,
|
| 97 |
+
use_cache=True
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
# Decode the response
|
| 101 |
+
response = processor.decode(output_ids[0], skip_special_tokens=True)
|
| 102 |
|
| 103 |
+
# Extract just the assistant's response (remove the conversation history)
|
| 104 |
+
if "assistant" in response:
|
| 105 |
+
response = response.split("assistant")[-1].strip()
|
| 106 |
+
|
| 107 |
+
print(f"Generated response: {response}")
|
| 108 |
return response
|
| 109 |
|
| 110 |
except Exception as e:
|
| 111 |
+
error_msg = f"Error processing video: {str(e)}"
|
| 112 |
+
print(error_msg)
|
| 113 |
+
import traceback
|
| 114 |
+
traceback.print_exc()
|
| 115 |
+
return error_msg
|
| 116 |
+
|
| 117 |
+
# Initialize model when the Space starts
|
| 118 |
+
print(f"Initializing {model_name}...")
|
| 119 |
+
model_loaded = load_model()
|
| 120 |
+
|
| 121 |
+
if not model_loaded:
|
| 122 |
+
print("β Failed to load model. Check the logs above for details.")
|
| 123 |
|
| 124 |
# Create the Gradio interface
|
| 125 |
+
with gr.Blocks(title="VideoLLaMA3 Demo", theme=gr.themes.Soft()) as demo:
|
| 126 |
+
gr.Markdown("# π₯ VideoRefer-VideoLLaMA3 Interactive Demo")
|
| 127 |
+
gr.Markdown(f"**Model:** `{model_name}`")
|
| 128 |
+
|
| 129 |
+
if model_loaded:
|
| 130 |
+
gr.Markdown("β
**Model Status:** Loaded and ready!")
|
| 131 |
+
else:
|
| 132 |
+
gr.Markdown("β **Model Status:** Failed to load. Check logs for details.")
|
| 133 |
+
|
| 134 |
gr.Markdown("Upload a video and ask questions about its content!")
|
| 135 |
|
| 136 |
with gr.Row():
|
|
|
|
| 142 |
question_input = gr.Textbox(
|
| 143 |
label="β Ask a question about the video",
|
| 144 |
placeholder="What is happening in this video?",
|
| 145 |
+
lines=3
|
| 146 |
)
|
| 147 |
+
submit_btn = gr.Button("π Analyze Video", variant="primary", size="lg")
|
| 148 |
|
| 149 |
with gr.Column(scale=1):
|
| 150 |
output_text = gr.Textbox(
|
| 151 |
label="π€ AI Response",
|
| 152 |
+
lines=12,
|
| 153 |
+
placeholder="The AI response will appear here...",
|
| 154 |
+
show_copy_button=True
|
| 155 |
)
|
| 156 |
|
| 157 |
# Examples section
|
| 158 |
+
with gr.Row():
|
| 159 |
+
gr.Markdown("""
|
| 160 |
+
### π‘ Example Questions:
|
| 161 |
+
- "What objects can you see in this video?"
|
| 162 |
+
- "Describe the main action happening in detail"
|
| 163 |
+
- "What is the setting or location of this video?"
|
| 164 |
+
- "How many people are in the video and what are they doing?"
|
| 165 |
+
- "What emotions or mood does this video convey?"
|
| 166 |
+
- "Describe the sequence of events in chronological order"
|
| 167 |
+
""")
|
| 168 |
|
| 169 |
# Connect the button to the function
|
| 170 |
submit_btn.click(
|
|
|
|
| 172 |
inputs=[video_input, question_input],
|
| 173 |
outputs=output_text
|
| 174 |
)
|
| 175 |
+
|
| 176 |
+
# Auto-submit when Enter is pressed in the question box
|
| 177 |
+
question_input.submit(
|
| 178 |
+
fn=process_video_question,
|
| 179 |
+
inputs=[video_input, question_input],
|
| 180 |
+
outputs=output_text
|
| 181 |
+
)
|
| 182 |
|
| 183 |
# Launch the app
|
| 184 |
if __name__ == "__main__":
|