Spaces:
Runtime error
Runtime error
kmckee95
commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pickle
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import seaborn as sns
|
| 7 |
+
print('Loading......')
|
| 8 |
+
|
| 9 |
+
# load the saved model
|
| 10 |
+
rfc_saved = pickle.load(open('rfc.pickle','rb'))
|
| 11 |
+
|
| 12 |
+
full_pipeline_saved = pickle.load(open('full_pipeline.pickle','rb'))
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# function to check the heart disease risk
|
| 16 |
+
def CheckHeartDisease(age,sex,ChestPainType,RestingBP,Cholesterol,
|
| 17 |
+
FastingBS,RestingECG,MaxHR,ExerciseAngina,Oldpeak,ST_Slope):
|
| 18 |
+
try:
|
| 19 |
+
df_model = pd.DataFrame([],columns=['Age','Sex','ChestPainType','RestingBP','Cholesterol','FastingBS','RestingECG','MaxHR','ExerciseAngina', 'Oldpeak','ST_Slope'])
|
| 20 |
+
|
| 21 |
+
df_model.loc[0] = [age,sex,ChestPainType,RestingBP,Cholesterol,FastingBS,RestingECG,MaxHR,ExerciseAngina,Oldpeak,ST_Slope]
|
| 22 |
+
|
| 23 |
+
# preprocess the person details
|
| 24 |
+
X_processed = full_pipeline_saved.transform(df_model)
|
| 25 |
+
|
| 26 |
+
# do the prediction
|
| 27 |
+
y_pred = rfc_saved.predict(X_processed)
|
| 28 |
+
|
| 29 |
+
# plot risk of heart disease based on sex
|
| 30 |
+
df = pd.read_csv('heart.csv')
|
| 31 |
+
target = df['HeartDisease'].replace([0,1],['Low','High'])
|
| 32 |
+
data = pd.crosstab(index=df['Sex'],
|
| 33 |
+
columns=target)
|
| 34 |
+
|
| 35 |
+
data.plot(kind='bar',stacked=True)
|
| 36 |
+
fig1 = plt.gcf()
|
| 37 |
+
plt.close()
|
| 38 |
+
|
| 39 |
+
# plot count of person within given age range, with heart disease risk
|
| 40 |
+
bins=[0,30,50,80]
|
| 41 |
+
sns.countplot(x=pd.cut(df.Age,bins=bins),hue=target,color='r')
|
| 42 |
+
fig2 = plt.gcf()
|
| 43 |
+
plt.close()
|
| 44 |
+
|
| 45 |
+
# plot graph based on ChestPainType
|
| 46 |
+
sns.countplot(x=target,hue=df.ChestPainType)
|
| 47 |
+
plt.xticks(np.arange(2), ['No', 'Yes'])
|
| 48 |
+
fig3 = plt.gcf()
|
| 49 |
+
|
| 50 |
+
if y_pred[0]==0:
|
| 51 |
+
return 'Low Risk of Heart Disease',fig1,fig2,fig3
|
| 52 |
+
else:
|
| 53 |
+
return 'High Risk of Heart Disease',fig1,fig2,fig3
|
| 54 |
+
|
| 55 |
+
except:
|
| 56 |
+
return 'Wrong inputs',fig1,fig2,fig3
|
| 57 |
+
|
| 58 |
+
# create GUI
|
| 59 |
+
iface = gr.Interface(
|
| 60 |
+
CheckHeartDisease, # its the function to be called with below parameters
|
| 61 |
+
[
|
| 62 |
+
gr.inputs.Number(label='Age (0-115)'),
|
| 63 |
+
gr.inputs.Dropdown(['M','F'],default='M'),
|
| 64 |
+
gr.inputs.Dropdown(['ATA', 'NAP', 'ASY','TA'],default='TA'),
|
| 65 |
+
gr.inputs.Number(label='RESTINGBP (0-200)'),
|
| 66 |
+
gr.inputs.Number(label='CHOLESTEROL (0-603)'),
|
| 67 |
+
gr.inputs.Number(label='FASTINGBS (0-1)'),
|
| 68 |
+
gr.inputs.Dropdown(['Normal', 'ST' ,'LVH'],default='ST'),
|
| 69 |
+
gr.inputs.Number(label='MAXHR (60-202)'),
|
| 70 |
+
|
| 71 |
+
gr.inputs.Dropdown(['Y','N'],default='Y'),
|
| 72 |
+
gr.inputs.Number(label='OLDPEAK (-2.6 to 6.2)'),
|
| 73 |
+
gr.inputs.Dropdown(['Up', 'Flat', 'Down'],default='Up')
|
| 74 |
+
],
|
| 75 |
+
[gr.outputs.Textbox(),"plot","plot","plot"]
|
| 76 |
+
|
| 77 |
+
, live=False,layout='vertical',title='Get Your Heart Disease Status',
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
iface.launch() # launch the gui
|