def deploy_service(ws, model, inference_config, service_name, compute_target): tags = {'model': '{}:{}'.format(model.name, model.version)} try: service = Webservice(ws, service_name) print("Service {} exists, update it".format(service_name)) service.update(models=[model], inference_config=inference_config, tags=tags) except Exception: print('deploy a new service {}'.format(service_name)) deployment_config = AksWebservice.deploy_configuration( cpu_cores=1, memory_gb=2, tags=tags, collect_model_data=True, enable_app_insights=True) service = Model.deploy(ws, service_name, [model], inference_config, deployment_config, compute_target) service.wait_for_deployment(show_output=True) if service.auth_enabled: token = service.get_keys()[0] elif service.token_auth_enabled: token = service.get_token()[0] return service.scoring_uri, token
def main(): ws = AzureMLUtils.get_workspace() print("Workspace lookup successful") # read command line parameters service_name = getRuntimeArgs() #look up service service = Webservice(ws, service_name) # look up scoring uri scoring_uri = service.scoring_uri #Get the first api key if service.compute_type == "AKS": api_key = service.get_keys()[0] elif service.compute_type == "ACI": api_key = "dummy" else: raise Exception("Unknown compute type") # This line is needed to for Azure Devops to set api key and scoring uri as environment variable. print("##vso[task.setvariable variable=TMP_SCORING_URI]", scoring_uri) print("##vso[task.setvariable variable=TMP_API_KEY]", api_key)
headers["Authorization"] = "Bearer {service_key}".format( service_key=service_key) # print ("Headers: {}") #don't show headers if we are going to save a key or other authentication print("Sending batch prediction request with inputs: {}".format(inputs)) response = requests.post(scoring_uri, data=json.dumps(inputs), headers=headers) preds = json.loads(response.text) print("Received response: {}".format(preds)) return preds #AzureML requires the input data to be in a Pandas data format #that has been converted to JSON using the **split** orientation #grab a record from the test data frame to test calling for a single score scoring_df = test_df.toPandas().iloc[[18]] query_input = scoring_df.to_json(orient='split') query_input = eval(query_input) query_input.pop('index', None) #collect values needed to call the service webserviceURI = webservice.scoring_uri service_auth_key = webservice.get_keys()[0] if len(webservice.get_keys( )) > 0 else None #note: there are more secure ways to do this result = query_endpoint_example(scoring_uri=webserviceURI, inputs=query_input, service_key=service_auth_key)
provisioning_configuration=prov_config) aks_target.wait_for_completion(show_output=True) print(aks_target.provisioning_state) print(aks_target.provisioning_errors) # Use the default configuration (can also provide parameters to customize) aks_config = AksWebservice.deploy_configuration(enable_app_insights=True) service = Webservice.deploy_from_image( workspace=ws, name=aks_service_name, image=image, deployment_config=aks_config, deployment_target=aks_target, ) service.wait_for_deployment(show_output=True) print(service.state) print("Deployed AKS Webservice: {} \nWebservice Uri: {}".format( service.name, service.scoring_uri)) # Writing the AKS details to /aml_config/aks_webservice.json aks_webservice = {} aks_webservice["aks_name"] = aks_name aks_webservice["aks_service_name"] = service.name aks_webservice["aks_url"] = service.scoring_uri aks_webservice["aks_keys"] = service.get_keys() with open("aml_config/aks_webservice.json", "w") as outfile: json.dump(aks_webservice, outfile)
print("Creating new webservice") # Create the web service configuration (using defaults) aks_config = AksWebservice.deploy_configuration(description=args.description, tags={ 'name': aks_name, 'image_id': image.id }) service = Webservice.deploy_from_image(workspace=ws, name=aks_service_name, image=image, deployment_config=aks_config, deployment_target=aks_target) service.wait_for_deployment(show_output=True) print(service.state) api_key, _ = service.get_keys() print( "Deployed AKS Webservice: {} \nWebservice Uri: {} \nWebservice API Key: {}" .format(service.name, service.scoring_uri, api_key)) aks_webservice = {} aks_webservice["aks_service_name"] = service.name aks_webservice["aks_service_url"] = service.scoring_uri aks_webservice["aks_service_api_key"] = api_key print("AKS Webservice Info") print(aks_webservice) print("Saving aks_webservice.json...") aks_webservice_filepath = os.path.join('./outputs', 'aks_webservice.json') with open(aks_webservice_filepath, "w") as f: json.dump(aks_webservice, f)
#conda_dep.add_pip_package("azureml-defaults") myenv.python.conda_dependencies = conda_dep inference_config = InferenceConfig(entry_script="score.py", environment=myenv) #image_config = ContainerImage.image_configuration(execution_script = "score.py", # runtime = "python", #conda_file = "turbofan.yml") try: service = Webservice(ws, webservice_name) service.delete() except Exception as e: print("No Existing Service") service = Model.deploy(ws, webservice_name, [model], inference_config, deployment_config) #service.get_logs() #service = Webservice.deploy_from_model(workspace=ws, # name = webservice_name, # deployment_config = aci_config, #models = [model], #image_config = image_config) service.wait_for_deployment(show_output=True) print(service.get_keys()) print(service.scoring_uri)
def query_endpoint_example(scoring_uri, inputs, service_key=None): headers = { "Content-Type": "application/json", } if service_key is not None: headers["Authorization"] = "Bearer {service_key}".format( service_key=service_key) print("Sending batch prediction request with inputs: {}".format(inputs)) response = requests.post(scoring_uri, data=json.dumps(inputs), headers=headers) preds = json.loads(response.text) print("Received response: {}".format(preds)) return preds # COMMAND ---------- prod_scoring_uri = prod_webservice.scoring_uri prod_service_key = prod_webservice.get_keys()[0] if len( prod_webservice.get_keys()) > 0 else None print("Webservice URL:", prod_scoring_uri) # COMMAND ---------- prod_prediction1 = query_endpoint_example(scoring_uri=prod_scoring_uri, service_key=prod_service_key, inputs=query_input)
subscription_id = config["subscription_id"] workspace_region = config["location"] #Interactive Authentication ws = Workspace(workspace_name=workspace_name, subscription_id=subscription_id, resource_group=resource_group, auth=cli_auth) webservice_name = 'turbofan-rul' try: service = Webservice(ws, webservice_name) uri = service.scoring_uri key = service.get_keys()[0] print(uri) train = pd.read_csv("data/turbofan.csv") sampleSize = round(5 / len(train), 4) testdf = train.sample(frac=sampleSize) X = testdf.drop('rul', axis=1) y = testdf.rul.tolist() input_data = json.dumps( X.to_json(orient='records')) # Create JSON of sample records headers = {'Content-Type': 'application/json'}
name = aks_name, provisioning_configuration = prov_config) aks_target.wait_for_completion(show_output = True) print(aks_target.provisioning_state) print(aks_target.provisioning_errors) # Use the default configuration (can also provide parameters to customize) aks_config = AksWebservice.deploy_configuration(enable_app_insights=True) service = Webservice.deploy_from_image(workspace = ws, name = aks_service_name, image = image, deployment_config = aks_config, deployment_target = aks_target) service.wait_for_deployment(show_output = True) print(service.state) print('Deployed AKS Webservice: {} \nWebservice Uri: {}'.format(service.name, service.scoring_uri)) # Writing the AKS details to /aml_config/aks_webservice.json aks_webservice = {} aks_webservice['aks_name'] = aks_name aks_webservice['aks_service_name'] = service.name aks_webservice['aks_url'] = service.scoring_uri aks_webservice['aks_keys'] = service.get_keys() with open('aml_config/aks_webservice.json', 'w') as outfile: json.dump(aks_webservice,outfile)
import requests import json from azureml.core.webservice import Webservice from azureml.core import Workspace # get scoring url workspace = Workspace.from_config() service_name = 'arxiv-nmt-service' service = Webservice(workspace, name=service_name) scoring_url = service.scoring_uri # set headers headers = {'Content-Type': 'application/json'} if service.auth_enabled: headers['Authorization'] = 'Bearer '+ service.get_keys()[0] elif service.token_auth_enabled: headers['Authorization'] = 'Bearer '+ service.get_token()[0] print(headers) # generate dummy example test_abstract = 'The groups $\gamma_{n,s}$ are defined in terms of homotopy ' \ 'equivalences of certain graphs, and are natural generalisations ' \ 'of $Out(Fn)$ and $Aut(Fn)$. They have appeared frequently in ' \ 'the study of free group automorphisms, for example in proofs of ' \ 'homological stability in [8,9] and in the proof that $Out(Fn)$ ' \ 'is a virtual duality group in [1]. More recently, in [5], their ' \ 'cohomology $H_i(\Gamma_{n,s})$, over a field of characteristic zero, ' \ 'was computed in ranks $n=1,2$ giving new constructions of unstable ' \ 'homology classes of $Out(Fn)$ and $Aut(Fn)$. In this paper we show ' \