<jupyter_start><jupyter_text>Deploy model<jupyter_code>class StringPredictor(RealTimePredictor):
    def __init__(self, endpoint_name, sagemaker_session):
        super(StringPredictor, self).__init__(endpoint_name, sagemaker_session, content_type='text/plain')

model = PyTorchModel(model_data='s3://sagemaker-eu-west-1-087816224558/sagemaker-pytorch-2020-06-06-17-15-12-540/output/model.tar.gz',
                     role = role,
                     framework_version='1.1.0',
                     entry_point='predict.py',
                     source_dir='serve',
                     predictor_cls=StringPredictor)
predictor = model.deploy(initial_instance_count=1,
                         instance_type='ml.m4.xlarge',
                         endpoint_name='ner-roberta-v12')
print(f'Endpoint name: {predictor.endpoint}')<jupyter_output>Endpoint name: ner-roberta-v12<jupyter_text>Test the endpoint on single input<jupyter_code>def predict_on_text(text, predictor):
    """
    Method for computing predicitons on given text using predictor.
    Parameters:
    text       - single string
    predictor  - PyTorchPredictor object
    Returns: pandas dataframe with input words and predicted tags
    """
    predictions = predictor.predict(text)
    pred_array = np.fromstring(predictions[1:-1], dtype=np.int, sep=', ')
    input_length = len(str(text).split())
    df_preds = pd.DataFrame(zip([tag2name[i] for i in pred_array[1:(input_length+1)].tolist()], str(text).split()))
    df_preds.columns = ['Tag', 'Word']
    return df_preds
text = 'I live in USA this is my IBM laptop'
df_preds = predict_on_text(text, predictor)
print(df_preds)<jupyter_output>Tag    Word
0      O       I
1      O    live
2      O      in
3  B-geo     USA
4      O    this
5      O      is
6      O      my
7  B-org     IBM
8      O  laptop<jupyter_text>Create predictor from endpoint<jupyter_code># from sagemaker.pytorch import PyTorchPredictor
# predictor = PyTorchPredictor('sagemaker-pytorch-2020-06-01-18-56-03-855',ContentType = 'text/plain')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10831721470019343, 'fraction_numerical': 0.03433268858800774, 'mean_word_length': 3.0648330058939095, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '389556', 'license': 'MIT', 'n_tokens_mistral': 726, 'n_tokens_neox': 676, 'n_words': 132, 'path': 'src/4_deploy_roberta.ipynb', 'repo_name': 'blawok/Named_Entity_Recognition', 'size': '2068'}