ruanhelansantos commited on
Commit
f09b4c8
·
verified ·
1 Parent(s): d82f42a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -14
app.py CHANGED
@@ -5,41 +5,31 @@
5
 
6
  import gradio as gr
7
  from utils import load_and_preprocess, validate_image
8
- from model import run_inference, get_top_prediction
9
  from explainer import generate_explanation, format_top3, match_condition
10
 
 
11
  def classify_ecg(image):
12
- # Step 1: Validate
13
  is_valid, message = validate_image(image)
14
  if not is_valid:
15
  return message, "Please upload a valid ECG image.", ""
16
 
17
- # Step 2: Preprocess
18
  try:
19
  processed_image = load_and_preprocess(image)
20
  except Exception as e:
21
  return f"Preprocessing error: {str(e)}", "", ""
22
 
23
- # Step 3: Run inference
24
  try:
25
- results = run_inference(processed_image, top_k=3)
26
  except Exception as e:
27
  return f"Model inference error: {str(e)}", "", ""
28
 
29
- # Step 4: Extract top prediction (returns 2 values)
30
  label, confidence = get_top_prediction(results)
31
-
32
- # Step 5: Look up condition name for display
33
  condition = match_condition(label)
34
  full_name = condition.get("summary", label)
35
-
36
- # Step 6: Generate explanation
37
  explanation_text, urgency = generate_explanation(label, confidence)
38
-
39
- # Step 7: Format confidence breakdown
40
  top3_text = format_top3(results)
41
 
42
- # Step 8: Build result string
43
  urgency_icon = {
44
  "Routine": "✅",
45
  "Elevated": "⚠️",
@@ -58,6 +48,19 @@ def classify_ecg(image):
58
  return result_text, explanation_text, top3_text
59
 
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  examples_description = """
62
  **How to use this tool:**
63
  1. Upload a 12-lead ECG image (PNG, JPG, or TIFF)
@@ -93,7 +96,7 @@ with gr.Blocks(title="ECG Condition Classifier") as demo:
93
  with gr.Column(scale=1):
94
  result_output = gr.Textbox(label="Classification Result", lines=4, interactive=False)
95
  explanation_output = gr.Textbox(label="Clinical Explanation", lines=10, interactive=False)
96
- top3_output = gr.Textbox(label="Confidence Breakdown", lines=5, interactive=False)
97
 
98
  submit_btn.click(
99
  fn=classify_ecg,
@@ -101,6 +104,13 @@ with gr.Blocks(title="ECG Condition Classifier") as demo:
101
  outputs=[result_output, explanation_output, top3_output]
102
  )
103
 
 
 
 
 
 
 
 
104
  gr.Markdown("---")
105
  gr.Markdown(
106
  "**Model:** ViT fine-tuned on ECG images | "
 
5
 
6
  import gradio as gr
7
  from utils import load_and_preprocess, validate_image
8
+ from model import run_inference, get_top_prediction, load_model
9
  from explainer import generate_explanation, format_top3, match_condition
10
 
11
+
12
  def classify_ecg(image):
 
13
  is_valid, message = validate_image(image)
14
  if not is_valid:
15
  return message, "Please upload a valid ECG image.", ""
16
 
 
17
  try:
18
  processed_image = load_and_preprocess(image)
19
  except Exception as e:
20
  return f"Preprocessing error: {str(e)}", "", ""
21
 
 
22
  try:
23
+ results = run_inference(processed_image, top_k=5)
24
  except Exception as e:
25
  return f"Model inference error: {str(e)}", "", ""
26
 
 
27
  label, confidence = get_top_prediction(results)
 
 
28
  condition = match_condition(label)
29
  full_name = condition.get("summary", label)
 
 
30
  explanation_text, urgency = generate_explanation(label, confidence)
 
 
31
  top3_text = format_top3(results)
32
 
 
33
  urgency_icon = {
34
  "Routine": "✅",
35
  "Elevated": "⚠️",
 
48
  return result_text, explanation_text, top3_text
49
 
50
 
51
+ def show_label_map():
52
+ """Shows the current index→label mapping from the loaded model config."""
53
+ try:
54
+ model, _ = load_model()
55
+ mapping = model.config.id2label
56
+ lines = ["Current model label mapping:\n"]
57
+ for idx in sorted(mapping.keys()):
58
+ lines.append(f" Index {idx} → {mapping[idx]}")
59
+ return "\n".join(lines)
60
+ except Exception as e:
61
+ return f"Error loading model: {str(e)}"
62
+
63
+
64
  examples_description = """
65
  **How to use this tool:**
66
  1. Upload a 12-lead ECG image (PNG, JPG, or TIFF)
 
96
  with gr.Column(scale=1):
97
  result_output = gr.Textbox(label="Classification Result", lines=4, interactive=False)
98
  explanation_output = gr.Textbox(label="Clinical Explanation", lines=10, interactive=False)
99
+ top3_output = gr.Textbox(label="Confidence Breakdown", lines=6, interactive=False)
100
 
101
  submit_btn.click(
102
  fn=classify_ecg,
 
104
  outputs=[result_output, explanation_output, top3_output]
105
  )
106
 
107
+ # ── Diagnostic tool to reveal raw model label mapping ─────────────────
108
+ with gr.Accordion("🔧 Developer: Show Label Map", open=False):
109
+ gr.Markdown("Click the button below to print the current index→label mapping from the model config. Use this to verify/fix the LABEL_MAP in model.py.")
110
+ label_map_btn = gr.Button("Show Label Map")
111
+ label_map_output = gr.Textbox(label="Label Map", lines=8, interactive=False)
112
+ label_map_btn.click(fn=show_label_map, inputs=[], outputs=[label_map_output])
113
+
114
  gr.Markdown("---")
115
  gr.Markdown(
116
  "**Model:** ViT fine-tuned on ECG images | "