Monday, December 11, 2017

TensorFlow Linear Regression Model Access with Custom REST API using Flask

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 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.

No comments: