Spaces:
Runtime error
Runtime error
| import pickle | |
| import pandas as pd | |
| import sklearn | |
| import gradio as gr | |
| def encode_df(df): | |
| sex_map = {"Male": 1, "Female": 0} | |
| df = df.replace({"Sex": sex_map}) | |
| chestpain_map = {"Typical": 0, "Asymptomatic": 1, "Nonanginal": 2, "Nontypical" : 3} | |
| df = df.replace({"ChestPain": chestpain_map}) | |
| thal_map = {"Fixed": 0, "Normal": 1, "Reversable": 2} | |
| df = df.replace({"Thal": thal_map}) | |
| exang_map = {"No": 0, "Yes": 1} | |
| df = df.replace({"ExAng": exang_map}) | |
| recg_map = {"Normal": 0, "Abnormal Wave": 1, "Left Ventricular Hypertrophy": 2} | |
| df = df.replace({"RestECG": recg_map}) | |
| slope_map = {"Upsloping": 1, "Flat": 2, "Downsloping": 3} | |
| df = df.replace({"Slope": slope_map}) | |
| df = df[ | |
| [ | |
| "Age", | |
| "Sex", | |
| "ChestPain", | |
| "RestECG", | |
| "MaxHR", | |
| "ExAng", | |
| "Oldpeak", | |
| "Slope", | |
| "Ca", | |
| "Thal", | |
| ] | |
| ] | |
| return df | |
| filename = './heart_model.sav' | |
| # load the model from disk | |
| loaded_model = pickle.load(open(filename, 'rb')) | |
| def predict(age, sex, cp, restecg, mhr, exang, oldpeak, slope, ca, thal): | |
| df = pd.DataFrame.from_dict( | |
| { | |
| "Age": [age], | |
| "Sex": [sex], | |
| "ChestPain": [cp], | |
| "RestECG": [restecg], | |
| "MaxHR": [mhr], | |
| "ExAng": [exang], | |
| "Oldpeak": [oldpeak], | |
| "Slope": [slope], | |
| "Ca": [ca], | |
| "Thal": [thal], | |
| } | |
| ) | |
| df = encode_df(df) | |
| pred = loaded_model.predict_proba(df)[0] | |
| return {"Possible Heart Disease": float(pred[0]), "Less chance of Heart Disease": float(pred[1])} | |
| title = "Interactive Demo for Heart Disease Prediction" | |
| des = 'This model predicts the possibility of a heart disease using a stacking classifier model that achieved an accuracy of 78%' | |
| article = "<p style='text-align: center'><a href='https://www.linkedin.com/in/oayodeji/'>Space by Wvle</a> | <a href='https://github.com/oayodeji/heart_disease'>Model</a>" | |
| demo = gr.Interface( | |
| predict, | |
| [ gr.Slider(0, 80, value=25, label='Age'), | |
| gr.Radio(["Male", "Female"], label='Sex'), | |
| gr.Dropdown(["Typical", "Asymptomatic", "Nonanginal", "Nontypical"], label="Chest Pain Type"), | |
| gr.Dropdown(["Normal", "Abnormal Wave", "Left Ventricular Hypertrophy"], label='Resting Electrocardiographic Results'), | |
| gr.Number(value=100, label='Maximum Heart Rate'), | |
| gr.Dropdown(["No", "Yes"], label='Exercise Induced Angina'), | |
| gr.Slider(0, 10, value=3.1, label='ST depression induced by exercise relation to rest'), | |
| gr.Radio(["Upsloping", "Flat", "Downsloping"], label='Slope of the Peak Exercise'), | |
| gr.Radio(["0", "1", "2", "3"], label="Number of Major Vessels"), | |
| gr.Dropdown(["Fixed", "Normal", "Reversable"], label="Blood Flow Disorder") | |
| ], | |
| "label", | |
| examples=[ | |
| [37,'Male','Typical',"Left Ventricular Hypertrophy",150,'No',2.3,'Downsloping',0,'Fixed'], | |
| [63,'Male','Typical',"Abnormal Wave",90,'No',1.0,'Downsloping',1,'Reversable'], | |
| [58,'Female','Asymptomatic',"Normal",111,'Yes',0.8,'Flat',0,'Normal'] | |
| ], | |
| title=title, | |
| description=des, | |
| article=article, | |
| interpretation="default", | |
| live=True, | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |