示例#1
0
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
示例#2
0
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
示例#3
0
 def on_start(self):
     """
     get token
     :return:
     """
     print "on start"
     self.oauth_enabled = getEnviron('OAUTH_ENABLED', "false")
     self.oauth_key = getEnviron('OAUTH_KEY', "key")
     self.oauth_secret = getEnviron('OAUTH_SECRET', "secret")
     self.data_size = int(getEnviron('DATA_SIZE', "1"))
     self.send_feedback = int(getEnviron('SEND_FEEDBACK', "1"))
     self.oauth_endpoint = getEnviron('OAUTH_ENDPOINT',
                                      "http://127.0.0.1:30015")
     #self.grpc_endpoint = getEnviron('GRPC_ENDPOINT',"127.0.0.1:30017")
     if self.oauth_enabled == "true":
         self.get_token()
     else:
         self.access_token = "NONE"
     channel = grpc.insecure_channel(HOST)
     self.stub = prediction_pb2_grpc.SeldonStub(channel)
     self.rewardProbas = [0.5, 0.2, 0.9, 0.3, 0.7]
     self.routeRewards = {}
     self.routesSeen = []
示例#4
0
def get_prediction(image,
                   server_host='127.0.0.1',
                   server_port=8080,
                   deployment_name="server",
                   timeout=10.0):
    """
    Retrieve a prediction from a TensorFlow model server

    :param image:       a MNIST image represented as a 1x784 array
    :param server_host: the address of the Seldon server
    :param server_port: the port used by the server
    :param deployment_name: the name of the deployment
    :param timeout:     the amount of time to wait for a prediction to complete
    :return 0:          the integer predicted in the MNIST image
    :return 1:          the confidence scores for all classes
    :return 2:          the version number of the model handling the request
    """

    try:
        # build request
        chosen = 0
        data = image[chosen].reshape(784)
        datadef = prediction_pb2.DefaultData(
            tensor=prediction_pb2.Tensor(shape=data.shape, values=data))

        # retrieve results
        request = prediction_pb2.SeldonMessage(data=datadef)
        print("connecting to:%s:%i" % (server_host, server_port))
        channel = grpc.insecure_channel(server_host + ":" + str(server_port))
        stub = prediction_pb2_grpc.SeldonStub(channel)
        metadata = [('seldon', deployment_name)]
        response = stub.Predict(request=request, metadata=metadata)
    except Exception as e:
        # server connection failed
        print("Could Not Connect to Server: " + str(e))
    return response.data.tensor.values
示例#5
0
def run_predict(args):
    contract = json.load(open(args.contract, 'r'))
    contract = unfold_contract(contract)
    feature_names = [feature["name"] for feature in contract["features"]]

    REST_url = "http://" + args.host + ":" + str(args.port) + "/predict"

    for i in range(args.n_requests):
        batch = generate_batch(contract, args.batch_size, 'features')
        if args.prnt:
            print('-' * 40)
            print("SENDING NEW REQUEST:")

        if not args.grpc:
            headers = {}
            REST_request = gen_REST_request(batch,
                                            features=feature_names,
                                            tensor=args.tensor)
            if args.prnt:
                print(REST_request)

            if args.oauth_key:
                token = get_token(args)
                headers = {'Authorization': 'Bearer ' + token}
                response = requests.post("http://" + args.host + ":" +
                                         str(args.port) +
                                         "/api/v0.1/predictions",
                                         json=REST_request,
                                         headers=headers)
            else:
                response = requests.post(
                    "http://" + args.host + ":" + str(args.port) +
                    args.ambassador_path + "/api/v0.1/predictions",
                    json=REST_request,
                    headers=headers)

            jresp = response.json()

            if args.prnt:
                print("RECEIVED RESPONSE:")
                print(jresp)
                print()
        else:
            GRPC_request = gen_GRPC_request(batch,
                                            features=feature_names,
                                            tensor=args.tensor)
            if args.prnt:
                print(GRPC_request)

            channel = grpc.insecure_channel('{}:{}'.format(
                args.host, args.port))
            stub = prediction_pb2_grpc.SeldonStub(channel)

            if args.oauth_key:
                token = get_token(args)
                metadata = [('oauth_token', token)]
                response = stub.Predict(request=GRPC_request,
                                        metadata=metadata)
            else:
                response = stub.Predict(request=GRPC_request)

            if args.prnt:
                print("RECEIVED RESPONSE:")
                print(response)
                print()
示例#6
0
def run_send_feedback(args):
    contract = json.load(open(args.contract, 'r'))
    contract = unfold_contract(contract)
    feature_names = [feature["name"] for feature in contract["features"]]
    response_names = [feature["name"] for feature in contract["targets"]]

    REST_url = "http://" + args.host + ":" + str(args.port) + "/send-feedback"

    for i in range(args.n_requests):
        batch = generate_batch(contract, args.batch_size, 'features')
        response = generate_batch(contract, args.batch_size, 'targets')
        if args.prnt:
            print('-' * 40)
            print("SENDING NEW REQUEST:")

        if not args.grpc:
            REST_request = gen_REST_request(batch,
                                            features=feature_names,
                                            tensor=args.tensor)
            REST_response = gen_REST_request(response,
                                             features=response_names,
                                             tensor=args.tensor)
            reward = 1.0
            REST_feedback = {
                "request": REST_request,
                "response": REST_response,
                "reward": reward
            }
            if args.prnt:
                print(REST_feedback)

            if args.oauth_key:
                token = get_token(args)
                headers = {'Authorization': 'Bearer ' + token}
                response = requests.post("http://" + args.host + ":" +
                                         str(args.port) + "/api/v0.1/feedback",
                                         json=REST_feedback,
                                         headers=headers)
            else:
                response = requests.post(
                    "http://" + args.host + ":" + str(args.port) +
                    args.ambassador_path + "/api/v0.1/feedback",
                    json=REST_feedback,
                    headers=headers)

            if args.prnt:
                print(response)

        elif args.grpc:
            GRPC_request = gen_GRPC_request(batch,
                                            features=feature_names,
                                            tensor=args.tensor)
            GRPC_response = gen_GRPC_request(response,
                                             features=response_names,
                                             tensor=args.tensor)
            reward = 1.0
            GRPC_feedback = prediction_pb2.Feedback(request=GRPC_request,
                                                    response=GRPC_response,
                                                    reward=reward)

            if args.prnt:
                print(GRPC_feedback)

            channel = grpc.insecure_channel('{}:{}'.format(
                args.host, args.port))
            stub = prediction_pb2_grpc.SeldonStub(channel)

            if args.oauth_key:
                token = get_token(args)
                metadata = [('oauth_token', token)]
                response = stub.SendFeedback(request=GRPC_feedback,
                                             metadata=metadata)
            else:
                response = stub.SendFeedback(request=GRPC_feedback)

            if args.prnt:
                print("RECEIVED RESPONSE:")
                print()