-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
191 lines (177 loc) · 7.13 KB
/
main.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
from wsgiref import simple_server
from flask import Flask, request, render_template
from flask import Response
import flask_monitoringdashboard as dashboard
import pandas as pd
import os
from flask_cors import CORS, cross_origin
from apps.training.train_model import TrainModel
from apps.prediction.predict_model import PredictModel
from apps.core.config import Config
app = Flask(__name__)
dashboard.bind(app)
CORS(app)
@app.route('/', methods=['POST','GET'])
def index_page():
"""
* method: index_page
* description: method to call index html page
* return: index.html
*
* who when version change (include bug# if apply)
* ---------- ----------- ------- ------------------------------
* shubhankar 05-JULY-2023 1.0 initial creation
*
* Parameters
* None
"""
return render_template('index.html')
@app.route('/training', methods=['POST'])
@cross_origin()
def training_route_client():
"""
* method: training_route_client
* description: method to call training route
* return: none
*
* who when version change (include bug# if apply)
* ---------- ----------- ------- ------------------------------
* shubhankar 05-JULY-2023 1.0 initial creation
*
* Parameters
* None
"""
try:
config = Config()
#get run id
run_id = config.get_run_id()
data_path = config.training_data_path
#trainmodel object initialization
trainModel=TrainModel(run_id,data_path)
#training the model
trainModel.training_model()
return Response("Training successfull! and its RunID is : "+str(run_id))
except ValueError:
return Response("Error Occurred! %s" % ValueError)
except KeyError:
return Response("Error Occurred! %s" % KeyError)
except Exception as e:
return Response("Error Occurred! %s" % e)
@app.route('/batchprediction', methods=['POST'])
@cross_origin()
def batch_prediction_route_client():
"""
* method: batch_prediction_route_client
* description: method to call batch prediction route
* return: none
*
* who when version change (include bug# if apply)
* ---------- ----------- ------- ------------------------------
* shubhankar 05-JULY-2023 1.0 initial creation
*
* Parameters
* None
"""
try:
config = Config()
#get run id
run_id = config.get_run_id()
data_path = config.prediction_data_path
#prediction object initialization
predictModel=PredictModel(run_id, data_path)
#prediction the model
predictModel.batch_predict_from_model()
return Response("Prediction successfull! and its RunID is : "+str(run_id))
except ValueError:
return Response("Error Occurred! %s" % ValueError)
except KeyError:
return Response("Error Occurred! %s" % KeyError)
except Exception as e:
return Response("Error Occurred! %s" % e)
@app.route('/prediction', methods=['POST'])
@cross_origin()
def single_prediction_route_client():
"""
* method: prediction_route_client
* description: method to call prediction route
* return: none
*
* who when version change (include bug# if apply)
* ---------- ----------- ------- ------------------------------
* shubhankar 05-JULY-2023 1.0 initial creation
*
* Parameters
* None
"""
try:
config = Config()
#get run id
run_id = config.get_run_id()
data_path = config.prediction_data_path
print('Test')
if request.method == 'POST':
high_bp = request.form['high_bp']
high_chol = request.form['high_chol']
chol_check = request.form['chol_check']
bmi = request.form['bmi']
smoker = request.form['smoker']
stroke = request.form['stroke']
heart_disease_or_attack = request.form['heart_disease_or_attack']
phys_activity = request.form['phys_activity']
fruits = request.form['fruits']
veggies = request.form['veggies']
hvy_alcohol_consump = request.form['hvy_alcohol_consump']
any_health_care = request.form['any_health_care']
no_doc_bc_cost = request.form['no_doc_bc_cost']
gen_hlth = request.form['gen_hlth']
ment_hlth = request.form['ment_hlth']
phys_hlth = request.form['phys_hlth']
diff_walk = request.form['diff_walk']
sex = request.form['sex']
age = request.form['age']
education = request.form['education']
income = request.form['income']
data = pd.DataFrame(data=[[0, high_bp, high_chol,chol_check,bmi,smoker,stroke,heart_disease_or_attack,phys_activity,fruits,veggies,hvy_alcohol_consump,any_health_care,no_doc_bc_cost,gen_hlth,ment_hlth,phys_hlth,diff_walk,sex,age,education,income]],
columns=['id','high_bp','high_chol','chol_check','bmi','smoker','stroke','heart_disease_or_attack','phys_activity','fruits','veggies','hvy_alcohol_consump','any_health_care','no_doc_bc_cost','gen_hlth','ment_hlth','phys_hlth','diff_walk','sex','age','education','income'])
# using dictionary to convert specific columns
convert_dict = {"id" : int,
"high_bp" : float,
"high_chol" : float,
"chol_check" : float,
"bmi" : float,
"smoker" : float,
"stroke" : float,
"heart_disease_or_attack" : float,
"phys_activity" : float,
"fruits" : float,
"veggies" : float,
"hvy_alcohol_consump" : float,
"any_health_care" : float,
"no_doc_bc_cost" : float,
"gen_hlth" : float,
"ment_hlth" : float,
"phys_hlth" : float,
"diff_walk" : float,
"sex" : float,
"age" : float,
"education" : float,
"income" : float}
data = data.astype(convert_dict)
# object initialization
predictModel = PredictModel(run_id, data_path)
# prediction the model
output = predictModel.single_predict_from_model(data)
print('output : '+str(output))
return Response("Predicted Output is : "+str(output))
except ValueError:
return Response("Error Occurred! %s" % ValueError)
except KeyError:
return Response("Error Occurred! %s" % KeyError)
except Exception as e:
return Response("Error Occurred! %s" % e)
if __name__ == "__main__":
#app.run()
host = '0.0.0.0'
port = 5000
httpd = simple_server.make_server(host, port, app)
httpd.serve_forever()