Encontre um projeto do Lookout for Vision com uma tag específica usando AWS um SDK - AWS Exemplos de código do SDK

Há mais exemplos de AWS SDK disponíveis no repositório AWS Doc SDK Examples GitHub .

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Encontre um projeto do Lookout for Vision com uma tag específica usando AWS um SDK

O exemplo de código a seguir mostra como encontrar um projeto do Lookout for Vision com uma tag específica.

Para obter mais informações, consulte Marcando modelos.

Python
SDK para Python (Boto3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no AWS Code Examples Repository.

import logging import argparse import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def find_tag(tags, key, value): """ Finds a tag in the supplied list of tags. :param tags: A list of tags associated with a Lookout for Vision model. :param key: The tag to search for. :param value: The tag key value to search for. :return: True if the tag value exists, otherwise False. """ found = False for tag in tags: if key == tag["Key"]: logger.info("\t\tMatch found for tag: %s value: %s.", key, value) found = True break return found def find_tag_in_projects(lookoutvision_client, key, value): """ Finds Lookout for Vision models tagged with the supplied key and value. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param key: The tag key to find. :param value: The value of the tag that you want to find. return: A list of matching model versions (and model projects) that were found. """ try: found_tags = [] found = False projects = lookoutvision_client.list_projects() # Iterate through each project and models within a project. for project in projects["Projects"]: logger.info("Searching project: %s ...", project["ProjectName"]) 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"], ) tags = lookoutvision_client.list_tags_for_resource( ResourceArn=model_description["ModelDescription"]["ModelArn"] ) logger.info( "\tSearching model: %s for tag: %s value: %s.", model_description["ModelDescription"]["ModelArn"], key, value, ) if find_tag(tags["Tags"], key, value) is True: found = True logger.info( "\t\tMATCH: Project: %s: model version %s", project["ProjectName"], model_description["ModelDescription"]["ModelVersion"], ) found_tags.append( { "Project": project["ProjectName"], "ModelVersion": model_description["ModelDescription"][ "ModelVersion" ], } ) if found is False: logger.info("No match for tag %s with value %s.", key, value) except ClientError: logger.exception("Problem finding tags.") raise else: return found_tags def main(): logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") parser = argparse.ArgumentParser(usage=argparse.SUPPRESS) parser.add_argument("tag", help="The tag that you want to find.") parser.add_argument("value", help="The tag value that you want to find.") args = parser.parse_args() key = args.tag value = args.value session = boto3.Session(profile_name="lookoutvision-access") lookoutvision_client = session.client("lookoutvision") print(f"Searching your models for tag: {key} with value: {value}.") tagged_models = find_tag_in_projects(lookoutvision_client, key, value) print("Matched models\n--------------") if len(tagged_models) > 0: for model in tagged_models: print(f"Project: {model['Project']}. model version:{model['ModelVersion']}") else: print("No matches found.") if __name__ == "__main__": main()