-
Notifications
You must be signed in to change notification settings - Fork 831
/
SKLearnServer.py
66 lines (58 loc) · 2.11 KB
/
SKLearnServer.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
import joblib
import numpy as np
import seldon_core
from seldon_core.user_model import SeldonComponent
from typing import Dict, List, Union, Iterable
import os
import logging
import yaml
logger = logging.getLogger(__name__)
JOBLIB_FILE = "model.joblib"
class SKLearnServer(SeldonComponent):
def __init__(self, model_uri: str = None, method: str = "predict_proba"):
super().__init__()
self.model_uri = model_uri
self.method = method
self.ready = False
logger.info(f"Model uri: {self.model_uri}")
logger.info(f"method: {self.method}")
self.load()
def load(self):
logger.info("load")
model_file = os.path.join(
seldon_core.Storage.download(self.model_uri), JOBLIB_FILE
)
logger.info(f"model file: {model_file}")
self._joblib = joblib.load(model_file)
self.ready = True
def predict(
self, X: np.ndarray, names: Iterable[str], meta: Dict = None
) -> Union[np.ndarray, List, str, bytes]:
try:
if not self.ready:
self.load()
if self.method == "predict_proba":
logger.info("Calling predict_proba")
result = self._joblib.predict_proba(X)
elif self.method == "decision_function":
logger.info("Calling decision_function")
result = self._joblib.decision_function(X)
else:
logger.info("Calling predict")
result = self._joblib.predict(X)
return result
except Exception as ex:
logging.exception("Exception during predict")
def init_metadata(self):
file_path = os.path.join(self.model_uri, "metadata.yaml")
try:
with open(file_path, "r") as f:
return yaml.safe_load(f.read())
except FileNotFoundError:
logger.debug(f"metadata file {file_path} does not exist")
return {}
except yaml.YAMLError:
logger.error(
f"metadata file {file_path} present but does not contain valid yaml"
)
return {}