github.com/alwaysproblem/mlserving-tutorial@v0.0.0-20221124033215-121cfddbfbf4/TFserving/ClientAPI/python/grpc_request.py (about)

     1  """Send grpc request"""
     2  import numpy as np
     3  
     4  import tensorflow as tf
     5  from tensorflow_serving.apis import predict_pb2, prediction_service_pb2_grpc
     6  # from grpc.beta import implementations
     7  import grpc
     8  
     9  host = "0.0.0.0"
    10  port = 8500
    11  server = host + ":" + str(port)
    12  timeout_req = 30.0
    13  
    14  req_data = np.array([[1., 2.], [1., 3.]])
    15  
    16  if __name__ == "__main__":
    17  
    18    import argparse
    19  
    20    parse = argparse.ArgumentParser(prog="the tensorflow client for python.")
    21    parse.add_argument(
    22        "-m", "--model", type=str, action="store", dest="model", default="Toy"
    23    )
    24    parse.add_argument(
    25        "-v", "--version", type=int, action="store", dest="version", default=-1
    26    )
    27    parse.add_argument(
    28        "-l",
    29        "--version-labels",
    30        type=str,
    31        action="store",
    32        dest="labels",
    33        default=None
    34    )
    35  
    36    args = parse.parse_args()
    37  
    38    repeat = 10000
    39    # channel = implementations.insecure_channel(host, int(port))
    40    channel = grpc.insecure_channel(server)
    41    # stub = (prediction_service_pb2_grpc
    42    #           .beta_create_PredictionService_stub(channel))
    43    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
    44  
    45    request = predict_pb2.PredictRequest()
    46    request.model_spec.name = args.model
    47  
    48    if args.version > -1:
    49      request.model_spec.version.value = args.version
    50  
    51    if args.labels is not None:
    52      request.model_spec.version_label = args.labels
    53  
    54    request.model_spec.signature_name = "serving_default"
    55  
    56    # if you have a structure data. example a dictionary,
    57    # you should exhausted every key to change them to protobuf patten
    58    # requset.input is the same as the keras model input layer.
    59    # so if you define a inputlayer
    60    # for f in sparse_features+varlen_features:
    61    #     data_proto = tf.make_tensor_proto(post_100[f], dtype=tf.int64)
    62    #     request.inputs[f].CopyFrom(data_proto)
    63  
    64    data_proto = tf.make_tensor_proto(req_data, dtype=tf.float32)
    65  
    66    request.inputs["input_1"].CopyFrom(data_proto)
    67  
    68    # only get outputs you want model to response.
    69    # request.output_filter.append("output_1")
    70  
    71    resp = stub.Predict(request, timeout_req)
    72  
    73    print(resp)
    74  
    75    # # for output filter out (you can also check the grpc api `predict.proto` )
    76    # print(resp.outputs["output_1"].float_val)