Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import QwenProcessor, QwenForVisionAndLanguageGeneration
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load the Qwen-VL model and processor (on CPU)
|
6 |
+
processor = QwenProcessor.from_pretrained("Qwen/Qwen-VL")
|
7 |
+
model = QwenForVisionAndLanguageGeneration.from_pretrained("Qwen/Qwen-VL")
|
8 |
+
|
9 |
+
# Define the function to process the video and return analysis
|
10 |
+
def analyze_exercise(video_path):
|
11 |
+
# Create the message prompt for exercise analysis
|
12 |
+
messages = [
|
13 |
+
{
|
14 |
+
"role": "user",
|
15 |
+
"content": [
|
16 |
+
{
|
17 |
+
"type": "video",
|
18 |
+
},
|
19 |
+
{
|
20 |
+
"type": "text",
|
21 |
+
"text": (
|
22 |
+
"Analyze the exercise shown in the video. "
|
23 |
+
"Please provide details about the exercise type, the number of repetitions, "
|
24 |
+
"and an estimate of calories burned during the video."
|
25 |
+
)
|
26 |
+
}
|
27 |
+
]
|
28 |
+
}
|
29 |
+
]
|
30 |
+
|
31 |
+
# Generate the prompt and inputs
|
32 |
+
text_prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
33 |
+
|
34 |
+
# Prepare inputs for the model with the uploaded video
|
35 |
+
inputs = processor(
|
36 |
+
text=[text_prompt],
|
37 |
+
videos=[video_path],
|
38 |
+
padding=True,
|
39 |
+
return_tensors="pt"
|
40 |
+
)
|
41 |
+
|
42 |
+
# Generate model output
|
43 |
+
output_ids = model.generate(**inputs, max_new_tokens=1024)
|
44 |
+
|
45 |
+
# Decode and return the text output
|
46 |
+
output_text = processor.batch_decode(
|
47 |
+
output_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
|
48 |
+
)
|
49 |
+
|
50 |
+
return output_text[0]
|
51 |
+
|
52 |
+
# Set up the Gradio interface
|
53 |
+
with gr.Blocks() as app:
|
54 |
+
gr.Markdown("## Exercise Video Analyzer")
|
55 |
+
gr.Markdown("Upload a video to analyze the exercise, count repetitions, and estimate calories burned.")
|
56 |
+
|
57 |
+
video_input = gr.Video(label="Upload Exercise Video")
|
58 |
+
text_output = gr.Textbox(label="Exercise Analysis")
|
59 |
+
|
60 |
+
analyze_button = gr.Button("Analyze Exercise")
|
61 |
+
|
62 |
+
# When analyze button is clicked, call the analyze_exercise function
|
63 |
+
analyze_button.click(analyze_exercise, inputs=video_input, outputs=text_output)
|
64 |
+
|
65 |
+
# Launch the app
|
66 |
+
app.launch()
|