-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyModel.py
69 lines (60 loc) · 2.64 KB
/
MyModel.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
import os
from typing import Dict, Iterable
import joblib
import numpy as np
from google.protobuf import json_format
from seldon_core.proto import prediction_pb2
from seldon_core.utils import array_to_grpc_datadef, array_to_rest_datadef, get_data_from_proto
class MyModel:
"""
Model template. You can load your model parameters in __init__ from a location accessible at runtime
This is a Layer 2 file (ie. modelset-specific, but parametrizable by model (by setting MODEL_GCS_LOCATION, or something similar))
"""
def __init__(self) -> None:
"""
This is a method for any initialization that happens only once, for example downloading a model binary, etc
"""
pass
def load(self) -> None:
"""
This is a method for any worker-level initialization, like loading the model
"""
self.model = joblib.load(f"/app/lgb.pkl")
def predict_raw(self, request, feature_names = None) -> np.ndarray:
"""
Return a prediction.
Parameters
----------
request: array-like
feature_names : array of feature names (optional)
"""
if isinstance(request, dict) and "data" in request:
output = self.model.predict(request["data"]["ndarray"], predict_disable_shape_check=True)
return {"data": array_to_rest_datadef("ndarray", output)}
elif isinstance(request, prediction_pb2.SeldonMessage):
data = get_data_from_proto(request)
output = self.model.predict(data, predict_disable_shape_check=True)
return prediction_pb2.SeldonMessage(data=array_to_grpc_datadef("ndarray", output))
else:
raise RuntimeError("Received unexpected request format.")
def handle_request_grpc(
self, request: prediction_pb2.SeldonMessage
) -> prediction_pb2.SeldonMessage:
data = json_format.MessageToDict(request.jsonData)
embedding = data["embedding"]
num_neighbors = int(data["num_neighbors"])
distances, indices = self.loaded_index.search(
np.array([np.array(embedding).astype(np.float32)]), num_neighbors
)
return prediction_pb2.SeldonMessage(
data=array_to_grpc_datadef("ndarray", indices[0])
)
def metadata(self) -> Dict:
return {
"name": "my-model-name",
"versions": ["my-model-version-01"],
"platform": "seldon",
"inputs": [{"name": "input", "datatype": "INT64", "shape": [1, 2]}],
"outputs": [{"name": "output", "datatype": "INT64", "shape": [1, 2]}],
"custom": {"model": os.environ.get("MODEL_GCS_LOCATION")},
}