Deploying machine learning models remains a significant challenge.Even though pushing your Machine Learning model to production is one of the most important steps of building a Machine Learning application , there aren’t many tutorials out there showing how to do so. Therefore in this article, I will go over how to productionize a Machine Learning model by building a normal website using the Flask web micro-framework.
Introduction to Amazon Web Services (AWS)
What is AWS? — Amazon Web Services(AWS) is a cloud service from Amazon, which provides services in the form of building blocks, these building blocks can be used to create and deploy any type of application in the cloud.
These services or building blocks are designed to work with each other, and result in applications which are sophisticated and highly scalable.
Introduction to Flask
What is Flask?
Flask is a web framework. This means flask provides you with tools, libraries and technologies that allow you to build a web application. This web application can be some web pages, a blog, a wiki or go as big as a web-based calendar application or a commercial website.
Flask is part of the categories of the micro-framework. Micro-framework are normally framework with little to no dependencies to external libraries. This has pros and cons. Pros would be that the framework is light, there are little dependency to update and watch for security bugs, cons is that some time you will have to do more work by yourself or increase yourself the list of dependencies by adding plugins. In the case of Flask, its dependencies are:
- Werkzeug a WSGI utility library
- jinja2 which is its template engine
7 simple steps to productionize your Machine Learning Model In AWS:
- Build a model on your local machine and store the model and other key model related variables in pickle files( .pkl files )
- Launch a micro instance on AWS.
- Connect to the AWS instance.
- Move the files to an AWS-EC2 instance
- Install all packages needed on the AWS instance.
- Run app.py on the AWS box.
- Check the output in the browser.
Software needed: (Anaconda)
To download Anaconda please click the links given below :
As an example I have taken the Amazon Fine Food Reviews Dataset. The Amazon Fine Food Reviews data-set consists of reviews of fine foods from Amazon.
Number of reviews: 568,454
Number of users: 256,059
Number of products: 74,258
Timespan: Oct 1999 — Oct 2012
Number of Attributes/Columns in data: 10
Let’s run the code in our Local Machine
Let me first show you the folder structure and files inside:
Step 1: Open Anaconda Prompt:
Step 2: Change to the code directory.
Step 3: Run “python app.py”
Step 4: Browser: http://localhost:8080/index
Command Prompt snapshot:
For Positive Review :
For Negative Review :
Let’s understand the code
- index.html : This is used to build a simple web page , where we are providing Review_text as input and on clicking the Submit button we are getting a prediction. Here we provide the name of the function (predict()) in form action tag.
2. app.py : This is where we write the code for creating an API using Flask.
This part of code crawls the URL (http://localhost:8080/) and if it finds “/” at the end it will just print “Hello World” .
This part of code crawls the URL (http://localhost:8080/index) and if it finds “/index” at the end it renders the html content and when it finds (‘/predict’, methods=[‘POST’]) in html form part it executes the predict() function.
Deploying the application on Amazon Web Server(AWS)
As we know that if we run any application on our local machine other people cannot access it, but if we deploy or host it on cloud servers then we can keep the instance running all the time and people can access it anytime. Thus, now let’s see how to deploy the Machine Learning Model in AWS.
Launch a micro-instance on AWS
Create an AWS account :
3. After login:
4. Launch the “ EC2 “ instance
5. Choose the “ Ubuntu Free Tire “
6. Click on ‘Select’
7. Choose “ t2.micro “ free tier eligible
8. Click on “ Review and Launch “
9. Click on ‘Launch’
10. Click on “Download Key Pair” and save the “.pem file” then click on “Launch Instance”
11. You will see this screen, you have successfully launched the an “EC2 “ instance, now we need to launch an “ Flask API “ in it
12. Final step:
13. Select the “Network & security” -> Security groups and then click “Create Security Group”
14. Then add the specific security group to network interface
15. Connect to your AWS instance
Follow the steps in the figure below to connect to your instance
16. Move the files to an AWS EC2 instance :
Open Command line and type below command to copy files to instance.
scp -r -i “for_live.pem” ./AFR ubuntu@ec2–13–59–191–237.us-east-2.compute.amazonaws.com
17. Install all the packages required to the AWS EC2 instance
sudo apt-get install python3-pip
pip3 install <each of the following packages>
pip3, pandas, numpy, sklearn, beautifulsoup4, lxml, flask, re
18. Run “ app.py “ on the AWS box.
19 .Check the output in the browser.
Productionizing your Machine Learning model is a important part of a machine learning project and Flask can be used to create a website with only a few lines of code.
Just follow all the steps to productionize your Machine Learning Model to AWS.
Where can you find my code?