Donlagon007 commited on
Commit
78247e7
·
verified ·
1 Parent(s): 0d5e2b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -14
app.py CHANGED
@@ -4,7 +4,6 @@ import gradio as gr
4
  from transformers import BertTokenizerFast, BertForTokenClassification
5
 
6
  # === ตั้งค่าโมเดลจาก Hub ===
7
- # เปลี่ยนเป็นโมเดลของคุณ เช่น "donla/htn-ner"
8
  MODEL_ID = "Donlagon007/htn-ner-v1"
9
 
10
  # โหลดโมเดล/โทเคนไนเซอร์ (CPU เป็นค่าเริ่มต้นใน Spaces)
@@ -140,16 +139,19 @@ def extract_structured(text):
140
  key_findings.append(row)
141
 
142
  risks = set()
 
143
  fpg = next((cn_num(p["value"]) for p in pairs if p["test"] == "空腹血糖"), None)
144
  a1c = next((cn_num(p["value"]) for p in pairs if p["test"] == "HbA1c"), None)
145
- ldl = next((cn_num(p["value"]) for p in pairs if p["test"] == "LDL"), None)
146
- bp_val = next((p["value"] for p in pairs if p["test"] in ["診間血壓","家庭血壓","24小時動態血壓"]), None)
147
  if (fpg is not None and fpg >= 126) or (a1c is not None and a1c >= 6.5):
148
  risks.add("糖尿病")
 
 
149
  if ldl is not None and ldl >= 160:
150
  risks.add("高血脂")
151
  elif ldl is not None and ldl >= 130:
152
  risks.add("高血脂(輕度)")
 
 
153
  if bp_val:
154
  sys, dia = parse_bp(bp_val)
155
  if sys and dia and (sys >= 140 or dia >= 90):
@@ -157,6 +159,7 @@ def extract_structured(text):
157
  if any(e["type"] == "DISEASE" and "高血壓" in e["text"] for e in entities):
158
  risks.add("高血壓")
159
 
 
160
  recs = []
161
  for e in entities:
162
  if e["type"] == "DRUG":
@@ -191,27 +194,69 @@ def extract_structured(text):
191
  }
192
  return tokens, entities, structured
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  # ---------- Gradio UI ----------
195
  EXAMPLE = "李偉(65歲,男),有高血壓與糖尿病。\n診間血壓152/94mmHg,空腹血糖138mg/dL,HbA1c 7.1%。\n建議使用ARB類藥物並低鹽飲食。"
196
 
197
  def run(text):
198
  tokens, entities, structured = extract_structured(text)
199
- return json.dumps(structured, ensure_ascii=False, indent=2), \
200
- json.dumps(entities, ensure_ascii=False, indent=2), \
201
- json.dumps(tokens, ensure_ascii=False, indent=2)
202
-
 
 
 
203
 
204
  with gr.Blocks(title="HTN NER (Chinese)") as demo:
205
- gr.Markdown("## Hypertension NER → Tokens / Entities / Structured JSON")
206
  inp = gr.Textbox(label="輸入文字 (中文)", lines=6, value=EXAMPLE)
207
  btn = gr.Button("Analyze")
208
- out_struct = gr.Code(label="Structured Reports")
 
209
  out_entities = gr.Code(label="Entities (spans)")
210
- out_tokens = gr.Code(label="Token-level (B/I/O)")
211
- btn.click(run, inputs=inp, outputs=[out_struct, out_entities, out_tokens])
212
-
213
- demo.launch()
214
-
215
 
216
  if __name__ == "__main__":
217
  demo.launch(server_name="0.0.0.0")
 
4
  from transformers import BertTokenizerFast, BertForTokenClassification
5
 
6
  # === ตั้งค่าโมเดลจาก Hub ===
 
7
  MODEL_ID = "Donlagon007/htn-ner-v1"
8
 
9
  # โหลดโมเดล/โทเคนไนเซอร์ (CPU เป็นค่าเริ่มต้นใน Spaces)
 
139
  key_findings.append(row)
140
 
141
  risks = set()
142
+ # 4.1 Diabetes
143
  fpg = next((cn_num(p["value"]) for p in pairs if p["test"] == "空腹血糖"), None)
144
  a1c = next((cn_num(p["value"]) for p in pairs if p["test"] == "HbA1c"), None)
 
 
145
  if (fpg is not None and fpg >= 126) or (a1c is not None and a1c >= 6.5):
146
  risks.add("糖尿病")
147
+ # 4.2 Hyperlipidemia via LDL
148
+ ldl = next((cn_num(p["value"]) for p in pairs if p["test"] == "LDL"), None)
149
  if ldl is not None and ldl >= 160:
150
  risks.add("高血脂")
151
  elif ldl is not None and ldl >= 130:
152
  risks.add("高血脂(輕度)")
153
+ # 4.3 Hypertension via BP or DISEASE mention
154
+ bp_val = next((p["value"] for p in pairs if p["test"] in ["診間血壓","家庭血壓","24小時動態血壓"]), None)
155
  if bp_val:
156
  sys, dia = parse_bp(bp_val)
157
  if sys and dia and (sys >= 140 or dia >= 90):
 
159
  if any(e["type"] == "DISEASE" and "高血壓" in e["text"] for e in entities):
160
  risks.add("高血壓")
161
 
162
+ # recommendations
163
  recs = []
164
  for e in entities:
165
  if e["type"] == "DRUG":
 
194
  }
195
  return tokens, entities, structured
196
 
197
+ # ---------- Human-readable report ----------
198
+ def make_readable_report(structured: dict) -> str:
199
+ name = structured.get("name") or "病人"
200
+ age = structured.get("age")
201
+ sex = structured.get("sex")
202
+ head = f"【健檢摘要】{name}"
203
+ if age is not None: head += f"({age}歲"
204
+ else: head += "(年齡不詳"
205
+ if sex: head += f",{sex})"
206
+ else: head += ")"
207
+
208
+ # key findings
209
+ kfs = structured.get("key_findings", [])
210
+ abn_lines, nor_lines = [], []
211
+ for k in kfs:
212
+ t, v = k.get("test"), k.get("value")
213
+ st = k.get("status")
214
+ if st in ("異常","偏高"):
215
+ abn_lines.append(f".{t}: {v}({st})")
216
+ elif st == "正常":
217
+ nor_lines.append(f".{t}: {v}(正常)")
218
+ else:
219
+ nor_lines.append(f".{t}: {v}")
220
+
221
+ risks = structured.get("disease_risk", [])
222
+ recs = structured.get("recommendations", [])
223
+ summary = structured.get("summary") or ""
224
+
225
+ sections = [head, ""]
226
+ if abn_lines:
227
+ sections += ["【異常/偏高項目】"] + abn_lines + [""]
228
+ if nor_lines:
229
+ sections += ["【其他檢測】"] + nor_lines + [""]
230
+ if risks:
231
+ sections += ["【疾病風險/診斷】", "." + "、".join(risks), ""]
232
+ if recs:
233
+ sections += ["【建議】", "." + "、".join(recs), ""]
234
+ if summary:
235
+ sections += ["【摘要敘述】", summary, ""]
236
+ return "\n".join(sections).strip()
237
+
238
  # ---------- Gradio UI ----------
239
  EXAMPLE = "李偉(65歲,男),有高血壓與糖尿病。\n診間血壓152/94mmHg,空腹血糖138mg/dL,HbA1c 7.1%。\n建議使用ARB類藥物並低鹽飲食。"
240
 
241
  def run(text):
242
  tokens, entities, structured = extract_structured(text)
243
+ human_report = make_readable_report(structured)
244
+ return (
245
+ human_report,
246
+ json.dumps(structured, ensure_ascii=False, indent=2),
247
+ json.dumps(entities, ensure_ascii=False, indent=2),
248
+ json.dumps(tokens, ensure_ascii=False, indent=2),
249
+ )
250
 
251
  with gr.Blocks(title="HTN NER (Chinese)") as demo:
252
+ gr.Markdown("## Hypertension NER → Human Report / JSON / Entities / Tokens")
253
  inp = gr.Textbox(label="輸入文字 (中文)", lines=6, value=EXAMPLE)
254
  btn = gr.Button("Analyze")
255
+ out_report = gr.Textbox(label="Doctor Report", lines=12)
256
+ out_struct = gr.Code(label="Structured JSON")
257
  out_entities = gr.Code(label="Entities (spans)")
258
+ out_tokens = gr.Code(label="Token-level (B/I/O)")
259
+ btn.click(run, inputs=inp, outputs=[out_report, out_struct, out_entities, out_tokens])
 
 
 
260
 
261
  if __name__ == "__main__":
262
  demo.launch(server_name="0.0.0.0")