def connect_webservice(ws): with open('config/aks_config.json') as f: aks_config = json.load(f) aks_service = AksWebservice(ws, name=aks_config["name"]) # print(aks_service.state) url = aks_service.scoring_uri key = aks_service.get_keys()[0] return url, key
def test_deployed_model_service(): service = AksWebservice(ws, deployment_name) assert service is not None key1, key2 = service.get_keys() uri = service.scoring_uri assert key1 is not None assert uri.startswith('http') headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {key1}' } response = requests.post(uri, test_sample, headers=headers) assert response.status_code is 200 assert abs(1 - sum(response.json()['predict_proba'][0])) < 0.01
def test_gpu_service(workspace): aks_service_name = "deepaksservice" assert aks_service_name in workspace.webservices, f"{aks_service_name} not found." aks_service = AksWebservice(workspace, name=aks_service_name) assert (aks_service.state == "Healthy" ), f"{aks_service_name} is in state {aks_service.state}." scoring_url = aks_service.scoring_uri print(scoring_url) api_key = aks_service.get_keys()[0] import requests headers = {"Authorization": ("Bearer " + api_key)} files = {"image": open("snowleopardgaze.jpg", "rb")} r_get = requests.get(scoring_url, headers=headers) assert r_get r_post = requests.post(scoring_url, files=files, headers=headers) assert r_post
def call_web_service(e, service_type, service_name): aml_workspace = Workspace.get(name=e.workspace_name, subscription_id=e.subscription_id, resource_group=e.resource_group) print('fetching webservice') if service_type == 'AKS': service = AksWebservice(aml_workspace, service_name) elif service_type == 'ACI': service = AciWebservice(aml_workspace, service_name) else: raise ValueError(f'no {service_type} is supported!') headers = {} if service.auth_enabled: service_keys = service.get_keys() headers['Authorization'] = 'Bearer ' + service_keys[0] scoring_url = service.scoring_uri print(f'scoring url: {scoring_url}') output = call_web_app(scoring_url, headers) return output
def test_aks(directory: str, aks_service: AksWebservice): """ Test AKS with sample call. :param directory: directory of data_folder with test data :param aks_service: AKS Web Service to Test """ num_dupes_to_score = 4 dupes_test = get_dupes_test(directory) text_to_score = dupes_test.iloc[0, num_dupes_to_score] json_text = text_to_json(text_to_score) scoring_url = aks_service.scoring_uri api_key = aks_service.get_keys()[0] headers = { "content-type": "application/json", "Authorization": ("Bearer " + api_key), } requests.post( scoring_url, data=json_text, headers=headers) # Run the request twice since the first time takes a r = requests.post( scoring_url, data=json_text, headers=headers) # little longer due to the loading of the model print(r) dupes_to_score = dupes_test.iloc[:5, num_dupes_to_score] text_data = list(map(text_to_json, dupes_to_score)) # Retrieve the text data for text in text_data: r = requests.post(scoring_url, data=text, headers=headers) print(r)
default="triton-densenet-onnx", help="name of the endpoint to test", ) parser.add_argument( "--data_file", type=str, default="../../data/raw/triton/peacock.jpg", help="filename to run through the classifier", ) args = parser.parse_args() ws = Workspace.from_config() aks_service = AksWebservice(ws, args.endpoint_name) # if (key) auth is enabled, fetch keys and include in the request key1, _ = aks_service.get_keys() headers = { "Content-Type": "application/octet-stream", "Authorization": "Bearer " + key1, } file_name = os.path.join( os.path.abspath(os.path.dirname(__file__)), "..", "data", args.data_file, ) test_sample = open(file_name, "rb").read() resp = requests.post(aks_service.scoring_uri, test_sample, headers=headers) print(resp.text)
data = read_tensor_from_image_file(file_name, input_height=input_height, input_width=input_width, input_mean=input_mean, input_std=input_std) raw_data = str(data.tolist()) # predict using the deployed model print("Sending image", f, "to service") response = service.run(input_data=raw_data) print("Service response:", response) print() print("Testings web service via HTTP call...") api_keys = service.get_keys() headers = { 'Content-Type': 'application/json', 'Authorization': ('Bearer ' + api_keys[0]) } file_name = "./resources/test-images/Daisy1.jpg" data = read_tensor_from_image_file(file_name, input_height=input_height, input_width=input_width, input_mean=input_mean, input_std=input_std) input_data = str(data.tolist()) print("POST to url", service.scoring_uri) resp = requests.post(service.scoring_uri, input_data, headers=headers)