<jupyter_start><jupyter_text>Load the model<jupyter_code>from azureml.core import Workspace
ws = Workspace.get(name='demo-aml',subscription_id='YOUR-SUSCRIPTION-ID',resource_group='demo-aml')
#List last model registered on ws in order to create the image
from azureml.core import Model
model_name = 'BABY-WEIGHT'
list_models = Model.list(workspace=ws, name=model_name)
model = list_models[0]
model<jupyter_output><empty_output><jupyter_text>Create inference configuration<jupyter_code>from azureml.core.model import InferenceConfig
from azureml.core.environment import Environment

#myenv = Environment.get(workspace=ws, name="keras-env")
myenv = Environment.get(workspace=ws, name="xgboost-env")
inference_config = InferenceConfig(entry_script="score.py", environment=myenv)<jupyter_output><empty_output><jupyter_text>Create Compute instance<jupyter_code>from azureml.core.compute import AksCompute, ComputeTarget
from azureml.core.compute_target import ComputeTargetException

# Create Compute AKS
aks_name ='aks-compute'
prov_config = AksCompute.provisioning_configuration(cluster_purpose="DevTest")

try:
    aks_target = AksCompute(ws,aks_name)
    print('Found existing AKS cluster, use it.')
except ComputeTargetException:
    print("Creating new AKS cluster.")
    aks_target = ComputeTarget.create(workspace = ws,
                                        name = aks_name,
                                        provisioning_configuration = prov_config)
    aks_target.wait_for_completion(show_output=True)<jupyter_output>Found existing AKS cluster, use it.<jupyter_text>Deploy web service<jupyter_code>from azureml.core.webservice import AksWebservice, Webservice

aks_config = AksWebservice.deploy_configuration(autoscale_enabled=True,
                                                auth_enabled=False,
                                                collect_model_data=True,
                                                enable_app_insights=True,
                                                tags = model.tags,
                                                description = model.description)
%%time

# Start web AKS service deployment
aks_target = AksCompute(ws, aks_name)
aks_service_name ='baby-weights-webservice-aks' 


try:
    Webservice.check_for_existing_webservice(name=aks_service_name, workspace=ws)
    service = Model.deploy(workspace=ws, 
                       name=aks_service_name, 
                       models=[model], 
                       inference_config=inference_config, 
                       deployment_config=aks_config, 
                       deployment_target=aks_target)

    service.wait_for_deployment(show_output = True)
    print(service.state)
    

except Exception as e: 
    #print(e)
    service = Webservice(name=aks_service_name, workspace=ws)
    # Update to new model(s).
    service.update(models=[model], inference_config=inference_config)
    
    service.wait_for_deployment(show_output=True)
    print(service.state)
      
print(service.get_logs())<jupyter_output><empty_output><jupyter_text>Test the endpoint<jupyter_code>import pandas as pd
X_val=pd.read_json('inputs/X_validation_data.json', orient="split")
X_val.head()

X_val = X_val.values
import requests
import numpy as np

# send a random row from the validatition set to score
random_index = np.random.randint(0, len(X_val)-1)
input_data = "{\"data\": [" + str(list(X_val[random_index])) + "]}"

headers = {'Content-Type':'application/json'}

resp = requests.post(service.scoring_uri, input_data, headers=headers)

print("POST to url", service.scoring_uri)
print("input data:", input_data)
print("prediction:", resp.text)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09315589353612168, 'fraction_numerical': 0.0008147745790331341, 'mean_word_length': 3.4588377723970942, 'pattern_counts': {'":': 1, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '253637', 'license': 'CC0-1.0', 'n_tokens_mistral': 1108, 'n_tokens_neox': 1074, 'n_words': 198, 'path': 'BirthWeight/4.0.deploy_aks.ipynb', 'repo_name': 'atabordal/azureml-tutorial', 'size': '3682'}