Deploy Flask App — with PIP & Virtualenv

Skillcate AI
6 min readSep 26, 2022

--

Do you know — around 90% of the Machine Learning Models never make it to production?

As a Data Scientist, you would definitely want to ensure your work sees the light of the day in production. Now to ensure that happens, it’s quite pertinent you practice deploying 10’s of academic/ hobby ML Projects into production-like environment.

In this article, I’ll give you a comprehensive walkthrough of deploying a Sentiment Analysis Model using Flask, PIP & Virtualenv.

Watch the video tutorial instead

If you are more of a video person, go ahead and watch it on YouTube, instead. Make sure to subscribe to my channel to get access to all of my latest content.

BTW, we earlier built a Sentiment Analysis Model here. In this tutorial, we shall be deploying this ML project in Flask. I strongly recommend you to go through this model building tutorial too, to gain an end-to-end visibility of formulating a problem, training ML model, deploying ML model & then testing it in a live environment.

As a data scientist you may not always deploy your ML models yourself, as you may have designated professionals, like: ML Engineers, to help you out on this. But, it is always good to know this whole process. Good data scientists always keep deployment constraints in mind while developing models. For example:

  • If you are building a search engine, you would want to have near real time response on your model predictions for a good user experience; and to achieve this you will always keep model latency in mind while training your model.
  • Or else, if your tech infra doesn’t have a high-end GPU, you would want to build a simpler model, rather than applying deep neural networks.

Anyways, let me give you guys refresher on some related topics before we move further with flask deployment. You may skip these ‘Prerequisites’ parts, if you wish..

Prerequisites: What is deployment?

As a Machine Learning Scientist, you work on solving a complex problem, like:

  • improving Recommendation Algorithm, or
  • efficiently matching Driver-to-Rider — like uber, Food Delivery Man-to-Delivery Address — like zomato, etc..

For all these projects, you will spend a lot many weeks writing code and doing model iterations. You will install 1000s of dependencies, libraries, packages, etc., during all this time. And when you finally have your eureka moment, you want to show your work to the world, and make it available for millions to use. That’s where you need to prepare a comprehensive package of all your project related files with which your project can run on any machine, of course with the given hardware requirements. And that’s what a deployment is, in essence.

Wait, how do I make project package? 😁

Prerequisites: What is VirtualEnv?

That brings me to this second point, which is virtual environment. It is basically a way to create a small working space within your machine, which is a virgin system, like you buying a new computer.

So, once you have built your model, you may want to run it in this virtual environment, where you install only the specific dependencies your model needs. And when your project finally runs in the isolated virtual environment, you extract a list of all dependencies you installed in this environment, and put them up into your project package as the requirements file, actually called requirements.txt.

This is exactly what we have also done in preparing our project package that we have hosted on GitHub. Now, what is GitHub? 😛

Prerequisites: What is GitHub?

Github is an internet hosting service that allows users to store, manage, track and control changes on their projects. For any new project, you create a Git repository with your project name, and then put all your project package files onto it.

This is our Skillcate Sentiment Analysis Project Repository on GitHub. Everything here is part of our project package. Let me give you a quick overview of these files now. I’ll go bottom up.

On the GitHub repo page, you will see a readme document, which is nothing but our GitHub Documentation on what this Git Repository is about. This doc is always visible below the Git Repo, for viewers’ easy reference on what this repo does.

Let’s get started with Flask deployment now..

Machine Learning Project Stages

Over here, this is an oversimplified ML Project Flow. There are two stages, first one is model building, where we write a training code and run it on labeled data to get our ML model with desired functionality.

Second stage is Deployment, where we deploy the developed Model for the desired target group to consume. For this, we additionally need a Web-App-Code, that helps us perform live queries on our Sentiment Analysis Model Pickle. Like this:

So, this is what we wish to achieve with our Model Deployment. This is basically a webpage, that shows:

  • a heading of Sentiment Analysis,
  • has a textbox that take any amazon alexa product review as input, and
  • a Predict button for sending query to model for prediction.

Basis model response, we get a Positive, if the input review is positive, or negative, if the review is negative displayed here.

Now to achieve this, we need to build a flask web app with this architecture.

Building a Web App

The flask app provides a cool front-end (or a webpage, what the user see) and a sophisticated back-end to interface with our model. To build a flask app, these are the building blocks: HTML Page Template, Model .pkl & dependencies.

The web app is basically an orchestrator that oversees the entire app performance. It has four sections to it. First one is to import all essential libraries used by our model. Second is to import the model

Within the flask app, we shall load our model pkl file along with the dependencies. We shall also write App routes “Home” & “Predict” to show our project HTML Webpage according to the information we need to display.

Refer to this GitHub repo, for deployment files. This is how our Web App looks like:

Create a VirtualEnv to run Web App?

Once you have the app.py file with you, next step is to create a virtual environment in your working directory. Code is provided in the Readme file on GitHub Repo..

  • For this you need to open Windows Powershell or Mac Terminal (based on your OS), and set your working directory to your project folder
cd <your project directory>
  • Clone my GitHub repo into your machine
git clone https://github.com/skillcate/sentiment_analysis_with_sklearn_pipeline.git
  • Create a virtual environment using virtualenv and activate it
pip install virtualenv
virtualenv ENV # create virtual environment with name ENV
.\ENV\Scripts\activate. # activate virtual environment

In case of Mac, use this line of code to activate virtual environment

source ENV/bin/activate
  • Then, install all dependencies into this new virtual environment, so it’s equipped to run the web app app.py
pip install -r .\requirements.txt    # for windowspip install -r requirements.txt      # for mac
  • And as last step, run the app.py. And boom!!
python app.py
  • You may also want to write a requirements.txt file from your virtual environment to make it a part of your project package files, which has list of all your project dependencies along with their versions

Conclusion

Among Machine Learning Professionals, deployment is considered to be the trickiest affair and there are thousands of crazy memes around Model Deployment. These are the few memes I leave you with, to excite you further to stay curious and keep learning.

Brief about Skillcate

At Skillcate, we are on a mission to bring you application based machine learning education. We launch new machine learning projects every week. So, make sure to subscribe to our youtube channel and also hit that bell icon, so you get notified when our new ML Projects go live.

Shall be back soon with a new ML project. Until then, happy learning 🤗!!

--

--

Skillcate AI

Project-based courses — solving real business problems end-to-end. Book a free 1:1 mentoring session at skillcate.com :)