In my previous post - TensorFlow - Getting Started with Docker Container and Jupyter Notebook I have described basics about how to install and run TensorFlow using Docker. Today I will describe how to give access to the machine learning model from outside of TensorFlow with REST. This is particularly useful while building JS UIs on top of TensorFlow (for example with Oracle JET).
TensorFlow supports multiple languages, but most common one is Python. I have implemented linear regression model using Python and now would like to give access to this model from the outside. For this reason I'm going to use Flask, micro-framework for Python to allow simple REST annotations directly in Python code.
To install Flask, enter into TensorFlow container:
docker exec -it RedSamuraiTensorFlowUI bash
Run Flask install:
pip install flask
Run Flask CORS install:
pip install -U flask-cors
I'm going to call REST from JS, this means TensorFlow should support CORS, otherwise request will be blocked. No worries, we can import Flask CORS support.
REST is enabled for TensorFlow model with Flask in these two lines of code:
As soon as Flask is imported and enabled we can annotate a method with REST operations and endpoint URL:
There is option to check what kind of REST operation is executed and read input parameters from POST request. This is useful to control model learning steps, for example:
If we want to collect all x/y value pairs and return in REST response, we can do that by collecting values into array:
And construct JSON response directly out of the array structure using Flask jsonify:
After we run TensorFlow model in Jupyter, it will print URL endpoint for REST service. For URL to be accessible outside TensorFlow Docker container, make sure to run TensorFlow model with 0.0.0.0 as in the screenshot below:
Here is example of TensorFlow model REST call from Postman. POST operation is executed payload and response:
All REST calls are logged in TensorFlow:
Download TensorFlow model enabled with REST from my GitHub.
TensorFlow supports multiple languages, but most common one is Python. I have implemented linear regression model using Python and now would like to give access to this model from the outside. For this reason I'm going to use Flask, micro-framework for Python to allow simple REST annotations directly in Python code.
To install Flask, enter into TensorFlow container:
docker exec -it RedSamuraiTensorFlowUI bash
Run Flask install:
pip install flask
Run Flask CORS install:
pip install -U flask-cors
I'm going to call REST from JS, this means TensorFlow should support CORS, otherwise request will be blocked. No worries, we can import Flask CORS support.
REST is enabled for TensorFlow model with Flask in these two lines of code:
As soon as Flask is imported and enabled we can annotate a method with REST operations and endpoint URL:
There is option to check what kind of REST operation is executed and read input parameters from POST request. This is useful to control model learning steps, for example:
If we want to collect all x/y value pairs and return in REST response, we can do that by collecting values into array:
And construct JSON response directly out of the array structure using Flask jsonify:
After we run TensorFlow model in Jupyter, it will print URL endpoint for REST service. For URL to be accessible outside TensorFlow Docker container, make sure to run TensorFlow model with 0.0.0.0 as in the screenshot below:
Here is example of TensorFlow model REST call from Postman. POST operation is executed payload and response:
All REST calls are logged in TensorFlow:
Download TensorFlow model enabled with REST from my GitHub.
Nice article, I'd like just to make two observations:
ReplyDelete1)For me this code only work outside jupyter, even a simple "Hello World" using
Flask didn't work inside jupyter notebook. The browser shows:"This site can’t be reached".
2) This code only works on Python(2.x) version, on Python(3.x) it shows a message:
TypeError: Object of type 'int64' is not JSON serializable
Just in case someone is facing for the same problem.
Thats strange, it works for me fine with Jupyter and Python which comes with TensorFlow Docker image.
ReplyDeleteProblem with type probably can be easily solved, indeed this could be related to diff Python versions.
Thanks for comment.
Andrejus