-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIrisPredictorResource.py
81 lines (67 loc) · 2.28 KB
/
IrisPredictorResource.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
import pickle
import sklearn
from sklearn.neighbors import KNeighborsClassifier
import json
import numpy as np
import falcon
def predict_knn(features, model):
"""
This function gets the features and models and predicts the output
Args:
features(list): list of features. It must include 4 floating numbers
model(sklearn model): knn sklearn model, loaded from models folder
output:
prediceted_class(str)
"""
classes = ['setosa', 'versicolor', 'virginica']
"""
TODO:
predict the class!
"""
return predicted_class
### resource
class IrisPredictorResource():
"""
TODO: Documentation
"""
def __init__(self, model_path, logger):
"""
TODO: Documentation
"""
self.logger = logger
self.model = pickle.load(open(model_path, 'rb'))
self.logger.info("Starting: IrisPredictor")
def on_post(self, req, resp):
"""
TODO: Documentation
"""
try:
self.logger.info("IrisPredictor: reading file")
request_bytes = req.stream.read()
try:
request = json.loads(request_bytes.decode("utf-8"))
except Exception as e:
self.logger.error(e, exc_info=True)
resp.status = falcon.HTTP_400
resp.body = "Invalid JSON\n"
return
"""
@TODO:
check the quality of the input file.
In case the quality of the input is not valid,
send back the correct resp.body and resp.status
"""
## In this part, you consider the input is correct and
## just need to return the result
prediction = predict_knn(features, self.model)
self.logger.info('IrisPredictor: the prediction is %s' % prediction)
response = {"predicted_class": prediction}
self.logger.info('IrisPredictor: Sending the results \n')
"""
TODO: use the correct HTTP status code
"""
resp.status = ## FILL HERE! ##
resp.body = json.dumps(response) + '\n'
except Exception as e:
self.logger.error(e, exc_info=True)
resp.status = falcon.HTTP_500