1
|
|
|
import pickle |
2
|
|
|
import os |
3
|
|
|
from flask_restful import Resource, reqparse |
|
|
|
|
4
|
|
|
from flask import url_for, current_app |
|
|
|
|
5
|
|
|
import cf_predict |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
def get_db(): |
9
|
|
|
"""Fetch Redis client.""" |
10
|
|
|
return current_app.extensions["redis"] |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class Catalogue(Resource): |
|
|
|
|
14
|
|
|
def get(self): |
15
|
|
|
"""Show a catalogue of available endpoints.""" |
16
|
|
|
return { |
17
|
|
|
"predict_url": url_for("api.predict", |
18
|
|
|
_external=True), |
19
|
|
|
"model_version_url": url_for("api.model", |
20
|
|
|
_external=True), |
21
|
|
|
"api_version": cf_predict.__version__ |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
class Model(Resource): |
26
|
|
|
def __init__(self): |
27
|
|
|
self.r = get_db() |
28
|
|
|
self.version = os.getenv("MODEL_VERSION") or "latest" |
29
|
|
|
if self.version == "latest": |
30
|
|
|
self.version = self.find_latest_version(self.version) |
31
|
|
|
try: |
32
|
|
|
self.model = self.load_model(self.version) |
33
|
|
|
except TypeError: |
34
|
|
|
current_app.logger.warning("No model found in Redis") |
35
|
|
|
|
36
|
|
|
def find_latest_version(self, version): |
37
|
|
|
"""Find model with the highest version number in Redis.""" |
38
|
|
|
latest_version = max(self.r.keys()) |
39
|
|
|
return latest_version |
40
|
|
|
|
41
|
|
|
def load_model(self, version): |
42
|
|
|
"""Deserialize and load model.""" |
43
|
|
|
return pickle.loads(self.r.get(version)) |
44
|
|
|
|
45
|
|
|
def get(self): |
46
|
|
|
"""Get current model version.""" |
47
|
|
|
return {"model_version": self.version} |
48
|
|
|
|
49
|
|
|
def put(self): |
50
|
|
|
"""Load a specific model version into memory from Redis. |
51
|
|
|
|
52
|
|
|
Either specifiy the model version or 'latest'. |
53
|
|
|
""" |
54
|
|
|
parser = reqparse.RequestParser() |
55
|
|
|
parser.add_argument("version", type=str, required=True) |
56
|
|
|
args = parser.parse_args() |
57
|
|
|
version = args["version"] |
58
|
|
|
if version == "latest": |
59
|
|
|
self.version = self.find_latest_version(version) |
60
|
|
|
else: |
61
|
|
|
self.version = args["version"] |
62
|
|
|
self.model = self.load_model(self.version) |
63
|
|
|
return {"model_version": self.version} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
class Predict(Resource): |
|
|
|
|
67
|
|
|
def get(self): |
68
|
|
|
"""Get prediction from model. |
69
|
|
|
|
70
|
|
|
Input: Feature array |
71
|
|
|
""" |
72
|
|
|
parser = reqparse.RequestParser() |
73
|
|
|
parser.add_argument("features", type=str, required=True) |
74
|
|
|
args = parser.parse_args() |
75
|
|
|
return args |
76
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.