# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Seldon and TensorFlow Serving MNIST Example
#
# This example shows how you can combine Seldon with Tensorflo Serving. We will use a Seldon Tensorflow Serving proxy model image that will forward Seldon internal microservice prediction calls out to a Tensorflow serving server.
#
# The example will use the MNIST digit classification task with the example MNIST model.
#

# ## Setup

# !pip install seldon-core

# %matplotlib inline
import requests
from random import randint,random
import json
from matplotlib import pyplot as plt
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
import sys
sys.path.append("../../../notebooks")
from visualizer import get_graph
from seldon_core.proto import prediction_pb2
from seldon_core.proto import prediction_pb2_grpc
import grpc
import tensorflow as tf
from tensorflow.core.framework.tensor_pb2 import TensorProto


# +
def gen_image(arr):
    two_d = (np.reshape(arr, (28, 28)) * 255).astype(np.uint8)
    plt.imshow(two_d,cmap=plt.cm.gray_r, interpolation='nearest')
    return plt

def download_mnist():
    return input_data.read_data_sets("MNIST_data/", one_hot = True)

def rest_predict_request(endpoint,data):
    request = {"data":{"ndarray":data.tolist()}}
    response = requests.post(
                "http://"+endpoint+"/predict",
                data={"json":json.dumps(request),"isDefault":True})
    return response.json()   

def rest_transform_input_request(endpoint,data):
    request = {"data":{"ndarray":data.tolist()}}
    response = requests.post(
                "http://"+endpoint+"/transform-input",
                data={"json":json.dumps(request),"isDefault":True})
    return response.json()   

def rest_transform_output_request(endpoint,data):
    request = {"data":{"ndarray":data.tolist()}}
    response = requests.post(
                "http://"+endpoint+"/transform-output",
                data={"json":json.dumps(request),"isDefault":True})
    return response.json()   

def rest_request_ambassador(deploymentName,namespace,endpoint="localhost:8003",arr=None):
    payload = {"data":{"names":["a","b"],"tensor":{"shape":[1,784],"values":arr.tolist()}}}
    response = requests.post(
                "http://"+endpoint+"/seldon/"+namespace+"/"+deploymentName+"/api/v0.1/predictions",
                json=payload)
    print(response.status_code)
    print(response.text)

def grpc_request_internal(data,endpoint="localhost:5000"):
    datadef = prediction_pb2.DefaultData(
            tftensor=tf.make_tensor_proto(data)
        )

    request = prediction_pb2.SeldonMessage(data = datadef)
    channel = grpc.insecure_channel(endpoint)
    stub = prediction_pb2_grpc.ModelStub(channel)
    response = stub.Predict(request=request)
    return response


def gen_mnist_data(mnist):
    batch_xs, batch_ys = mnist.train.next_batch(1)
    chosen=0
    gen_image(batch_xs[chosen]).show()
    data = batch_xs[chosen].reshape((1,784))
    return data



# -

mnist = download_mnist()

# ## Create MNIST Model Repository
# You will need tensorflow installed to run these steps.

# ### Train Tensorflow MNIST example model

# !git clone https://github.com/tensorflow/serving.git

# !cd serving/tensorflow_serving/example && python mnist_saved_model.py --training_iteration=100000 ../../../mnist-model

# ### Copy Model to Google Bucket

# CHANGE THIS TO YOUR OWN CHOSEN GOOGLE BUCKET NAME
# %env MODEL_REPOSITORY_BUCKET=gs://seldon-tfserving-store

# !gsutil mb ${MODEL_REPOSITORY_BUCKET}

# !gsutil cp -r mnist-model ${MODEL_REPOSITORY_BUCKET}

# ## Test From GCP Cluster

# ### Run MNIST Inference Graph

# !kubectl -n kube-system create sa tiller
# !kubectl create clusterrolebinding tiller --clusterrole cluster-admin --serviceaccount=kube-system:tiller
# !helm init --service-account tiller

# !kubectl rollout status deploy/tiller-deploy -n kube-system

# !kubectl create namespace seldon

# !kubectl config set-context $(kubectl config current-context) --namespace=seldon

# !helm install ../../../helm-charts/seldon-core-operator --name seldon-core --set usageMetrics.enabled=true --namespace seldon-system

# !kubectl rollout status statefulset.apps/seldon-operator-controller-manager -n seldon-system

# ### Setup Ingress
# There are gRPC issues with the latest Ambassador, so we rewcommend 0.40.2 until these are fixed.

# !helm install stable/ambassador --name ambassador --set crds.keep=false

# !kubectl rollout status deployment.apps/ambassador

# !helm install tfserving-mnist --name tfserving-mnist --namespace seldon --set tfserving.model_base_path=${MODEL_REPOSITORY_BUCKET}/mnist-model 

# !helm template tfserving-mnist --name tfserving-mnist --namespace seldon --set tfserving.model_base_path=${MODEL_REPOSITORY_BUCKET}/mnist-model > mnist.json

# !sed '1,2d' mnist.json > tmp.json

get_graph("tmp.json")

# !pygmentize mnist.json

# **Port forward Ambassador**
#
# ```
# kubectl port-forward $(kubectl get pods -n seldon -l app.kubernetes.io/name=ambassador -o jsonpath='{.items[0].metadata.name}') -n seldon 8003:8080
# ```

data = gen_mnist_data(mnist)
data = data.reshape((784))
rest_request_ambassador("tfserving-mnist","seldon",endpoint="localhost:8003",arr=data)

# ## Analytics and Load Test

# !helm install ../../../helm-charts/seldon-core-analytics --name seldon-core-analytics \
#     --set grafana_prom_admin_password=password \
#     --set persistence.enabled=false \
#     --namespace seldon

# !kubectl label nodes $(kubectl get nodes -o jsonpath='{.items[0].metadata.name}') role=locust

# !helm install seldon-core-loadtesting --name loadtest  \
#     --namespace seldon \
#     --repo https://storage.googleapis.com/seldon-charts \
#     --set locust.script=mnist_rest_locust.py \
#     --set locust.host=http://tf-mnist:8000 \
#     --set oauth.enabled=false \
#     --set locust.hatchRate=1 \
#     --set locust.clients=1 \
#     --set loadtest.sendFeedback=1 \
#     --set locust.minWait=0 \
#     --set locust.maxWait=0 \
#     --set replicaCount=1 \
#     --set data.size=784


# You should port-foward the grafana dashboard
#
# ```
# kubectl port-forward $(kubectl get pods -n seldon -l app=grafana-prom-server -o jsonpath='{.items[0].metadata.name}') -n seldon 3000:3000
# ```
#
# You can then view an analytics dashboard inside the cluster at http://localhost:3000/dashboard/db/prediction-analytics?refresh=5s&orgId=1. 



StarcoderdataJupyterScriptsDedupFiltered