PinHsuan commited on
Commit
2eb3982
·
verified ·
1 Parent(s): 0a077cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -21
app.py CHANGED
@@ -56,16 +56,19 @@ scaler_ccmq = joblib.load(f"scaler_ccmq_fold_{FOLD}.pkl")
56
  scaler_osdi = joblib.load(f"scaler_osdi_fold_{FOLD}.pkl")
57
 
58
  def analyze_and_predict(*all_answers):
59
-
60
  ccmq_map = {"總是": 5, "經常": 4, "有時": 3, "很少": 2, "沒有": 1}
61
  osdi_map = {"總是": 4, "經常": 3, "一半一半": 2, "偶而": 1, "完全不曾": 0}
62
 
63
-
 
64
  try:
65
- x1_raw = np.array([[ccmq_map[a] for a in all_answers[:24]]])
66
- x2_raw = np.array([[osdi_map[a] for a in all_answers[24:34]]])
 
 
 
67
 
68
-
69
  x1_scaled = scaler_ccmq.transform(x1_raw)
70
  x2_scaled = scaler_osdi.transform(x2_raw)
71
  sx1 = torch.tensor(x1_scaled, dtype=torch.float32).to(DEVICE)
@@ -77,30 +80,26 @@ def analyze_and_predict(*all_answers):
77
  probs = torch.softmax(logits, dim=1)
78
  pred_idx = torch.argmax(probs, dim=1).item()
79
  conf = probs[0, pred_idx].item()
 
80
  except Exception as e:
81
- raise gr.Error(f"填寫不完整或轉換錯誤: {str(e)}")
 
82
 
83
 
84
  res_label = " 乾眼風險 (SJS/DES)" if pred_idx == 1 else " 正常/健康"
85
  table_data = [[f"項次 {i+1}", str(all_answers[i]), "已記錄"] for i in range(len(all_answers))]
86
 
87
- report_text = f"""
88
- ### 🧬 深度學習模型分析說明
89
- - **信心度評估**:{conf:.2%}
90
- - **診斷結論**:{res_label}
91
- - **模型架構**:雙流 Transformer 融合模型
92
- - **分析結果**:系統檢測到您的中醫體質訊號與 OSDI 症狀分佈呈現強關聯。
93
- """
94
 
95
  return (
96
- gr.update(visible=False),
97
- gr.update(visible=True),
98
- f"### {res_label}",
99
- report_text,
100
- {"風險機率": conf if pred_idx==1 else 1-conf, "健康機率": 1-(conf if pred_idx==1 else 1-conf)}, # res_prob
101
- table_data,
102
- None,
103
- None
104
  )
105
 
106
  def reset_system():
 
56
  scaler_osdi = joblib.load(f"scaler_osdi_fold_{FOLD}.pkl")
57
 
58
  def analyze_and_predict(*all_answers):
59
+ # 1. 映射表
60
  ccmq_map = {"總是": 5, "經常": 4, "有時": 3, "很少": 2, "沒有": 1}
61
  osdi_map = {"總是": 4, "經常": 3, "一半一半": 2, "偶而": 1, "完全不曾": 0}
62
 
63
+ print(f"--- 偵錯資訊:收到 {len(all_answers)} 題回答 ---")
64
+
65
  try:
66
+ x1_raw = np.array([[ccmq_map.get(a, 1) for a in all_answers[:24]]])
67
+
68
+ # .get(a, 0) 代表:如果 a 是 None 或找不到,就預設給 0 (OSDI 的 '完全不曾')
69
+ x2_raw = np.array([[osdi_map.get(a, 0) for a in all_answers[24:34]]])
70
+
71
 
 
72
  x1_scaled = scaler_ccmq.transform(x1_raw)
73
  x2_scaled = scaler_osdi.transform(x2_raw)
74
  sx1 = torch.tensor(x1_scaled, dtype=torch.float32).to(DEVICE)
 
80
  probs = torch.softmax(logits, dim=1)
81
  pred_idx = torch.argmax(probs, dim=1).item()
82
  conf = probs[0, pred_idx].item()
83
+
84
  except Exception as e:
85
+ print(f"❌ 推論發生錯誤: {e}")
86
+ raise gr.Error(f"系統轉換異常,請重新整理頁面。錯誤原因: {str(e)}")
87
 
88
 
89
  res_label = " 乾眼風險 (SJS/DES)" if pred_idx == 1 else " 正常/健康"
90
  table_data = [[f"項次 {i+1}", str(all_answers[i]), "已記錄"] for i in range(len(all_answers))]
91
 
92
+ report_text = f"### 診斷結論:{res_label}\nAI 信心度:{conf:.2%}"
 
 
 
 
 
 
93
 
94
  return (
95
+ gr.update(visible=False), # stage_1
96
+ gr.update(visible=True), # stage_2
97
+ f"### {res_label}",
98
+ report_text,
99
+ {"風險機率": conf if pred_idx==1 else 1-conf, "健康機率": 1-(conf if pred_idx==1 else 1-conf)},
100
+ table_data,
101
+ None,
102
+ None
103
  )
104
 
105
  def reset_system():