-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
182 lines (148 loc) · 4.92 KB
/
Copy pathserver.py
File metadata and controls
182 lines (148 loc) · 4.92 KB
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
from flask import Flask
from flask import request
from joblib import dump, load
import json
import os
app = Flask(__name__)
model = load('life_expectancy_model.joblib')
features = ['Status', 'infant_deaths', 'percentage_expenditure', 'Hepatitis_B', \
'Measles', 'BMI', 'Polio', 'HIV/AIDS', 'Population', \
'thinness_1-19_years']
@app.route('/')
def index():
html = '''
<!DOCTYPE html>
<html>
<head>
<title>Eran's website</title>
</head>
<body>
<h2>
Welcome to life expectancy prediction model<br>
<a href="https://www.kaggle.com/kumarajarshi/life-expectancy-who">Kaggle's dataset</a>
</h2>
<form action="/predict_single_ui">
'''
html += get_features_table()
html += '''
<input type="submit" value="Submit">
</form>
<p>Click the "Submit" button when finished.</p>
<a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ">Premium content</a>
</body>
</html>
'''
return html
@app.route("/predict_single")
def predict_single():
dict_for_prediction = request.args.to_dict(flat=False)
if len(dict_for_prediction) == len(features):
return 'The prediction for ' + str(dict_for_prediction) + ' is ' + str(get_prediction(dict_for_prediction))
return 'Please go back and check you put values for all of the features'
@app.route("/predict_single_ui")
def predict_single_ui():
dict_for_prediction = request.args.to_dict(flat=False)
if len(dict_for_prediction) == len(features) and not [''] in list(dict_for_prediction.values()):
html = '''
<!DOCTYPE html>
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<h2>Life expectancy prediction table</h2>
<table>
<tr>
'''
for feature in features:
html += '<th>'
html += feature
html += '</th>'
html += '<th>prediction</th>'
html += '</tr><tr>'
for item in dict_for_prediction.items():
html += '<td>'
html += str(item[1][0])
html += '</td>'
html += '<td>'
html += str(round(get_prediction(dict_for_prediction)[0], 2))
html += '</td>'
html += '''
</tr></table>
</body>
</html>
'''
return html
# return 'The prediction for ' + str(dict_for_prediction) + ' is ' + str(get_prediction(dict_for_prediction))
return 'Please go back and check you put values for all of the features'
@app.route("/json", methods=["POST"])
def multiple_predictions():
# Validate the request body contains JSON
if request.is_json:
req = request.get_json()
predictions = get_predictions_json(req)
pred_list = []
for pred in predictions:
pred_list.append(pred[0])
json_pred = json.dumps(pred_list)
return json_pred, 200
else:
return "Request was not JSON", 400
def get_predictions_json(req):
predictions = []
for r in req:
predictions.append(get_prediction(r))
return predictions
def get_features_table():
html = '''
<table style="width:100%">
<tr>
'''
for feature in features:
html += '<th><label for="'
html += feature
html += '">'
if feature == 'Statues':
html += 'Status (1 - Developed; 0 - Developing)'
else:
html += feature
html += '</label></th>'
html += '</tr><tr>'
for feature in features:
html += '<th><input type="text" id="'
html += feature
html += '" name="'
html += feature
html += '" ></th>'
html += '</tr></table><br>'
return html
def get_prediction(dict_for_prediction):
list_to_predict = []
for k, v in dict_for_prediction.items():
if isinstance(v, list):
list_to_predict.append(float(v[0]))
else:
list_to_predict.append(float(v))
return model.predict([list_to_predict])
def main():
app.run()
if __name__ == '__main__':
port = os.environ.get('PORT')
if port:
app.run(host='0.0.0.0', port=int(port))
else:
app.run()