File size: 2,182 Bytes
f599177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import gradio as gr
from transformers import QwenProcessor, QwenForVisionAndLanguageGeneration
import torch

# Load the Qwen-VL model and processor (on CPU)
processor = QwenProcessor.from_pretrained("Qwen/Qwen-VL")
model = QwenForVisionAndLanguageGeneration.from_pretrained("Qwen/Qwen-VL")

# Define the function to process the video and return analysis
def analyze_exercise(video_path):
    # Create the message prompt for exercise analysis
    messages = [
        {
            "role": "user",
            "content": [
                {
                    "type": "video",
                },
                {
                    "type": "text",
                    "text": (
                        "Analyze the exercise shown in the video. "
                        "Please provide details about the exercise type, the number of repetitions, "
                        "and an estimate of calories burned during the video."
                    )
                }
            ]
        }
    ]

    # Generate the prompt and inputs
    text_prompt = processor.apply_chat_template(messages, add_generation_prompt=True)

    # Prepare inputs for the model with the uploaded video
    inputs = processor(
        text=[text_prompt],
        videos=[video_path],
        padding=True,
        return_tensors="pt"
    )

    # Generate model output
    output_ids = model.generate(**inputs, max_new_tokens=1024)

    # Decode and return the text output
    output_text = processor.batch_decode(
        output_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
    )
    
    return output_text[0]

# Set up the Gradio interface
with gr.Blocks() as app:
    gr.Markdown("## Exercise Video Analyzer")
    gr.Markdown("Upload a video to analyze the exercise, count repetitions, and estimate calories burned.")
    
    video_input = gr.Video(label="Upload Exercise Video")
    text_output = gr.Textbox(label="Exercise Analysis")

    analyze_button = gr.Button("Analyze Exercise")

    # When analyze button is clicked, call the analyze_exercise function
    analyze_button.click(analyze_exercise, inputs=video_input, outputs=text_output)

# Launch the app
app.launch()