Esempio n. 1
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.endpoint = getEnviron('API_ENDPOINT', "external")
     #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)
     if self.endpoint == "external":
         self.stub = prediction_pb2_grpc.SeldonStub(channel)
     else:
         self.stub = prediction_pb2_grpc.ModelStub(channel)
     self.rewardProbas = [0.5, 0.2, 0.9, 0.3, 0.7]
     self.routeRewards = {}
     self.routesSeen = []
Esempio n. 2
0
def run(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)
        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.ModelStub(channel)
            response = stub.Predict(GRPC_request)

            if args.prnt:
                print("RECEIVED RESPONSE:")
                print(response)
                print()
Esempio n. 3
0
def run(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)
        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)
            if args.prnt:
                print(REST_request)

            t1 = time()
            response = requests.post(REST_url,
                                     data={
                                         "json": json.dumps(REST_request),
                                         "isDefault": True
                                     })
            t2 = time()
            jresp = response.json()

            if args.prnt:
                print("RECEIVED RESPONSE:")
                print(jresp)
                print()
                print("Time " + str(t2 - t1))
        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.ModelStub(channel)
            response = stub.Predict(GRPC_request)

            if args.prnt:
                print("RECEIVED RESPONSE:")
                print(response)
                print()
 def on_start(self):
     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', "0"))
     self.endpoint = getEnviron('API_ENDPOINT', "external")
     if self.oauth_enabled == "true":
         self.get_token()
     else:
         self.access_token = "NONE"
     channel = grpc.insecure_channel(HOST)
     if self.endpoint == "external":
         self.stub = prediction_pb2_grpc.SeldonStub(channel)
     else:
         self.stub = prediction_pb2_grpc.ModelStub(channel)
     self.mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
Esempio n. 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 and not args.fbs:
            REST_request = gen_REST_request(batch,
                                            features=feature_names,
                                            tensor=args.tensor)
            if args.prnt:
                print(REST_request)

            t1 = time()
            response = requests.post(REST_url,
                                     data={
                                         "json": json.dumps(REST_request),
                                         "isDefault": True
                                     })
            t2 = time()
            jresp = response.json()

            if args.prnt:
                print("RECEIVED RESPONSE:")
                print(jresp)
                print()
                print("Time " + str(t2 - t1))
        elif args.grpc:
            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.ModelStub(channel)
            response = stub.Predict(GRPC_request)

            if args.prnt:
                print("RECEIVED RESPONSE:")
                print(response)
                print()
        elif args.fbs:
            import socket
            import struct
            from tester_flatbuffers import NumpyArrayToSeldonRPC, SeldonRPCToNumpyArray
            data = NumpyArrayToSeldonRPC(batch, feature_names)
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((args.host, args.port))
            totalsent = 0
            MSGLEN = len(data)
            print("Will send", MSGLEN, "bytes")
            while totalsent < MSGLEN:
                sent = s.send(data[totalsent:])
                if sent == 0:
                    raise RuntimeError("socket connection broken")
                totalsent = totalsent + sent
            data = s.recv(4)
            obj = struct.unpack('<i', data)
            len_msg = obj[0]
            data = s.recv(len_msg)
            arr = SeldonRPCToNumpyArray(data)
            print(arr)
Esempio n. 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 and not args.fbs:
            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)

            t1 = time()
            response = requests.post(REST_url,
                                     data={"json": json.dumps(REST_feedback)})
            t2 = time()

            if args.prnt:
                print("Time " + str(t2 - t1))
                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.ModelStub(channel)
            response = stub.SendFeedback(GRPC_feedback)

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