

Building a RESTful application using Flask in LINUX which consumes a Machine Lea...
source link: https://blogs.sap.com/2021/06/09/building-a-restful-application-using-flask-in-linux-which-consumes-a-machine-learning-model-to-predict-data/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.
Prerequisites
You need to have training data and test data ready with you.
Installation required in Windows:
- Install PuTTY.exe, Puttygen.exe, Pscp.exe files.
- Install Anaconda
- Install TensorFlow
Installation required in LINUX:
- Install python (usually LINUX systems have Python already installed in them).
- Install pip.
Command: sudo zypper install python3-pip
- Install Flask.
Command: pip3 install flask
- Install Anaconda.
Command: wget https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh bash Anaconda3-2020.02-Linux-x86_64.sh
- 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:
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 out 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.
Recommend
-
14
NewsMarch 26, 2020How much more power does 5G consume over 4G? Redmi has answer
-
7
Home News Bitcoin mining consumes 0.5% of all electricity used globally and 7 times Google’s total usage Bitcoin...
-
12
Building RESTful APIs With Flask: The DIY Approach REpresentational State Transfer (REST) is a w...
-
10
Building RESTful APIs With Flask: An ORM With SQLAlchemy by Shalabh AggarwalOct 12, 2016(Updated Apr 11, 2022)
-
8
Building RESTful APIs With Flask: ORM Independentby Shalabh AggarwalOct 19, 2016(Updated Apr 27, 2022)Read...
-
12
Siv Scripts Solving Problems Using Code (Note: This post is part of my
-
6
TechTron Consumes 99.9% Less Power Than Bitcoin and Ethereum, Crypto Researcher SaysThe network consumed power equiva...
-
9
13-flask博客项目之restful api详解1-概念
-
2
Jack Daniel's-Fed Whiskey Fungus Consumes Tennessee Town Fueled by evaporating Jack Daniel's whiskey, the fungus is coating the town in a gray...
-
6
Nvidia's RTX 4070 consumes just 186W while gaming, leaked slide reveals A new Ada Lovelace card is on the horizon By
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK