Webservice Class

Defines base functionality for deploying models as web service endpoints in Azure Machine Learning.

Webservice constructor is used to retrieve a cloud representation of a Webservice object associated with the provided Workspace. Returns an instance of a child class corresponding to the specific type of the retrieved Webservice object. The Webservice class allows for deploying machine learning models from either a Model or Image object.

For more information about working with Webservice, see Deploy models with Azure Machine Learning.

Initialize the Webservice instance.

The Webservice constructor retrieves a cloud representation of a Webservice object associated with the provided workspace. It will return an instance of a child class corresponding to the specific type of the retrieved Webservice object.

Inheritance
Webservice

Constructor

Webservice(workspace, name)

Parameters

workspace
Workspace
Required

The workspace object containing the Webservice object to retrieve.

name
str
Required

The name of the of the Webservice object to retrieve.

workspace
Workspace
Required

The workspace object containing the Webservice object to retrieve.

name
str
Required

The name of the of the Webservice object to retrieve.

Remarks

The following sample shows the recommended deployment pattern where you first create a configuration object with the deploy_configuration method of the child class of Webservice (in this case AksWebservice) and then use the configuration with the deploy method of the Model class.


   # Set the web service configuration (using default here)
   aks_config = AksWebservice.deploy_configuration()

   # # Enable token auth and disable (key) auth on the webservice
   # aks_config = AksWebservice.deploy_configuration(token_auth_enabled=True, auth_enabled=False)

Full sample is available from https://github.com/Azure/MachineLearningNotebooks/blob/master/how-to-use-azureml/deployment/production-deploy-to-aks/production-deploy-to-aks.ipynb

The following sample shows how to find an existing AciWebservice in a workspace and delete it if it exists so the name can be reused.


   from azureml.core.model import InferenceConfig
   from azureml.core.webservice import AciWebservice


   service_name = 'my-custom-env-service'

   inference_config = InferenceConfig(entry_script='score.py', environment=environment)
   aci_config = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1)

   service = Model.deploy(workspace=ws,
                          name=service_name,
                          models=[model],
                          inference_config=inference_config,
                          deployment_config=aci_config,
                          overwrite=True)
   service.wait_for_deployment(show_output=True)

There are a number of ways to deploy a model as a webservice, including with the:

  • deploy method of the Model for models already registered in the workspace.

  • deploy_from_image method of Webservice for images already created from a model.

  • deploy_from_model method of Webservice for models already registered in the workspace. This method will create an image.

  • deploy method of the Webservice, which will register a model and create an image.

For information on working with webservices, see

The Variables section lists attributes of a local representation of the cloud Webservice object. These variables should be considered read-only. Changing their values will not be reflected in the corresponding cloud object.

Variables

auth_enabled
bool

Whether or not the Webservice has auth enabled.

compute_type
str

What type of compute the Webservice is deployed to.

created_time
datetime

When the Webservice was created.

azureml.core.Webservice.description

A description of the Webservice object.

azureml.core.Webservice.tags

A dictionary of tags for the Webservice object.

azureml.core.Webservice.name

The name of the Webservice.

azureml.core.Webservice.properties

Dictionary of key value properties for the Webservice. These properties cannot be changed after deployment, however new key value pairs can be added.

created_by
str

The user that created the Webservice.

error
str

If the Webservice failed to deploy, this will contain the error message for why it failed.

azureml.core.Webservice.state

The current state of the Webservice.

updated_time
datetime

The last time the Webservice was updated.

azureml.core.Webservice.workspace

The Azure Machine Learning Workspace which contains this Webservice.

token_auth_enabled
bool

Whether or not the Webservice has token auth enabled.

Methods

check_for_existing_webservice

Check webservice exists.

delete

Delete this Webservice from its associated workspace.

This function call is not asynchronous. The call runs until the resource is deleted. A WebserviceException is raised if there is a problem deleting the model from the Model Management Service.

deploy

Deploy a Webservice from zero or more Model objects.

This function will register any models files provided and create an image in the process, all associated with the specified Workspace. Use this function when you have a directory of models to deploy that haven't been previously registered.

The resulting Webservice is a real-time endpoint that can be used for inference requests. For more information, see Consume a model deployed as a web service.

deploy_from_image

Deploy a Webservice from an Image object.

Use this function if you already have an Image object created for a model.

The resulting Webservice is a real-time endpoint that can be used for inference requests. For more information, see Consume a model deployed as a web service.

deploy_from_model

Deploy a Webservice from zero or more Model objects.

This function is similar to deploy, but does not register the models. Use this function if you have model objects that are already registered. This will create an image in the process, associated with the specified Workspace.

The resulting Webservice is a real-time endpoint that can be used for inference requests. For more information, see Consume a model deployed as a web service.

deploy_local_from_model

Build and deploy a LocalWebservice for testing.

Requires Docker to be installed and configured.

deserialize

Convert a Model Management Service response JSON object into a Webservice object.

Will fail if the provided workspace is not the workspace the Webservice is registered under.

get_keys

Retrieve auth keys for this Webservice.

get_logs

Retrieve logs for this Webservice.

get_token

Retrieve auth token for this Webservice, scoped to the current user.

list

List the Webservices associated with the corresponding Workspace.

The results returned can be filtered using parameters.

regen_key

Regenerate one of the Webservice's keys, either the 'Primary' or 'Secondary' key.

A WebserviceException is raised if key is not specified or is not 'Primary' or 'Secondary'.

run

Call this Webservice with the provided input.

Abstract method implemented by child classes of Webservice.

serialize

Convert this Webservice object into a JSON serialized dictionary.

Use deserialize to convert back into a Webservice object.

update

Update the Webservice parameters.

This is an abstract method implemented by child classes of Webservice. Possible parameters to update vary based on Webservice child type. For example, for Azure Container Instances webservices, see update for specific parameters.

update_deployment_state

Refresh the current state of the in-memory object.

Perform an in-place update of the properties of the object based on the current state of the corresponding cloud object. Primarily useful for manual polling of creation state.

wait_for_deployment

Automatically poll on the running Webservice deployment.

Wait for the Webservice to reach a terminal state. Will throw a WebserviceException if it reaches a non-successful terminal state or exceeds the provided timeout.

check_for_existing_webservice

Check webservice exists.

static check_for_existing_webservice(workspace, name, overwrite=False, request_func=None, check_func=None)

Parameters

workspace
Workspace
Required
name
str
Required
overwrite
bool
default value: False
request_func
<xref:function>
default value: None

function to request service to check if service name exists

check_func
<xref:function>
default value: None

function to check response content of request_func

Exceptions

delete

Delete this Webservice from its associated workspace.

This function call is not asynchronous. The call runs until the resource is deleted. A WebserviceException is raised if there is a problem deleting the model from the Model Management Service.

delete()

Exceptions

deploy

Deploy a Webservice from zero or more Model objects.

This function will register any models files provided and create an image in the process, all associated with the specified Workspace. Use this function when you have a directory of models to deploy that haven't been previously registered.

The resulting Webservice is a real-time endpoint that can be used for inference requests. For more information, see Consume a model deployed as a web service.

static deploy(workspace, name, model_paths, image_config, deployment_config=None, deployment_target=None, overwrite=False)

Parameters

workspace
Workspace
Required

A Workspace object to associate the Webservice with.

name
str
Required

The name to give the deployed service. Must be unique to the workspace, only consist of lowercase letters, numbers, or dashes, start with a letter, and be between 3 and 32 characters long.

model_paths
list[str]
Required

A list of on-disk paths to model files or folder. Can be an empty list.

image_config
ImageConfig
Required

An ImageConfig object used to determine required Image properties.

deployment_config
WebserviceDeploymentConfiguration
default value: None

A WebserviceDeploymentConfiguration used to configure the webservice. If one is not provided, an empty configuration object will be used based on the desired target.

deployment_target
ComputeTarget
default value: None

A ComputeTarget to deploy the Webservice to. As Azure Container Instances has no associated ComputeTarget, leave this parameter as None to deploy to Azure Container Instances.

overwrite
bool
default value: False

Overwrite the existing service if service with name already exists.

Returns

A Webservice object corresponding to the deployed webservice.

Return type

Exceptions

deploy_from_image

Deploy a Webservice from an Image object.

Use this function if you already have an Image object created for a model.

The resulting Webservice is a real-time endpoint that can be used for inference requests. For more information, see Consume a model deployed as a web service.

static deploy_from_image(workspace, name, image, deployment_config=None, deployment_target=None, overwrite=False)

Parameters

workspace
Workspace
Required

A Workspace object to associate the Webservice with.

name
str
Required

The name to give the deployed service. Must be unique to the workspace, only consist of lowercase letters, numbers, or dashes, start with a letter, and be between 3 and 32 characters long.

image
Image
Required

An Image object to deploy.

deployment_config
WebserviceDeploymentConfiguration
default value: None

A WebserviceDeploymentConfiguration used to configure the webservice. If one is not provided, an empty configuration object will be used based on the desired target.

deployment_target
ComputeTarget
default value: None

A ComputeTarget to deploy the Webservice to. As Azure Container Instances has no associated ComputeTarget, leave this parameter as None to deploy to Azure Container Instances.

overwrite
bool
default value: False

Overwrite the existing service if service with name already exists.

Returns

A Webservice object corresponding to the deployed webservice.

Return type

Exceptions

deploy_from_model

Deploy a Webservice from zero or more Model objects.

This function is similar to deploy, but does not register the models. Use this function if you have model objects that are already registered. This will create an image in the process, associated with the specified Workspace.

The resulting Webservice is a real-time endpoint that can be used for inference requests. For more information, see Consume a model deployed as a web service.

static deploy_from_model(workspace, name, models, image_config, deployment_config=None, deployment_target=None, overwrite=False)

Parameters

workspace
Workspace
Required

A Workspace object to associate the Webservice with.

name
str
Required

The name to give the deployed service. Must be unique to the workspace, only consist of lowercase letters, numbers, or dashes, start with a letter, and be between 3 and 32 characters long.

models
list[Model]
Required

A list of model objects. Can be an empty list.

image_config
ImageConfig
Required

An ImageConfig object used to determine required Image properties.

deployment_config
WebserviceDeploymentConfiguration
default value: None

A WebserviceDeploymentConfiguration used to configure the webservice. If one is not provided, an empty configuration object will be used based on the desired target.

deployment_target
ComputeTarget
default value: None

A ComputeTarget to deploy the Webservice to. As ACI has no associated ComputeTarget, leave this parameter as None to deploy to ACI.

overwrite
bool
default value: False

Overwrite the existing service if service with name already exists.

Returns

A Webservice object corresponding to the deployed webservice.

Return type

Exceptions

deploy_local_from_model

Build and deploy a LocalWebservice for testing.

Requires Docker to be installed and configured.

static deploy_local_from_model(workspace, name, models, image_config, deployment_config=None, wait=False)

Parameters

workspace
Workspace
Required

A Workspace object with which to associate the Webservice.

name
str
Required

The name to give the deployed service. Must be unique on the local machine.

models
list[Model]
Required

A list of model objects. Can be an empty list.

image_config
ImageConfig
Required

An ImageConfig object used to determine required service image properties.

deployment_config
LocalWebserviceDeploymentConfiguration
default value: None

A LocalWebserviceDeploymentConfiguration used to configure the webservice. If one is not provided, an empty configuration object will be used.

wait
bool
default value: False

Whether to wait for the LocalWebservice's Docker container to report as healthy. Throws an exception if the container crashes. The default is False.

Return type

Exceptions

deserialize

Convert a Model Management Service response JSON object into a Webservice object.

Will fail if the provided workspace is not the workspace the Webservice is registered under.

deserialize(workspace, webservice_payload)

Parameters

cls
Required

Indicates that this is a class method.

workspace
Workspace
Required

The workspace object the Webservice is registered under.

webservice_payload
dict
Required

A JSON object to convert to a Webservice object.

Returns

The Webservice representation of the provided JSON object.

Return type

Exceptions

get_keys

Retrieve auth keys for this Webservice.

get_keys()

Returns

The auth keys for this Webservice.

Return type

(str, str)

Exceptions

get_logs

Retrieve logs for this Webservice.

get_logs(num_lines=5000, init=False)

Parameters

num_lines
int
default value: 5000

The maximum number of log lines to retrieve.

init
bool
default value: False

Get logs of init container

Returns

The logs for this Webservice.

Return type

str

Exceptions

get_token

Retrieve auth token for this Webservice, scoped to the current user.

get_token()

Returns

The auth token for this Webservice and when it should be refreshed after.

Return type

Exceptions

list

List the Webservices associated with the corresponding Workspace.

The results returned can be filtered using parameters.

static list(workspace, compute_type=None, image_name=None, image_id=None, model_name=None, model_id=None, tags=None, properties=None, image_digest=None)

Parameters

workspace
Workspace
Required

The Workspace object to list the Webservices in.

compute_type
str
default value: None

Filter to list only specific Webservice types. Options are 'ACI', 'AKS'.

image_name
str
default value: None

Filter list to only include Webservices deployed with the specific image name.

image_id
str
default value: None

Filter list to only include Webservices deployed with the specific image ID.

model_name
str
default value: None

Filter list to only include Webservices deployed with the specific model name.

model_id
str
default value: None

Filter list to only include Webservices deployed with the specific model ID.

tags
list
default value: None

Filter based on the provided list, by either 'key' or '[key, value]'. Ex. ['key', ['key2', 'key2 value']]

properties
list
default value: None

Filter based on the provided list, by either 'key' or '[key, value]'. Ex. ['key', ['key2', 'key2 value']]

image_digest
str
default value: None

Filter list to only include Webservices deployed with the specific image digest.

Returns

A filtered list of Webservices in the provided Workspace.

Return type

Exceptions

regen_key

Regenerate one of the Webservice's keys, either the 'Primary' or 'Secondary' key.

A WebserviceException is raised if key is not specified or is not 'Primary' or 'Secondary'.

regen_key(key, set_key=None)

Parameters

key
str
Required

The key to regenerate. Options are 'Primary' or 'Secondary'.

set_key
str
default value: None

A user specified value allowing for manual specification of the key's value

Exceptions

run

Call this Webservice with the provided input.

Abstract method implemented by child classes of Webservice.

abstract run(input)

Parameters

input
<xref:varies>
Required

The input data to call the Webservice with. This is the data your machine learning model expects as an input to run predictions.

Returns

The result of calling the Webservice. This will return predictions run from your machine learning model.

Return type

Exceptions

serialize

Convert this Webservice object into a JSON serialized dictionary.

Use deserialize to convert back into a Webservice object.

serialize()

Returns

The JSON representation of this Webservice.

Return type

Exceptions

update

Update the Webservice parameters.

This is an abstract method implemented by child classes of Webservice. Possible parameters to update vary based on Webservice child type. For example, for Azure Container Instances webservices, see update for specific parameters.

abstract update(*args)

Parameters

args
<xref:varies>
Required

Values to update.

Exceptions

update_deployment_state

Refresh the current state of the in-memory object.

Perform an in-place update of the properties of the object based on the current state of the corresponding cloud object. Primarily useful for manual polling of creation state.

update_deployment_state()

Exceptions

wait_for_deployment

Automatically poll on the running Webservice deployment.

Wait for the Webservice to reach a terminal state. Will throw a WebserviceException if it reaches a non-successful terminal state or exceeds the provided timeout.

wait_for_deployment(show_output=False, timeout_sec=None)

Parameters

show_output
bool
default value: False

Indicates whether to print more verbose output.

timeout_sec
float
default value: None

Raise an exception if deployment exceeds the given timeout.

Exceptions