def predict_grpc(self, request): print("Predict grpc called") default_data_type = request.data.WhichOneof("data_oneof") print(default_data_type) if default_data_type == "tftensor": tfrequest = predict_pb2.PredictRequest() tfrequest.model_spec.name = self.model_name tfrequest.model_spec.signature_name = self.signature_name tfrequest.inputs[self.model_input].CopyFrom(request.data.tftensor) result = self.stub.Predict(tfrequest) print(result) datadef = prediction_pb2.DefaultData( tftensor=result.outputs[self.model_output]) return prediction_pb2.SeldonMessage(data=datadef) else: features = get_data_from_proto(request) datadef = request.data data_type = request.WhichOneof("data_oneof") predictions = self.predict(features, datadef.names) predictions = np.array(predictions) if len(predictions.shape) > 1: class_names = get_class_names(self, predictions.shape[1]) else: class_names = [] if data_type == "data": default_data_type = request.data.WhichOneof("data_oneof") else: default_data_type = "tensor" data = array_to_grpc_datadef(predictions, class_names, default_data_type) return prediction_pb2.SeldonMessage(data=data)
def TransformOutput(self, request, context): if hasattr(self.user_model, "transform_output_grpc"): return self.user_model.transform_output_grpc(request) else: features = get_data_from_proto(request) datadef = request.data data_type = request.WhichOneof("data_oneof") # Construct meta data meta = prediction_pb2.Meta() metaJson = {} tags = get_custom_tags(self.user_model) if tags: metaJson["tags"] = tags metrics = get_custom_metrics(self.user_model) if metrics: metaJson["metrics"] = metrics json_format.ParseDict(metaJson, meta) transformed = transform_output(self.user_model, features, datadef.names) if isinstance(transformed, np.ndarray) or data_type == "data": transformed = np.array(transformed) class_names = get_class_names(self.user_model, datadef.names) if data_type == "data": default_data_type = request.data.WhichOneof("data_oneof") else: default_data_type = "tensor" data = array_to_grpc_datadef(transformed, class_names, default_data_type) return prediction_pb2.SeldonMessage(data=data, meta=meta) else: return prediction_pb2.SeldonMessage(binData=transformed, meta=meta)
def Predict(self, request, context): if hasattr(self.user_model, "predict_grpc"): return self.user_model.predict_grpc(request) else: features = get_data_from_proto(request) meta = get_meta_from_proto(request) datadef = request.data data_type = request.WhichOneof("data_oneof") predictions = predict(self.user_model, features, datadef.names, meta=meta) # Construct meta data meta = prediction_pb2.Meta() metaJson = {} tags = get_custom_tags(self.user_model) if tags: metaJson["tags"] = tags metrics = get_custom_metrics(self.user_model) if metrics: metaJson["metrics"] = metrics json_format.ParseDict(metaJson, meta) if isinstance(predictions, np.ndarray) or data_type == "data": predictions = np.array(predictions) if len(predictions.shape) > 1: class_names = get_class_names(self.user_model, predictions.shape[1]) else: class_names = [] if data_type == "data": default_data_type = request.data.WhichOneof("data_oneof") else: default_data_type = "tensor" data = array_to_grpc_datadef(predictions, class_names, default_data_type) return prediction_pb2.SeldonMessage(data=data, meta=meta) else: return prediction_pb2.SeldonMessage(binData=predictions, meta=meta)
def Aggregate(self, request, context): if hasattr(self.user_model, "aggregate_grpc"): return self.user_model.aggregate_grpc(request) else: features_list = [] names_list = [] for msg in request.seldonMessages: features = get_data_from_proto(msg) features_list.append(features) names_list.append(msg.data.names) data_type = request.seldonMessages[0].WhichOneof("data_oneof") aggregated = aggregate(self.user_model, features_list, names_list) # Construct meta data meta = prediction_pb2.Meta() metaJson = {} tags = get_custom_tags(self.user_model) if tags: metaJson["tags"] = tags metrics = get_custom_metrics(self.user_model) if metrics: metaJson["metrics"] = metrics json_format.ParseDict(metaJson, meta) if isinstance(aggregated, np.ndarray) or data_type == "data": aggregated = np.array(aggregated) feature_names = get_feature_names(self.user_model, []) if data_type == "data": default_data_type = request.seldonMessages[ 0].data.WhichOneof("data_oneof") else: default_data_type = "tensor" data = array_to_grpc_datadef(aggregated, feature_names, default_data_type) return prediction_pb2.SeldonMessage(data=data, meta=meta) else: return prediction_pb2.SeldonMessage(binData=aggregated, meta=meta)