forked from kritikaparmar-programmer/HealthCheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
161 lines (132 loc) · 6 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
# Importing essential libraries
from flask import Flask, render_template, request
import pickle
import numpy as np
import joblib
app = Flask(__name__)
# Load the Random Forest CLassifier model
filename = 'Models/diabetes-model.pkl'
filename1 = 'Models/cancer-model.pkl'
classifier = pickle.load(open(filename, 'rb'))
rf = pickle.load(open(filename1, 'rb'))
@app.route('/')
def home():
return render_template('index.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/diabetes')
def diabetes():
return render_template('diabetes.html')
@app.route('/predict_diabetes', methods=['POST'])
def predict_diabetes():
if request.method == 'POST':
preg = int(request.form['pregnancies'])
glucose = int(request.form['glucose'])
bp = int(request.form['bloodpressure'])
st = int(request.form['skinthickness'])
insulin = int(request.form['insulin'])
bmi = float(request.form['bmi'])
dpf = float(request.form['dpf'])
age = int(request.form['age'])
data = np.array([[preg, glucose, bp, st, insulin, bmi, dpf, age]])
my_prediction = classifier.predict(data)
return render_template('d_result.html', prediction=my_prediction)
@app.route('/cancer')
def cancer():
return render_template('cancer.html')
@app.route('/predict_cancer', methods=['POST'])
def predict_cancer():
if request.method == 'POST':
rad = float(request.form['Radius_mean'])
tex = float(request.form['Texture_mean'])
par = float(request.form['Perimeter_mean'])
area = float(request.form['Area_mean'])
smooth = float(request.form['Smoothness_mean'])
compact = float(request.form['Compactness_mean'])
con = float(request.form['Concavity_mean'])
concave = float(request.form['concave points_mean'])
sym = float(request.form['symmetry_mean'])
frac = float(request.form['fractal_dimension_mean'])
rad_se = float(request.form['radius_se'])
tex_se = float(request.form['texture_se'])
par_se = float(request.form['perimeter_se'])
area_se = float(request.form['area_se'])
smooth_se = float(request.form['smoothness_se'])
compact_se = float(request.form['compactness_se'])
con_se = float(request.form['concavity_se'])
concave_se = float(request.form['concave points_se'])
sym_se = float(request.form['symmetry_se'])
frac_se = float(request.form['fractal_dimension_se'])
rad_worst = float(request.form['radius_worst'])
tex_worst = float(request.form['texture_worst'])
par_worst = float(request.form['perimeter_worst'])
area_worst = float(request.form['area_worst'])
smooth_worst = float(request.form['smoothness_worst'])
compact_worst = float(request.form['compactness_worst'])
con_worst = float(request.form['concavity_worst'])
concave_worst = float(request.form['concave points_worst'])
sym_worst = float(request.form['symmetry_worst'])
frac_worst = float(request.form['fractal_dimension_worst'])
data = np.array([[rad, tex, par, area, smooth, compact, con, concave, sym, frac, rad_se, tex_se, par_se, area_se, smooth_se, compact_se, con_se, concave_se, sym_se, frac_se, rad_worst, tex_worst, par_worst, area_worst, smooth_worst, compact_worst, con_worst, concave_worst, sym_worst, frac_worst]])
my_prediction = rf.predict(data)
return render_template('c_result.html', prediction=my_prediction)
def ValuePredictor(to_predict_list, size):
loaded_model = joblib.load('models/heart_model')
to_predict = np.array(to_predict_list).reshape(1,size)
result = loaded_model.predict(to_predict)
return result[0]
@app.route('/heart')
def heart():
return render_template('heart.html')
@app.route('/predict_heart', methods=['POST'])
def predict_heart():
if request.method == 'POST':
to_predict_list = request.form.to_dict()
to_predict_list=list(to_predict_list.values())
to_predict_list = list(map(float, to_predict_list))
result = ValuePredictor(to_predict_list,11)
if(int(result)==1):
prediction=1
else:
prediction=0
return render_template('h_result.html', prediction=prediction)
# this function use to predict the output for Fetal Health from given data
def fetal_health_value_predictor(data):
try:
# after get the data from html form then we collect the values and
# converts into 2D numpy array for prediction
data = list(data.values())
data = list(map(float, data))
data = np.array(data).reshape(1,-1)
# load the saved pre-trained model for new prediction
model_path = 'Models/fetal-health-model.pkl'
model = pickle.load(open(model_path, 'rb'))
result = model.predict(data)
result = int(result[0])
status = True
# returns the predicted output value
return (result,status)
except Exception as e:
result = str(e)
status = False
return (result,status)
# this route for prediction of Fetal Health
@app.route('/fetal_health', methods=['GET','POST'])
def fetal_health_prediction():
if request.method == 'POST':
# geting the form data by POST method
data = request.form.to_dict()
# passing form data to castome predict method to get the result
result,status = fetal_health_value_predictor(data)
if status:
# if prediction happens successfully status=True and then pass uotput to html page
return render_template('fetal_health.html', result= result)
else:
# if any error occured during prediction then the error msg will be displayed
return f'<h2>Error : {result}</h2>'
# if the user send a GET request to '/fetal_health' route then we just render the html page
# which contains a form for prediction
return render_template('fetal_health.html', result=None)
if __name__ == '__main__':
app.run(debug=True)