def __init__( self, name: str, user_func: Callable[[Any], Any], protocol: Protocol = None, inputs: ModelDataType = None, outputs: ModelDataType = None, ): self._name = name self._user_func = user_func self.protocol = protocol input_args = [] output_args = [] if inputs is None and outputs is None: if user_func is not None: hints = get_type_hints(user_func) for k, v in hints.items(): if k == "return": if isinstance(v, typing._GenericAlias): targs = v.__args__ for targ in targs: output_args.append(ModelDataArg(ty=targ)) else: output_args.append(ModelDataArg(ty=v)) else: input_args.append(ModelDataArg(name=k, ty=v)) else: if type(outputs) == Dict: for k, v in outputs.items(): output_args.append(ModelDataArg(name=k, ty=v)) elif type(outputs) == Tuple: for ty in list(outputs): output_args.append(ModelDataArg(ty=ty)) else: output_args.append(ModelDataArg(ty=outputs)) if type(inputs) == Dict: for k, v in inputs.items(): input_args.append(ModelDataArg(name=k, ty=v)) elif type(inputs) == Tuple: for ty in list(inputs): input_args.append(ModelDataArg(ty=ty)) else: input_args.append(ModelDataArg(ty=inputs)) self.inputs: ModelDataArgs = ModelDataArgs(args=input_args) self.outputs: ModelDataArgs = ModelDataArgs(args=output_args) self.cls = None
def test_v2_from_protocol_response(): res = { "outputs": [{ "name": "a", "data": [97, 98, 99], "datatype": "BYTES" }] } modelTyArgs = ModelDataArgs(args=[ModelDataArg(ty=str, name=None)]) v2 = V2Protocol() res = v2.from_protocol_response(res, modelTyArgs)
def test_model_data_arg(): m = ModelDataArg(ty=str) s = m.json() j = json.loads(s) assert j["ty"] == "builtins.str" m = ModelDataArg(ty=np.ndarray) s = m.json() j = json.loads(s) assert j["ty"] == "numpy.ndarray" m = ModelDataArg(**j)
def _get_args( self, inputs: ModelDataType = None, outputs: ModelDataType = None ) -> Tuple[ModelDataArgs, ModelDataArgs]: input_args = [] output_args = [] if inputs is None and outputs is None: if self._user_func is not None: hints = get_type_hints(self._user_func) for k, v in hints.items(): if k == "return": if hasattr(v, "__args__"): # NOTE: If `__args__` are present, assume this as a # `typing.Generic`, like `Tuple` targs = v.__args__ for targ in targs: output_args.append(ModelDataArg(ty=targ)) else: output_args.append(ModelDataArg(ty=v)) else: input_args.append(ModelDataArg(name=k, ty=v)) else: if isinstance(outputs, dict): for k, v in outputs.items(): output_args.append(ModelDataArg(name=k, ty=v)) elif isinstance(outputs, tuple): for ty in list(outputs): output_args.append(ModelDataArg(ty=ty)) else: output_args.append(ModelDataArg(ty=outputs)) if isinstance(inputs, dict): for k, v in inputs.items(): input_args.append(ModelDataArg(name=k, ty=v)) elif isinstance(inputs, tuple): for ty in list(inputs): input_args.append(ModelDataArg(ty=ty)) else: input_args.append(ModelDataArg(ty=inputs)) return ModelDataArgs(args=input_args), ModelDataArgs(args=output_args)
def _get_args(self, inputs: ModelDataType = None, outputs: ModelDataType = None ) -> Tuple[ModelDataArgs, ModelDataArgs]: input_args = [] output_args = [] if inputs is None and outputs is None: if self._user_func is not None: hints = get_type_hints(self._user_func) for k, v in hints.items(): if k == "return": if isinstance(v, typing._GenericAlias): targs = v.__args__ for targ in targs: output_args.append(ModelDataArg(ty=targ)) else: output_args.append(ModelDataArg(ty=v)) else: input_args.append(ModelDataArg(name=k, ty=v)) else: if type(outputs) == Dict: for k, v in outputs.items(): output_args.append(ModelDataArg(name=k, ty=v)) elif type(outputs) == Tuple: for ty in list(outputs): output_args.append(ModelDataArg(ty=ty)) else: output_args.append(ModelDataArg(ty=outputs)) if type(inputs) == Dict: for k, v in inputs.items(): input_args.append(ModelDataArg(name=k, ty=v)) elif type(inputs) == Tuple: for ty in list(inputs): input_args.append(ModelDataArg(ty=ty)) else: input_args.append(ModelDataArg(ty=inputs)) return ModelDataArgs(args=input_args), ModelDataArgs(args=output_args)
def test_model_spec(): ms = ModelSpec( model_details=ModelDetails( name="test", local_folder="", uri="", platform=ModelFramework.XGBoost, inputs=ModelDataArgs(args=[ModelDataArg(ty=str)]), outputs=ModelDataArgs(args=[]), ), protocol=V2Protocol(), runtime_options=KFServingOptions().local_options, ) s = ms.json() j = json.loads(s) ms2 = ModelSpec(**j) assert isinstance(ms2.protocol, V2Protocol) assert ms2.model_details.inputs.args[0].ty == str
def test_model_data_args(): args = ModelDataArgs(args=[ModelDataArg(ty=str)]) s = args.json() j = json.loads(s) assert j["args"][0]["ty"] == "builtins.str"