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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"id": "5114c17a",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [
|
| 9 |
+
{
|
| 10 |
+
"name": "stdout",
|
| 11 |
+
"output_type": "stream",
|
| 12 |
+
"text": [
|
| 13 |
+
"Loading......\n",
|
| 14 |
+
"Running on local URL: http://127.0.0.1:7866/\n",
|
| 15 |
+
"\n",
|
| 16 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
| 17 |
+
]
|
| 18 |
+
},
|
| 19 |
+
{
|
| 20 |
+
"data": {
|
| 21 |
+
"text/html": [
|
| 22 |
+
"\n",
|
| 23 |
+
" <iframe\n",
|
| 24 |
+
" width=\"900\"\n",
|
| 25 |
+
" height=\"500\"\n",
|
| 26 |
+
" src=\"http://127.0.0.1:7866/\"\n",
|
| 27 |
+
" frameborder=\"0\"\n",
|
| 28 |
+
" allowfullscreen\n",
|
| 29 |
+
" \n",
|
| 30 |
+
" ></iframe>\n",
|
| 31 |
+
" "
|
| 32 |
+
],
|
| 33 |
+
"text/plain": [
|
| 34 |
+
"<IPython.lib.display.IFrame at 0x26e56928e50>"
|
| 35 |
+
]
|
| 36 |
+
},
|
| 37 |
+
"metadata": {},
|
| 38 |
+
"output_type": "display_data"
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"data": {
|
| 42 |
+
"text/plain": [
|
| 43 |
+
"(<fastapi.applications.FastAPI at 0x26e43876550>,\n",
|
| 44 |
+
" 'http://127.0.0.1:7866/',\n",
|
| 45 |
+
" None)"
|
| 46 |
+
]
|
| 47 |
+
},
|
| 48 |
+
"execution_count": 1,
|
| 49 |
+
"metadata": {},
|
| 50 |
+
"output_type": "execute_result"
|
| 51 |
+
}
|
| 52 |
+
],
|
| 53 |
+
"source": [
|
| 54 |
+
"import pickle\n",
|
| 55 |
+
"import pandas as pd\n",
|
| 56 |
+
"import numpy as np\n",
|
| 57 |
+
"import gradio as gr\n",
|
| 58 |
+
"import matplotlib.pyplot as plt\n",
|
| 59 |
+
"import seaborn as sns\n",
|
| 60 |
+
"print('Loading......')\n",
|
| 61 |
+
"\n",
|
| 62 |
+
"# load the saved model\n",
|
| 63 |
+
"rfc_saved = pickle.load(open('rfc.pickle','rb'))\n",
|
| 64 |
+
"\n",
|
| 65 |
+
"full_pipeline_saved = pickle.load(open('full_pipeline.pickle','rb'))\n",
|
| 66 |
+
"\n",
|
| 67 |
+
"\n",
|
| 68 |
+
"# function to check the heart disease risk\n",
|
| 69 |
+
"def CheckHeartDisease(age,sex,ChestPainType,RestingBP,Cholesterol,\n",
|
| 70 |
+
" FastingBS,RestingECG,MaxHR,ExerciseAngina,Oldpeak,ST_Slope):\n",
|
| 71 |
+
" try:\n",
|
| 72 |
+
" df_model = pd.DataFrame([],columns=['Age','Sex','ChestPainType','RestingBP','Cholesterol','FastingBS','RestingECG','MaxHR','ExerciseAngina', 'Oldpeak','ST_Slope'])\n",
|
| 73 |
+
"\n",
|
| 74 |
+
" df_model.loc[0] = [age,sex,ChestPainType,RestingBP,Cholesterol,FastingBS,RestingECG,MaxHR,ExerciseAngina,Oldpeak,ST_Slope]\n",
|
| 75 |
+
" \n",
|
| 76 |
+
" # preprocess the person details\n",
|
| 77 |
+
" X_processed = full_pipeline_saved.transform(df_model)\n",
|
| 78 |
+
" \n",
|
| 79 |
+
" # do the prediction\n",
|
| 80 |
+
" y_pred = rfc_saved.predict(X_processed)\n",
|
| 81 |
+
" \n",
|
| 82 |
+
" # plot risk of heart disease based on sex\n",
|
| 83 |
+
" df = pd.read_csv('heart.csv')\n",
|
| 84 |
+
" target = df['HeartDisease'].replace([0,1],['Low','High'])\n",
|
| 85 |
+
" data = pd.crosstab(index=df['Sex'],\n",
|
| 86 |
+
" columns=target)\n",
|
| 87 |
+
" \n",
|
| 88 |
+
" data.plot(kind='bar',stacked=True)\n",
|
| 89 |
+
" fig1 = plt.gcf()\n",
|
| 90 |
+
" plt.close()\n",
|
| 91 |
+
" \n",
|
| 92 |
+
" # plot count of person within given age range, with heart disease risk\n",
|
| 93 |
+
" bins=[0,30,50,80]\n",
|
| 94 |
+
" sns.countplot(x=pd.cut(df.Age,bins=bins),hue=target,color='r')\n",
|
| 95 |
+
" fig2 = plt.gcf()\n",
|
| 96 |
+
" plt.close()\n",
|
| 97 |
+
"\n",
|
| 98 |
+
" # plot graph based on ChestPainType\n",
|
| 99 |
+
" sns.countplot(x=target,hue=df.ChestPainType)\n",
|
| 100 |
+
" plt.xticks(np.arange(2), ['No', 'Yes']) \n",
|
| 101 |
+
" fig3 = plt.gcf()\n",
|
| 102 |
+
"\n",
|
| 103 |
+
" if y_pred[0]==0:\n",
|
| 104 |
+
" return 'No Heart Disease',fig1,fig2,fig3\n",
|
| 105 |
+
" else:\n",
|
| 106 |
+
" return 'High Chances of Heart Disease',fig1,fig2,fig3\n",
|
| 107 |
+
" \n",
|
| 108 |
+
" except:\n",
|
| 109 |
+
" return 'Wrong inputs',fig1,fig2,fig3\n",
|
| 110 |
+
"\n",
|
| 111 |
+
"# create GUI\n",
|
| 112 |
+
"iface = gr.Interface(\n",
|
| 113 |
+
" CheckHeartDisease, # its the function to be called with below parameters\n",
|
| 114 |
+
" [\n",
|
| 115 |
+
" gr.inputs.Number(label='Age (0-115)'), \n",
|
| 116 |
+
" gr.inputs.Dropdown(['M','F'],default='M'), \n",
|
| 117 |
+
" gr.inputs.Dropdown(['ATA', 'NAP', 'ASY','TA'],default='TA'),\n",
|
| 118 |
+
" gr.inputs.Number(label='RESTINGBP (0-200)'), \n",
|
| 119 |
+
" gr.inputs.Number(label='CHOLESTEROL (0-603)'), \n",
|
| 120 |
+
" gr.inputs.Number(label='FASTINGBS (0-1)'), \n",
|
| 121 |
+
" gr.inputs.Dropdown(['Normal', 'ST' ,'LVH'],default='ST'),\n",
|
| 122 |
+
" gr.inputs.Number(label='MAXHR (60-202)'), \n",
|
| 123 |
+
"\n",
|
| 124 |
+
" gr.inputs.Dropdown(['Y','N'],default='Y'),\n",
|
| 125 |
+
" gr.inputs.Number(label='OLDPEAK (-2.6 to 6.2)'),\n",
|
| 126 |
+
" gr.inputs.Dropdown(['Up', 'Flat', 'Down'],default='Up')\n",
|
| 127 |
+
" ],\n",
|
| 128 |
+
" [gr.outputs.Textbox(),\"plot\",\"plot\",\"plot\"]\n",
|
| 129 |
+
" \n",
|
| 130 |
+
" , live=False,layout='vertical',title='Get Your Heart Disease Status',\n",
|
| 131 |
+
")\n",
|
| 132 |
+
"\n",
|
| 133 |
+
"iface.launch() # launch the gui\n"
|
| 134 |
+
]
|
| 135 |
+
},
|
| 136 |
+
{
|
| 137 |
+
"cell_type": "code",
|
| 138 |
+
"execution_count": null,
|
| 139 |
+
"id": "5d0cfc37",
|
| 140 |
+
"metadata": {},
|
| 141 |
+
"outputs": [],
|
| 142 |
+
"source": []
|
| 143 |
+
}
|
| 144 |
+
],
|
| 145 |
+
"metadata": {
|
| 146 |
+
"kernelspec": {
|
| 147 |
+
"display_name": "Python 3 (ipykernel)",
|
| 148 |
+
"language": "python",
|
| 149 |
+
"name": "python3"
|
| 150 |
+
},
|
| 151 |
+
"language_info": {
|
| 152 |
+
"codemirror_mode": {
|
| 153 |
+
"name": "ipython",
|
| 154 |
+
"version": 3
|
| 155 |
+
},
|
| 156 |
+
"file_extension": ".py",
|
| 157 |
+
"mimetype": "text/x-python",
|
| 158 |
+
"name": "python",
|
| 159 |
+
"nbconvert_exporter": "python",
|
| 160 |
+
"pygments_lexer": "ipython3",
|
| 161 |
+
"version": "3.9.7"
|
| 162 |
+
}
|
| 163 |
+
},
|
| 164 |
+
"nbformat": 4,
|
| 165 |
+
"nbformat_minor": 5
|
| 166 |
+
}
|