@staticmethoddefdelete_model(lookoutvision_client, project_name, model_version):"""
Deletes a Lookout for Vision model. The model must first be stopped and can't
be in training.
:param lookoutvision_client: A Boto3 Lookout for Vision client.
:param project_name: The name of the project that contains the desired model.
:param model_version: The version of the model that you want to delete.
"""try:
logger.info("Deleting model: %s", model_version)
lookoutvision_client.delete_model(
ProjectName=project_name, ModelVersion=model_version
)
model_exists = Truewhile model_exists:
response = lookoutvision_client.list_models(ProjectName=project_name)
model_exists = Falsefor model in response["Models"]:
if model["ModelVersion"] == model_version:
model_exists = Trueif model_exists isFalse:
logger.info("Model deleted")
else:
logger.info("Model is being deleted...")
time.sleep(2)
logger.info("Deleted Model: %s", model_version)
except ClientError:
logger.exception("Couldn't delete model.")
raise
/**
* Deletes an Amazon Lookout for Vision model.
*
* @param lfvClient An Amazon Lookout for Vision client. Returns after the model is deleted.
* @param projectName The name of the project that contains the model that you want to delete.
* @param modelVersion The version of the model that you want to delete.
* @return void
*/publicstaticvoiddeleteModel(LookoutVisionClient lfvClient,
String projectName,
String modelVersion)throws LookoutVisionException, InterruptedException {
DeleteModelRequest deleteModelRequest = DeleteModelRequest.builder()
.projectName(projectName)
.modelVersion(modelVersion)
.build();
lfvClient.deleteModel(deleteModelRequest);
boolean deleted = false;
do{
ListModelsRequest listModelsRequest = ListModelsRequest.builder()
.projectName(projectName)
.build();
// Get a list of models in the supplied project.
ListModelsResponse response = lfvClient.listModels(listModelsRequest);
ModelMetadata modelMetadata = response.models().stream()
.filter(model -> model.modelVersion().equals(modelVersion)).findFirst()
.orElse(null);
if (modelMetadata == null) {
deleted = true;
logger.log(Level.INFO, "Deleted: Model version {0} of project {1}.",
new Object[] { modelVersion, projectName });
} else{
logger.log(Level.INFO, "Not yet deleted: Model version {0} of project {1}.",
new Object[] { modelVersion, projectName });
TimeUnit.SECONDS.sleep(60);
}
} while (!deleted);
}