smartTranscend commited on
Commit
ce13abe
·
verified ·
1 Parent(s): 6da2603

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -57
app.py CHANGED
@@ -1,22 +1,25 @@
1
  import gradio as gr
2
 
3
- # ===== 分子層 =====
 
 
4
  from translation import translate_rna, apply_point_mutation
5
  from mutation_analysis import classify_mutation
6
 
7
- # ===== Bayes 教學核心 =====
8
- from bayes_update import (
9
- bayes_prior_no_kids,
10
- calc_updated_risk
11
- )
12
 
13
- # ===== LLM(只解釋,不計算)=====
 
 
14
  from llm_client import chat_teaching_question
15
 
16
 
17
- # =========================
18
- # Page 1:序列與突變分
19
- # =========================
20
  def analyze_sequence(rna_sequence, mutation_description):
21
  wt_protein = translate_rna(rna_sequence)
22
  mut_rna = apply_point_mutation(rna_sequence, mutation_description)
@@ -29,54 +32,58 @@ def analyze_sequence(rna_sequence, mutation_description):
29
  f"【RNA(突變後)】\n{mut_rna}\n\n"
30
  f"【Protein(WT)】\n{wt_protein}\n\n"
31
  f"【Protein(Mutant)】\n{mut_protein}\n\n"
32
- f"【突變類型】\n{mutation_type}"
33
  )
34
 
35
 
36
- # =========================
37
- # Page 2:Bayes 教學(完全照 PPT)
38
- # =========================
39
- def bayes_teaching(p, n):
 
40
  posterior_no_kids = bayes_prior_no_kids(p)
41
- posterior_updated, next_child_risk = calc_updated_risk(p, n)
42
-
43
- return f"""
44
- 📊【貝氏推導結果(教學用)】
45
-
46
- 【前提】
47
- - RB1 為自體顯性
48
- - D 為致病等位基因
49
- - 疾病穿透力 p = {p}
50
- - 已有健康小孩數 n = {n}
51
- - 媽媽假設為 dd(不帶病,固定)
52
-
53
- 【Step 1|爸爸未發病】
54
- P(爸爸是 Dd | 未發病)
55
- = {posterior_no_kids:.4f}
56
-
57
- 【Step 2|已有 {n} 個健康小孩】
58
- P(爸爸是 Dd | n 個健康)
59
- = {posterior_updated:.4f}
60
 
61
- Step 3|下一胎風險】
62
- P( {n+1} 個小孩發病)
63
- = {next_child_risk*100:.2f} %
64
 
65
- ⚠️ 僅供 Bayes 與遺傳學教學使用,不構成醫療建議
66
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
 
69
- # =========================
70
  # UI
71
- # =========================
72
  with gr.Blocks() as demo:
73
  gr.Markdown("# 🧬 RB1(視網膜母細胞瘤)遺傳諮詢 × Bayes 教學系統")
74
  gr.Markdown("⚠️ 僅供教學與研究使用,不提供醫療診斷或個人化建議。")
75
 
76
- # -------- Page 1 --------
77
  with gr.Tab("🧬 序列與突變(分子層)"):
78
- rna = gr.Textbox(label="RNA 序列", placeholder="例如:AUGGAGUUU...")
79
- mutation = gr.Textbox(label="突變描述", placeholder="例如:c.4G>A")
 
 
 
 
 
 
80
  seq_output = gr.Textbox(label="分析結果", lines=14)
81
 
82
  gr.Button("分析序列").click(
@@ -85,29 +92,55 @@ with gr.Blocks() as demo:
85
  outputs=seq_output
86
  )
87
 
88
- # -------- Page 2 --------
89
  with gr.Tab("📊 機率與穿透力(Bayes 教學)"):
90
- penetrance = gr.Slider(0, 1, value=0.6, step=0.05, label="疾病穿透力 p")
91
- n_children = gr.Number(value=0, precision=0, label="已知健康小孩數 n")
 
 
 
 
 
 
 
 
92
 
93
- bayes_output = gr.Textbox(label="Bayes 推導結果", lines=18)
 
 
 
94
 
95
- gr.Button("計算風險(教學)").click(
96
- bayes_teaching,
97
- inputs=[penetrance, n_children],
 
 
 
 
 
 
 
 
 
 
98
  outputs=bayes_output
99
  )
100
 
101
- # -------- Page 3 --------
102
  with gr.Tab("💬 教學問答(LLM 解釋,不計算)"):
103
  gr.Markdown(
104
- "請針對 **上方 Bayes 結果** 提問。\n\n"
105
- "⚠️ AI 僅做概念說明,不重新計算、不供醫療建議。"
 
 
 
 
 
 
106
  )
107
 
108
  question = gr.Textbox(
109
- label="你的問題",
110
- placeholder="例如:為什麼健康小孩越多,風險會下降?",
111
  lines=3
112
  )
113
 
@@ -120,4 +153,3 @@ with gr.Blocks() as demo:
120
  )
121
 
122
  demo.launch()
123
-
 
1
  import gradio as gr
2
 
3
+ # ======================
4
+ # 分子層(你原本的)
5
+ # ======================
6
  from translation import translate_rna, apply_point_mutation
7
  from mutation_analysis import classify_mutation
8
 
9
+ # ======================
10
+ # Bayes(完全照 PPT)
11
+ # ======================
12
+ from bayes_update import bayes_prior_no_kids, calc_updated_risk
 
13
 
14
+ # ======================
15
+ # LLM(只解釋,不算)
16
+ # ======================
17
  from llm_client import chat_teaching_question
18
 
19
 
20
+ # =================================================
21
+ # Tab 1:序列與突變子層)
22
+ # =================================================
23
  def analyze_sequence(rna_sequence, mutation_description):
24
  wt_protein = translate_rna(rna_sequence)
25
  mut_rna = apply_point_mutation(rna_sequence, mutation_description)
 
32
  f"【RNA(突變後)】\n{mut_rna}\n\n"
33
  f"【Protein(WT)】\n{wt_protein}\n\n"
34
  f"【Protein(Mutant)】\n{mut_protein}\n\n"
35
+ f"【突變類型判定】\n{mutation_type}"
36
  )
37
 
38
 
39
+ # =================================================
40
+ # Tab 2:Bayes 教學(完全照 PPT)
41
+ # =================================================
42
+ def bayes_teaching_pipeline(p, n):
43
+ # Step 1:只有「爸爸未發病」
44
  posterior_no_kids = bayes_prior_no_kids(p)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
+ # Step 2 + 3:加入 n 個健康小孩
47
+ posterior_updated, next_child_risk = calc_updated_risk(p, n)
 
48
 
49
+ return (
50
+ "📊【Bayes 推導結果(教學用)】\n\n"
51
+ "【固定前提】\n"
52
+ "- 疾病:RB1(自體顯性)\n"
53
+ "- D 為疾病基因\n"
54
+ "- 母親基因型:dd(不帶疾病,不參與推論)\n"
55
+ "- 父親目前未發病,基因型未知\n"
56
+ "- 每個孩子是否發病彼此獨立\n\n"
57
+ "---------------------------------\n"
58
+ f"【Step 1|僅根據父親未發病】\n"
59
+ f"P(父親是 Dd | 未發病) = {posterior_no_kids:.4f}\n\n"
60
+ "---------------------------------\n"
61
+ f"【Step 2|加入 {n} 個健康小孩】\n"
62
+ f"P(父親是 Dd | {n} 個健康小孩) = {posterior_updated:.4f}\n\n"
63
+ "---------------------------------\n"
64
+ f"【Step 3|第 {n+1} 個小孩】\n"
65
+ f"P(第 {n+1} 個小孩發病) ≈ {next_child_risk*100:.2f}%\n\n"
66
+ "⚠️ 僅為機率與 Bayes 教學示例,不構成醫療建議"
67
+ )
68
 
69
 
70
+ # =================================================
71
  # UI
72
+ # =================================================
73
  with gr.Blocks() as demo:
74
  gr.Markdown("# 🧬 RB1(視網膜母細胞瘤)遺傳諮詢 × Bayes 教學系統")
75
  gr.Markdown("⚠️ 僅供教學與研究使用,不提供醫療診斷或個人化建議。")
76
 
77
+ # ---------------- Tab 1 ----------------
78
  with gr.Tab("🧬 序列與突變(分子層)"):
79
+ rna = gr.Textbox(
80
+ label="RNA 序列",
81
+ placeholder="例如:AUGGAGUUU..."
82
+ )
83
+ mutation = gr.Textbox(
84
+ label="突變描述",
85
+ placeholder="例如:c.4G>A"
86
+ )
87
  seq_output = gr.Textbox(label="分析結果", lines=14)
88
 
89
  gr.Button("分析序列").click(
 
92
  outputs=seq_output
93
  )
94
 
95
+ # ---------------- Tab 2 ----------------
96
  with gr.Tab("📊 機率與穿透力(Bayes 教學)"):
97
+ gr.Markdown(
98
+ """
99
+ ### 🧠 教學前提(固定,不可修改)
100
+ - 疾病為 **自體顯性**
101
+ - **D 為疾病基因**
102
+ - 母親為 **dd(不帶病)**
103
+ - 父親目前未發病,但是否為 Dd 不確定
104
+ - 使用 Bayes 定理反推父親基因型
105
+ """
106
+ )
107
 
108
+ p = gr.Slider(
109
+ 0, 1, value=0.6, step=0.05,
110
+ label="疾病穿透力 p(帶 D 基因者實際發病機率)"
111
+ )
112
 
113
+ n = gr.Number(
114
+ value=0, precision=0,
115
+ label="已知健康小孩數 n"
116
+ )
117
+
118
+ bayes_output = gr.Textbox(
119
+ label="Bayes 推導與風險結果(教學用)",
120
+ lines=16
121
+ )
122
+
123
+ gr.Button("計算風險(教學用)").click(
124
+ bayes_teaching_pipeline,
125
+ inputs=[p, n],
126
  outputs=bayes_output
127
  )
128
 
129
+ # ---------------- Tab 3 ----------------
130
  with gr.Tab("💬 教學問答(LLM 解釋,不計算)"):
131
  gr.Markdown(
132
+ """
133
+ 請根據 **Bayes 教學頁的結果** 問,例如:
134
+ - 為什麼健康小孩越多,父親是 Dd 的機率會下降?
135
+ - 為什麼母親是 dd 就不用算?
136
+ - 為什麼不能一開始就假設爸爸是 Dd?
137
+
138
+ ⚠️ AI **只解釋概念,不重新計算、不提供醫療建議**
139
+ """
140
  )
141
 
142
  question = gr.Textbox(
143
+ label="你的問題(教學性)",
 
144
  lines=3
145
  )
146
 
 
153
  )
154
 
155
  demo.launch()