suayptalha commited on
Commit
fc79af8
·
verified ·
1 Parent(s): f795395

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ from scipy.signal import find_peaks
5
+
6
+ model = tf.keras.models.load_model('aritmi_distilled.h5')
7
+
8
+ def extract_features(ekg_signal):
9
+ if len(ekg_signal) < 10:
10
+ return None
11
+
12
+ r_peaks, _ = find_peaks(ekg_signal, height=0.5, distance=60)
13
+
14
+ if len(r_peaks) < 2:
15
+ return None
16
+
17
+ features = []
18
+ rr_intervals = np.diff(r_peaks)
19
+
20
+ if len(rr_intervals) == 0:
21
+ return None
22
+
23
+ features.append(np.nanmean(rr_intervals))
24
+ features.append(np.nanstd(rr_intervals))
25
+ features.append(np.nanmean(ekg_signal))
26
+ features.append(np.nanmean(r_peaks))
27
+ features.append(np.nanmean(np.diff(r_peaks)))
28
+
29
+ pq_interval = np.nanmean(rr_intervals)
30
+ qt_interval = np.nanmean(rr_intervals)
31
+ st_interval = np.nanmean(rr_intervals)
32
+
33
+ features.extend([pq_interval, qt_interval, st_interval])
34
+ return features
35
+
36
+ def predict(ekg_signal):
37
+ try:
38
+ ekg_signal_list = [float(value.strip()) for value in ekg_signal.split(',') if value.strip()]
39
+ ekg_signal_array = np.array(ekg_signal_list, dtype=np.float32)
40
+ except Exception as e:
41
+ return f"Invalid dtype. Error: {str(e)}"
42
+
43
+ if ekg_signal_array.size == 0:
44
+ return "Empty ECG signal."
45
+
46
+ features = extract_features(ekg_signal_array)
47
+ if features is None:
48
+ return "Couldn't extract feature."
49
+
50
+ features = np.array(features).reshape(1, -1)
51
+ prediction = model.predict(features)
52
+ risk_probabilities = prediction[0]
53
+ aritmi_riski_yuzde = risk_probabilities[1] * 100
54
+
55
+ return f"Arrhythmia risk: %{aritmi_riski_yuzde:.2f}"
56
+
57
+
58
+
59
+ interface = gr.Interface(
60
+ fn=predict,
61
+ inputs=gr.Textbox(
62
+ label="ECG Signal",
63
+ placeholder="0.034, 0.181, 0.264, ...",
64
+ ),
65
+ outputs="text",
66
+ description="Arrhythmia risk predicting model from ECG data",
67
+ live=False
68
+ )
69
+
70
+
71
+
72
+ if __name__ == "__main__":
73
+ interface.launch()