# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Purpose Amazon Lookout for Vision model hosting examples used in the service documentation: https://docs.aws.amazon.com/lookout-for-vision/latest/developer-guide/running-model.html Shows how to start and stop a model. Also, how to get a list of running models in your AWS account. """ import time import logging from botocore.exceptions import ClientError logger = logging.getLogger(__name__) # snippet-start:[python.example_code.lookoutvision.Hosting] class Hosting: # snippet-end:[python.example_code.lookoutvision.Hosting] """ Shows how to start and stop a Lookout for Vision Model. Also shows how to list the models that are currently running. """ # snippet-start:[python.example_code.lookoutvision.StartModel] @staticmethod def start_model( lookoutvision_client, project_name, model_version, min_inference_units ): """ Starts the hosting of a Lookout for Vision model. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that contains the version of the model that you want to start hosting. :param model_version: The version of the model that you want to start hosting. :param min_inference_units: The number of inference units to use for hosting. """ try: logger.info( "Starting model version %s for project %s", model_version, project_name ) lookoutvision_client.start_model( ProjectName=project_name, ModelVersion=model_version, MinInferenceUnits=min_inference_units, ) print("Starting hosting...") status = "" finished = False # Wait until hosted or failed. while finished is False: model_description = lookoutvision_client.describe_model( ProjectName=project_name, ModelVersion=model_version ) status = model_description["ModelDescription"]["Status"] if status == "STARTING_HOSTING": logger.info("Host starting in progress...") time.sleep(10) continue if status == "HOSTED": logger.info("Model is hosted and ready for use.") finished = True continue logger.info("Model hosting failed and the model can't be used.") finished = True if status != "HOSTED": logger.error("Error hosting model: %s", status) raise Exception(f"Error hosting model: {status}") except ClientError: logger.exception("Couldn't host model.") raise # snippet-end:[python.example_code.lookoutvision.StartModel] # snippet-start:[python.example_code.lookoutvision.StopModel] @staticmethod def stop_model(lookoutvision_client, project_name, model_version): """ Stops a running Lookout for Vision Model. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that contains the version of the model that you want to stop hosting. :param model_version: The version of the model that you want to stop hosting. """ try: logger.info("Stopping model version %s for %s", model_version, project_name) response = lookoutvision_client.stop_model( ProjectName=project_name, ModelVersion=model_version ) logger.info("Stopping hosting...") status = response["Status"] finished = False # Wait until stopped or failed. while finished is False: model_description = lookoutvision_client.describe_model( ProjectName=project_name, ModelVersion=model_version ) status = model_description["ModelDescription"]["Status"] if status == "STOPPING_HOSTING": logger.info("Host stopping in progress...") time.sleep(10) continue if status == "TRAINED": logger.info("Model is no longer hosted.") finished = True continue logger.info("Failed to stop model: %s ", status) finished = True if status != "TRAINED": logger.error("Error stopping model: %s", status) raise Exception(f"Error stopping model: {status}") except ClientError: logger.exception("Couldn't stop hosting model.") raise # snippet-end:[python.example_code.lookoutvision.StopModel] # snippet-start:[python.example_code.lookoutvision.Scenario_ListHostedModels] @staticmethod def list_hosted(lookoutvision_client): """ Displays a list of models in your account that are currently hosted. :param lookoutvision_client: A Boto3 Lookout for Vision client. """ try: response = lookoutvision_client.list_projects() hosted = 0 print("Hosted models\n-------------") for project in response["Projects"]: response_models = lookoutvision_client.list_models( ProjectName=project["ProjectName"] ) for model in response_models["Models"]: model_description = lookoutvision_client.describe_model( ProjectName=project["ProjectName"], ModelVersion=model["ModelVersion"], ) if model_description["ModelDescription"]["Status"] == "HOSTED": print( f"Project: {project['ProjectName']} Model version: " f"{model['ModelVersion']}" ) hosted += 1 print(f"{hosted} model(s) hosted") except ClientError: logger.exception("Problem listing hosted models.") raise # snippet-end:[python.example_code.lookoutvision.Scenario_ListHostedModels]