File size: 1,890 Bytes
6d16e6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import gradio as gr
import torch
from transformers import AutoProcessor, AutoModelForCausalLM

# Load the processor and model from Hugging Face
processor = AutoProcessor.from_pretrained("lmms-lab/LLaVA-Video-7B-Qwen2")
model = AutoModelForCausalLM.from_pretrained("lmms-lab/LLaVA-Video-7B-Qwen2")

# Set the device (use GPU if available)
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)

def analyze_video(video_path):
    """
    This function accepts the path to a video file,
    then uses the LLaVA-Video-7B-Qwen2 model to analyze the video.
    
    The prompt instructs the model to analyze the video and return 
    the moment when the crowd is most engaged.
    """
    # Define the prompt for the model
    prompt = "Analyze this video of a concert and determine the moment when the crowd is most engaged."
    
    # Process the video and prompt.
    # Note: The processor is expected to handle the video input (e.g., by reading frames).
    inputs = processor(text=prompt, video=video_path, return_tensors="pt")
    
    # Move all tensor inputs to the selected device
    inputs = {key: value.to(device) for key, value in inputs.items()}
    
    # Generate the model's response
    outputs = model.generate(**inputs, max_new_tokens=100)
    
    # Decode the generated tokens to a human-readable string
    answer = processor.decode(outputs[0], skip_special_tokens=True)
    return answer

# Create the Gradio Interface
iface = gr.Interface(
    fn=analyze_video,
    inputs=gr.Video(label="Upload Concert/Event Video", type="filepath"),
    outputs=gr.Textbox(label="Engagement Analysis"),
    title="Crowd Engagement Analyzer",
    description=(
        "Upload a video of a concert or event and the model will analyze "
        "the video to identify the moment when the crowd is most engaged."
    )
)

if __name__ == "__main__":
    iface.launch()