<jupyter_start><jupyter_text>Model with Binary DataExample testing a model which uses binary data<jupyter_code>!cp ../../../proto/prediction.proto ./proto
!cp -r ../../../proto/tensorflow/tensorflow .
!python -m grpc.tools.protoc -I. --python_out=. --grpc_python_out=. ./proto/prediction.proto
import json
import requests
import base64
from proto import prediction_pb2
from proto import prediction_pb2_grpc
import grpc
import numpy as np
import pickle

def get_payload():
    array_2d = np.arange(9).reshape(3, 3)
    print(array_2d)
    bdata = pickle.dumps(array_2d)
    return bdata
def rest_request_ambassador(deploymentName,namespace,request,endpoint="localhost:8003"):
    response = requests.post(
                "http://"+endpoint+"/seldon/"+namespace+"/"+deploymentName+"/api/v0.1/predictions",
                json=request)
    print(response.status_code)
    print(response.text)
    return response.json()

def grpc_request_ambassador(deploymentName,namespace,data,endpoint="localhost:8004"):
    request = prediction_pb2.SeldonMessage(binData = data)
    channel = grpc.insecure_channel(endpoint)
    stub = prediction_pb2_grpc.SeldonStub(channel)
    metadata = [('seldon',deploymentName),('namespace',namespace)]
    response = stub.Predict(request=request,metadata=metadata)
    return response

def rest_request_docker(request,endpoint="localhost:5000"):
    response = requests.post(
                "http://"+endpoint+"/predict",
                 data={"json":json.dumps(request),"isDefault":True})
    print(response.text)
    return response.json()

def grpc_request_docker(data,endpoint="localhost:5000"):
    request = prediction_pb2.SeldonMessage(binData = data)
    channel = grpc.insecure_channel(endpoint)
    stub = prediction_pb2_grpc.ModelStub(channel)
    response = stub.Predict(request=request)
    return response<jupyter_output><empty_output><jupyter_text>REST<jupyter_code>!s2i build -E environment_rest . seldonio/seldon-core-s2i-python36:0.13 model-with-bindata-rest:0.1
!docker run --name "model-with-bindata" -d --rm -p 5000:5000 model-with-bindata-rest:0.1
bdata = get_payload()
bdata_base64 = base64.b64encode(bdata).decode('utf-8')
payload = {"meta":{},"binData":bdata_base64}
response = rest_request_docker(payload)
bdata2 = base64.b64decode(response["binData"])
arr_resp = pickle.loads(bdata2)
print(arr_resp)
print(arr_resp.shape)
!docker rm model-with-bindata --force<jupyter_output>model-with-bindata<jupyter_text>gRPC<jupyter_code>!s2i build -E environment_grpc . seldonio/seldon-core-s2i-python36:0.13 model-with-bindata-grpc:0.1
!docker run --name "model-with-bindata" -d --rm -p 5000:5000 model-with-bindata-grpc:0.1
payload = get_payload()
bdata = get_payload()
resp = grpc_request_docker(bdata)
bdata2 = resp.binData
arr_resp = pickle.loads(bdata2)
print(arr_resp)
print(arr_resp.shape)
!docker rm model-with-bindata --force<jupyter_output>model-with-bindata<jupyter_text>Test using Minikube**Due to a [minikube/s2i issue](https://github.com/SeldonIO/seldon-core/issues/253) you will need [s2i >= 1.1.13](https://github.com/openshift/source-to-image/releases/tag/v1.1.13)**<jupyter_code>!minikube start --vm-driver kvm2 --memory 4096<jupyter_output>😄  minikube v1.0.0 on linux (amd64)
🤹  Downloading Kubernetes v1.14.0 images in the background ...
💡  Tip: Use 'minikube start -p <name>' to create a new cluster, or 'minikube delete' to delete this one.
🏃  Re-using the currently running kvm2 VM for "minikube" ...
⌛  Waiting for SSH access ...
📶  "minikube" IP address is 192.168.39.225
🐳  Configuring Docker as the container runtime ...<jupyter_text>Setup Seldon CoreUse the setup notebook to [Setup Cluster](../../seldon_core_setup.ipynbSetup-Cluster) with [Ambassador Ingress](../../seldon_core_setup.ipynbAmbassador) and [Install Seldon Core](../../seldon_core_setup.ipynbInstall-Seldon-Core). Instructions [also online](./seldon_core_setup.html).  REST<jupyter_code>!eval $(minikube docker-env) && s2i build -E environment_rest . seldonio/seldon-core-s2i-python36:0.13 model-with-bindata-rest:0.1
!kubectl create -f deployment-rest.json
!kubectl rollout status deploy/mymodel-mymodel-b0e3779<jupyter_output>deployment "mymodel-mymodel-b0e3779" successfully rolled out<jupyter_text>Test predict<jupyter_code>minikube_ip = !minikube ip
minikube_port = !kubectl get svc -l app.kubernetes.io/name=ambassador -o jsonpath='{.items[0].spec.ports[0].nodePort}'
bdata = get_payload()
bdata_base64 = base64.b64encode(bdata).decode('utf-8')
payload = {"meta":{},"binData":bdata_base64}
response = rest_request_ambassador("mymodel","default",payload,minikube_ip[0]+":"+minikube_port[0])
bdata2 = base64.b64decode(response["binData"])
arr_resp = pickle.loads(bdata2)
print(arr_resp)
print(arr_resp.shape)
!kubectl delete -f deployment-rest.json<jupyter_output>seldondeployment.machinelearning.seldon.io "mymodel" deleted<jupyter_text>gRPC<jupyter_code>!eval $(minikube docker-env) && s2i build -E environment_grpc . seldonio/seldon-core-s2i-python36:0.13 model-with-bindata-grpc:0.1
!kubectl create -f deployment-grpc.json<jupyter_output>seldondeployment.machinelearning.seldon.io/mymodel created<jupyter_text>Wait until ready (replicas == replicasAvailable)<jupyter_code>!kubectl rollout status deploy/mymodel-mymodel-a9ecaa4
bdata = get_payload()
response = grpc_request_ambassador("mymodel","default",bdata,minikube_ip[0]+":"+minikube_port[0])
bdata2 = response.binData
arr_resp = pickle.loads(bdata2)
print(arr_resp)
print(arr_resp.shape)
!kubectl delete -f deployment-grpc.json
!minikube delete<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13468436154949784, 'fraction_numerical': 0.032101865136298424, 'mean_word_length': 5.576650943396227, 'pattern_counts': {'":': 8, '<': 28, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '94469', 'license': 'Apache-2.0', 'n_tokens_mistral': 2113, 'n_tokens_neox': 2012, 'n_words': 346, 'path': 'examples/models/template_model_bindata/modelWithBindata.ipynb', 'repo_name': 'nav13n/seldon-core', 'size': '5576'}