-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
177 lines (142 loc) · 5.05 KB
/
app.py
File metadata and controls
177 lines (142 loc) · 5.05 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
import os
import json
import pickle
import joblib
import pandas as pd
from flask import Flask, jsonify, request
from peewee import (
Model, IntegerField, FloatField,
TextField, IntegrityError
)
from playhouse.shortcuts import model_to_dict
from playhouse.db_url import connect
########################################
# Begin database stuff
# The connect function checks if there is a DATABASE_URL env var.
# If it exists, it uses it to connect to a remote postgres db.
# Otherwise, it connects to a local sqlite db stored in predictions.db.
DB = connect(os.environ.get('DATABASE_URL') or 'sqlite:///predictions.db')
class Prediction(Model):
observation_id = TextField(unique=True)
observation = TextField()
prediction = FloatField()
proba = FloatField()
true_class = IntegerField(null=True)
class Meta:
database = DB
DB.create_tables([Prediction], safe=True)
# End database stuff
########################################
########################################
# Unpickle the previously-trained model
with open('columns.json') as fh:
columns = json.load(fh)
pipeline = joblib.load('pipeline.pickle')
with open('dtypes.pickle', 'rb') as fh:
dtypes = pickle.load(fh)
# End model un-pickling
########################################
########################################
# Begin field verification
def verify_data_types(data):
expected_types = {
"observation_id": [str],
"Type": [str],
"Date": [str],
"Part of a policing operation": [bool],
"Latitude": [float, int],
"Longitude": [float, int],
"Gender": [str],
"Age range": [str],
"Officer-defined ethnicity": [str],
"Legislation": [str],
"Object of search": [str],
"station": [str]
}
selected_columns = [
'Type',
'Gender',
'Age range',
'Officer-defined ethnicity',
'Object of search',
'station',
]
for key in data.keys():
if key not in expected_types.keys():
return True, {'error': f"Unexpected key: {key}"}
for col, expected_type in expected_types.items():
if col not in data:
return (True, {'error': f"{col} column not found"})
actual_type = type(data[col])
if actual_type not in expected_type and col in selected_columns:
return (True, {'error': f"{col} column has wrong data type. Expected {expected_type}, got {actual_type}"})
return (False, "All data types are correct")
# End field verification
########################################
########################################
# Begin webserver
app = Flask(__name__)
@app.route('/should_search/', methods=['POST'])
def predict():
try:
obs_dict = request.get_json()
except:
response = {'error': 'Could not parse the request'}
return jsonify(response), 405
is_error, error_msg = verify_data_types(obs_dict)
if is_error:
response = {'error': error_msg}
return jsonify(response), 405
_id = obs_dict['observation_id']
observation = obs_dict
try:
obs = pd.DataFrame([observation], columns=columns).astype(dtypes)
except Exception as e:
response = {'error': f'error malformed request {e.message}'}
return jsonify(response), 405
except:
response = {'error': f'error malformed request'}
return jsonify(response), 405
# Now get ourselves an actual prediction of the positive class.
proba = pipeline.predict_proba(obs)[0, 1]
prediction = int(proba >= 0.3) # apply the threshold
response = {'outcome': bool(prediction)}
p = Prediction(
observation_id=_id,
proba=proba,
prediction= prediction,
observation=request.data
)
try:
p.save()
except IntegrityError:
error_msg = 'Observation ID: "{}" already exists'.format(_id)
response['error'] = error_msg
print(error_msg)
DB.rollback()
return jsonify(response), 405
return jsonify(response)
@app.route('/search_result/', methods=['POST'])
def update():
obs = request.get_json()
try:
p = Prediction.get(Prediction.observation_id == obs['observation_id'])
p.true_class = obs['outcome']
p.save()
return jsonify(
{
"observation_id": p.observation_id,
"outcome": p.true_class,
"predicted_outcome": bool(p.prediction)
}
)
except Prediction.DoesNotExist:
error_msg = 'Observation ID: "{}" does not exist'.format(obs['observation_id'])
return jsonify({'error': error_msg}), 405
except:
error_msg = 'error malformed request'
return jsonify({'error': error_msg}), 405
# End webserver stuff
########################################
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True, port=5000)