Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,42 @@
|
|
|
|
1 |
import pickle
|
2 |
import pandas as pd
|
3 |
import sklearn
|
4 |
import gradio as gr
|
5 |
import joblib
|
6 |
|
|
|
|
|
|
|
|
|
7 |
def encode_df(df):
|
8 |
-
|
9 |
-
#Gender
|
10 |
sex_map = {"Male": 1, "Female": 0}
|
11 |
df = df.replace({"Gender": sex_map})
|
12 |
-
|
13 |
-
#Tipe Angina
|
14 |
-
chestpain_map = {"Typical": 0, "Asymptomatic": 1,
|
|
|
15 |
df = df.replace({"Tipe_angina": chestpain_map})
|
16 |
-
|
17 |
-
#Gula_darah_puasa
|
18 |
fastingBLP_map = {"<120": 0, ">120": 1, "120": 2}
|
19 |
df = df.replace({"Gula_darah_puasa": fastingBLP_map})
|
20 |
-
|
21 |
-
#Angina_aktivitas
|
22 |
exang_map = {"No": 0, "Yes": 1}
|
23 |
df = df.replace({"Angina_aktivitas": exang_map})
|
24 |
-
|
25 |
-
#Hasil_Elektrokardiografi
|
26 |
-
recg_map = {"Normal": 0, "Abnormal Wave": 1,
|
|
|
27 |
df = df.replace({"Hasil_Elektrokardiografi": recg_map})
|
28 |
-
|
29 |
-
#st_slope_ECG
|
30 |
slope_map = {"Upsloping": 1, "Flat": 2, "Downsloping": 3}
|
31 |
df = df.replace({"st_slope_ECG": slope_map})
|
32 |
-
|
33 |
df = df[
|
34 |
[
|
35 |
"Umur",
|
@@ -47,22 +54,27 @@ def encode_df(df):
|
|
47 |
]
|
48 |
return df
|
49 |
|
|
|
50 |
filename = 'ACS_model_random_forest.sav'
|
51 |
|
52 |
# load the model from disk
|
53 |
loaded_model = joblib.load(filename)
|
54 |
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
57 |
Denyut_jantung_max, Angina_aktivitas, st_depression_ECG, st_slope_ECG):
|
58 |
df = pd.DataFrame.from_dict(
|
59 |
{
|
60 |
"Umur": [Umur],
|
61 |
"Gender": [Gender],
|
62 |
"Tipe_angina": [Tipe_angina],
|
63 |
-
"Tekanan_darah_istirahat"
|
64 |
-
"Kolesterol"
|
65 |
-
"Gula_darah_puasa"
|
66 |
"Hasil_Elektrokardiografi": [Hasil_Elektrokardiografi],
|
67 |
"Denyut_jantung_max": [Denyut_jantung_max],
|
68 |
"Angina_aktivitas": [Angina_aktivitas],
|
@@ -70,43 +82,54 @@ def predict(Umur, Gender, Tipe_angina, Tekanan_darah_istirahat, Kolesterol,
|
|
70 |
"st_slope_ECG": [st_slope_ECG],
|
71 |
}
|
72 |
)
|
73 |
-
|
74 |
df = encode_df(df)
|
75 |
pred = loaded_model.predict_proba(df)[0]
|
76 |
-
|
|
|
|
|
|
|
|
|
77 |
|
78 |
title = "Interactive Demonstration for Acute Coronary Syndrome Prediction System"
|
79 |
-
des = '''This model predicts the possibility of a heart disease using a hybrid sampling SMOTE-TOMEK model that achieved an high accuracy of 85
|
80 |
article = "<p style='text-align: center'><a href='https://www.linkedin.com/in/m-afif-rizky-a-a96048182/'>Created by @Vrooh933 Production</a> | <a href='https://github.com/afifrizkyandika11551100310'>GitHub Profile</a>"
|
81 |
|
82 |
-
demo = gr.Interface(
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
if __name__ == "__main__":
|
112 |
-
demo.launch()
|
|
|
1 |
+
|
2 |
import pickle
|
3 |
import pandas as pd
|
4 |
import sklearn
|
5 |
import gradio as gr
|
6 |
import joblib
|
7 |
|
8 |
+
|
9 |
+
# In[38]:
|
10 |
+
|
11 |
+
|
12 |
def encode_df(df):
|
13 |
+
|
14 |
+
# Gender
|
15 |
sex_map = {"Male": 1, "Female": 0}
|
16 |
df = df.replace({"Gender": sex_map})
|
17 |
+
|
18 |
+
# Tipe Angina
|
19 |
+
chestpain_map = {"Typical": 0, "Asymptomatic": 1,
|
20 |
+
"Nonanginal": 2, "Nontypical": 3}
|
21 |
df = df.replace({"Tipe_angina": chestpain_map})
|
22 |
+
|
23 |
+
# Gula_darah_puasa
|
24 |
fastingBLP_map = {"<120": 0, ">120": 1, "120": 2}
|
25 |
df = df.replace({"Gula_darah_puasa": fastingBLP_map})
|
26 |
+
|
27 |
+
# Angina_aktivitas
|
28 |
exang_map = {"No": 0, "Yes": 1}
|
29 |
df = df.replace({"Angina_aktivitas": exang_map})
|
30 |
+
|
31 |
+
# Hasil_Elektrokardiografi
|
32 |
+
recg_map = {"Normal": 0, "Abnormal Wave": 1,
|
33 |
+
"Left Ventricular Hypertrophy": 2}
|
34 |
df = df.replace({"Hasil_Elektrokardiografi": recg_map})
|
35 |
+
|
36 |
+
# st_slope_ECG
|
37 |
slope_map = {"Upsloping": 1, "Flat": 2, "Downsloping": 3}
|
38 |
df = df.replace({"st_slope_ECG": slope_map})
|
39 |
+
|
40 |
df = df[
|
41 |
[
|
42 |
"Umur",
|
|
|
54 |
]
|
55 |
return df
|
56 |
|
57 |
+
|
58 |
filename = 'ACS_model_random_forest.sav'
|
59 |
|
60 |
# load the model from disk
|
61 |
loaded_model = joblib.load(filename)
|
62 |
|
63 |
+
|
64 |
+
# In[51]:
|
65 |
+
|
66 |
+
|
67 |
+
def predict(Umur, Gender, Tipe_angina, Tekanan_darah_istirahat, Kolesterol,
|
68 |
+
Gula_darah_puasa, Hasil_Elektrokardiografi,
|
69 |
Denyut_jantung_max, Angina_aktivitas, st_depression_ECG, st_slope_ECG):
|
70 |
df = pd.DataFrame.from_dict(
|
71 |
{
|
72 |
"Umur": [Umur],
|
73 |
"Gender": [Gender],
|
74 |
"Tipe_angina": [Tipe_angina],
|
75 |
+
"Tekanan_darah_istirahat": [Tekanan_darah_istirahat],
|
76 |
+
"Kolesterol": [Kolesterol],
|
77 |
+
"Gula_darah_puasa": [Gula_darah_puasa],
|
78 |
"Hasil_Elektrokardiografi": [Hasil_Elektrokardiografi],
|
79 |
"Denyut_jantung_max": [Denyut_jantung_max],
|
80 |
"Angina_aktivitas": [Angina_aktivitas],
|
|
|
82 |
"st_slope_ECG": [st_slope_ECG],
|
83 |
}
|
84 |
)
|
85 |
+
|
86 |
df = encode_df(df)
|
87 |
pred = loaded_model.predict_proba(df)[0]
|
88 |
+
output = {"Possible Heart Disease": float(pred[1]),
|
89 |
+
"Less chance of Heart Disease": float(pred[0])}
|
90 |
+
|
91 |
+
return output
|
92 |
+
|
93 |
|
94 |
title = "Interactive Demonstration for Acute Coronary Syndrome Prediction System"
|
95 |
+
des = '''This model predicts the possibility of a heart disease using a hybrid sampling SMOTE-TOMEK model that achieved an high accuracy of 85% with Random Forest Algorithm'''
|
96 |
article = "<p style='text-align: center'><a href='https://www.linkedin.com/in/m-afif-rizky-a-a96048182/'>Created by @Vrooh933 Production</a> | <a href='https://github.com/afifrizkyandika11551100310'>GitHub Profile</a>"
|
97 |
|
98 |
+
demo = gr.Interface(predict,
|
99 |
+
[gr.Slider(0, 88, value=25, label='Umur'),
|
100 |
+
gr.Radio(["Male", "Female"], label='Gender'),
|
101 |
+
gr.Dropdown(["Typical", "Asymptomatic", "Nonanginal",
|
102 |
+
"Nontypical"], label="Tipe_angina"),
|
103 |
+
|
104 |
+
gr.Slider(0, 200, value=125,
|
105 |
+
label='Tekanan_darah_istirahat'),
|
106 |
+
gr.Slider(0, 603, value=50, label='Kolesterol'),
|
107 |
+
gr.Radio(["<120", ">120", '120'],
|
108 |
+
label='Gula_darah_puasa'),
|
109 |
+
gr.Dropdown(["Normal", "Abnormal Wave", "Left Ventricular Hypertrophy"],
|
110 |
+
label='Hasil_Elektrokardiografi'),
|
111 |
+
gr.Number(value=100, label='Denyut_jantung_max'),
|
112 |
+
gr.Dropdown(["No", "Yes"], label='Angina_aktivitas'),
|
113 |
+
gr.Slider(-2.6, 6.2, value=3.1,
|
114 |
+
label='st_depression_ECG'),
|
115 |
+
gr.Radio(["Upsloping", "Flat", "Downsloping"],
|
116 |
+
label='st_slope_ECG'),
|
117 |
+
], "label",
|
118 |
+
examples=[[37, 'Male', 'Typical', 89, 276, '>120', "Left Ventricular Hypertrophy", 150, 'No', 2.3, 'Downsloping', 0, 'Fixed'],
|
119 |
+
[63, 'Male', 'Typical', 50, 100, '<120', "Abnormal Wave",
|
120 |
+
90, 'No', 1.0, 'Downsloping', 1, 'Reversable'],
|
121 |
+
[58, 'Female', 'Asymptomatic', 70, 500, '>120',
|
122 |
+
"Normal", 111, 'Yes', 0.8, 'Flat', 0, 'Normal']
|
123 |
+
],
|
124 |
+
title=title,
|
125 |
+
description=des,
|
126 |
+
article=article,
|
127 |
+
live=True,
|
128 |
+
)
|
129 |
+
|
130 |
+
|
131 |
+
# In[53]:
|
132 |
+
|
133 |
|
134 |
if __name__ == "__main__":
|
135 |
+
demo.launch()
|