<jupyter_start><jupyter_text>Deploy Annual Reports Mgmt Discussion Analysis Model Package from AWS Marketplace The solution uses text matching, NLP and Machine Learning to localize and identify management discussions. This will help financial and equity analysts to automatically run analytical models on unstructured text and draw insights from them.This sample notebook shows you how to deploy Annual Reports Mgmt Discussion Analysis using Amazon SageMaker.> **Note**: This is a reference notebook and it cannot run unless you make changes suggested in the notebook. Pre-requisites:1. **Note**: This notebook contains elements which render correctly in Jupyter interface. Open this notebook from an Amazon SageMaker Notebook Instance or Amazon SageMaker Studio.1. Ensure that IAM role used has **AmazonSageMakerFullAccess**1. To deploy this ML model successfully, ensure that:    1. Either your IAM role has these three permissions and you have authority to make AWS Marketplace subscriptions in the AWS account used:         1. **aws-marketplace:ViewSubscriptions**        1. **aws-marketplace:Unsubscribe**        1. **aws-marketplace:Subscribe**      2. or your AWS account has a subscription to Explainable AI: Algorithm Bias Detection. If so, skip step: [Subscribe to the model package](1.-Subscribe-to-the-model-package) Contents:1. [Subscribe to the model package](1.-Subscribe-to-the-model-package)2. [Create an endpoint and perform real-time inference](2.-Create-an-endpoint-and-perform-real-time-inference)   1. [Create an endpoint](A.-Create-an-endpoint)   2. [Create input payload](B.-Create-input-payload)   3. [Perform real-time inference](C.-Perform-real-time-inference)   4. [Visualize output](D.-Visualize-output)   5. [Delete the endpoint](E.-Delete-the-endpoint)3. [Perform batch inference](3.-Perform-batch-inference) 4. [Clean-up](4.-Clean-up)    1. [Delete the model](A.-Delete-the-model)    2. [Unsubscribe to the listing (optional)](B.-Unsubscribe-to-the-listing-(optional))     Usage instructionsYou can run this notebook one cell at a time (By using Shift+Enter for running a cell).  1. Subscribe to the model package To subscribe to the model package:1. Open the model package listing page **Explainable AI: Algorithm Bias Detection**1. On the AWS Marketplace listing, click on the **Continue to subscribe** button.1. On the **Subscribe to this software** page, review and click on **"Accept Offer"** if you and your organization agrees with EULA, pricing, and support terms. 1. Once you click on **Continue to configuration button** and then choose a **region**, you will see a **Product Arn** displayed. This is the model package ARN that you need to specify while creating a deployable model using Boto3. Copy the ARN corresponding to your region and specify the same in the following cell.<jupyter_code>model_package_arn='arn:aws:sagemaker:us-east-2:786796469737:model-package/mphasis-marketplace-financial-content-extraction'
import base64
import json 
import uuid
from sagemaker import ModelPackage
import sagemaker as sage
from sagemaker import get_execution_role
from sagemaker import ModelPackage
#from urllib.parse import urlparse
import boto3
from IPython.display import Image
from PIL import Image as ImageEdit
#import urllib.request
import numpy as np
role = get_execution_role()

sagemaker_session = sage.Session()

bucket=sagemaker_session.default_bucket()
bucket<jupyter_output><empty_output><jupyter_text>2. Create an endpoint and perform real-time inference If you want to understand how real-time inference with Amazon SageMaker works, see [Documentation](https://docs.aws.amazon.com/sagemaker/latest/dg/how-it-works-hosting.html).<jupyter_code>model_name='mgmt-discussions-analyzer'

content_type='application/pdf'

real_time_inference_instance_type='ml.m5.xlarge'
batch_transform_inference_instance_type='ml.m5.large'<jupyter_output><empty_output><jupyter_text>A. Create an endpoint<jupyter_code>def predict_wrapper(endpoint, session):
    return sage.RealTimePredictor(endpoint, session,content_type)

#create a deployable model from the model package.
model = ModelPackage(role=role,
                    model_package_arn=model_package_arn,
                    sagemaker_session=sagemaker_session,
                    predictor_cls=predict_wrapper)

#Deploy the model
predictor = model.deploy(1, real_time_inference_instance_type, endpoint_name=model_name)<jupyter_output>-----------!<jupyter_text>Once endpoint has been created, you would be able to perform real-time inference.  B. Create input payload<jupyter_code># To show pdf-s inside ipython/jupyter notebooks you can use IFrame
from IPython.display import IFrame
IFrame("./Input/Sample.pdf", width=600, height=300)<jupyter_output><empty_output><jupyter_text><jupyter_code>file_name = "./Input/Sample.pdf"
output_file_name = 'output.json'<jupyter_output><empty_output><jupyter_text>C. Perform real-time inference<jupyter_code>!aws sagemaker-runtime invoke-endpoint \
    --endpoint-name 'mgmt-discussions-analyzer' \
    --body fileb://$file_name \
    --content-type 'application/pdf' \
    --region us-east-2 \
    output.json<jupyter_output>{
    "InvokedProductionVariant": "AllTraffic", 
    "ContentType": "application/json"
}<jupyter_text>D. Visualize output<jupyter_code>import json
with open('./output.json') as f:
    data = json.load(f)

print("Output: ")

print(data)<jupyter_output>Output: 
{u'Credit file': {}, u'slashed prices': {}, u'Out of business': {}, u'Bankrupt': {}, u'Profit': {u'para-num-4': {u'count': 2, u'page_number': u'page72', u'para': u'Sitting fee indicated above also includes payment for Board-level committee meetings.The above remuneration of Executive Directors does not include provision for incremental liability on account of pension, gratuity, compensated absences and long service awards since actuarial valuation is done for the Company as a whole. The appointment of executive directors is by virtue of their employment with the Company as management employees and therefore, their terms of employment are governed by the applicable policies at the relevant point in time. Commission is subject to adequate profits being earned. A fair portion of the Executive Director\u2019s Performance Linked Incentive is linked to Company\u2019s performance. This creates alignment with the strategy and business priorities to enhance shareholder value. The total[...]<jupyter_text>E. Delete the endpoint Now that you have successfully performed a real-time inference, you do not need the endpoint any more. You can terminate the endpoint to avoid being charged.<jupyter_code>predictor=sage.RealTimePredictor(model_name, sagemaker_session,content_type)
predictor.delete_endpoint(delete_endpoint_config=True)<jupyter_output><empty_output><jupyter_text>3. Perform batch inference In this section, you will perform batch inference using multiple input payloads together. If you are not familiar with batch transform, and want to learn more, see these links:1. [How it works](https://docs.aws.amazon.com/sagemaker/latest/dg/ex1-batch-transform.html)2. [How to run a batch transform job](https://docs.aws.amazon.com/sagemaker/latest/dg/how-it-works-batch.html)<jupyter_code>#upload the batch-transform job input files to S3
transform_input_folder = "Input"
transform_input = sagemaker_session.upload_data(transform_input_folder, key_prefix=model_name) 
print("Transform input uploaded to " + transform_input)
#Run the batch-transform job
transformer = model.transformer(1, 'ml.m5.4xlarge')
transformer.transform(transform_input, content_type=content_type)
transformer.wait()
#output is available on following path
transformer.output_path
s3_conn = boto3.client("s3")
bucket_name="sagemaker-us-east-2-786796469737"
with open('./output.json', 'wb') as f:
    s3_conn.download_fileobj(bucket_name, 'mphasis-marketplace-financial-content-e-2021-02-01-06-56-51-895'+'/Sample.pdf.out', f)
    print("Output file loaded from bucket")
output = open("./output.json","r+")
#print(output.read())

# copy and paste the output below in a browser tab to see the result
data = output.read()
print(data)<jupyter_output>{"Acquire": {}, "Business Failure": {}, "credit": {}, "Dun & Bradstreet": {}, "Acquisition": {}, "cancellation": {}, "Credit file": {}, "earnings guidance": {}, "Acquisitive interest": {}, "Cash Collateral": {}, "Credit rating": {}, "earnings release": {}, "Adequate protection": {}, "Charge": {"para-num-0": {"page_number": "page72", "para": "The Committee oversees corporate social responsibility, corporate governance and other business related matters which may be referred by the Board or the Chairman, as and when deemed necessary, for the consideration and recommendation of the Committee. This Committee also discharges the role of Corporate Social Responsibility Committee under Section 135 of the Companies Act, 2013 which includes formulating and recommending to the Board, a Corporate Social Responsibility (CSR) Policy indicating the activities to be undertaken by the Company as per Schedule VII to the Companies Act, 2013; recommending the amount of expenditure to be incurred; and mon[...]<jupyter_text>4. Clean-up  A. Delete the model<jupyter_code>model.delete_model()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09538727449497678, 'fraction_numerical': 0.013611321162363616, 'mean_word_length': 5.460572226099093, 'pattern_counts': {'":': 19, '<': 37, '<?xml version=': 0, '>': 38, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '389533', 'license': 'Apache-2.0', 'n_tokens_mistral': 2693, 'n_tokens_neox': 2547, 'n_words': 923, 'path': 'Annual Reports Mgmt Discussion Analysis.ipynb', 'repo_name': 'Mphasis-ML-Marketplace/Annual-Reports-Mgmt-Discussion-Analysis', 'size': '9257'}