Beispiel #1
0
def test_router_proto_lowlevel_raw_ok():
    user_object = UserObjectLowLevelRaw()
    seldon_metrics = SeldonMetrics()
    app = SeldonModelGRPC(user_object, seldon_metrics)
    arr = np.array([1, 2])
    datadef = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=(2, 1), values=arr))
    request = prediction_pb2.SeldonMessage(data=datadef)
    resp = app.Route(request, None)
    jStr = json_format.MessageToJson(resp)
    j = json.loads(jStr)
    logging.info(j)
    assert j["data"]["tensor"]["shape"] == [1, 1]
    assert j["data"]["tensor"]["values"] == [1]
def test_aggregate_proto_ok():
    user_object = UserObject()
    app = SeldonModelGRPC(user_object)
    arr1 = np.array([1, 2])
    datadef1 = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=(2, 1), values=arr1))
    arr2 = np.array([3, 4])
    datadef2 = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=(2, 1), values=arr2))
    msg1 = prediction_pb2.SeldonMessage(data=datadef1)
    msg2 = prediction_pb2.SeldonMessage(data=datadef2)
    request = prediction_pb2.SeldonMessageList(seldonMessages=[msg1, msg2])
    resp = app.Aggregate(request, None)
    jStr = json_format.MessageToJson(resp)
    j = json.loads(jStr)
    print(j)
    assert j["meta"]["tags"] == {"mytag": 1}
    # add default type
    assert j["meta"]["metrics"][0]["key"] == user_object.metrics()[0]["key"]
    assert j["meta"]["metrics"][0]["value"] == user_object.metrics(
    )[0]["value"]
    assert j["data"]["tensor"]["shape"] == [2, 1]
    assert j["data"]["tensor"]["values"] == [1, 2]
def test_transform_output_proto_lowlevel_ok():
    user_object = UserObjectLowLevelGrpc()
    app = SeldonModelGRPC(user_object)
    arr = np.array([1, 2])
    datadef = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=(2, 1), values=arr)
    )
    request = prediction_pb2.SeldonMessage(data=datadef)
    resp = app.TransformOutput(request, None)
    jStr = json_format.MessageToJson(resp)
    j = json.loads(jStr)
    logging.info(j)
    assert j["data"]["tensor"]["shape"] == [2, 1]
    assert j["data"]["tensor"]["values"] == [9, 9]
Beispiel #4
0
    def transform_input_raw(
        self, request: Union[prediction_pb2.SeldonMessage, List, Dict]
    ) -> Union[prediction_pb2.SeldonMessage, List, Dict]:

        is_proto = isinstance(request, prediction_pb2.SeldonMessage)

        arr = np.array([9, 9])
        datadef = prediction_pb2.DefaultData(
            tensor=prediction_pb2.Tensor(shape=(2, 1), values=arr))
        response = prediction_pb2.SeldonMessage(data=datadef)
        if is_proto:
            return response
        else:
            return seldon_message_to_json(response)
Beispiel #5
0
def array_to_grpc_datadef(
        data_type: str,
        array: np.ndarray,
        names: Optional[Iterable[str]] = []) -> prediction_pb2.DefaultData:
    """
    Convert numpy array and optional column names into a SeldonMessage DefaultData proto

    Parameters
    ----------
    array
       numpy array
    names
       column names
    data_type
       The SeldonMessage type to convert to

    Returns
    -------
       SeldonMessage DefaultData

    """
    if data_type == "tensor":
        datadef = prediction_pb2.DefaultData(
            names=names,
            tensor=prediction_pb2.Tensor(shape=array.shape,
                                         values=array.ravel().tolist()))
    elif data_type == "ndarray":
        datadef = prediction_pb2.DefaultData(
            names=names, ndarray=array_to_list_value(array))
    elif data_type == "tftensor":
        datadef = prediction_pb2.DefaultData(
            names=names, tftensor=tf.make_tensor_proto(array))
    else:
        datadef = prediction_pb2.DefaultData(
            names=names, ndarray=array_to_list_value(array))

    return datadef
    def predict_grpc(self,request):
        """
        predict_grpc will be called only when there is a GRPC call to the server
        which in this case, the request will be sent to the TFServer directly.
        """
        log.debug("Preprocessing contents for predict function")
        request_data_type = request.WhichOneof("data_oneof")
        default_data_type = request.data.WhichOneof("data_oneof")
        log.debug(f"Request data type: {request_data_type}, Default data type: {default_data_type}")

        if request_data_type != "data":
            raise Exception("strData, binData and jsonData not supported.")

        tfrequest = predict_pb2.PredictRequest()
        tfrequest.model_spec.name = self.model_name
        tfrequest.model_spec.signature_name = self.signature_name

        # For GRPC case, if we have a TFTensor message we can pass it directly
        if default_data_type == "tftensor":
            tfrequest.inputs[self.model_input].CopyFrom(request.data.tftensor)
            result = self.stub.Predict(tfrequest)
            datadef = prediction_pb2.DefaultData(
                tftensor=result.outputs[self.model_output]
            )
            return prediction_pb2.SeldonMessage(data=datadef)

        else:
            data_arr = grpc_datadef_to_array(request.data)
            tfrequest.inputs[self.model_input].CopyFrom(
                tf.contrib.util.make_tensor_proto(
                    data_arr.tolist(),
                    shape=data_arr.shape))
            result = self.stub.Predict(tfrequest)
            datadef = prediction_pb2.DefaultData(
                tftensor=result.outputs[self.model_output]
            )
            return prediction_pb2.SeldonMessage(data=datadef)
Beispiel #7
0
def test_aggregate_proto_combines_tags():
    user_object = UserObject()
    app = SeldonModelGRPC(user_object)

    arr1 = np.array([1, 2])
    meta1 = prediction_pb2.Meta()
    json_format.ParseDict({"tags": {"input-1": "yes", "common": 1}}, meta1)
    datadef1 = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=(2, 1), values=arr1))

    arr2 = np.array([3, 4])
    meta2 = prediction_pb2.Meta()
    json_format.ParseDict({"tags": {"input-2": "yes", "common": 2}}, meta2)
    datadef2 = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=(2, 1), values=arr2))

    msg1 = prediction_pb2.SeldonMessage(data=datadef1, meta=meta1)
    msg2 = prediction_pb2.SeldonMessage(data=datadef2, meta=meta2)
    request = prediction_pb2.SeldonMessageList(seldonMessages=[msg1, msg2])
    resp = app.Aggregate(request, None)
    jStr = json_format.MessageToJson(resp)
    j = json.loads(jStr)
    logging.info(j)

    assert j["meta"]["tags"] == {
        "common": 2,
        "input-1": "yes",
        "input-2": "yes",
        "mytag": 1,
    }

    # add default type
    assert j["meta"]["metrics"][0]["key"] == user_object.metrics()[0]["key"]
    assert j["meta"]["metrics"][0]["value"] == user_object.metrics(
    )[0]["value"]
    assert j["data"]["tensor"]["shape"] == [2, 1]
    assert j["data"]["tensor"]["values"] == [1, 2]
def test_model_template_app_grpc(tracing):
    with start_microservice(
        join(dirname(__file__), "model-template-app"), tracing=tracing, grpc=True
    ):
        data = np.array([[1, 2]])
        datadef = prediction_pb2.DefaultData(
            tensor=prediction_pb2.Tensor(shape=data.shape, values=data.flatten())
        )
        request = prediction_pb2.SeldonMessage(data=datadef)
        channel = grpc.insecure_channel("0.0.0.0:5000")
        stub = prediction_pb2_grpc.ModelStub(channel)
        response = stub.Predict(request=request)
        assert response.data.tensor.shape[0] == 1
        assert response.data.tensor.shape[1] == 2
        assert response.data.tensor.values[0] == 1
        assert response.data.tensor.values[1] == 2

        arr = np.array([1, 2])
        datadef = prediction_pb2.DefaultData(
            tensor=prediction_pb2.Tensor(shape=(2, 1), values=arr)
        )
        request = prediction_pb2.SeldonMessage(data=datadef)
        feedback = prediction_pb2.Feedback(request=request, reward=1.0)
        response = stub.SendFeedback(request=request)
def test_proto_requestPath_ok():
    user_object = UserObject()
    seldon_metrics = SeldonMetrics()
    app = SeldonModelGRPC(user_object, seldon_metrics)
    arr = np.array([1, 2])
    datadef = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=(2, 1), values=arr))
    meta = prediction_pb2.Meta()
    json_format.ParseDict({"tags": {"foo": "bar"}}, meta)
    request = prediction_pb2.SeldonMessage(data=datadef, meta=meta)
    resp = app.Predict(request, None)
    jStr = json_format.MessageToJson(resp)
    j = json.loads(jStr)
    logging.info(j)
    assert j["meta"]["requestPath"] == {"my-test-model": "my-test-model-image"}
Beispiel #10
0
def test_proto_tftensor_ok():
    user_object = UserObject()
    app = SeldonModelGRPC(user_object)
    arr = np.array([1, 2])
    datadef = prediction_pb2.DefaultData(tftensor=tf.make_tensor_proto(arr))
    request = prediction_pb2.SeldonMessage(data=datadef)
    resp = app.Predict(request, None)
    jStr = json_format.MessageToJson(resp)
    j = json.loads(jStr)
    print(j)
    assert j["meta"]["tags"] == {"mytag": 1}
    assert j["meta"]["metrics"][0]["key"] == user_object.metrics()[0]["key"]
    assert j["meta"]["metrics"][0]["value"] == user_object.metrics(
    )[0]["value"]
    arr2 = tf.make_ndarray(resp.data.tftensor)
    assert np.array_equal(arr, arr2)
Beispiel #11
0
def test_proto_ok():
    user_object = UserObject()
    app = SeldonModelGRPC(user_object)
    arr = np.array([1, 2])
    datadef = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=(2, 1), values=arr))
    request = prediction_pb2.SeldonMessage(data=datadef)
    resp = app.Predict(request, None)
    jStr = json_format.MessageToJson(resp)
    j = json.loads(jStr)
    print(j)
    assert j["meta"]["tags"] == {"mytag": 1}
    assert j["meta"]["metrics"][0]["key"] == user_object.metrics()[0]["key"]
    assert j["meta"]["metrics"][0]["value"] == user_object.metrics(
    )[0]["value"]
    assert j["data"]["tensor"]["shape"] == [2, 1]
    assert j["data"]["tensor"]["values"] == [1, 2]
Beispiel #12
0
def test_model_template_app_grpc_tags(microservice):
    data = np.array([[1, 2]])
    datadef = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=data.shape, values=data.flatten()))

    meta = prediction_pb2.Meta()
    json_format.ParseDict({"tags": {"foo": "bar"}}, meta)

    request = prediction_pb2.SeldonMessage(data=datadef, meta=meta)
    channel = grpc.insecure_channel("0.0.0.0:5000")
    stub = prediction_pb2_grpc.ModelStub(channel)
    response = retry_method(stub.Predict, kwargs=dict(request=request))
    assert response.data.tensor.shape[0] == 1
    assert response.data.tensor.shape[1] == 2
    assert response.data.tensor.values[0] == 1
    assert response.data.tensor.values[1] == 2

    assert response.meta.tags["foo"].string_value == "bar"
def test_proto_feedback():
    user_object = UserObject()
    app = SeldonModelGRPC(user_object)
    arr = np.array([1, 2])
    datadef = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=(2, 1), values=arr))
    meta = prediction_pb2.Meta()
    metaJson = {}
    routing = {"1": 1}
    metaJson["routing"] = routing
    json_format.ParseDict(metaJson, meta)

    request = prediction_pb2.SeldonMessage(data=datadef)
    response = prediction_pb2.SeldonMessage(meta=meta, data=datadef)
    feedback = prediction_pb2.Feedback(request=request,
                                       response=response,
                                       reward=1.0)
    resp = app.SendFeedback(feedback, None)
def test_proto_passes_through_tags():
    user_object = UserObject()
    app = SeldonModelGRPC(user_object)
    arr = np.array([1, 2])
    datadef = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=(2, 1), values=arr))
    meta = prediction_pb2.Meta()
    json_format.ParseDict({"tags": {"foo": "bar"}}, meta)
    request = prediction_pb2.SeldonMessage(data=datadef, meta=meta)
    resp = app.Predict(request, None)
    jStr = json_format.MessageToJson(resp)
    j = json.loads(jStr)
    logging.info(j)
    assert j["meta"]["tags"] == {"foo": "bar", "mytag": 1}
    assert j["meta"]["metrics"][0]["key"] == user_object.metrics()[0]["key"]
    assert j["meta"]["metrics"][0]["value"] == user_object.metrics(
    )[0]["value"]
    assert j["data"]["tensor"]["shape"] == [2, 1]
    assert j["data"]["tensor"]["values"] == [1, 2]
def grpc_request_api_gateway(oauth_key,oauth_secret,namespace,rest_endpoint="localhost:8002",grpc_endpoint="localhost:8003",data_size=5,rows=1,data=None):
    token = get_token(oauth_key,oauth_secret,namespace,rest_endpoint)
    if data is None:
        shape, arr = create_random_data(data_size,rows)
    else:
        shape = data.shape
        arr = data.flatten()
    datadef = prediction_pb2.DefaultData(
            tensor = prediction_pb2.Tensor(
                shape = shape,
                values = arr
                )
            )
    request = prediction_pb2.SeldonMessage(data = datadef)
    channel = grpc.insecure_channel(grpc_endpoint)
    stub = prediction_pb2_grpc.SeldonStub(channel)
    metadata = [('oauth_token', token)]
    response = stub.Predict(request=request,metadata=metadata)
    return response
def test_transform_output_proto_ok():
    user_object = UserObject()
    seldon_metrics = SeldonMetrics()
    app = SeldonModelGRPC(user_object, seldon_metrics)
    arr = np.array([1, 2])
    datadef = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=(2, 1), values=arr))
    request = prediction_pb2.SeldonMessage(data=datadef)
    resp = app.TransformOutput(request, None)
    jStr = json_format.MessageToJson(resp)
    j = json.loads(jStr)
    logging.info(j)
    assert j["meta"]["tags"] == {"mytag": 1}
    # add default type
    assert j["meta"]["metrics"][0]["key"] == user_object.metrics()[0]["key"]
    assert j["meta"]["metrics"][0]["value"] == user_object.metrics(
    )[0]["value"]
    assert j["data"]["tensor"]["shape"] == [2, 1]
    assert j["data"]["tensor"]["values"] == [1, 2]
Beispiel #17
0
 def transform_input_grpc(self, request):
     logger.info("Transform called")
     start_time = datetime.datetime.now()
     X = np.frombuffer(request.binData, dtype=np.uint8)
     X = cv2.imdecode(X, cv2.IMREAD_COLOR)  # BGR format
     X = self.crop_resize(X, self.size, self.size)
     X = X.astype(self.dtype)
     X = X.transpose(2, 0, 1).reshape(1, 3, self.size, self.size)
     logger.info("Shape: %s; Dtype: %s; Min: %s; Max: %s", X.shape, X.dtype,
                 np.amin(X), np.amax(X))
     jpeg_time = datetime.datetime.now()
     jpeg_duration = (jpeg_time - start_time).total_seconds() * 1000
     logger.info("jpeg preprocessing: %s ms", jpeg_duration)
     datadef = prediction_pb2.DefaultData(names='x',
                                          tftensor=tf.make_tensor_proto(X))
     end_time = datetime.datetime.now()
     duration = (end_time - start_time).total_seconds() * 1000
     logger.info("Total transformation: %s ms", duration)
     request = prediction_pb2.SeldonMessage(data=datadef)
     return request
def grpc_request_ambassador(
    deploymentName, namespace, endpoint="localhost:8004", data_size=5, rows=1, data=None
):
    if data is None:
        shape, arr = create_random_data(data_size, rows)
    else:
        shape = data.shape
        arr = data.flatten()
    datadef = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=shape, values=arr)
    )
    request = prediction_pb2.SeldonMessage(data=datadef)
    channel = grpc.insecure_channel(endpoint)
    stub = prediction_pb2_grpc.SeldonStub(channel)
    if namespace is None:
        metadata = [("seldon", deploymentName)]
    else:
        metadata = [("seldon", deploymentName), ("namespace", namespace)]
    response = stub.Predict(request=request, metadata=metadata)
    return response
Beispiel #19
0
def test_proto_gets_meta():
    user_object = UserObject(ret_meta=True)
    app = SeldonModelGRPC(user_object)
    arr = np.array([1, 2])
    datadef = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=(2, 1), values=arr))
    meta = prediction_pb2.Meta()
    metaJson = {"puid": "abc"}
    json_format.ParseDict(metaJson, meta)
    request = prediction_pb2.SeldonMessage(data=datadef, meta=meta)
    resp = app.Predict(request, None)
    jStr = json_format.MessageToJson(resp)
    j = json.loads(jStr)
    print(j)
    assert j["meta"]["tags"] == {"inc_meta": {"puid": "abc"}}
    assert j["meta"]["metrics"][0]["key"] == user_object.metrics()[0]["key"]
    assert j["meta"]["metrics"][0]["value"] == user_object.metrics(
    )[0]["value"]
    assert j["data"]["tensor"]["shape"] == [2, 1]
    assert j["data"]["tensor"]["values"] == [1, 2]
Beispiel #20
0
def test_model_template_app_grpc_tags(tracing):
    with start_microservice(join(dirname(__file__), "model-template-app"),
                            tracing=tracing,
                            grpc=True):
        data = np.array([[1, 2]])
        datadef = prediction_pb2.DefaultData(tensor=prediction_pb2.Tensor(
            shape=data.shape, values=data.flatten()))

        meta = prediction_pb2.Meta()
        json_format.ParseDict({"tags": {"foo": "bar"}}, meta)

        request = prediction_pb2.SeldonMessage(data=datadef, meta=meta)
        channel = grpc.insecure_channel("0.0.0.0:5000")
        stub = prediction_pb2_grpc.ModelStub(channel)
        response = stub.Predict(request=request)
        assert response.data.tensor.shape[0] == 1
        assert response.data.tensor.shape[1] == 2
        assert response.data.tensor.values[0] == 1
        assert response.data.tensor.values[1] == 2

        assert response.meta.tags["foo"].string_value == "bar"
Beispiel #21
0
def test_model_tftensor_ok():
    user_object = UserObject()
    seldon_metrics = SeldonMetrics()
    app = get_rest_microservice(user_object, seldon_metrics)
    client = app.test_client()
    arr = np.array([1, 2])
    datadef = prediction_pb2.DefaultData(tftensor=tf.make_tensor_proto(arr))
    request = prediction_pb2.SeldonMessage(data=datadef)
    jStr = json_format.MessageToJson(request)
    rv = client.get("/predict?json=" + jStr)
    j = json.loads(rv.data)
    logging.info(j)
    assert rv.status_code == 200
    assert j["meta"]["tags"] == {"mytag": 1}
    assert j["meta"]["metrics"][0]["key"] == user_object.metrics()[0]["key"]
    assert j["meta"]["metrics"][0]["value"] == user_object.metrics()[0]["value"]
    assert "tftensor" in j["data"]
    tfp = TensorProto()
    json_format.ParseDict(j["data"].get("tftensor"), tfp, ignore_unknown_fields=False)
    arr2 = tf.make_ndarray(tfp)
    assert np.array_equal(arr, arr2)
Beispiel #22
0
def test_proto_seldon_metrics_route(cls, client_gets_metrics):
    user_object = cls()
    seldon_metrics = SeldonMetrics()

    app = SeldonModelGRPC(user_object, seldon_metrics)
    datadef = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=(2, 1), values=np.array([1, 2])))

    request = prediction_pb2.SeldonMessage(data=datadef)
    resp = app.Route(request, None)
    assert ("metrics" in json.loads(
        json_format.MessageToJson(resp))["meta"]) == client_gets_metrics

    data = seldon_metrics.data[os.getpid()]
    verify_seldon_metrics(data, 1, [0.0202], ROUTER_METRIC_METHOD_TAG)
    resp = app.Route(request, None)
    assert ("metrics" in json.loads(
        json_format.MessageToJson(resp))["meta"]) == client_gets_metrics

    data = seldon_metrics.data[os.getpid()]
    verify_seldon_metrics(data, 2, [0.0202, 0.0202], ROUTER_METRIC_METHOD_TAG)
def test_proto_seldon_runtime_data_transform_output(cls, client_gets_metrics):
    user_object = cls()
    seldon_metrics = SeldonMetrics()

    app = SeldonModelGRPC(user_object, seldon_metrics)
    datadef = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=(2, 1), values=np.array([1, 2])))

    request = prediction_pb2.SeldonMessage(data=datadef)

    resp = app.TransformOutput(request, None)
    j = json.loads(json_format.MessageToJson(resp))
    assert j["data"] == {
        "names": ["t:0"],
        "tensor": {
            "shape": [2, 1],
            "values": [1.0, 2.0]
        },
    }
    assert j["meta"]["tags"] == EXPECTED_TAGS
    assert ("metrics" in j["meta"]) == client_gets_metrics
    data = seldon_metrics.data[os.getpid()]
    verify_seldon_metrics(data, 1, [0.0202],
                          OUTPUT_TRANSFORM_METRIC_METHOD_TAG)

    resp = app.TransformOutput(request, None)
    j = json.loads(json_format.MessageToJson(resp))
    assert j["data"] == {
        "names": ["t:0"],
        "tensor": {
            "shape": [2, 1],
            "values": [1.0, 2.0]
        },
    }
    assert j["meta"]["tags"] == EXPECTED_TAGS
    assert ("metrics" in j["meta"]) == client_gets_metrics
    data = seldon_metrics.data[os.getpid()]
    verify_seldon_metrics(data, 2, [0.0202, 0.0202],
                          OUTPUT_TRANSFORM_METRIC_METHOD_TAG)
Beispiel #24
0
def test_model_template_app_grpc_metrics(microservice):
    data = np.array([[1, 2]])
    datadef = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=data.shape, values=data.flatten())
    )

    meta = prediction_pb2.Meta()
    json_format.ParseDict(
        {"metrics": [{"key": "mygauge", "type": "GAUGE", "value": 100}]}, meta
    )

    request = prediction_pb2.SeldonMessage(data=datadef, meta=meta)
    channel = grpc.insecure_channel("0.0.0.0:5000")
    stub = prediction_pb2_grpc.ModelStub(channel)
    response = retry_method(stub.Predict, kwargs=dict(request=request))
    assert response.data.tensor.shape[0] == 1
    assert response.data.tensor.shape[1] == 2
    assert response.data.tensor.values[0] == 1
    assert response.data.tensor.values[1] == 2

    assert response.meta.metrics[0].key == "mygauge"
    assert response.meta.metrics[0].value == 100
Beispiel #25
0
    def predict_raw(self, request):
        log.debug("Predict raw")
        request_data_type = request.WhichOneof("data_oneof")
        default_data_type = request.data.WhichOneof("data_oneof")
        log.debug(str(request_data_type), str(default_data_type))
        if default_data_type == "tftensor" and self.grpc:
            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)
            log.debug(result)
            datadef = prediction_pb2.DefaultData(
                tftensor=result.outputs[self.model_output])
            return prediction_pb2.SeldonMessage(data=datadef)

        elif request_data_type == "jsonData":
            features = get_data_from_proto(request)
            predictions = self.predict(features, features_names=[])
            try:
                sm = json_to_seldon_message({"jsonData": predictions})
            except ParseError as e:
                sm = prediction_pb2.SeldonMessage(strData=predictions)
            return sm

        else:
            features = get_data_from_proto(request)
            datadef = request.data
            predictions = self.predict(features, datadef.names)
            predictions = np.array(predictions)

            if request_data_type is not "data":
                default_data_type = "tensor"

            class_names = []
            data = array_to_grpc_datadef(predictions, class_names,
                                         default_data_type)

            return prediction_pb2.SeldonMessage(data=data)
    def _predict_grpc_data(self, request, default_data_type):
        tfrequest = predict_pb2.PredictRequest()

        # handle input
        if default_data_type == "tftensor":
            # For GRPC case, if we have a TFTensor message we can pass it directly
            tfrequest.inputs[self.model_input].CopyFrom(request.data.tftensor)
        else:
            data_arr = grpc_datadef_to_array(request.data)
            tfrequest.inputs[self.model_input].CopyFrom(
                tf.make_tensor_proto(data_arr.tolist(), shape=data_arr.shape)
            )

        # handle prediction
        tfresponse = self._handle_grpc_prediction(tfrequest)

        # handle result
        datadef = prediction_pb2.DefaultData(
            tftensor=tfresponse.outputs[self.model_output]
        )

        return prediction_pb2.SeldonMessage(data=datadef)
 def transform_output_grpc(self, X):
     arr = np.array([9, 9])
     datadef = prediction_pb2.DefaultData(
         tensor=prediction_pb2.Tensor(shape=(2, 1), values=arr))
     request = prediction_pb2.SeldonMessage(data=datadef)
     return request
def test_proto_seldon_metrics_endpoint(cls, client_gets_metrics):
    def _match_label(line):
        _data, value = line.split()
        name, labels = _data.split()[0].split("{")
        labels = labels[:-1]
        return name, value, eval(f"dict({labels})")

    def _iterate_metrics(text):
        for line in text.split("\n"):
            if not line or line[0] == "#":
                continue
            yield _match_label(line)

    user_object = cls()
    seldon_metrics = SeldonMetrics()

    app = SeldonModelGRPC(user_object, seldon_metrics)
    datadef = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=(2, 1), values=np.array([1, 2])))

    request = prediction_pb2.SeldonMessage(data=datadef)

    metrics_app = get_metrics_microservice(seldon_metrics)
    metrics_client = metrics_app.test_client()

    rv = metrics_client.get("/metrics")
    assert rv.status_code == 200
    assert rv.data.decode() == ""

    resp = app.Predict(request, None)
    j = json.loads(json_format.MessageToJson(resp))
    assert j["data"] == {
        "names": ["t:0"],
        "tensor": {
            "shape": [2, 1],
            "values": [1.0, 2.0]
        },
    }
    assert j["meta"]["tags"] == EXPECTED_TAGS
    assert ("metrics" in j["meta"]) == client_gets_metrics
    rv = metrics_client.get("/metrics")
    text = rv.data.decode()

    timer_present = False
    for name, value, labels in _iterate_metrics(text):
        if name == "mytimer_bucket":
            timer_present = True

        if name == "mycounter_total":
            assert value == "1.0"
            assert labels["worker_id"] == str(os.getpid())

        if name == "mygauge":
            assert value == "100.0"
            assert labels["worker_id"] == str(os.getpid())

        if name == "customtag":
            assert value == "200.0"
            assert labels["mytag"] == "mytagvalue"

    assert timer_present
 def route_grpc(self, request):
     arr = np.array([1])
     datadef = prediction_pb2.DefaultData(
         tensor=prediction_pb2.Tensor(shape=(1, 1), values=arr))
     request = prediction_pb2.SeldonMessage(data=datadef)
     return request