File size: 5,336 Bytes
cedb621
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
{
 "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",
       "        <iframe\n",
       "            width=\"900\"\n",
       "            height=\"500\"\n",
       "            src=\"http://127.0.0.1:7866/\"\n",
       "            frameborder=\"0\"\n",
       "            allowfullscreen\n",
       "            \n",
       "        ></iframe>\n",
       "        "
      ],
      "text/plain": [
       "<IPython.lib.display.IFrame at 0x26e56928e50>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(<fastapi.applications.FastAPI at 0x26e43876550>,\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
}