import boto3 import os import json client = boto3.client('servicecatalog') cwd = os.getcwd().split('/') i= cwd.index('S3Downloads') pp_name = cwd[i + 1] pp = client.describe_provisioned_product(Name=pp_name) record_id = pp['ProvisionedProductDetail']['LastSuccessfulProvisioningRecordId'] record = client.describe_record(Id=record_id) keys = [ x['OutputKey'] for x in record['RecordOutputs'] if 'OutputKey' and 'OutputValue' in x] values = [ x['OutputValue'] for x in record['RecordOutputs'] if 'OutputKey' and 'OutputValue' in x] stack_output = dict(zip(keys, values)) with open(f'/root/S3Downloads/{pp_name}/stack_outputs.json', 'w') as f: json.dump(stack_output, f) import boto3 import sagemaker from sagemaker.pytorch import PyTorchModel import sys sys.path.insert(0, '../package') from package import config, utils current_folder = utils.get_current_folder(globals()) sagemaker_client = boto3.client('sagemaker') model_name = "{}-entity-recognition".format(config.SOLUTION_PREFIX) model = PyTorchModel( name=model_name, model_data=f'{config.SOURCE_S3_PATH}/models/empty.tar.gz', entry_point='entry_point.py', source_dir='../containers/entity_recognition', role=config.IAM_ROLE, framework_version='1.5.0', py_version='py3', code_location='s3://' + config.S3_BUCKET + '/code' ) from sagemaker.serializers import JSONSerializer from sagemaker.deserializers import JSONDeserializer predictor = model.deploy( endpoint_name=model_name, instance_type=config.HOSTING_INSTANCE_TYPE, initial_instance_count=1, serializer=JSONSerializer(), deserializer=JSONDeserializer() ) # sagemaker_client.delete_endpoint(EndpointName=model_name) # sagemaker_client.delete_endpoint_config(EndpointConfigName=model_name) # sagemaker_client.delete_model(ModelName=model_name) data = {'text': 'Amazon SageMaker is a fully managed service that provides every developer and data scientist with the ability to build, train, and deploy machine learning (ML) models quickly.'} response = predictor.predict(data=data) print(response['entities']) print(response['noun_chunks']) sagemaker_client.delete_endpoint(EndpointName=model_name) sagemaker_client.delete_endpoint_config(EndpointConfigName=model_name)