diff --git a/components/alibi-detect-server/adserver/ad_model.py b/components/alibi-detect-server/adserver/ad_model.py index 0a92370f65..53d3cc8019 100644 --- a/components/alibi-detect-server/adserver/ad_model.py +++ b/components/alibi-detect-server/adserver/ad_model.py @@ -4,6 +4,7 @@ import numpy as np from adserver.constants import HEADER_RETURN_INSTANCE_SCORE from .numpy_encoder import NumpyEncoder +from adserver.protocols.util import read_inputs_as_numpy from alibi_detect.utils.saving import load_detector, Data from adserver.base import CEModel, ModelResponse from adserver.base.storage import download_model @@ -60,7 +61,7 @@ def process_event(self, inputs: Union[List, Dict], headers: Dict) -> ModelRespon logging.info(str(headers)) logging.info("----") try: - X = np.array(inputs) + X = read_inputs_as_numpy(inputs) except Exception as e: raise Exception( "Failed to initialize NumPy array from inputs: %s, %s" % (e, inputs) diff --git a/components/alibi-detect-server/adserver/od_model.py b/components/alibi-detect-server/adserver/od_model.py index 56e66262c0..3c1b09f6f5 100644 --- a/components/alibi-detect-server/adserver/od_model.py +++ b/components/alibi-detect-server/adserver/od_model.py @@ -4,6 +4,7 @@ import logging import numpy as np from .numpy_encoder import NumpyEncoder +from adserver.protocols.util import read_inputs_as_numpy from adserver.base import CEModel, ModelResponse from alibi_detect.utils.saving import load_detector, Data from adserver.base.storage import download_model @@ -85,7 +86,7 @@ def process_event(self, inputs: Union[List, Dict], headers: Dict) -> Optional[Mo logging.info(str(headers)) logging.info("----") try: - X = np.array(inputs) + X = read_inputs_as_numpy(inputs) except Exception as e: raise Exception( "Failed to initialize NumPy array from inputs: %s, %s" % (e, inputs) diff --git a/components/alibi-detect-server/adserver/protocols/util.py b/components/alibi-detect-server/adserver/protocols/util.py index 22c89923b1..4e5fa5421e 100644 --- a/components/alibi-detect-server/adserver/protocols/util.py +++ b/components/alibi-detect-server/adserver/protocols/util.py @@ -1,5 +1,5 @@ import json - +from typing import List, Dict, Union import numpy as np @@ -35,3 +35,17 @@ def default(self, obj): # pylint: disable=arguments-differ,method-hidden elif isinstance(obj, (np.ndarray,)): return obj.tolist() return json.JSONEncoder.default(self, obj) + + +def read_inputs_as_numpy(inputs: Union[List, Dict]) -> np.ndarray: + """ + Read payload inputs as np.ndarray enforcing float32/int32 dtypes. + See: https://github.com/SeldonIO/seldon-core/issues/3940 for details. + """ + x = np.array(inputs) + if x.dtype == np.float64: + return x.astype(np.float32) + elif x.dtype == np.int64: + return x.astype(np.int32) + else: + return x