taybeyond commited on
Commit
dd3e399
·
verified ·
1 Parent(s): 4183849

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +50 -0
  2. requirements.txt +11 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import gradio as gr
3
+ from PIL import Image
4
+ from transformers import AutoTokenizer, AutoProcessor, AutoModelForCausalLM
5
+
6
+ model_id = "Qwen/Qwen-VL-Chat"
7
+
8
+ # 自动加载模型到合适设备(防止 OOM)
9
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
10
+ processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
11
+ model = AutoModelForCausalLM.from_pretrained(
12
+ model_id,
13
+ trust_remote_code=True,
14
+ device_map="auto" # 自动拆分到 GPU/CPU
15
+ ).eval()
16
+
17
+ def analyze_posture(image: Image):
18
+ if image is None:
19
+ return "❗ Please upload a clear photo showing your sitting posture.请上传一张坐姿清晰的照片。"
20
+
21
+ if image.size[0] < 300 or image.size[1] < 300:
22
+ return "⚠️ 图像分辨率过低,建议上传更清晰的坐姿照片(宽高 > 300px)"
23
+
24
+ question = "这个人坐姿是否良好?是否驼背?用简洁中文回答,再用英文总结。"
25
+ prompt = f"<|im_start|>user\n{question}<|im_end|>\n<|im_start|>assistant\n"
26
+
27
+ start = time.time()
28
+ try:
29
+ inputs = processor(images=image, text=prompt, return_tensors="pt").to(model.device)
30
+ outputs = model.generate(**inputs, max_new_tokens=512)
31
+ result = tokenizer.decode(outputs[0], skip_special_tokens=True)
32
+ final = result.split("<|im_start|>assistant\n")[-1].strip()
33
+ end = time.time()
34
+ return f"⏱️ 回答时间:{round(end - start, 2)} 秒\n\n{final}"
35
+ except Exception as e:
36
+ return f"❌ 出现错误:{str(e)}\n\n💡 建议:确认图片清晰,或稍后重试。"
37
+
38
+ # UI 设置
39
+ demo = gr.Interface(
40
+ fn=analyze_posture,
41
+ inputs=gr.Image(type="pil", label="上传你的坐姿照片"),
42
+ outputs=gr.Textbox(label="Analysis Result (Chinese + English Reminder) 分析结果(中文+英文提醒)"),
43
+ title="🪑 Posture Monitoring Demo 坐姿监测Demo",
44
+ description="Upload a photo to detect whether your sitting posture is good or if you have a hunchback issue. Automatically generates reminders in both Chinese and English (powered by Qwen-VL).上传照片,识别你是否坐姿良好或有驼背问题,自动生成中英文提醒(由 Qwen-VL 支持)",
45
+ theme="soft",
46
+ allow_flagging="never"
47
+ )
48
+
49
+ if __name__ == "__main__":
50
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ transformers>=4.36.2
2
+ torch
3
+ gradio
4
+ einops
5
+ tiktoken
6
+ torchvision
7
+ optimum
8
+ auto-gptq
9
+ opencv-python
10
+ pyttsx3
11
+ transformers_stream_generator