PinHsuan commited on
Commit
f283a81
·
verified ·
1 Parent(s): abd5be4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -84
app.py CHANGED
@@ -37,90 +37,6 @@ def analyze_and_predict(*all_answers):
37
  x1_vals = [ccmq_map.get(a, 1) for a in all_answers[:24]]
38
  x2_vals = [osdi_map.get(a, 0) for a in all_answers[24:34]]
39
 
40
-
41
- x1_scaled = scaler_ccmq.transform(np.array([x1_vals]))
42
- x2_scaled = scaler_osdi.transform(np.array([x2_vals]))
43
-
44
- sx1 = torch.tensor(x1_scaled, dtype=torch.float32).to(DEVICE)
45
- sx2 = torch.tensor(x2_scaled, dtype=torch.float32).to(DEVICE)
46
-
47
- with torch.no_grad():
48
- feats = model(sx1, sx2)
49
- logits = metric_fc.predict(feats)
50
- probs = torch.softmax(logits, dim=1)
51
- pred_idx = torch.argmax(probs, dim=1).item()
52
- conf = probs[0, pred_idx].item()
53
- except Exception as e:
54
- raise gr.Error(f"推論出錯: {e}")
55
-
56
- res_label = " 乾眼風險 (SJS/DES)" if pred_idx == 1 else " 正常/健康"
57
- table_data = [[f"項目 {i+1}", str(all_answers[i]), "OK"] for i in range(len(all_answers))]
58
-
59
-
60
- return (
61
- gr.update(visible=False),
62
- gr.update(visible=True),
63
- f"### {res_label}",
64
- f"AI 推論成功!診斷信心度為 {conf:.2%}。分析已整合 CCMQ 體質與 OSDI 臨床特徵。",
65
- {"Risk": conf if pred_idx==1 else 1-conf, "Healthy": 1 - (conf if pred_idx==1 else 1-conf)},
66
- table_data
67
- )
68
-
69
- with gr.Blocks(css=".scroll-box { height: 400px; overflow-y: auto; border: 1px solid #ddd; padding: 10px; }") as demo:
70
- gr.Markdown("# 中醫 AI 多模態診斷系統")
71
-
72
-
73
- with gr.Column(visible=True) as input_area:
74
- with gr.Row():
75
- with gr.Column():
76
- gr.Markdown("#### Step 1: CCMQ 體質量表")
77
- with gr.Group(elem_classes="scroll-box"):
78
- ccmq_labels = ["惡寒惡風", "自汗", "胸悶腹脹","咽喉痰梗感","多愁善感","易受驚","面部暗沉","黑眼圈","健忘","唇色暗","身熱、面熱","膚乾口乾","唇紅","便祕","兩顴紅","眼乾澀","四肢冷","惡寒、腰膝冷","飲冷腹瀉","口苦口臭","帶下色黃/下陰潮濕","鼻塞流涕","變天咳喘","過敏"]
79
- all_ccmq = [gr.Radio(["總是", "經常", "有時", "很少", "沒有"], label=f"{i+1}. {txt}", value="沒有") for i, txt in enumerate(ccmq_labels)]
80
-
81
- with gr.Column():
82
- gr.Markdown("#### Step 2: OSDI 症狀量表")
83
- with gr.Group(elem_classes="scroll-box"):
84
- osdi_labels = ["1. 對光敏感", "2. 眼睛疼痛", "3. 視線模糊", "4. 視力減退", "5. 閱讀限制", "6. 夜間駕駛", "7. 電腦操作", "8. 觀看電視", "9. 刮風不適", "10. 空調不適"]
85
- all_osdi = [gr.Radio(["總是", "經常", "一半一半", "偶而", "完全不曾"], label=txt, value="完全不曾") for txt in osdi_labels]
86
-
87
- submit_btn = gr.Button("提交診斷並生成報告", variant="primary", size="lg")
88
-
89
-
90
- with gr.Column(visible=False) as result_area:
91
- gr.Markdown("### AI 診斷分析報告")
92
- res_title = gr.Markdown("### 結論加載中...")
93
- with gr.Row():
94
- with gr.Column():
95
- res_prob = gr.Label(label="預測機率分佈")
96
- res_desc = gr.Markdown("報告詳情...")
97
- with gr.Column():
98
- res_table = gr.Dataframe(headers=["項目", "回答", "狀態"], interactive=False)
99
-
100
- reset_btn = gr.Button("重新開始測驗", variant="secondary")
101
-
102
- all_inputs = all_ccmq + all_osdi
103
-
104
- submit_btn.click(
105
- fn=analyze_and_predict,
106
- inputs=all_inputs,
107
- outputs=[input_area, result_area, res_title, res_desc, res_prob, res_table]
108
- )
109
-
110
- reset_btn.click(
111
- fn=lambda: [gr.update(visible=True), gr.update(visible=False)] + ["沒有"]*24 + ["完全不曾"]*10,
112
- outputs=[input_area, result_area] + all_inputs
113
- )
114
-
115
- if __name__ == "__main__":
116
-
117
- demo.launch(
118
- ssr_mode=False,
119
- show_api=False,
120
- quiet=True
121
- ) x1_vals = [ccmq_map.get(a, 1) for a in all_answers[:24]]
122
- x2_vals = [osdi_map.get(a, 0) for a in all_answers[24:34]]
123
-
124
  x1_raw = np.array([x1_vals])
125
  x2_raw = np.array([x2_vals])
126
 
 
37
  x1_vals = [ccmq_map.get(a, 1) for a in all_answers[:24]]
38
  x2_vals = [osdi_map.get(a, 0) for a in all_answers[24:34]]
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  x1_raw = np.array([x1_vals])
41
  x2_raw = np.array([x2_vals])
42