enshicoolsoda commited on
Commit
ffa1fde
·
verified ·
1 Parent(s): 82c62db

Add AUC as an additional metric

Browse files
Files changed (1) hide show
  1. app.py +56 -1
app.py CHANGED
@@ -2,7 +2,7 @@ import json
2
  import os
3
  import requests
4
  import gradio as gr
5
-
6
  # -----------------------------
7
  # 1. Configuration & Data Mapping
8
  # -----------------------------
@@ -132,6 +132,53 @@ def run_evaluation(cancer_type, strategy):
132
  | **Actual NO** | {fp} (FP) | {tn} (TN) |
133
  """
134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  # -----------------------------
136
  # 4. Helper UI Logic (Chat)
137
  # -----------------------------
@@ -168,6 +215,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
168
  results_md = gr.Markdown("Select criteria and start to see metrics.")
169
 
170
  # TAB 2: Clinical Assistant
 
 
 
 
 
 
 
 
171
  with gr.TabItem("💬 Clinical Assistant"):
172
  with gr.Row():
173
  with gr.Column(scale=1):
 
2
  import os
3
  import requests
4
  import gradio as gr
5
+ from sklearn.metrics import roc_auc_score
6
  # -----------------------------
7
  # 1. Configuration & Data Mapping
8
  # -----------------------------
 
132
  | **Actual NO** | {fp} (FP) | {tn} (TN) |
133
  """
134
 
135
+ def run_auc_experiment(cancer_type):
136
+ data_path = CANCER_MAP.get(cancer_type)
137
+ gt_path = "data/ground_truth_5yr_recurrence.json"
138
+
139
+ with open(data_path, 'r') as f: patient_db = json.load(f)
140
+ with open(gt_path, 'r') as f: all_gt = json.load(f)
141
+
142
+ gt_labels = all_gt.get(GT_MAP[cancer_type], {})
143
+ eval_ids = [pid for pid in gt_labels.keys() if pid in patient_db]
144
+
145
+ y_true = [] # Actual labels (1 for Yes, 0 for No)
146
+ y_scores = [] # Model's predicted probabilities
147
+
148
+ yield f"📊 Starting AUC experiment for {len(eval_ids)} patients..."
149
+
150
+ for i, pid in enumerate(eval_ids):
151
+ # Convert "Yes"/"No" ground truth to 1/0
152
+ y_true.append(1 if gt_labels[pid] == "Yes" else 0)
153
+
154
+ patient_json = json.dumps(patient_db[pid])
155
+
156
+ # PROMPT CHANGE: Ask for a probability score
157
+ auc_prompt = [
158
+ {"role": "system", "content": "You are an oncology expert. Provide a 5-year recurrence probability score from 0 to 100. Respond ONLY with the number (e.g., '75')."},
159
+ {"role": "user", "content": f"Patient Data: {patient_json}"}
160
+ ]
161
+
162
+ raw_res = ollama_chat(auc_prompt, temperature=0).strip()
163
+
164
+ # Extract the first number found in the response
165
+ try:
166
+ score = float(re.findall(r"(\d+)", raw_res)[0]) / 100.0
167
+ y_scores.append(score)
168
+ except:
169
+ y_scores.append(0.5) # Fallback to neutral if parsing fails
170
+
171
+ if (i + 1) % 5 == 0:
172
+ yield f"🔄 Processing: {i+1}/{len(eval_ids)}..."
173
+
174
+ # Calculate AUC
175
+ try:
176
+ auc_value = roc_auc_score(y_true, y_scores)
177
+ result_md = f"## AUC Result: {cancer_type}\n**Area Under the Curve (AUC):** {auc_value:.4f}"
178
+ except Exception as e:
179
+ result_md = f"Error calculating AUC: {e}. Check if you have both classes present in the data."
180
+
181
+ yield result_md
182
  # -----------------------------
183
  # 4. Helper UI Logic (Chat)
184
  # -----------------------------
 
215
  results_md = gr.Markdown("Select criteria and start to see metrics.")
216
 
217
  # TAB 2: Clinical Assistant
218
+ with gr.TabItem("📈 AUC Experiment"):
219
+ gr.Markdown("### Calculate AUC using Probability Scores")
220
+ auc_cancer = gr.Dropdown(label="Cancer Type", choices=list(CANCER_MAP.keys()))
221
+ auc_btn = gr.Button("Run AUC Test", variant="primary")
222
+ auc_results = gr.Markdown()
223
+
224
+ auc_btn.click(run_auc_experiment, auc_cancer, auc_results)
225
+
226
  with gr.TabItem("💬 Clinical Assistant"):
227
  with gr.Row():
228
  with gr.Column(scale=1):