Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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, "Nonanginal": 2, "Nontypical" : 3}
|
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, "Left Ventricular Hypertrophy": 2}
|
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",
|
36 |
+
"Gender",
|
37 |
+
"Tipe_angina",
|
38 |
+
"Tekanan_darah_istirahat",
|
39 |
+
"Kolesterol",
|
40 |
+
"Gula_darah_puasa",
|
41 |
+
"Hasil_Elektrokardiografi",
|
42 |
+
"Denyut_jantung_max",
|
43 |
+
"Angina_aktivitas",
|
44 |
+
"st_depression_ECG",
|
45 |
+
"st_slope_ECG",
|
46 |
+
]
|
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 |
+
def predict(Umur, Gender, Tipe_angina, Tekanan_darah_istirahat, Kolesterol,
|
56 |
+
Gula_darah_puasa, Hasil_Elektrokardiografi,
|
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" : [Tekanan_darah_istirahat],
|
64 |
+
"Kolesterol" : [Kolesterol],
|
65 |
+
"Gula_darah_puasa" : [Gula_darah_puasa],
|
66 |
+
"Hasil_Elektrokardiografi": [Hasil_Elektrokardiografi],
|
67 |
+
"Denyut_jantung_max": [Denyut_jantung_max],
|
68 |
+
"Angina_aktivitas": [Angina_aktivitas],
|
69 |
+
"st_depression_ECG": [st_depression_ECG],
|
70 |
+
"st_slope_ECG": [st_slope_ECG],
|
71 |
+
}
|
72 |
+
)
|
73 |
+
|
74 |
+
df = encode_df(df)
|
75 |
+
pred = loaded_model.predict_proba(df)[0]
|
76 |
+
return {"Possible Heart Disease": float(pred[1]), "Less chance of Heart Disease": float(pred[0])}
|
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%, and f1-score 94% with Random Forest Algorithm'''
|
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 |
+
predict,
|
84 |
+
[gr.Slider(0, 88, value=25, label='Umur'),
|
85 |
+
gr.Radio(["Male", "Female"], label='Gender'),
|
86 |
+
gr.Dropdown(["Typical", "Asymptomatic", "Nonanginal", "Nontypical"], label="Tipe_angina"),
|
87 |
+
|
88 |
+
gr.Slider(0, 200, value=125, label= 'Tekanan_darah_istirahat'),
|
89 |
+
gr.Slider(0, 603, value=50, label= 'Kolesterol'),
|
90 |
+
gr.Radio(["<120", ">120", '120'], label='Gula_darah_puasa'),
|
91 |
+
|
92 |
+
|
93 |
+
gr.Dropdown(["Normal", "Abnormal Wave", "Left Ventricular Hypertrophy"], label='Hasil_Elektrokardiografi'),
|
94 |
+
gr.Number(value=100, label='Denyut_jantung_max'),
|
95 |
+
gr.Dropdown(["No", "Yes"], label='Angina_aktivitas'),
|
96 |
+
gr.Slider(-2.6, 6.2, value=3.1, label='st_depression_ECG'),
|
97 |
+
gr.Radio(["Upsloping", "Flat", "Downsloping"], label='st_slope_ECG'),
|
98 |
+
],
|
99 |
+
"label",
|
100 |
+
examples=[
|
101 |
+
[37,'Male','Typical', 89, 276, '>120', "Left Ventricular Hypertrophy",150,'No',2.3,'Downsloping',0,'Fixed'],
|
102 |
+
[63,'Male','Typical', 50, 100, '<120', "Abnormal Wave",90,'No',1.0,'Downsloping',1,'Reversable'],
|
103 |
+
[58,'Female','Asymptomatic', 70, 500, '>120', "Normal",111,'Yes',0.8,'Flat',0,'Normal']
|
104 |
+
],
|
105 |
+
title=title,
|
106 |
+
description=des,
|
107 |
+
article=article,
|
108 |
+
live=True,
|
109 |
+
)
|