-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
291 lines (225 loc) · 11 KB
/
app.py
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import streamlit as st
import pandas as pd
import tensorflow as tf
from tensorflow import keras
import numpy as np
from PIL import Image
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import pickle
import joblib
@st.cache_resource
def load_models():
try:
# Load the Kidney model
with open('models/RandomForest_Kidney_model.pkl', 'rb') as file:
kidney_model = joblib.load(file)
except Exception as e:
st.error(f"Error loading Kidney model: {e}")
return None, None, None
try:
# Load the Diabetes model
with open('models/Decision_tree_diabetes_model.pkl', 'rb') as file:
diabetes_model = joblib.load(file)
except Exception as e:
st.error(f"Error loading Diabetes model: {e}")
return None, None, None
try:
# Load the Heart Disease model
heart_model = keras.models.load_model('models/heart_disease_model.hdf5')
except Exception as e:
st.error(f"Error loading Heart Disease model: {e}")
return None, None, None
return kidney_model, diabetes_model, heart_model
# Load models
kidney_model, diabetes_model, heart_model = load_models()
# Check if models were loaded successfully
if not all([kidney_model, diabetes_model, heart_model]):
st.stop()
# Streamlit app
def main():
st.title('PredictWell: Chronic Disease Predictor')
# Sidebar navigation
st.sidebar.title('PredictWell')
selected_disease = st.sidebar.radio('Select Disease:', ['Kidney Disease', 'Heart Disease', 'Diabetes'])
healthy = Image.open("./cards/ok.png")
unhealthy = Image.open("./cards/help.png")
if selected_disease == 'Kidney Disease':
st.subheader('Kidney Disease Prediction')
# Add input fields and prediction logic for kidney disease prediction
# Age input
kd_age = st.slider("Age", min_value=0, max_value=100, value=30)
gender = st.selectbox("Gender", ('Male','Female'))
# Blood pressure input
kd_bp = st.slider("Blood Pressure", min_value=50, max_value=180, value=70)
# Albumin input
kd_al = st.slider("Albumin", min_value=0, max_value=5, value=0)
# Sugar input
kd_su = st.slider("Sugar", min_value=0, max_value=5, value=0)
# Bacteria input
ba_options = ['Not Present', 'Present']
k_ba = st.selectbox("Bacteria", ba_options)
if k_ba == 'Not Present':
kd_ba = 0
elif k_ba == 'Present':
kd_ba = 1
# Blood glucose random input
kd_bgr = st.slider("Blood Glucose", min_value=22.0, max_value=500.0, value=145.0)
# Blood urea input
kd_bu = st.slider("Blood Urea", min_value=0, max_value=400, value=56)
# Serum creatinine input
kd_sc = st.slider("Serum Creatinine", min_value=0.0, max_value=16.0, value=2.997)
# Sodium input
kd_sod = st.slider("Sodium", min_value=80, max_value=180, value=135)
# Potassium input
kd_pot = st.slider("Potassium", min_value=0, max_value=60, value=4)
# Hemoglobin input
kd_hemo = st.slider("Hemoglobin", min_value=0.0, max_value=25.0, value=12.5)
# Packed cell volume input
kd_pcv = st.slider("Packed Cell Volume", min_value=0.0, max_value=70.0, value=29.8)
# White blood cell count input
kd_wc = st.slider("White Blood Cell Count", min_value=3000, max_value=40000, value=6500)
# Red blood cell count input
kd_rc = st.slider("Red Blood Cell Count", min_value=0.0, max_value=10.0, value=4.5)
# Hypertension input
htn_options = ['No', 'Yes']
k_htn = st.selectbox("Hypertension", htn_options)
if k_htn == 'No':
kd_htn = 0
elif k_htn == 'Yes':
kd_htn = 1
# Diabetes mellitus input
dm_options = ['No', 'Yes']
k_dm = st.selectbox("Diabetes Mellitus", dm_options)
if k_dm == 'No':
kd_dm = 0
elif k_dm == 'Yes':
kd_dm = 1
#Coronary Artery Disease input
cad_options = ['No', 'Yes']
k_cad = st.selectbox("Coronary Artery Disease", cad_options)
if k_cad == 'No':
kd_cad = 0
elif k_cad == 'Yes':
kd_cad = 1
appet_options = ['Good', 'Poor']
k_appet = st.selectbox('Appetite:', appet_options)
if k_appet == 'Poor':
kd_appet = 0
elif k_appet == 'Good':
kd_appet = 1
def predict_kidney(kd_age,kd_bp,kd_al,kd_su,kd_ba,kd_bgr,kd_bu,kd_sc,kd_sod,kd_pot,kd_hemo,kd_pcv,kd_wc,kd_rc,kd_htn,kd_dm,kd_cad,kd_appet):
input_data = np.array([[kd_age,kd_bp,kd_al,kd_su,kd_ba,kd_bgr,kd_bu,kd_sc,kd_sod,kd_pot,kd_hemo,kd_pcv,kd_wc,kd_rc,kd_htn,kd_dm,kd_cad,kd_appet]])
data_2d = input_data.reshape(1, -1)
kd_prediction = kidney_model.predict(data_2d)
return kd_prediction
if st.button("Predict"):
kd_prediction = predict_kidney(kd_age,kd_bp,kd_al,kd_su,kd_ba,kd_bgr,kd_bu,kd_sc,kd_sod,kd_pot,kd_hemo,kd_pcv,kd_wc,kd_rc,kd_htn,kd_dm,kd_cad,kd_appet)
print(kd_prediction)
if kd_prediction == 0:
st.write("Based on the input data, it is likely that you DO NOT HAVE Kidney Disease.")
st.image(healthy, caption='healthy')
elif kd_prediction == 1:
st.write("Based on the input data, it is you HAVE Kidney Disease.")
st.image(unhealthy, caption='unhealthy')
elif selected_disease == 'Heart Disease':
st.subheader('Heart Disease Prediction')
#Heart diaseases prediction Starts
# Add questions specific to Heart Disease
age = st.slider("Age", min_value=1, max_value=100, value=30)
gender = st.selectbox("Gender", ('Male','Female'))
if gender == 'Male':
sex = 0
elif gender == 'Female':
sex = 1
cp = st.selectbox("Chest Pain Type (CP)", [0, 1, 2, 3])
trestbps = st.slider("Resting Blood Pressure (trestbps)", min_value=90, max_value=200, value=120)
chol = st.slider("Cholesterol (Chol)", min_value=100, max_value=600, value=200)
fbs_value = st.slider("Fasting Blood Sugar (FBS)", min_value=0, max_value=400, value=120)
if fbs_value < 120:
fbs = 0
else:
fbs = 1
restecg = st.selectbox("Resting Electrocardiographic Results (restECG)", [0, 1, 2])
thalach = st.slider("Maximum Heart Rate Achieved (thalach)", min_value=70, max_value=420, value=150)
exang = st.selectbox("Exercise-Induced Angina (exang)", [0, 1])
oldpeak = st.slider("ST Depression (oldpeak)", min_value=0.0, max_value=6.2, value=1.0)
slope = st.selectbox("Slope of the Peak Exercise ST Segment (slope)", [0, 1, 2])
ca = st.selectbox("Number of Major Vessels Colored by Fluoroscopy (Ca)", [0, 1, 2, 3])
thal = st.selectbox("Thallium Stress Test (thal)", [0, 1, 2, 3, 4, 5, 6, 7])
# Add more questions as needed
df = pd.read_csv("Heart_Disease_Prediction.csv")
x = df.iloc[:, :-1] # Select all columns except the last one (features)
y = df.iloc[:, -1] # Select the last column (target variable)
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=0, test_size=0.35)
def predict_heart_disease(age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal):
input_data = np.array([[age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal]])
sc_x = StandardScaler()
sc_x.fit(x_train)
user_input_scaled = sc_x.transform(input_data)
prediction = heart_model.predict(user_input_scaled)
return prediction
if st.button("Predict"):
prediction = predict_heart_disease(age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal)
print(prediction)
if prediction >= 0.5:
st.write("Based on the input data, it is likely that you HAVE heart disease.")
st.image(unhealthy, caption='unhealthy')
else:
st.write("Based on the input data, it is likely that you DO NOT HAVE heart disease.")
st.image(healthy, caption='healthy')
#Heart diaseases prediction Ends
elif selected_disease == 'Diabetes':
st.subheader('Diabetes Prediction')
# Add input fields and prediction logic for diabetes prediction
db_gender = st.selectbox("Gender", ('Male','Female','Other'))
if db_gender == 'Male':
db_sex = 0
elif db_gender == 'Female':
db_sex = 1
elif db_gender == 'Other':
db_sex = 2
db_age = st.slider("Age", min_value=1, max_value=100, value=30)
db_hyper = st.selectbox("Do you have Hypertension?", ('Yes','No'))
if db_hyper == 'No':
db_ht = 0
elif db_hyper == 'Yes':
db_ht = 1
db_heart_diseases = st.selectbox("Do you have any Heart Diseases?", ('Yes','No'))
if db_heart_diseases == 'No':
db_hd = 0
elif db_heart_diseases == 'Yes':
db_hd = 1
db_smoke = st.selectbox("Smoking Habits", ('Daily','Often','Occasionally','Quited','Never','Other'))
if db_smoke == 'Daily':
db_sh = 0
elif db_smoke == 'Often':
db_sh = 1
elif db_smoke == 'Occasionally':
db_sh = 2
elif db_smoke == 'Quited':
db_sh = 3
elif db_smoke == 'Never':
db_sh = 4
elif db_smoke == 'Other':
db_sh = 5
db_bmi = st.slider("BMI(Body Mass Index)", min_value=0, max_value=100, value=24)
db_hgb = st.slider("Hemoglobin A1C (HbA1c) levels(in %)", min_value=0.0, max_value=15.0, value=4.0)
db_fbs = st.slider("Fasting Blood Sugar (FBS)", min_value=0, max_value=400, value=120)
def predict_diabetes(db_sex, db_age, db_ht, db_hd, db_sh, db_bmi, db_hgb, db_fbs):
input_data = np.array([[db_sex, db_age, db_ht, db_hd, db_sh, db_bmi, db_hgb, db_fbs]])
data_2d = input_data.reshape(1, -1)
db_prediction = diabetes_model.predict(data_2d)
return db_prediction
if st.button("Predict"):
db_prediction = predict_diabetes(db_sex, db_age, db_ht, db_hd, db_sh, db_bmi, db_hgb, db_fbs)
print(db_prediction)
if db_prediction == 0:
st.write("Based on the input data, it is likely that you DO NOT HAVE Diabetes.")
st.image(healthy, caption='healthy')
elif db_prediction == 1:
st.write("Based on the input data, it is likely that you HAVE Diabetes.")
st.image(unhealthy, caption='unhealthy')
st.show()
if __name__ == '__main__':
main()