Deploy the machine learning model might be weird for the beginners, or even some experience guys. That’s why I decided to take some time to write on it.
Before going to our article, little introduction on REST API
What Is A REST API?
Let’s say with an example, you are looking to know today weather. so you will search the current location on the search box or either it’s will detect your location. and you see a list of results as current temperature, humidity, wind speed so on… As same way you search for something, and you get a list of results back from the service you’re requesting from.
Each URL is called a request while the data sent back to you is called a response.
Now we has to come our post:-
As I already created the TFID model from my previous post. Please look on it
1. Create Flask API
First we have install the flask library on your machine.
$ pip install flask
After installation done, create new directory for the flask API and create python file app.py
Simple flask hello world code:-
from flask import Flask
app = Flask(__name__)@app.route('/')
return 'Hello, World!'
either you can get full code with git clone.
Change the directory into Flask API from git clone project
Now you can run the flask
$ flask run
After successfully run the flask, you can see the flask server is running on http://127.0.0.1:5000/
Go to browser and hit the URL http://127.0.0.1:5000/Search?query=computer science
The result of request it’s look like it:-
Flask API explanation:
Flask is divided into 3 parts are import the library, model, Routes
Firstly import the libraries:-
In this section we have add the require libraries, which you are using for your project.
Model parts for your API:-
Here we can load the required file or model and method declaration, which we are using in the next section of the API.
In this section, we have to define route mapping based on your API requirements.
Full code of app.py file:-
2. Make ready for Azure Deployment
Before going to deployment on azure, we have to create a python requirement file, it’s will install the required libraries on production environment. The file name must be as “requirements.txt”
For our project require the below libraries, it’ll be variable depends on your project requirement.
Now have to login with Azure subscription account
for the creation of web app on app services, you have select your subscription plan with the resource group. In the Name section, give your API name which you want it.
Here we are using the code file for python 3.7 Linux server, so choose the code on publish column with runtime stack is Python 3.7 of OS Linux. Finally, click on the review+create button. It’ll consume few a minutes to create it.
After successfully done, you can check with url will show as the below images for confirmation.
Now we have to open your azure account and find the app service, which we have already created for the deployment. Then go to deployment center tab and continue with the selection of local Git.
On the next screen, continue with the selection of App Service build service section.
At the last, click on the Deployment credentials tab for getting the username and password of local Git. and saved on the text file, we’ll used for authenticate on deployment time.
3. Deploy on Azure App service
Go the project directory on the terminal and run the below command to deploy the local project into azure. After running the last command($ git push azure master), it’ll ask the username & password, kindly provide the username & password, which we already saved on the text file.
$ git init
$ git add .
$ git remote add azure https://flaskapi.scm.azurewebsites.net:443/flaskapi.git$ git push azure master
On the below screenshot, we can verify the code is deployed successfully.
Kindly add the below command on the startup command section of configuration tab.
gunicorn — bind=0.0.0.0 — timeout 600 app:app
4. NLTK download via SSH shell
open SSH tab on the azure and change the root directory (cd ..)
$ cd ..
$ source antenv/bin/activate
$ pip install nltk
$ python -m nltk.downloader all
At finally we deployed the flask API on azure. Click the below url for confirmation. The url is different in your case
This was only a very simple example of building a Flask REST API for a TFID model. The same process can be applied to other machine learning or deep learning models once you have trained and saved them.