<jupyter_start><jupyter_text>Azure ML & Azure Databricks notebooks by <NAME>.Copyright (c) Microsoft Corporation. All rights reserved.Licensed under the MIT License. This notebook uses image from ACI notebook for deploying to AKS.<jupyter_code>import azureml.core

# Check core SDK version number
print("SDK version:", azureml.core.VERSION)
# Set auth to be used by workspace related APIs.
# For automation or CI/CD ServicePrincipalAuthentication can be used.
# https://docs.microsoft.com/en-us/python/api/azureml-core/azureml.core.authentication.serviceprincipalauthentication?view=azure-ml-py
auth = None
from azureml.core import Workspace

ws = Workspace.from_config(auth = auth)
print('Workspace name: ' + ws.name, 
      'Azure region: ' + ws.location, 
      'Subscription id: ' + ws.subscription_id, 
      'Resource group: ' + ws.resource_group, sep = '\n')
#Register the model
import os
from azureml.core.model import Model

model_name = "AdultCensus_runHistory_aks.mml" # 
model_name_dbfs = os.path.join("/dbfs", model_name)

print("copy model from dbfs to local")
model_local = "file:" + os.getcwd() + "/" + model_name
dbutils.fs.cp(model_name, model_local, True)

mymodel = Model.register(model_path = model_name, # this points to a local file
                       model_name = model_name, # this is the name the model is registered as, am using same name for both path and name.                 
                       description = "ADB trained model by Parashar",
                       workspace = ws)

print(mymodel.name, mymodel.description, mymodel.version)
#%%writefile score_sparkml.py
score_sparkml = """
 
import json
 
def init():
    # One-time initialization of PySpark and predictive model
    import pyspark
    from azureml.core.model import Model
    from pyspark.ml import PipelineModel
 
    global trainedModel
    global spark
 
    spark = pyspark.sql.SparkSession.builder.appName("ADB and AML notebook by Parashar").getOrCreate()
    model_name = "{model_name}" #interpolated
    model_path = Model.get_model_path(model_name)
    trainedModel = PipelineModel.load(model_path)
    
def run(input_json):
    if isinstance(trainedModel, Exception):
        return json.dumps({{"trainedModel":str(trainedModel)}})
      
    try:
        sc = spark.sparkContext
        input_list = json.loads(input_json)
        input_rdd = sc.parallelize(input_list)
        input_df = spark.read.json(input_rdd)
    
        # Compute prediction
        prediction = trainedModel.transform(input_df)
        #result = prediction.first().prediction
        predictions = prediction.collect()
 
        #Get each scored result
        preds = [str(x['prediction']) for x in predictions]
        result = ",".join(preds)
        # you can return any data type as long as it is JSON-serializable
        return result.tolist()
    except Exception as e:
        result = str(e)
        return result
    
""".format(model_name=model_name)
 
exec(score_sparkml)
 
with open("score_sparkml.py", "w") as file:
    file.write(score_sparkml)
from azureml.core.conda_dependencies import CondaDependencies 

myacienv = CondaDependencies.create(conda_packages=['scikit-learn','numpy','pandas']) #showing how to add libs as an eg. - not needed for this model.

with open("mydeployenv.yml","w") as f:
    f.write(myacienv.serialize_to_string())
#create AKS compute
#it may take 20-25 minutes to create a new cluster

from azureml.core.compute import AksCompute, ComputeTarget
from azureml.core.compute_target import ComputeTargetException

aks_name = 'ps-aks-demo2' 

try:
    aks_target = ComputeTarget(workspace=ws, name=aks_name)
    print('Found existing cluster, use it.')
except ComputeTargetException:
    # Use the default configuration (can also provide parameters to customize)
    prov_config = AksCompute.provisioning_configuration()
    
    # Create the cluster
    aks_target = ComputeTarget.create(workspace = ws, 
                                  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)
#deploy to AKS
from azureml.core.webservice import AksWebservice, Webservice
from azureml.exceptions import WebserviceException
from azureml.core.model import InferenceConfig

aks_config = AksWebservice.deploy_configuration(enable_app_insights=True)

service_name = 'ps-aks-service'

# Remove any existing service under the same name.
try:
    Webservice(ws, service_name).delete()
except WebserviceException:
    pass

inference_config = InferenceConfig(runtime = 'spark-py', 
                                   entry_script ='score_sparkml.py',
                                   conda_file ='mydeployenv.yml')

aks_service = Model.deploy(ws, service_name, [mymodel], inference_config, aks_config, aks_target)
aks_service.wait_for_deployment(show_output=True)
aks_service.deployment_status
#for using the Web HTTP API 
print(aks_service.scoring_uri)
print(aks_service.get_keys())
import json

#get the some sample data
test_data_path = "AdultCensusIncomeTest"
test = spark.read.parquet(test_data_path).limit(5)

test_json = json.dumps(test.toJSON().collect())

print(test_json)
#using data defined above predict if income is >50K (1) or <=50K (0)
aks_service.run(input_data=test_json)
#comment to not delete the web service
aks_service.delete()
#model.delete()
aks_target.delete()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09117326552851435, 'fraction_numerical': 0.002179440610243371, 'mean_word_length': 3.7148972602739727, 'pattern_counts': {'":': 1, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '372593', 'license': 'MIT', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1658, 'n_words': 434, 'path': 'how-to-use-azureml/azure-databricks/amlsdk/deploy-to-aks-05.ipynb', 'repo_name': 'MustAl-Du/MachineLearningNotebooks', 'size': '5513'}