Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,205 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import seaborn as sns
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import pickle
|
7 |
+
import gradio as gr
|
8 |
+
from math import sqrt
|
9 |
+
|
10 |
+
df= pd.read_csv("/content/heart.csv")
|
11 |
+
df
|
12 |
+
|
13 |
+
df.info()
|
14 |
+
|
15 |
+
cp_data= df['cp'].value_counts().reset_index()
|
16 |
+
cp_data['index'][3]= 'asymptomatic'
|
17 |
+
cp_data['index'][2]= 'non-anginal'
|
18 |
+
cp_data['index'][1]= 'Atyppical Anigma'
|
19 |
+
cp_data['index'][0]= 'Typical Anigma'
|
20 |
+
cp_data
|
21 |
+
|
22 |
+
ecg_data= df['restecg'].value_counts().reset_index()
|
23 |
+
ecg_data['index'][0]= 'normal'
|
24 |
+
ecg_data['index'][1]= 'having ST-T wave abnormality'
|
25 |
+
ecg_data['index'][2]= 'showing probable or definite left ventricular hypertrophy by Estes'
|
26 |
+
|
27 |
+
ecg_data
|
28 |
+
|
29 |
+
def outbreak(feature):
|
30 |
+
|
31 |
+
fig = plt.figure()
|
32 |
+
plt.rcParams.update({'font.size': 10})
|
33 |
+
plt.rc('xtick', labelsize=5)
|
34 |
+
|
35 |
+
if (feature == "Age"):
|
36 |
+
|
37 |
+
|
38 |
+
plt.title("Age of Patients")
|
39 |
+
plt.xlabel("Age")
|
40 |
+
|
41 |
+
|
42 |
+
sns.countplot(x='age',data=df);
|
43 |
+
return fig
|
44 |
+
|
45 |
+
elif (feature == "Sex"):
|
46 |
+
plt.title("Sex of Patients,0=Female and 1=Male")
|
47 |
+
sns.countplot(x='sex',data=df);
|
48 |
+
return fig
|
49 |
+
|
50 |
+
elif (feature == "Chest Pain"):
|
51 |
+
plt.title("Chest Pain of Patients")
|
52 |
+
|
53 |
+
sns.barplot(x=cp_data['index'],y= cp_data['cp']);
|
54 |
+
return fig
|
55 |
+
|
56 |
+
elif (feature == "ECG"):
|
57 |
+
plt.title("ECG data of Patients")
|
58 |
+
|
59 |
+
sns.barplot(x=ecg_data['index'],y= ecg_data['restecg']);
|
60 |
+
return fig
|
61 |
+
|
62 |
+
elif (feature == "Blood Pressure"):
|
63 |
+
plt.title("Resting Blood Pressure (mmHg)")
|
64 |
+
|
65 |
+
sns.distplot(df['trestbps'], kde=True, color = 'magenta')
|
66 |
+
plt.xlabel("Resting Blood Pressure (mmHg)")
|
67 |
+
return fig
|
68 |
+
|
69 |
+
def op(target, sex, cp, age, bp, ch):
|
70 |
+
|
71 |
+
fig = plt.figure()
|
72 |
+
plt.rcParams.update({'font.size': 10})
|
73 |
+
plt.rc('xtick', labelsize=5)
|
74 |
+
|
75 |
+
print(target, sex, cp, age, bp, ch)
|
76 |
+
|
77 |
+
data = df[((df['target'] == 1) & df['sex'] == sex) & (df['cp'] == cp) & (df['age'] >= age) & (df['trestbps'] >= bp) & (df['chol'] >= ch) ]
|
78 |
+
|
79 |
+
if (data.empty):
|
80 |
+
return fig
|
81 |
+
|
82 |
+
if (target == "Age"):
|
83 |
+
|
84 |
+
plt.title("Count of age of diseased people")
|
85 |
+
plt.xlabel("Age")
|
86 |
+
sns.countplot(x='age',data=data);
|
87 |
+
return fig
|
88 |
+
|
89 |
+
elif (target == "Sex"):
|
90 |
+
|
91 |
+
plt.title("Count of sex of diseased people")
|
92 |
+
plt.xlabel("Sex")
|
93 |
+
sns.countplot(x='sex',data=data);
|
94 |
+
return fig
|
95 |
+
|
96 |
+
if (target == "Chest Pain"):
|
97 |
+
|
98 |
+
plt.title("Count of diseased people with cheast pain")
|
99 |
+
plt.xlabel("Chest Pain")
|
100 |
+
sns.countplot(x='cp',data=data);
|
101 |
+
return fig
|
102 |
+
|
103 |
+
if (target == "ECG"):
|
104 |
+
|
105 |
+
plt.title("Count of people with low glucose")
|
106 |
+
plt.xlabel("ECG")
|
107 |
+
sns.countplot(x='restecg',data=data);
|
108 |
+
return fig
|
109 |
+
|
110 |
+
if (target == "Blood Pressure"):
|
111 |
+
|
112 |
+
plt.title("Count of diseased people with high BP")
|
113 |
+
plt.xlabel("BP")
|
114 |
+
sns.countplot(x='trestbps',data=data);
|
115 |
+
return fig
|
116 |
+
|
117 |
+
def prd(model, age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal):
|
118 |
+
|
119 |
+
if model == "Random Forest":
|
120 |
+
|
121 |
+
filename = '/content/randomforest.pkl'
|
122 |
+
X_test = np.array([[age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal],[52, 1, 0, 125, 212, 0, 1, 168, 0, 1.0, 2, 2, 3]])
|
123 |
+
loaded_model = pickle.load(open(filename, 'rb'))
|
124 |
+
result = loaded_model.predict(X_test)[0]
|
125 |
+
|
126 |
+
else:
|
127 |
+
filename = '/content/scaling.pkl'
|
128 |
+
X_test = np.array([[age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal],[52, 1, 0, 125, 212, 0, 1, 168, 0, 1.0, 2, 2, 3]])
|
129 |
+
loaded_model = pickle.load(open(filename, 'rb'))
|
130 |
+
result = loaded_model.predict(X_test)[0]
|
131 |
+
|
132 |
+
|
133 |
+
return "π, You may a Heart Disease" if (result == 1) else "π, You are Healthy!!!"
|
134 |
+
|
135 |
+
inputs = gr.Dropdown(["Age", "Sex", "Chest Pain", "ECG", "Blood Pressure"], label="Input Feature")
|
136 |
+
|
137 |
+
outputs = gr.Plot()
|
138 |
+
|
139 |
+
visualisation = gr.Interface(
|
140 |
+
fn=outbreak,
|
141 |
+
inputs=inputs,
|
142 |
+
outputs=outputs,
|
143 |
+
|
144 |
+
)
|
145 |
+
|
146 |
+
vis = gr.Interface(
|
147 |
+
inputs = [
|
148 |
+
gr.Radio(["Age", "Sex", "Chest Pain", "ECG", "Blood Pressure"], label = "Target Feature"),
|
149 |
+
gr.Radio([1, 0], label = "Sex"),
|
150 |
+
gr.Radio([0,1,2,3], label = "Chest Pain"),
|
151 |
+
gr.Slider(25, 80, value=50, step = 1, label = "Age"),
|
152 |
+
gr.Slider(94, 200, value=150, step = 1, label = "Blood Pressure"),
|
153 |
+
gr.Slider(126, 564, value=130, step = 1, label = "Cholestrol")
|
154 |
+
|
155 |
+
],
|
156 |
+
|
157 |
+
fn=op,
|
158 |
+
outputs = gr.Plot(),
|
159 |
+
|
160 |
+
|
161 |
+
examples=[
|
162 |
+
["Age", 1, 2, 50, 100, 222],
|
163 |
+
["Sex", 0, 1, 30, 150, 322],
|
164 |
+
["Chest Pain", 1, 0, 40, 120, 422],
|
165 |
+
["ECG", 1, 3, 70, 98, 522],
|
166 |
+
["Blood Pressure", 0, 1, 28, 170, 262],
|
167 |
+
]
|
168 |
+
)
|
169 |
+
|
170 |
+
pred = gr.Interface(
|
171 |
+
inputs = [gr.Radio(["Random Forest", "Scaler"], label = "Model"),
|
172 |
+
"number",
|
173 |
+
gr.Radio([0, 1], label = "Sex"),
|
174 |
+
gr.Radio([0,1,2,3], label = "Chest Pain"),
|
175 |
+
gr.Slider(94, 200, value=150, step = 1, label = "Blood Pressure"),
|
176 |
+
gr.Slider(126, 564, value=130, step = 1, label = "Cholestrol"),
|
177 |
+
gr.Radio([0, 1], label = "FBS"),
|
178 |
+
gr.Radio([0, 1, 2], label = "RestECG"),
|
179 |
+
gr.Slider(71, 202, value=50, step = 1, label = "Thalach"),
|
180 |
+
gr.Radio([0, 1], label = "exang"),
|
181 |
+
gr.Slider(0, 6.2, value=3, label = "OldPeak"),
|
182 |
+
gr.Radio([0, 1, 2], label = "Slope"),
|
183 |
+
gr.Slider(0, 4, value=3, step = 1, label = "CA"),
|
184 |
+
gr.Slider(0, 3, value=50, step = 1, label = "Thal"),
|
185 |
+
|
186 |
+
|
187 |
+
|
188 |
+
],
|
189 |
+
|
190 |
+
fn=prd,
|
191 |
+
outputs = "text",
|
192 |
+
|
193 |
+
|
194 |
+
examples=[
|
195 |
+
["Random Forest", 52, 1, 0, 125, 212, 0, 1, 168, 0, 1.0, 2, 2, 3],
|
196 |
+
["Scaler", 62, 0, 0, 138, 294, 1, 1, 106, 0, 1.9, 1, 3, 2],
|
197 |
+
["Random Forest", 44, 0, 2, 108, 141, 0, 1, 175, 0, 0.6, 1, 0, 2],
|
198 |
+
["Scaler", 68, 0, 2, 120, 211, 0, 0, 115, 0, 1.5, 1, 0, 2]
|
199 |
+
]
|
200 |
+
)
|
201 |
+
|
202 |
+
interface = gr.TabbedInterface([visualisation, vis, pred], ["Visualisation", "Real Time Analysis", "Predictions"])
|
203 |
+
|
204 |
+
interface.launch(inline = False)
|
205 |
+
|