<jupyter_start><jupyter_text>Imports<jupyter_code>import sagemaker as sage
import os
import boto3
from sagemaker import get_execution_role
from sagemaker import transformer
from sagemaker import model

role = get_execution_role()<jupyter_output><empty_output><jupyter_text>Create the sessionThe session remembers our connection parameters to Amazon SageMaker. We'll use it to perform all of our SageMaker operations.<jupyter_code>sess = sage.Session()

account = sess.boto_session.client('sts').get_caller_identity()['Account']
region = sess.boto_session.region_name<jupyter_output><empty_output><jupyter_text>Create ModelNow we use the Bar/QR-code Scanner Model Package to create a model<jupyter_code>from src.model_package_arns import ModelPackageArnProvider
from sagemaker import ModelPackage
modelpackage_arn = ModelPackageArnProvider.get_model_package_arn(sess.boto_region_name)

# Define predictor wrapper class
def predict_wrapper(endpoint, session):
    return sage.RealTimePredictor(endpoint, session, content_type='image/jpeg')

# Create a deployable model
model = ModelPackage(role=role,
                                      model_package_arn = modelpackage_arn,
                                      sagemaker_session = sess,
                                      predictor_cls = predict_wrapper)
# ARN
print(modelpackage_arn)<jupyter_output>arn:aws:sagemaker:us-east-2:057799348421:model-package/bar-qr-code-reader-1571413449-a35a47dda86b16474ecd69ff9f20b46f<jupyter_text>Live Inference EndpointNow we demonstrate the creation of an endpoint for live inference<jupyter_code># Deploy the model
predictor = model.deploy(1, 'ml.m4.xlarge', endpoint_name='barcode-endpoint')<jupyter_output><empty_output><jupyter_text>Testing on a single file The model takes an image file (.jpg/.png) as input. We give the location of the sample input file and prepare it for payload. An example has been shown in the following code shell.We get the response for our request as a dictionary of Barcode Data and the Barcode Type associated with the barcode. In case of a QR code, the Barcode Type is returned as "QR-code".<jupyter_code>file_name = 'data/test_files/QRcode_example5.jpg'<jupyter_output><empty_output><jupyter_text>Preparing the input file for payload and viewing the response<jupyter_code># Open the file and read the image into a bytearray.
with open(file_name, "rb") as image:
  b = bytearray(image.read())

# Perform a prediction
result = predictor.predict(b).decode('utf-8')

# View the prediction
print(result)<jupyter_output>{"upi://pay?aid=uGICAgICAg_ziOw&pa=nishanthballal.9-1@okicici&pn=Nishanth%20Ballal": "QRCODE"}<jupyter_text>Batch Transform JobNow let's use the model built to run a batch inference job and verify it works.  Batch Transform Input Preparation<jupyter_code>bucket = sess.default_bucket()

transform_input_prefix = "barcode/validation"
TRANSFORM_WORKDIR = "data/transform" 
# upload data from local directory to bucket

transform_input = sess.upload_data(TRANSFORM_WORKDIR, key_prefix=transform_input_prefix) 

print ("Transform Data Location " + transform_input)<jupyter_output>Transform Data Location s3://sagemaker-us-east-2-428712150059/barcode/validation<jupyter_text>Run Batch TransformNow that our batch transform input is setup, we run the transformation job next<jupyter_code>transformer = model.transformer(1, 'ml.m4.xlarge', 
                                output_path="s3://"+bucket+"/barcode/batch-transform-output")
transformer.transform(transform_input, content_type='image/jpeg')
transformer.wait()

print("Batch Transform output saved to " + transformer.output_path)
# Print the outputs

s3 = boto3.resource('s3')
my_bucket = s3.Bucket(sess.default_bucket())

for object_summary in my_bucket.objects.filter(Prefix="barcode/batch-transform-output"+ "/"):
   file_name = object_summary.key.split('/')[-1]
   print(file_name)
   s3.Bucket(sess.default_bucket()).download_file("barcode/"+"batch-transform-output" +"/"+ file_name,  'batch_results')
   with open('batch_results') as f:
       results = f.readlines()  
   
   print("".join(results))<jupyter_output>QRcode_example5.jpg.out
{"upi://pay?aid=uGICAgICAg_ziOw&pa=nishanthballal.9-1@okicici&pn=Nishanth%20Ballal": "QRCODE"}
barcode_example.png.out
{"{\"author\": \"Adrian\", \"site\": \"PyImageSearch\"}": "QRCODE", "https://www.pyimagesearch.com/": "QRCODE", "PyImageSearch": "QRCODE", "AdrianRosebrock": "CODE128"}
barcode_example3.jpg.out
{"14MF3IM01": "CODE39"}<jupyter_text>Clean Up Endpoint<jupyter_code>predictor.delete_endpoint()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10360655737704919, 'fraction_numerical': 0.019453551912568305, 'mean_word_length': 5.670553935860059, 'pattern_counts': {'":': 9, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '529194', 'license': 'Apache-2.0', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1444, 'n_words': 333, 'path': 'BarcodeQRcodeScannerNB-ARN.ipynb', 'repo_name': 'dreamInCoDeforlife/AWSHackathon', 'size': '4575'}