oewis16 commited on
Commit
26924da
Β·
verified Β·
1 Parent(s): b9fe86b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -16
app.py CHANGED
@@ -4,8 +4,6 @@ import matplotlib
4
  matplotlib.use('Agg')
5
  import matplotlib.pyplot as plt
6
  from sklearn.preprocessing import MinMaxScaler
7
- import tensorflow as tf
8
- from tensorflow.keras import layers, models, regularizers
9
  import os
10
  import spaces
11
 
@@ -27,9 +25,15 @@ CLASS_RISK = {
27
  }
28
  COLORS = ['#2ecc71','#3498db','#e74c3c','#f39c12','#9b59b6']
29
 
30
- # ── Build model on CPU at startup ─────────────────────────────────────────────
31
- def build_model():
32
- with tf.device('/CPU:0'):
 
 
 
 
 
 
33
  m = models.Sequential([
34
  layers.Input(shape=(187, 1)),
35
  layers.Conv1D(64, 7, padding='same', activation='relu',
@@ -52,12 +56,8 @@ def build_model():
52
  ], name="CNN_ECG")
53
  m.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
54
  m.load_weights("cnn_weights.weights.h5")
55
- return m
56
-
57
- model = build_model()
58
 
59
- # ── Predict ───────────────────────────────────────────────────────────────────
60
- def predict_ecg(text_input):
61
  try:
62
  cleaned = text_input.replace(",", " ").replace("\n", " ").replace("\t", " ")
63
  parsed = [float(x) for x in cleaned.split() if x.strip()]
@@ -71,12 +71,9 @@ def predict_ecg(text_input):
71
 
72
  scaler = MinMaxScaler()
73
  signal_scaled = scaler.fit_transform(features.reshape(-1, 1)).reshape(1, 187, 1)
74
-
75
- with tf.device('/CPU:0'):
76
- probs = model.predict(signal_scaled, verbose=0)[0]
77
-
78
- pred_class = int(np.argmax(probs))
79
- confidence = float(np.max(probs)) * 100
80
 
81
  fig, axes = plt.subplots(1, 2, figsize=(14, 4))
82
  fig.patch.set_facecolor('#0e1117')
 
4
  matplotlib.use('Agg')
5
  import matplotlib.pyplot as plt
6
  from sklearn.preprocessing import MinMaxScaler
 
 
7
  import os
8
  import spaces
9
 
 
25
  }
26
  COLORS = ['#2ecc71','#3498db','#e74c3c','#f39c12','#9b59b6']
27
 
28
+ # ── Load model lazily inside GPU function ─────────────────────────────────────
29
+ model = None
30
+
31
+ @spaces.GPU
32
+ def predict_ecg(text_input):
33
+ global model
34
+ if model is None:
35
+ import tensorflow as tf
36
+ from tensorflow.keras import layers, models, regularizers
37
  m = models.Sequential([
38
  layers.Input(shape=(187, 1)),
39
  layers.Conv1D(64, 7, padding='same', activation='relu',
 
56
  ], name="CNN_ECG")
57
  m.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
58
  m.load_weights("cnn_weights.weights.h5")
59
+ model = m
 
 
60
 
 
 
61
  try:
62
  cleaned = text_input.replace(",", " ").replace("\n", " ").replace("\t", " ")
63
  parsed = [float(x) for x in cleaned.split() if x.strip()]
 
71
 
72
  scaler = MinMaxScaler()
73
  signal_scaled = scaler.fit_transform(features.reshape(-1, 1)).reshape(1, 187, 1)
74
+ probs = model.predict(signal_scaled, verbose=0)[0]
75
+ pred_class = int(np.argmax(probs))
76
+ confidence = float(np.max(probs)) * 100
 
 
 
77
 
78
  fig, axes = plt.subplots(1, 2, figsize=(14, 4))
79
  fig.patch.set_facecolor('#0e1117')