Webservice class

Definition

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.

Webservice(workspace, name)
Inheritance
builtins.object
Webservice

Parameters

workspace
Workspace

The workspace object containing the Webservice object to retrieve.

name
str

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 import Webservice
   from azureml.core.model import InferenceConfig
   from azureml.core.webservice import AciWebservice
   from azureml.exceptions import WebserviceException


   service_name = 'my-custom-env-service'

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

   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)
   service.wait_for_deployment(show_output=True)

Full sample is available from https://github.com/Azure/MachineLearningNotebooks/blob/master/how-to-use-azureml/deployment/deploy-to-cloud/model-register-and-deploy.ipynb

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

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
<xref:datetime.datetime>
When the Webservice was created.
azureml.core.Webservice.description
bool
A description of the Webservice object.
azureml.core.Webservice.tags
bool
A dictionary of tags for the Webservice object.
azureml.core.Webservice.name
bool
The name of the Webservice.
azureml.core.Webservice.properties
bool
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
bool
The current state of the Webservice.
updated_time
<xref:datetime.datetime>
The last time the Webservice was updated.
azureml.core.Webservice.workspace
bool
The Azure Machine Learning Workspace which contains this Webservice.
token_auth_enabled
bool
Whether or not the Webservice has token auth enabled.

Methods

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(workspace, name, model_paths, image_config, deployment_config=None, deployment_target=None, overwrite=False)

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(workspace, name, image, deployment_config=None, deployment_target=None, overwrite=False)

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(workspace, name, models, image_config, deployment_config=None, deployment_target=None, overwrite=False)

Deploy a Webservice from zero or more Model objects.

This function is similar to deploy(workspace, name, model_paths, image_config, deployment_config=None, deployment_target=None, overwrite=False), 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(workspace, name, models, image_config, deployment_config=None, wait=False)

Build and deploy a LocalWebservice for testing.

Requires Docker to be installed and configured.

deserialize(workspace, webservice_payload)

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(num_lines=5000)

Retrieve logs for this Webservice.

get_token()

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

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

List the Webservices associated with the corresponding Workspace.

Can be filtered with specific parameters.

regen_key(key, set_key=None)

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(input)

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(workspace, webservice_payload) to convert back into a Webservice object.

update(*args)

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(image=None, tags=None, properties=None, description=None, auth_enabled=None, ssl_enabled=None, ssl_cert_pem_file=None, ssl_key_pem_file=None, ssl_cname=None, enable_app_insights=None, models=None, inference_config=None) 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(show_output=False)

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.

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()

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

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(workspace, name, model_paths, image_config, deployment_config=None, deployment_target=None, overwrite=False)

Parameters

workspace
Workspace

A Workspace object to associate the Webservice with.

name
str

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]

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

image_config
ImageConfig

An ImageConfig object used to determine required Image properties.

deployment_config
azureml.core.webservice.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(workspace, name, image, deployment_config=None, deployment_target=None, overwrite=False)

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_image(workspace, name, image, deployment_config=None, deployment_target=None, overwrite=False)

Parameters

workspace
Workspace

A Workspace object to associate the Webservice with.

name
str

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

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(workspace, name, models, image_config, deployment_config=None, deployment_target=None, overwrite=False)

Deploy a Webservice from zero or more Model objects.

This function is similar to deploy(workspace, name, model_paths, image_config, deployment_config=None, deployment_target=None, overwrite=False), 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_from_model(workspace, name, models, image_config, deployment_config=None, deployment_target=None, overwrite=False)

Parameters

workspace
Workspace

A Workspace object to associate the Webservice with.

name
str

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]

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

image_config
ImageConfig

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(workspace, name, models, image_config, deployment_config=None, wait=False)

Build and deploy a LocalWebservice for testing.

Requires Docker to be installed and configured.

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

Parameters

workspace
Workspace

A Workspace object with which to associate the Webservice.

name
str

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

models
list[Model]

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

image_config
ImageConfig

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(workspace, webservice_payload)

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

Indicates that this is a class method.

workspace
Workspace

The workspace object the Webservice is registered under.

webservice_payload
dict

A JSON object to convert to a Webservice object.

Returns

The Webservice representation of the provided JSON object.

Return type

get_keys()

Retrieve auth keys for this Webservice.

get_keys()

Returns

The auth keys for this Webservice.

Return type

(str, str)

get_logs(num_lines=5000)

Retrieve logs for this Webservice.

get_logs(num_lines=5000)

Parameters

num_lines
int
default value: 5000

The maximum number of log lines to retrieve.

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

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

List the Webservices associated with the corresponding Workspace.

Can be filtered with specific parameters.

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

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(key, set_key=None)

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

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(input)

Call this Webservice with the provided input.

Abstract method implemented by child classes of Webservice.

run(input)

Parameters

input
varies

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(workspace, webservice_payload) to convert back into a Webservice object.

serialize()

Returns

The JSON representation of this Webservice.

Return type

update(*args)

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(image=None, tags=None, properties=None, description=None, auth_enabled=None, ssl_enabled=None, ssl_cert_pem_file=None, ssl_key_pem_file=None, ssl_cname=None, enable_app_insights=None, models=None, inference_config=None) for specific parameters.

update(*args)

Parameters

args
varies

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()

wait_for_deployment(show_output=False)

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.

wait_for_deployment(show_output=False)

Parameters

show_output
bool
default value: False

Indicates whether to print more verbose output.

Exceptions