# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (TensorFlow 2.3 Python 3.7 CPU Optimized)
#     language: python
#     name: python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:eu-west-1:470317259841:image/tensorflow-2.3-cpu-py37-ubuntu18.04-v1
# ---

# Update this with your own stack name (not the endpoint name)
stack_name = 'endpoint-one-model-2021-07-21-12-46-07'

# +
import boto3

cf = boto3.client('cloudformation')
response = cf.describe_stacks(StackName=stack_name)
endpoint_name = response['Stacks'][0]['Outputs'][0]['OutputValue']

print(endpoint_name)

# +
import os
import numpy as np
from tensorflow.keras.datasets import fashion_mnist

(x_train, y_train), (x_val, y_val) = fashion_mnist.load_data()

os.makedirs("./data", exist_ok = True)
np.savez('./data/training', image=x_train, label=y_train)
np.savez('./data/validation', image=x_val, label=y_val)

# +
import random, json
import sagemaker
from sagemaker.predictor import Predictor

tf_predictor = Predictor(endpoint_name)
tf_predictor.serializer =  sagemaker.serializers.JSONSerializer()
tf_predictor.deserializer =  sagemaker.deserializers.JSONDeserializer()


def predict_fmnist(num_samples=10):
    while True:
        indices = random.sample(range(x_val.shape[0] - 1), num_samples)
        images = x_val[indices]/255
        labels = y_val[indices]
        payload = images.reshape(num_samples, 28, 28, 1)
        response = tf_predictor.predict(payload)
        #print(response)
        prediction = np.array(response['predictions'])
        #predicted_label = prediction.argmax(axis=1)
        #print('Predicted labels are: {}'.format(predicted_label))


# +
from threading import Thread
from time import sleep

thread = Thread(target=predict_fmnist)
thread.start()
# -



StarcoderdataJupyterScriptsDedupFiltered