# MAGIC ## Create a new web service deployment from a model image # COMMAND ---------- from azureml.core.webservice import Webservice, AksWebservice # Set configuration and service name webservice_name = "<prod_webservice_name>" #default configuration can be modified for custom requirements webservice_deployment_config = AksWebservice.deploy_configuration() # Deploy from image selected above webservice = Webservice.deploy_from_image( workspace=workspace, name=webservice_name, image=model_image, deployment_config=webservice_deployment_config, deployment_target=aks_target) #wait for the webservice to be deployed webservice.wait_for_deployment(show_output=True) # COMMAND ---------- print(webservice.get_logs()) # COMMAND ---------- # MAGIC %md if you receive an error deploying your image as a webservice, you can use the following code to get information from the image creation logs # COMMAND ----------
image, = (m for m in images if m.version==image_version and m.name == image_name) print('From image.json, Model used: {}\nModel Version {}'.format(config["model_name"], config["model_version"])) print('From image.json, Image used to deploy webservice on ACI: {}\nImage Version: {}\nImage Location = {}'.format(image.name, image.version, image.image_location)) #delete old service first before new one try: oldservice = Webservice(workspace=ws, name=service_name) print("delete " + service_name + " before creating new one") oldservice.delete() except: print(service_name + " does not exist, create new one") aciconfig = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1, tags={"data": "Income", "method" : "SparkML"}, description='Predict Income with sparkML') service = Webservice.deploy_from_image(deployment_config=aciconfig, image=image, name=service_name, workspace=ws) service.wait_for_deployment(show_output=True) print(service.scoring_uri) aci_webservice = {} aci_webservice['aci_name'] = service.name aci_webservice['aci_url'] = service.scoring_uri with open('conf/aci_webservice.json', 'w') as outfile: json.dump(aci_webservice,outfile)
while status != 'Succeeded' and status != 'Failed': print('current status: {} - waiting...'.format(status)) time.sleep(10) status = aks_target.get_status() print("Deploying the service to Azure Kubernetes Service...") # Activate Data Collection and App Insights through updating AKS Webservice configuration aks_config = AksWebservice.deploy_configuration(collect_model_data=True, enable_app_insights=True) # Deploy your service aks_service_name = 'diabetes-aks-svc' aks_service = Webservice.deploy_from_image(workspace=ws, name=aks_service_name, image=image, deployment_config=aks_config, deployment_target=aks_target) aks_service.wait_for_deployment(show_output=True) print(aks_service.state) print("Testing deployed service via SDK...") # Test Service test_sample = json.dumps({ 'data': [[1, 2, 3, 4, 54, 6, 7, 8, 88, 10], [10, 9, 8, 37, 36, 45, 4, 33, 2, 1]] }) test_sample = bytes(test_sample, encoding='utf8') prediction = aks_service.run(input_data=test_sample)
tags=deployment_settings["image"]["tags"], properties=deployment_settings["image"]["properties"], description=deployment_settings["image"]["description"], gpu_cores=aks_service_settings["gpu_cores"], period_seconds=aks_service_settings["period_seconds"], initial_delay_seconds=aks_service_settings["initial_delay_seconds"], timeout_seconds=aks_service_settings["timeout_seconds"], success_threshold=aks_service_settings["success_threshold"], failure_threshold=aks_service_settings["failure_threshold"], namespace=aks_service_settings["namespace"], token_auth_enabled=aks_service_settings["token_auth_enabled"]) # Deploying test web service from image test_service = Webservice.deploy_from_image( workspace=ws, name=aks_service_settings["name"], image=image, deployment_config=aks_config, deployment_target=aks_test_cluster) # Show output of the deployment on stdout test_service.wait_for_deployment(show_output=True) print(test_service.state) # Checking status of test web service print("Checking status of AKS Test Deployment") if test_service.state != "Healthy": raise Exception( "Test Deployment on AKS failed with the following status: {} and logs: \n{}" .format(test_service.state, test_service.get_logs())) # Testing AKS web service print("Testing AKS test web service")
# MAGIC # MAGIC Using the Azure ML SDK, deploy the Container Image for the trained MLflow model to ACI. # COMMAND ---------- from azureml.core.webservice import AciWebservice, Webservice from azureml.core.image import Image model_image = Image(workspace, id=model_image_id) dev_webservice_name = "wine-quality-aci" dev_webservice_deployment_config = AciWebservice.deploy_configuration() dev_webservice = Webservice.deploy_from_image( name=dev_webservice_name, image=model_image, deployment_config=dev_webservice_deployment_config, workspace=workspace, deployment_target=None, overwrite=True) dev_webservice.wait_for_deployment() # COMMAND ---------- # MAGIC %md ## Query the deployed model in "dev" # COMMAND ---------- # MAGIC %md ### Load dataset # COMMAND ----------
image.wait_for_creation(show_output=True) for i in ws.images(tag="diabetes"): print('{}(v.{} [{}]) stored at {} with build log {}'.format( i.name, i.version, i.creation_state, i.image_location, i.image_build_log_uri)) aciconfig = AciWebservice.deploy_configuration( cpu_cores=1, memory_gb=1, tags=['regression', 'diabetes'], description='Predict diabetes using regression model') service = Webservice.deploy_from_image(workspace=ws, name=web_service_name, image=image, deployment_config=aciconfig) service.wait_for_deployment(show_output=True) import json test_sample = json.dumps({ 'data': [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]] }) test_sample = bytes(test_sample, encoding='utf8') prediction = service.run(input_data=test_sample) print(prediction) for w in ws.list_webservices():
# In[80]: from azureml.core.webservice import AciWebservice aciconfig = AciWebservice.deploy_configuration(cpu_cores = 2, memory_gb = 2, tags = {"data": "mnist", "type": "classification"}, description = 'Image recognition') # In[96]: service = Webservice.deploy_from_image(workspace=ws, name='challenge5service3', deployment_config=aciconfig, image=image2) service.wait_for_deployment(show_output=True) print(service.state) # In[97]: print(service.get_logs()) # In[10]: import sys
from azureml.core.webservice import AciWebservice aciconfig = AciWebservice.deploy_configuration(cpu_cores = 1, memory_gb = 1, tags = {'area': "Credit scoring", 'type': "classification"}, description = 'Predict risk of credit default using Azure AutoML.') # COMMAND ---------- from azureml.core.webservice import Webservice aci_service_name = "credit-scoring-" + notebook_username print(aci_service_name) aci_service = Webservice.deploy_from_image(deployment_config = aciconfig, image = image, name = aci_service_name, workspace = ws) aci_service.wait_for_deployment(True) print(aci_service.state) # COMMAND ---------- aci_service # COMMAND ---------- # MAGIC %md ### Test web service # COMMAND ---------- # MAGIC %md Call the web service with some input data about potential credit applicants to get a prediction.
print('..9.completed') print('') print('') print('10. REST service creation on AKS cluster') # Create the service configuration (using defaults) aksConfig = AksWebservice.deploy_configuration(description=args.description, tags={ 'name': aksName, 'image_id': containerImage.id }) aksRestService = Webservice.deploy_from_image(workspace=amlWs, name=aksServiceName, image=containerImage, deployment_config=aksConfig, deployment_target=aksTarget) aksRestService.wait_for_deployment(show_output=True) print(aksRestService.state) print('..10.completed') print('') print('') print('11. Create output Json with Rest service details') api_key, _ = aksRestService.get_keys() print( "....Deployed AKS REST service: {} \nREST service Uri: {} \nREST service API Key: {}" .format(aksRestService.name, aksRestService.scoring_uri, api_key))
# exist_okay=True) azure_workspace = Workspace.get(name=workspace_name, subscription_id=subscription_id, resource_group=resource_group) # Build an Azure ML container image for deployment azure_image, azure_model = mlflow.azureml.build_image(model_uri="trained_model", workspace=azure_workspace, description="Wine regression model 1", synchronous=True) # If your image build failed, you can access build logs at the following URI: print("Access the following URI for build logs: {}".format(azure_image.image_build_log_uri)) # Deploy the container image to ACI webservice_deployment_config = AciWebservice.deploy_configuration() webservice = Webservice.deploy_from_image( image=azure_image, workspace=azure_workspace, name="wine-quality") webservice.wait_for_deployment() # After the image deployment completes, requests can be posted via HTTP to the new ACI # webservice's scoring URI. The following example posts a sample input from the wine dataset # used in the MLflow ElasticNet example: # https://github.com/mlflow/mlflow/tree/master/examples/sklearn_elasticnet_wine print("Scoring URI is: %s", webservice.scoring_uri) import requests import json # `sample_input` is a JSON-serialized pandas DataFrame with the `split` orientation sample_input = { "columns": [ "alcohol",
memory_gb=profiling_result["memory"], tags=deployment_settings["image"]["tags"], properties=deployment_settings["image"]["properties"], description=deployment_settings["image"]["description"], location=aci_settings["location"], auth_enabled=aci_settings["auth_enabled"], ssl_enabled=aci_settings["ssl_enabled"], ssl_cert_pem_file=aci_settings["ssl_cert_pem_file"], ssl_key_pem_file=aci_settings["ssl_key_pem_file"], ssl_cname=aci_settings["ssl_cname"], enable_app_insights=aci_settings["enable_app_insights"], dns_name_label=aci_settings["dns_name_label"]) # Deploying dev web service from image dev_service = Webservice.deploy_from_image(workspace=ws, name=aci_settings["name"], image=image, deployment_config=aci_config) # Show output of the deployment on stdout dev_service.wait_for_deployment(show_output=True) print("State of Service: {}".format(dev_service.state)) # Checking status of web service print("Checking status of ACI Dev Deployment") if dev_service.state != "Healthy": raise Exception( "Dev Deployment on ACI failed with the following status: {} and logs: \n{}".format( dev_service.state, dev_service.get_logs() ) )
from azureml.core import Workspace from azureml.core.webservice import AciWebservice, Webservice import mlflow.azureml ws = Workspace.from_config('config_devtalks.json') mlflow.set_tracking_uri(ws.get_mlflow_tracking_uri()) # Creating Image run_id1 = "2936f7ac-6a29-4e38-a12f-540ecac0e7af" model_uri = "runs:/" + run_id1 + "/model" model_image, azure_model = mlflow.azureml.build_image( model_uri=model_uri, workspace=ws, model_name="model_wine_dev_talks", image_name="model", description="wine_quality_model_devtalks", synchronous=False) model_image.wait_for_creation(show_output=True) # Create micro service dev_webservice_name = "elasticnet" dev_webservice_deployment_config = AciWebservice.deploy_configuration() dev_webservice = Webservice.deploy_from_image( name=dev_webservice_name, image=model_image, deployment_config=dev_webservice_deployment_config, workspace=ws) dev_webservice.wait_for_deployment()
if backend == "python" else "score_R_model.py", # must be in cwd runtime="python", conda_file="conda_file.yml", docker_file="docker_file", dependencies=["init.py"] if backend == "python" else ["install_package.R", "hmsPM"]) image = ContainerImage.create(workspace=ws, name="titanic-image", models=[model], image_config=image_config) image.wait_for_creation(show_output=True) print(image.image_build_log_uri) os.chdir(tmp) except Exception as e: os.chdir(tmp) print(e) # --- Create webservice --------------------------------------------------------------------------- aci_config = AciWebservice.deploy_configuration( cpu_cores=1, memory_gb=1, tags={'sample name': 'AML 101'}, description='This is a great example.') service = Webservice.deploy_from_image( workspace=ws, name='titanic-webservice-new', # crashes when already exist image=image, deployment_config=aci_config) service.wait_for_deployment(show_output=True) print(service.get_logs())