Tumo505 commited on
Commit
7d1acad
·
1 Parent(s): a1abe84

Fix context manager and launch order

Browse files
Files changed (1) hide show
  1. app.py +10 -14
app.py CHANGED
@@ -14,7 +14,7 @@ import tempfile
14
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
 
16
  # Constants
17
- MODEL_ID = "Tumo505/SSL-ECG-CLASSIFICATION"
18
  CLASS_LABELS = ["NORM", "MI", "STTC", "HYP", "CD"]
19
  CLASS_COLORS = {
20
  "NORM": "#90EE90",
@@ -42,7 +42,6 @@ def predict_ecg(file_obj):
42
  return (
43
  "**Model Loading Error**\n"
44
  "The model failed to load. Please try again or contact support.",
45
- None,
46
  None
47
  )
48
 
@@ -65,9 +64,8 @@ def predict_ecg(file_obj):
65
  if ecg.ndim != 2:
66
  return (
67
  "**Invalid Format**\n"
68
- f"Expected 2D array, got shape {{ecg.shape}}\n"
69
  "Expected: (12 leads, N samples)",
70
- None,
71
  None
72
  )
73
 
@@ -78,9 +76,8 @@ def predict_ecg(file_obj):
78
  else:
79
  return (
80
  "**Invalid Dimensions**\n"
81
- f"Got shape {{ecg.shape}}, expected (12, N)\n"
82
  "Ensure file has 12 leads (rows) × N samples (columns)",
83
- None,
84
  None
85
  )
86
 
@@ -173,17 +170,16 @@ def predict_ecg(file_obj):
173
  return output_md, fig, None
174
 
175
  except FileNotFoundError:
176
- return "**File Error:** Could not read uploaded file", None, None
177
  except Exception as e:
178
  import traceback
179
- error_msg = f"**Error:** {{str(e)}}\n\nDebug: {{traceback.format_exc()}}"
180
- return error_msg, None, None
181
 
182
 
183
  # Create interface
184
  with gr.Blocks(
185
- title="ECG Classification with Self-Supervised Learning",
186
- theme=gr.themes.Soft(primary_hue="emerald")
187
  ) as demo:
188
 
189
  gr.Markdown("""
@@ -245,9 +241,9 @@ with gr.Blocks(
245
  submit_btn.click(
246
  fn=predict_ecg,
247
  inputs=[file_input],
248
- outputs=[output_text, chart_output, None]
249
  )
250
-
251
  # Info section
252
  gr.Markdown("""
253
  ---
@@ -269,7 +265,7 @@ with gr.Blocks(
269
 
270
  **Research Only** - Not validated for clinical use
271
 
272
- [View Model Card](https://huggingface.co/{MODEL_ID})
273
  [GitHub Repository](https://github.com/Tumo505/SSL-for-ECG-classification)
274
  """)
275
 
 
14
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
 
16
  # Constants
17
+ MODEL_ID = "Tumo505/ssrl-ecg-simclr-finetuned"
18
  CLASS_LABELS = ["NORM", "MI", "STTC", "HYP", "CD"]
19
  CLASS_COLORS = {
20
  "NORM": "#90EE90",
 
42
  return (
43
  "**Model Loading Error**\n"
44
  "The model failed to load. Please try again or contact support.",
 
45
  None
46
  )
47
 
 
64
  if ecg.ndim != 2:
65
  return (
66
  "**Invalid Format**\n"
67
+ f"Expected 2D array, got shape {ecg.shape}\n"
68
  "Expected: (12 leads, N samples)",
 
69
  None
70
  )
71
 
 
76
  else:
77
  return (
78
  "**Invalid Dimensions**\n"
79
+ f"Got shape {ecg.shape}, expected (12, N)\n"
80
  "Ensure file has 12 leads (rows) × N samples (columns)",
 
81
  None
82
  )
83
 
 
170
  return output_md, fig, None
171
 
172
  except FileNotFoundError:
173
+ return "**File Error:** Could not read uploaded file", None
174
  except Exception as e:
175
  import traceback
176
+ error_msg = f"**Error:** {str(e)}\n\nDebug: {traceback.format_exc()}"
177
+ return error_msg, None
178
 
179
 
180
  # Create interface
181
  with gr.Blocks(
182
+ title="ECG Classification with Self-Supervised Learning"
 
183
  ) as demo:
184
 
185
  gr.Markdown("""
 
241
  submit_btn.click(
242
  fn=predict_ecg,
243
  inputs=[file_input],
244
+ outputs=[output_text, chart_output]
245
  )
246
+
247
  # Info section
248
  gr.Markdown("""
249
  ---
 
265
 
266
  **Research Only** - Not validated for clinical use
267
 
268
+ [View Model Card](https://huggingface.co/Tumo505/ssrl-ecg-simclr-finetuned)
269
  [GitHub Repository](https://github.com/Tumo505/SSL-for-ECG-classification)
270
  """)
271