Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
+
from llama_cpp.llama_chat_format import Qwen2VLChatHandler
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
import base64
|
| 7 |
+
|
| 8 |
+
# 1. 权限设置
|
| 9 |
+
token = os.getenv("HF_TOKEN")
|
| 10 |
+
model_repo = "edge-physio-ai/rehab_expert_q4"
|
| 11 |
+
|
| 12 |
+
# 2. 下载模型 (如果报错说找不到文件,请检查文件名是否准确)
|
| 13 |
+
print("--- 正在从仓库拉取模型文件 ---")
|
| 14 |
+
model_path = hf_hub_download(repo_id=model_repo, filename="model_q4_k_m.gguf", token=token)
|
| 15 |
+
|
| 16 |
+
# 3. 加载推理引擎
|
| 17 |
+
llm = Llama(
|
| 18 |
+
model_path=model_path,
|
| 19 |
+
chat_handler=Qwen2VLChatHandler(),
|
| 20 |
+
n_ctx=1024,
|
| 21 |
+
n_threads=2 # 免费版 CPU 只有 2 核,设为 2 最稳
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
def analyze(image_path):
|
| 25 |
+
if not image_path:
|
| 26 |
+
return "请先上传一张康复动作照片。"
|
| 27 |
+
|
| 28 |
+
# 编码图片
|
| 29 |
+
with open(image_path, "rb") as f:
|
| 30 |
+
base64_image = base64.b64encode(f.read()).decode("utf-8")
|
| 31 |
+
|
| 32 |
+
messages = [
|
| 33 |
+
{
|
| 34 |
+
"role": "user",
|
| 35 |
+
"content": [
|
| 36 |
+
{"type": "text", "text": "你是一位专业的康复医学专家。请分析图中患者动作的标准度,并给出改进建议。"},
|
| 37 |
+
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}
|
| 38 |
+
]
|
| 39 |
+
}
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
print("--- 正在生成分析报告 (CPU 推理中) ---")
|
| 43 |
+
response = llm.create_chat_completion(messages=messages, max_tokens=512)
|
| 44 |
+
return response["choices"][0]["message"]["content"]
|
| 45 |
+
|
| 46 |
+
# 4. Gradio 6.x 界面布局
|
| 47 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 48 |
+
gr.Markdown("# 🏃 具身康复专家 AI (Gradio 6.5)")
|
| 49 |
+
with gr.Row():
|
| 50 |
+
with gr.Column():
|
| 51 |
+
input_img = gr.Image(type="filepath", label="上传动作图片")
|
| 52 |
+
btn = gr.Button("开始专家评估", variant="primary")
|
| 53 |
+
with gr.Column():
|
| 54 |
+
output_text = gr.Textbox(label="康复分析报告", lines=10)
|
| 55 |
+
|
| 56 |
+
btn.click(fn=analyze, inputs=input_img, outputs=output_text)
|
| 57 |
+
|
| 58 |
+
demo.launch()
|