File size: 4,858 Bytes
d27ea0c
61fe07e
 
 
 
 
 
d27ea0c
 
 
 
61fe07e
d27ea0c
 
61fe07e
 
d27ea0c
 
 
 
61fe07e
d27ea0c
 
61fe07e
 
d27ea0c
 
61fe07e
 
d27ea0c
 
 
 
61fe07e
d27ea0c
 
61fe07e
 
d27ea0c
61fe07e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27ea0c
61fe07e
 
 
 
 
d27ea0c
 
 
 
 
 
61fe07e
 
 
 
 
 
d27ea0c
 
 
61fe07e
 
 
 
 
 
 
d27ea0c
61fe07e
 
d27ea0c
 
 
 
 
61fe07e
54c7784
 
61fe07e
 
d27ea0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31786b5
 
d27ea0c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

import pickle
import pandas as pd
import sklearn
import gradio as gr
import joblib


# In[38]:


def encode_df(df):

    # Gender
    sex_map = {"Male": 1, "Female": 0}
    df = df.replace({"Gender": sex_map})

    # Tipe Angina
    chestpain_map = {"Typical": 0, "Asymptomatic": 1,
                     "Nonanginal": 2, "Nontypical": 3}
    df = df.replace({"Tipe_angina": chestpain_map})

    # Gula_darah_puasa
    fastingBLP_map = {"<120": 0, ">120": 1, "120": 2}
    df = df.replace({"Gula_darah_puasa": fastingBLP_map})

    # Angina_aktivitas
    exang_map = {"No": 0, "Yes": 1}
    df = df.replace({"Angina_aktivitas": exang_map})

    # Hasil_Elektrokardiografi
    recg_map = {"Normal": 0, "Abnormal Wave": 1,
                "Left Ventricular Hypertrophy": 2}
    df = df.replace({"Hasil_Elektrokardiografi": recg_map})

    # st_slope_ECG
    slope_map = {"Upsloping": 1, "Flat": 2, "Downsloping": 3}
    df = df.replace({"st_slope_ECG": slope_map})

    df = df[
        [
            "Umur",
            "Gender",
            "Tipe_angina",
            "Tekanan_darah_istirahat",
            "Kolesterol",
            "Gula_darah_puasa",
            "Hasil_Elektrokardiografi",
            "Denyut_jantung_max",
            "Angina_aktivitas",
            "st_depression_ECG",
            "st_slope_ECG",
        ]
    ]
    return df


filename = 'ACS_model_random_forest.sav'

# load the model from disk
loaded_model = joblib.load(filename)


# In[51]:


def predict(Umur, Gender, Tipe_angina, Tekanan_darah_istirahat, Kolesterol,
            Gula_darah_puasa, Hasil_Elektrokardiografi,
            Denyut_jantung_max, Angina_aktivitas, st_depression_ECG, st_slope_ECG):
    df = pd.DataFrame.from_dict(
        {
            "Umur": [Umur],
            "Gender": [Gender],
            "Tipe_angina": [Tipe_angina],
            "Tekanan_darah_istirahat": [Tekanan_darah_istirahat],
            "Kolesterol": [Kolesterol],
            "Gula_darah_puasa": [Gula_darah_puasa],
            "Hasil_Elektrokardiografi": [Hasil_Elektrokardiografi],
            "Denyut_jantung_max": [Denyut_jantung_max],
            "Angina_aktivitas": [Angina_aktivitas],
            "st_depression_ECG": [st_depression_ECG],
            "st_slope_ECG": [st_slope_ECG],
        }
    )

    df = encode_df(df)
    pred = loaded_model.predict_proba(df)[0]
    output = {"Possible Heart Disease": float(pred[1]),
              "Less chance of Heart Disease": float(pred[0])}

    return output


title = "Interactive Demonstration for ACS Prediction System"
des = '''This model predicts the possibility of a Acute Coronary Syndrome using a hybrid sampling SMOTE-TOMEK with Random Forest Algorithm model that achieved an high accuracy of 85%'''
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>"

demo = gr.Interface(predict,
                    [gr.Slider(0, 88, value=25, label='Umur'),
                     gr.Radio(["Male", "Female"], label='Gender'),
                     gr.Dropdown(["Typical", "Asymptomatic", "Nonanginal",
                                 "Nontypical"], label="Tipe_angina"),

                     gr.Slider(0, 200, value=125,
                               label='Tekanan_darah_istirahat'),
                     gr.Slider(0, 603, value=50, label='Kolesterol'),
                     gr.Radio(["<120", ">120", '120'],
                              label='Gula_darah_puasa'),
                     gr.Dropdown(["Normal", "Abnormal Wave", "Left Ventricular Hypertrophy"],
                                 label='Hasil_Elektrokardiografi'),
                     gr.Number(value=100, label='Denyut_jantung_max'),
                     gr.Dropdown(["No", "Yes"], label='Angina_aktivitas'),
                     gr.Slider(-2.6, 6.2, value=3.1,
                               label='st_depression_ECG'),
                     gr.Radio(["Upsloping", "Flat", "Downsloping"],
                              label='st_slope_ECG'),
                     ], "label",
                    examples=[[37, 'Male', 'Typical', 89, 276, '>120', "Left Ventricular Hypertrophy", 150, 'No', 2.3, 'Downsloping', 0, 'Fixed'],
                              [63, 'Male', 'Typical', 50, 100, '<120', "Abnormal Wave",
                                  90, 'No', 1.0, 'Downsloping', 1, 'Reversable'],
                              [58, 'Female', 'Asymptomatic', 70, 500, '>120',
                               "Normal", 111, 'Yes', 0.8, 'Flat', 0, 'Normal']
                              ],
                    title=title,
                    description=des,
                    article=article,
                    live=True,
                    )


# In[53]:


if __name__ == "__main__":
    demo.launch()