Missing dependencies in notebooks/kubectl_demo_minikube.ipynb
This issue refers to the 'Deploying Machine Learning Models using kubectl' Jupyter Notebook (notebooks/kubectl_demo_minikube.ipynb) run under Python 3 (rather than 2). It was tested in Python 3.5 with Minikube 0.25.0 installation (KVM virtualization) running on Ubuntu 16.04 LTS.
Eventually I managed to get responses from the REST API, obtaining predictions for the model deployed in the notebook (seldonio/mock_classifier:1.0 /notebooks/resources/model.json). However, some dependencies were initially missing that required modification before the code could be executable in Python 3.x
- Two modules from the 'proto' library were unavailable or not exposed, raising this error:
ImportError Traceback (most recent call last)
<ipython-input-42-f18b8a6f5241> in <module>()
3 # import proto
4 from proto import prediction_pb2
----> 5 from proto import prediction_pb2_grpc
6 import grpc
7 # import commands
~/Documents/python/seldon/seldon-core/notebooks/proto/prediction_pb2_grpc.py in <module>()
2 import grpc
3
----> 4 import prediction_pb2 as prediction__pb2
5
6
ImportError: No module named 'prediction_pb2'
This issue can be solved by replacing the specific modules with the entire 'proto' library:
import proto
# from proto import prediction_pb2
# from proto import prediction_pb2_grpc
- The 'commands' module has been deprecated and removed from Python 3.x (link). Trying to use it in Python 3.x will cause this error message:
ImportError Traceback (most recent call last)
<ipython-input-44-f4c737095fff> in <module>()
5 # from proto import prediction_pb2_grpc
6 import grpc
----> 7 import commands
8 # import subprocess
9
ImportError: No module named 'commands'
This issue can be solved by replacing all references to this module in the Notebook with its successor module 'subprocess' (which has compatible function names), leaving a code like this:
# import commands
import subprocess
NAMESPACE='seldon'
MINIKUBE_IP=subprocess.getoutput('minikube ip')
MINIKUBE_HTTP_PORT=subprocess.getoutput("kubectl get svc -n "+NAMESPACE+" -l app=seldon-apiserver-container-app -o jsonpath='{.items[0].spec.ports[0].nodePort}'")
MINIKUBE_GRPC_PORT=subprocess.getoutput("kubectl get svc -n "+NAMESPACE+" -l app=seldon-apiserver-container-app -o jsonpath='{.items[0].spec.ports[1].nodePort}'")
Collecting the above corrections produces a code that executes cleanly in Python 3.x and we finally get our predictions from the server:
import requests
from requests.auth import HTTPBasicAuth
import proto
# from proto import prediction_pb2
# from proto import prediction_pb2_grpc
import grpc
# import commands
import subprocess
NAMESPACE='seldon'
MINIKUBE_IP=subprocess.getoutput('minikube ip')
MINIKUBE_HTTP_PORT=subprocess.getoutput("kubectl get svc -n "+NAMESPACE+" -l app=seldon-apiserver-container-app -o jsonpath='{.items[0].spec.ports[0].nodePort}'")
MINIKUBE_GRPC_PORT=subprocess.getoutput("kubectl get svc -n "+NAMESPACE+" -l app=seldon-apiserver-container-app -o jsonpath='{.items[0].spec.ports[1].nodePort}'")
[..]
However, all gRPC requests fail due to the missing 'prediction_pb2_grpc' module:
{"access_token":"7f5646a1-35c2-431d-a775-aca2b59307b7","token_type":"bearer","expires_in":28304,"scope":"read write"}
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-46-b1d1323ec6bf> in <module>()
----> 1 grpc_request()
<ipython-input-45-f36a438b7865> in grpc_request()
44 request = prediction_pb2.SeldonMessage(data = datadef)
45 channel = grpc.insecure_channel(MINIKUBE_IP+":"+MINIKUBE_GRPC_PORT)
---> 46 stub = prediction_pb2_grpc.SeldonStub(channel)
47 metadata = [('oauth_token', token)]
48 response = stub.Predict(request=request,metadata=metadata)
NameError: name 'prediction_pb2_grpc' is not defined
Hi,
Thanks for reporting this. The code that generates the pb2 files in notebooks/Makefile needs to be updated to make them compatible with python 3. This has been done in wrappers/Makefile but we forgot this one. We will fix that and then you should be able to import them without a problem.
We will also replace commands with subprocess like you did.