Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
Showing results for 
Search instead for 
Did you mean: 
In this blog, I will be covering how you can create a Rest API endpoint using Flask in Linux which consumes a Machine Learning model to predict data.


You need to have training data and test data ready with you.

Installation required in Windows:

  1. Install PuTTY.exe, Puttygen.exe, Pscp.exe files.

  2. Install Anaconda

  3. Install TensorFlow

Installation required in LINUX:

  1. Install python (usually LINUX systems have Python already installed in them).

  2. Install pip.

Command: sudo zypper install python3-pip

  1. Install Flask.

Command: pip3 install flask

  1. Install Anaconda.


  1. Install TensorFlow.

Command: pip install tensorflow


What is REST API?

Rest API stands for Representation State Transfer Application Programming Interface. When a client makes a request to Rest API for some data, Rest API receives the request, gathers, and parses the data and returns it back to the client with response headers. Rest API allows us to fully separate the presentation of content from the content itself.

Connecting to your GCP Instance

Follow the steps given in the following webpages to connect to your GCP instance along with creating your SSH Key and .ppk file:

  1. Connect to LINUX GCP instance via PuTTY

  2. Maintaining SSH Key for GCP

  3. Generating an SSH Key manually on Windows

Training model in Windows

Using Jupyter, create your own Machine Learning Model using TensorFlow and train your model. If you already have your model, just run and check if it works properly.

Transferring files from Windows to LINUX

Once your model is ready and it is working fine, you need to transfer your data files and your model to the LINUX system. To transfer the files from Windows to LINUX machine, you need to have pscp.exe file downloaded in your system.

Run the following command in your Command Prompt in Windows to transfer your files from Windows to LINUX:
pscp.exe -i key.ppk <filepath> user@host:<destinationpath>

key.ppk is the .ppk file which you generated manually using Puttygen.exe.

<filepath> is the path to your file which you need to move into your LINUX system.

User is your username.

Host is your GCP instance host name.

<destinationpath> is the path where you want to save your files in your LINUX system.

Note: While running the above command you should be in the directory where your pscp.exe file is present.

Now, your files and models are in your LINUX system.

Training your ML Model in LINUX

To train your ML Model in LINUX, run the following command:
jupyter nbconvert –-to notebook -–execute <filename.ipynb> --output <filename.ipynb>

Once, your model is trained you can use the model to predict some data using Rest API endpoints.

Note: Change the location of your data files (.csv) mentioned in your <model>.ipynb file to point it to your LINUX directory where you have saved those files. Since you transferred the file from your Windows to LINUX system, the filepath may change.

What is Flask?

Flask is a lightweight microframework written in Python which gives you the underlying features you need to build web applications. It is a WSGI (Web Server Gateway Interface) toolkit which implements request, response, and other such functions.

Creating a file which uses Flask to create Rest API endpoints

Now, you need to create a RESTful application through flask, which uses the ML Model you developed to predict the data. To create such application, we are going to use flask.

Here’s the code of the application:

We use the .json file to create our model again and we use the .h5 file to load the weights into our model. Later, we pass the data into our model to do the prediction and that predicted value is then stored in a variable.

@app.route adds a ‘/predict’ route to the endpoint where the contents of the printPrediction() method is displayed.

Note: If you skip the @app.route code block, then your webpage will have nothing to display and will thus, give you an error.

Running the Application

Run the command 'python3 <filename>.py' in your terminal, you will see this message once your application is running:

To run your Rest API endpoint URL into your browser through terminal, use the following command:
curl http://<apiendpointurl:port>/predict

As soon as you run the command, you will see the predicted value as an output.

This is how you create a RESTful application using Flask in Linux consuming your Machine Learning model.
1 Comment
Labels in this area