ACS_Prediction / app.py
Vrooh933's picture
Update app.py
31786b5
raw
history blame
4.21 kB
import pickle
import pandas as pd
import sklearn
import gradio as gr
import joblib
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)
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]
return {"Possible Heart Disease": float(pred[1]), "Less chance of Heart Disease": float(pred[0])}
title = "Interactive Demonstration for Acute Coronary Syndrome Prediction System"
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'''
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,
)
if __name__ == "__main__":
demo.launch(share = True)