Easily Share your ML Model with Stakeholders via the Web
Add a few lines of Flask and expose your code on the web
As discussed in my previous story, as a Data Scientist, I’ve been wondering:
- Does the model need to be trained every single time someone wants to see the result?
2. what if, I need to share my code with a stakeholder who doesn’t use Jupyter?
3. What if they don’t even have Python installed? How can a new dataset feed into the model and get output?
In Pickle your model in Python, I discussed the means of packaging your model using pickle
, however, questions 2 and 3 from the list above were not answered. In this article, we cover how to easily expose our application (either a simple or the most sophisticated one) as a web app.
If you haven’t yet, make sure you read previous article first.
The intention is by introducing these simple components, in the future we will be able to come up with an end-to-end solution to deploy a Machine Learning model so that users can interact with the model via their browsers in a responsive way. 😎…cool, right?
How?
To do so, we need to use the following components:
- Pickle (previous article)
- Flask (current article)
- Docker (coming next 😉)
🛑 Please note that I use all my work on a Mac OS.
Flask
Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It has become one of the most popular Python web application frameworks.
What problem does it solve?
Let’s say that I have a Python application (either a simple “Hello World” or a sophisticated “Spam Detection AI”), and I want to expose it on the web. Flask is my to-go 👍
Why Flask?
When it comes to web frameworks, there are various discussions on the web. But why Flask? 🤔
In a nutshell, Flask is extremely flexible and easy to use which makes it a perfect candidate for Data Scientists like me who do not have that much of a programming background. What’ s more, it is Pythonic which uses the Python constructs and data structures with clean, readable idioms.
Let’s get started with Flask
Full documentation is available here.
Create an environment
Use a virtual environment to manage the dependencies for your project, both in development and in production.
- What problem does a virtual environment solve?
The more Python projects you have, the more likely it is that you need to work with different versions of Python libraries, or even Python itself. Newer versions of libraries for one project can break compatibility in another project.
Virtual environments are independent groups of Python libraries, one for each project. Packages installed for one project will not affect other projects or the operating system’s packages.
Open a new terminal and navigate to your project folder.
A cool way to navigate directly to a folder on Mac Terminal 👇👇👇
Using the Terminal, create a project folder and a venv
folder within:
mkdir SpamDetect
cd SpamDetect
python3 -m venv venv
If you browse to that folder now, you see the new venv
folder is created.
Activate the environment
. venv/bin/activate
We can see that the venv
is activated.
Install Flask
pip install Flask
Since I do already have Flask
installed, it has suggested that I upgrade to the higher version.
Your Flask application
In the current article for simplicity purpose, before jumping into the Machine Learning model and seeing complex codes all at the same time, I would like to show you a simple Flask app.
Step 1. Create a new file called hello.py
with Python extension.
$ touch hello.py
Step 2. A new file appears in your folder:
Open the file and save the Flask app in the code.
# A minimal Flask appfrom flask import Flask, escape, requestapp = Flask(__name__)@app.route('/')
def hello():
name = request.args.get("name", "World")
return f'Hello, {escape(name)}!'
What did that code do?
- First we imported the
Flask
, flask’sescape
andrequest
class. - Next we create an instance of this class. The first argument is the name of the application’s module or package. If you are using a single module (as in this example), you should use
__name__
because depending on if it’s started as application or imported as module the name will be different ('__main__'
versus the actual import name). This is needed so that Flask knows where to look for templates, static files, and so on. For more information have a look at theFlask
documentation. - We then use the
route()
decorator to tell Flask what URL should trigger our function. In the documentations, you see that flask has its ownescape
andrequest
format. - The function is given a name which is also used to generate URLs for that particular function, and returns the message we want to display in the user’s browser.
Step 3. Export FLASK_APP env variable
To run the application, use the flask run
command or python -m flask
. Before you can do that you need to tell your terminal the application to work with by exporting the FLASK_APP
environment variable:
$ export FLASK_APP=hello.py
$ flask run
Step 4. Go to browser and enter the URL provided in your Terminal.
Flask Summary
We learned:
how to create an environment and activate it
how to install Flask
how to call a simple Flask application and see the result in browser
Upcoming Next…
Now we know how flask
can become handy when we wanna provide a UI to our ML model users. So we park flask
here and in the next article move to Docker and see how Flask is included in your dockerised container.