{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "5114c17a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loading......\n",
"Running on local URL: http://127.0.0.1:7866/\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(,\n",
" 'http://127.0.0.1:7866/',\n",
" None)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pickle\n",
"import pandas as pd\n",
"import numpy as np\n",
"import gradio as gr\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"print('Loading......')\n",
"\n",
"# load the saved model\n",
"rfc_saved = pickle.load(open('rfc.pickle','rb'))\n",
"\n",
"full_pipeline_saved = pickle.load(open('full_pipeline.pickle','rb'))\n",
"\n",
"\n",
"# function to check the heart disease risk\n",
"def CheckHeartDisease(age,sex,ChestPainType,RestingBP,Cholesterol,\n",
" FastingBS,RestingECG,MaxHR,ExerciseAngina,Oldpeak,ST_Slope):\n",
" try:\n",
" df_model = pd.DataFrame([],columns=['Age','Sex','ChestPainType','RestingBP','Cholesterol','FastingBS','RestingECG','MaxHR','ExerciseAngina', 'Oldpeak','ST_Slope'])\n",
"\n",
" df_model.loc[0] = [age,sex,ChestPainType,RestingBP,Cholesterol,FastingBS,RestingECG,MaxHR,ExerciseAngina,Oldpeak,ST_Slope]\n",
" \n",
" # preprocess the person details\n",
" X_processed = full_pipeline_saved.transform(df_model)\n",
" \n",
" # do the prediction\n",
" y_pred = rfc_saved.predict(X_processed)\n",
" \n",
" # plot risk of heart disease based on sex\n",
" df = pd.read_csv('heart.csv')\n",
" target = df['HeartDisease'].replace([0,1],['Low','High'])\n",
" data = pd.crosstab(index=df['Sex'],\n",
" columns=target)\n",
" \n",
" data.plot(kind='bar',stacked=True)\n",
" fig1 = plt.gcf()\n",
" plt.close()\n",
" \n",
" # plot count of person within given age range, with heart disease risk\n",
" bins=[0,30,50,80]\n",
" sns.countplot(x=pd.cut(df.Age,bins=bins),hue=target,color='r')\n",
" fig2 = plt.gcf()\n",
" plt.close()\n",
"\n",
" # plot graph based on ChestPainType\n",
" sns.countplot(x=target,hue=df.ChestPainType)\n",
" plt.xticks(np.arange(2), ['No', 'Yes']) \n",
" fig3 = plt.gcf()\n",
"\n",
" if y_pred[0]==0:\n",
" return 'No Heart Disease',fig1,fig2,fig3\n",
" else:\n",
" return 'High Chances of Heart Disease',fig1,fig2,fig3\n",
" \n",
" except:\n",
" return 'Wrong inputs',fig1,fig2,fig3\n",
"\n",
"# create GUI\n",
"iface = gr.Interface(\n",
" CheckHeartDisease, # its the function to be called with below parameters\n",
" [\n",
" gr.inputs.Number(label='Age (0-115)'), \n",
" gr.inputs.Dropdown(['M','F'],default='M'), \n",
" gr.inputs.Dropdown(['ATA', 'NAP', 'ASY','TA'],default='TA'),\n",
" gr.inputs.Number(label='RESTINGBP (0-200)'), \n",
" gr.inputs.Number(label='CHOLESTEROL (0-603)'), \n",
" gr.inputs.Number(label='FASTINGBS (0-1)'), \n",
" gr.inputs.Dropdown(['Normal', 'ST' ,'LVH'],default='ST'),\n",
" gr.inputs.Number(label='MAXHR (60-202)'), \n",
"\n",
" gr.inputs.Dropdown(['Y','N'],default='Y'),\n",
" gr.inputs.Number(label='OLDPEAK (-2.6 to 6.2)'),\n",
" gr.inputs.Dropdown(['Up', 'Flat', 'Down'],default='Up')\n",
" ],\n",
" [gr.outputs.Textbox(),\"plot\",\"plot\",\"plot\"]\n",
" \n",
" , live=False,layout='vertical',title='Get Your Heart Disease Status',\n",
")\n",
"\n",
"iface.launch() # launch the gui\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5d0cfc37",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}