예제 #1
0
    def predict(self, deployment_name, df):
        """
        Predict on the specified deployment using the provided dataframe.

        Compute predictions on the pandas DataFrame ``df`` using the specified deployment.
        Note that the input/output types of this method matches that of `mlflow pyfunc predict`
        (we accept a pandas DataFrame as input and return either a pandas DataFrame,
        pandas Series, or numpy array as output).

        :param deployment_name: Name of deployment to predict against
        :param df: Pandas DataFrame to use for inference
        :return: A pandas DataFrame, pandas Series, or numpy array
        """
        try:
            service = Webservice(self.workspace, deployment_name)
        except Exception as e:
            raise MlflowException(
                'Failure retrieving deployment to predict against') from e

        # Take in DF, parse to json using split orient
        input_data = _get_jsonable_obj(df, pandas_orient='split')

        if not service.scoring_uri:
            raise MlflowException(
                'Error attempting to call webservice, scoring_uri unavailable. '
                'This could be due to a failed deployment, or the service is not ready yet.\n'
                'Current State: {}\n'
                'Errors: {}'.format(service.state, service.error))

        # Pass split orient json to webservice
        # Take records orient json from webservice
        resp = ClientBase._execute_func(service._webservice_session.post,
                                        service.scoring_uri,
                                        data=json.dumps(
                                            {'input_data': input_data}))

        if resp.status_code == 401:
            if service.auth_enabled:
                service_keys = service.get_keys()
                service._session.headers.update(
                    {'Authorization': 'Bearer ' + service_keys[0]})
            elif service.token_auth_enabled:
                service_token, refresh_token_time = service.get_access_token()
                service._refresh_token_time = refresh_token_time
                service._session.headers.update(
                    {'Authorization': 'Bearer ' + service_token})
            resp = ClientBase._execute_func(service._webservice_session.post,
                                            service.scoring_uri,
                                            data=input_data)

        if resp.status_code == 200:
            # Parse records orient json to df
            return parse_json_input(json.dumps(resp.json()), orient='records')
        else:
            raise MlflowException('Failure during prediction:\n'
                                  'Response Code: {}\n'
                                  'Headers: {}\n'
                                  'Content: {}'.format(resp.status_code,
                                                       resp.headers,
                                                       resp.content))
예제 #2
0
def update_deployed_model(ws, aci_service_name, model_name, mlapp_env,
                          entry_script):
    inference_config = InferenceConfig(source_directory=os.getcwd(),
                                       entry_script=entry_script,
                                       environment=mlapp_env)

    model = Model(ws, name=model_name)
    service = Webservice(name=aci_service_name, workspace=ws)
    service.update(models=[model], inference_config=inference_config)

    print(service.state)
    print(service.get_logs())
예제 #3
0
    def get_deployment(self, name):
        """
        Retrieve details for the specified deployment.

        Returns a dictionary describing the specified deployment. The dict is guaranteed to contain an 'name' key
        containing the deployment name.

        :param name: Name of deployment to retrieve
        """
        try:
            service = Webservice(self.workspace, name)
            return service.serialize()
        except WebserviceException as e:
            raise MlflowException(
                'There was an error retrieving the deployment: \n{}'.format(
                    e.message)) from e
예제 #4
0
def test_deployed_model_service():
    service = Webservice(ws, deployment_name)
    assert service is not None

    key1, key2 = service.get_keys()
    uri = service.scoring_uri

    assert key1 is not None
    assert uri.startswith('http')

    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {key1}'
    }
    response = requests.post(uri, test_sample, headers=headers)
    assert response.status_code is 200
    assert abs(1 - sum(response.json()['predict_proba'][0])) < 0.01
예제 #5
0
    def delete_deployment(self, name):
        """
        Delete the deployment with name ``name``.

        :param name: Name of deployment to delete
        :return: None
        """
        try:
            service = Webservice(self.workspace, name)
            service.delete()
        except WebserviceException as e:
            if 'WebserviceNotFound' not in e.message:
                _logger.info(
                    'Deployment with name {} not found, no service to delete'.
                    format(name))
                return
            raise MlflowException(
                'There was an error deleting the deployment: \n{}'.format(
                    e.message)) from e
예제 #6
0
    def list_deployments(self):
        """
        List deployments.

        :return: A list of dicts corresponding to deployments.
        """
        try:
            service_list = []
            services = Webservice.list(self.workspace)
            for service in services:
                service_list.append(service.serialize())
            return service_list
        except WebserviceException as e:
            raise MlflowException(
                'There was an error listing deployments: \n{}'.format(
                    e.message)) from e
예제 #7
0
def create_aks_service(name: str, image_config: ImageConfig,
                       models: List[Model], target: ComputeTarget,
                       ws: Workspace) -> Webservice:

    print("Loading AKS deploy config from deployconfig_aks.yml")
    deploy_conf = AksWebservice.deploy_configuration()
    print(models)
    service = Webservice.deploy_from_model(workspace=ws,
                                           name=name,
                                           deployment_target=target,
                                           models=models,
                                           deployment_config=deploy_conf,
                                           image_config=image_config)

    service.wait_for_deployment(show_output=True)
    return service
    def __init__(self, CONTAINER_NAME, BLOB_NAME, REQUEST_ID):
        """
		First, we will initialize different variables that we will use in the code below
		"""

        self.CONTAINER_NAME = CONTAINER_NAME
        self.BLOB_NAME = BLOB_NAME
        self.REQUEST_ID = REQUEST_ID
        # "container042021"

        self.blob_service_client = BlobServiceClient.from_connection_string(
            conn_str=conf.AZURE_STORAGE_CONNECTION_STRING)
        self.container_client = self.blob_service_client.get_container_client(
            self.CONTAINER_NAME)

        interactive_auth = InteractiveLoginAuthentication(
            tenant_id=conf.TENANT_ID)
        self.ws = Workspace.from_config('resources', auth=interactive_auth)

        self.service = Webservice(workspace=self.ws, name='mynewservice')
예제 #9
0
def main(args, ws):
    dir = os.path.dirname(os.path.abspath(__file__))
    ic = InferenceConfig(runtime='python',
                         source_directory=dir,
                         entry_script='score.py',
                         conda_file='environment.yml')

    dc = AciWebservice.deploy_configuration(cpu_cores=args.cores,
                                            memory_gb=args.memory)
    m = ws.models[args.model_name]

    # Remove any existing service under the same name.
    try:
        Webservice(ws, args.service_name).delete()
        print(f'Deleted webservice with name {args.service_name}.')
    except WebserviceException:
        pass

    service = Model.deploy(ws, args.service_name, [m], ic, dc)
    service.wait_for_deployment(True)
    print(service.state)
    print(service.get_logs())
예제 #10
0
# Get environment
workspace = Workspace.from_config()
environment = Environment.get(workspace=workspace, name=METADATA['env_name'])

# Deploy container
inference_config = InferenceConfig(
    entry_script='./src/score.py',
    environment=environment,
)
aci_config = AciWebservice.deploy_configuration(
    cpu_cores=CPU_CORES, memory_gb=MEMORY_GB, description=SERVICE_DESCRIPTION)

# Deploy as web service
try:
    # Remove any existing service under the same name.
    Webservice(workspace, SERVICE_NAME).delete()
except WebserviceException:
    pass

model = Model(workspace, METADATA['model_name'])

webservice = Model.deploy(workspace=workspace,
                          name=SERVICE_NAME,
                          models=[model],
                          inference_config=inference_config,
                          deployment_config=aci_config,
                          overwrite=True)

webservice.wait_for_deployment(show_output=True)

webservice.get_logs()
예제 #11
0
# Authentication via service principle
ws = Workspace.get(
    args.ws,
    ServicePrincipalAuthentication(
        tenant_id=os.getenv('tenant_id'),
        service_principal_id=os.getenv('service_principal_id'),
        service_principal_password=os.getenv('service_principal_password')),
    subscription_id=os.getenv('subscription_id'),
    resource_group=args.rg)

model = Model(ws, "new_model")
deployment_target = ComputeTarget(ws, args.aksname)

img = create_image_config("score.py", "scoringenv.yml")

servicename = args.servicename
try:
    service = Webservice(ws, servicename)
except Exception as e:
    print(e)
    service = None
if service:
    print("Updating existing service with new image...")
    try:
        # create new image
        service = update_service(service, [model], img, ws)
    except Exception as e:
        print("Unable to link existing service.\n {}".format(e))
else:
    create_aks_service(servicename, img, [model], deployment_target, ws)
예제 #12
0
)
parser.add_argument(
    "--webservicename",
    type=str,
    default="freezerchain-prediction-v0-2",
    help="Name of the deployed Webservice",
)
args = parser.parse_args()

run = Run.get_context()
ws = run.experiment.workspace

freezer_environment = ws.environments["sktime_freezer_environment"]

try:
    service = Webservice(ws, args.webservicename)
except WebserviceException:
    service = None

if args.redeploy:
    if service is not None:
        service.delete()
        print("deleted existing Webservice.")

    model = Model(ws, "sktime_freezer_classifier")

    inference_config = InferenceConfig(
        entry_script="score.py", source_directory="./", environment=freezer_environment
    )

    aci_config = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1)
          memory_gb = 1,
          auth_enabled=False, #passer à True pour enclencher la sécurité par clés
          tags = {"data": "New York citibike", "method": "sklearn regression"},
          description = "Trip duration regression")

# COMMAND ----------

from azureml.core import Webservice
from azureml.core.webservice import AciWebservice
from azureml.exceptions import WebserviceException

service_name = 'dbx-tripduration-service'

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

service = Model.deploy(workspace=ws,
                       name=service_name,
                       models=[model],
                       inference_config=inference_config,
                       deployment_config=aciconfig)

service.wait_for_deployment(show_output=True)

# COMMAND ----------

from azureml.core.image import ContainerImage
예제 #14
0
import json

import requests
from azureml.core import Webservice, Workspace

from config_deepensemble_1 import CONFIG

from cgmml.common.data_utilities import mlpipeline_utils

if __name__ == "__main__":
    if CONFIG.LOCALTEST:
        uri = 'http://localhost:6789/'
    else:
        workspace = Workspace.from_config()
        service = Webservice(workspace=workspace, name=CONFIG.ENDPOINT_NAME)
        uri = service.scoring_uri

    requests.get(uri)
    depthmap = mlpipeline_utils.get_depthmaps(
        CONFIG.TEST_FILES).tolist()  # Make JSON serializable

    headers = {"Content-Type": "application/json"}
    data = {
        "data": depthmap,
    }

    data = json.dumps(data)

    response = requests.post(uri, data=data, headers=headers)
    print(response.json())

from azureml.core import Workspace
ws = Workspace.from_config()
print(ws)


# ## Find Web Service by Name and get Connection Details 

# We select the web service by the name and this will provide us information on the URLs such as scoring and [swagger](https://swagger.io/) calls

# In[ ]:


from azureml.core import Webservice
service = Webservice(workspace=ws, name='<insert web service name here>')
print("Score URI: " + str(service.scoring_uri))
print("Swagger URI: " + str(service.swagger_uri))

primary, secondary = service.get_keys()
print(primary)


# ## Data Input to REST API Schema 

# Now we need to understand the schema of the data to be input into the REST call.
# 
# You can get this sample input information from the auto-generated scoring script that was created in the Azure Portal

# In[ ]:
import requests
import json
from azureml.core import Webservice
from azureml.core import Workspace

# URL for the web service
scoring_uri = 'http://97d4b24f-8f41-46e7-b875-32d30bfb8deb.westus2.azurecontainer.io/score'
ws = Workspace.get('Mohith_workspace',
                   auth=None,
                   subscription_id='c229fecc-7c30-489c-b79f-1de37cd2de58',
                   resource_group='Mohith_group')
print(ws)

services = Webservice.list(ws)
print(services)
print(services[0].scoring_uri)
print(services[0].swagger_uri)

service = services[0]
print(service)

# Set the content type
headers = {'Content-Type': 'application/json'}

if service.auth_enabled:
    headers['Authorization'] = 'Bearer ' + service.get_keys()[0]
elif service.token_auth_enabled:
    headers['Authorization'] = 'Bearer ' + service.get_token()[0]
# If the service is authenticated, set the key or token
key = service.get_keys()[0]
          memory_gb = 1,
          auth_enabled=False, #passer à True pour enclencher la sécurité par clés
          tags = {"data": "New York citibike", "method": "sklearn ridge regression"},
          description = "Trip duration regression")

# COMMAND ----------

from azureml.core import Webservice
from azureml.exceptions import WebserviceException


service_name = 'tripduration-service'

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

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)

# COMMAND ----------

print(service.get_logs())
예제 #18
0
파일: deploy.py 프로젝트: nerav-doshi/mlapp
def deploy_model(ws, aci_service_name, experiment_name, asset_name,
                 asset_label, run_id, cpu_cores, memory_gb, entry_script):
    env = create_env_from_requirements(endpoint=True)
    inference_config = InferenceConfig(source_directory=os.getcwd(),
                                       entry_script=entry_script,
                                       environment=env)

    deployment_config = AciWebservice.deploy_configuration(cpu_cores=cpu_cores,
                                                           memory_gb=memory_gb)

    # model name
    model_name = get_model_register_name(run_id)
    try:
        model = Model(ws, name=model_name)
    except:
        # creating directory for download Model files for Model register
        tmp_path = create_tempdir(name='download_tmp')
        register_path = create_directory(AML_MLAPP_FOLDER, path=tmp_path)

        # getting RUN context
        experiment = Experiment(workspace=ws, name=experiment_name)
        tags = {"run_id": run_id, "asset_name": asset_name}
        if asset_label is not None:
            tags["asset_label"] = asset_label

        selected_run_id = None
        for run in Run.list(experiment,
                            tags=tags,
                            include_children=True,
                            status='Completed'):
            run_metrics = run.get_metrics()
            exp_saved_run_id = run_metrics.get("run_id")
            if exp_saved_run_id == run_id:
                selected_run_id = run.id
                break
        if selected_run_id is None:
            raise Exception(
                'ERROR: there is no matching Run object that associated with the run id %s in this experiment.'
                % str(run_id))
        current_run = Run(experiment=experiment, run_id=selected_run_id)

        # download files from run object
        current_run.download_files(output_directory=register_path)

        # register model
        model = Model.register(ws,
                               model_path=register_path,
                               model_name=model_name,
                               tags=tags,
                               description=asset_name)

        # deletes tmp dir and all content
        delete_directory_with_all_contents(tmp_path)

    # deploy model
    service = None
    try:
        service = Webservice(ws, name=aci_service_name)
        service.update(models=[model], inference_config=inference_config)
    except WebserviceException as e:
        if service:
            service.delete()
        service = Model.deploy(ws, aci_service_name, [model], inference_config,
                               deployment_config)

    service.wait_for_deployment(True)
예제 #19
0
    def update_deployment(self,
                          name,
                          model_uri=None,
                          flavor=None,
                          config=None):
        """
        Update the deployment specified by name.

        Update the deployment with the specified name. You can update the URI of the model, the
        flavor of the deployed model (in which case the model URI must also be specified), and/or
        any target-specific attributes of the deployment (via `config`). By default, this method
        should block until deployment completes (i.e. until it's possible to perform inference
        with the updated deployment). See target-specific plugin documentation for additional
        detail on support for asynchronous deployment and other configuration.

        :param name: Unique name of deployment to update
        :param model_uri: URI of a new model to deploy.
        :param flavor: (optional) new model flavor to use for deployment. If provided,
                       ``model_uri`` must also be specified. If ``flavor`` is unspecified but
                       ``model_uri`` is specified, a default flavor will be chosen and the
                       deployment will be updated using that flavor.
        :param config: (optional) dict containing updated target-specific configuration for the
                       deployment
        :return: None
        """
        try:
            service = Webservice(self.workspace, name)
        except Exception as e:
            raise MlflowException(
                'Error retrieving deployment to update') from e

        models = None
        inference_config = None

        with TempDir(chdr=True) as tmp_dir:
            if model_uri:
                model_name, model_version = handle_model_uri(model_uri, name)
                try:
                    aml_model = Model(self.workspace,
                                      id='{}:{}'.format(
                                          model_name, model_version))
                except Exception as e:
                    raise MlflowException(
                        'Failed to retrieve model to deploy') from e
                models = [aml_model]

                inference_config = create_inference_config(
                    tmp_dir, model_name, model_version, name)

            deploy_config = None
            if 'deploy-config-file' in config:
                try:
                    # TODO: Tags, properties, and description are not in the config file for some reason?
                    with open(config['deploy-config-file'],
                              'r') as deploy_file_stream:
                        deploy_config_obj = file_stream_to_object(
                            deploy_file_stream)
                        deploy_config = deploy_config_dict_to_obj(
                            deploy_config_obj, None, None, None)
                except Exception as e:
                    raise MlflowException(
                        'Failed to parse provided deployment config file'
                    ) from e

            aks_endpoint_version_config = None  # TODO deployment or version/service? Talk to PMs
            if 'aks-endpoint-deployment-config' in config:
                aks_endpoint_version_config = config[
                    'aks-endpoint-deployment-config']

            try:
                submit_update_call(service, models, inference_config,
                                   deploy_config, aks_endpoint_version_config)

                if 'async' in config and config['async']:
                    _logger.info(
                        'AzureML deployment in progress, you can use get_deployment to check on the current '
                        'deployment status.')
                else:
                    service.wait_for_deployment(show_output=True)
            except Exception as e:
                raise MlflowException(
                    'Error submitting deployment update') from e
예제 #20
0
# In[ ]:

from azureml.core import Workspace

ws = Workspace.from_config()
print(ws)

# ## Find Web Service by Name and get Connection Details

# We select the web service by the name and this will provide us information on the URLs such as scoring and [swagger](https://swagger.io/) calls

# In[ ]:

from azureml.core import Webservice

service = Webservice(workspace=ws, name='<insert web service name here>')
print("Score URI: " + str(service.scoring_uri))
print("Swagger URI: " + str(service.swagger_uri))

# ## Data Input to REST API Schema

# Now we need to understand the schema of the data to be input into the REST call.
#
# You can get this sample input information from the auto-generated scoring script that was created in the Azure Portal

# In[ ]:

import pandas as pd

input_sample = pd.DataFrame(data=[{
    'Escalated': 0,
예제 #21
0
# Set the web service configuration (using default here)
aks_config = AksWebservice.deploy_configuration(
    cpu_cores=2,
    auth_enabled=True,  # this flag generates API keys to secure access
    memory_gb=8,
    #tags={'name': 'mnist', 'framework': 'Keras'},
    #max_request_wait_time=300000,scoring_timeout_ms=300000,
    description='X-Ray ML Estimator AKS endpoint')

#########################################################################################################

service_name = 'mlops-estimator-model-aks'

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

aks_service = Model.deploy(workspace=ws,
                           name=service_name,
                           models=[model],
                           inference_config=inference_config,
                           deployment_config=aks_config,
                           deployment_target=aks_gpu_cluster)

#########################################################################################################

aks_service.wait_for_deployment(show_output=True)
print(aks_service.state)
예제 #22
0
from azureml.core import Workspace
from azureml.core.authentication import AzureCliAuthentication
from azureml.core import Webservice
from azureml.core.model import InferenceConfig
from azureml.core.webservice import AciWebservice
from azureml.exceptions import WebserviceException
import json
import numpy

# load Azure ML workspace
azureml_workspace = Workspace.from_config(auth=AzureCliAuthentication())

# the name of the scoring service
service_name = 'comments-clf-westeurope'

service = Webservice(azureml_workspace, service_name)

input_payload = json.dumps({"data": "this is some text about gaming"})

request = json.loads(input_payload)["data"]

output = service.run(input_payload)

print(output)
from azureml.core import Workspace
from azureml.core import Webservice
ws = Workspace.from_config()

service = Webservice(ws, 'lpr')
scoring_uri = service.scoring_ui
primary, secondary = service.get_keys()
print(primary)
print(service.get_logs())
예제 #24
0
    },
    properties=None,
    description=
    "Sktime classifier to predict if freezer chain was interrupted.",
    datasets=[("training_data", dataset)],
    resource_configuration=ResourceConfiguration(cpu=1, memory_in_gb=0.5),
)
logger.info("Model registered")

# DEPLOY

logger.info("Starting deployment")
freezer_environment = ws.environments["sktime_freezer_environment"]

try:
    Webservice(ws, SERVICENAME).delete()
    print("deleted existing Webservice.")
except WebserviceException:
    pass

service = Model.deploy(
    workspace=ws,
    name=SERVICENAME,
    models=[model],
    inference_config=InferenceConfig(
        entry_script="score.py",
        source_directory="deployment",
        environment=freezer_environment,
    ),
    deployment_config=AciWebservice.deploy_configuration(
        cpu_cores=1,
예제 #25
0
from azureml.core import Run, Webservice

run = Run.get_context()
ws = run.experiment.workspace

parser = argparse.ArgumentParser()
parser.add_argument(
    "--webservicename",
    type=str,
    default="freezerchain-prediction-v0-2",
    help="Name of the deployed Webservice",
)
args = parser.parse_args()

service = Webservice(ws, args.webservicename)

service.update_deployment_state()

retries = 0
max_wait_time_minutes = 5
polling_frequency_seconds = 15
max_retries = int(max_wait_time_minutes * 60 / polling_frequency_seconds)

assert max_retries > 0

while service.state == "Transitioning" and retries <= max_retries:
    retries += 1
    print("Service still transitioning")

    time.sleep(polling_frequency_seconds)