You built this amazing machine learning model—this one, let’s say—but now what?

How do you take your model and turn it into something that you can display on the web? How do you turn it into something that other people can interact with? How do you make it useful?

You deploy it!

Photo by Collin Armstrong on Unsplash

Having the knowledge and ability to deploy your machine learning model is an absolute necessity. Whether you’re building a model or generating reports, you need this skill. It takes that model that you poured your blood, sweat, and tears into and turns it into something that absolutely anyone can play with and admire.

This article will walk you through the basics of deploying a machine learning model. We're going to deploy a PyTorch image classifier with Flask. This is the first critical step towards turning your model into an app.

By the end of this article, you’ll be able to take a PyTorch image classifier and turn it into a cool web app. In this app, users will be able to upload an image of a flower to see what kind of flower it is.

Your deep learning image classifier will now be an awesome image prediction app.

Let’s get started!

First, we should hit the basics. (You can find the official installation guide here if you want to take a look!)

It’s a good idea to set up a virtual environment to manage the dependencies of your project. You can do that by setting up a folder for your project, then going to your terminal and running:

mkdir myproject cd myproject python3 -m venv venv

I should let you know now that everything that I’m going to do here works on a Mac with Python 3. If you’re working on Windows or running Python 2, you might want to head on over to the official documentation to see what you might need to tweak to get up and running.

Next, activate your environment.

. venv/bin/activate

Now we can install Flask.

pip install Flask

You’re ready to go!

GIF via GIPHY

The quickstart guide is a really helpful document to check out if you’re interested in learning a bit more about the basics. I’m going to start you out with a little information that’s very similar to the information provided in that guide. There isn’t a better or clearer explanation of the basics of Flask than that one.

To create a seriously minimal Flask application, you start by creating a file.

Create the file and open it in your favorite text editor. Then type

from flask import Flask

app = Flask(__name__)



@app.route('/')

def hello_world():

return 'Hello, there!'

What does the code above do?

First of all, we imported the Flask class. Next, we created an instance of the class. The first argument is the name of the application’s module. If you’re using a single module, you’ll use (__name__) so that Flask knows where to look for stuff. The “route” part tells Flask what URL is supposed to trigger our function. We give the function a name that’s also used to generate URLs for that function and returns the message we want to display in the user’s browser.

You can save this as hello.py or whatever.py or anything else that makes you happy. Just don’t save it as flask.py because that will conflict with Flask. I like to go with app.py for the main flask file because that’s going to be what Flask wants to find later.

If you want to run it, go to your terminal and type

export FLASK_APP=app.py

and then

flask run

If everything’s working, you’ll see something like this

Running on http://127.0.0.1:5000/

Now you can click (command-click) on that web address or copy and paste it into your browser. See if it works!

(Any time you want to shut it down, just type control-C in your terminal window.)

Now, here’s the thing I really like to run when I’m trying to create something in Flask:

export FLASK_ENV=development

I run that command before I run flask run . This puts you in development mode. That means that instead of having to do a manual restart every single time you make a change to your code, your server will reload itself when you change your code. It will also provide you with a seriously helpful debugger when things go wrong!

Image by Miryams-Fotos on Pixabay

That being said, putting flask into development mode presents a major security risk, so you never, ever, ever want to use it on production machines.

The quickstart guide also tells you how to bind functions to meaningful URLs. That makes it easier for people to come back to your web app, how to create unique URLs, how to render templates, and more! It walks you through how to read and store cookies, how to upload files, and how to set up redirects and errors. Check it out if you’re looking for more of the basics.