Deep Learning Deployment Basics - Neural Network Web Apps

Deep Learning Course - Level: Intermediate

Deploy Keras neural network to Flask web service | Part 2 - Build your first Flask app

video

expand_more chevron_left

text

expand_more chevron_left

Build your first Flask web service

In this episode, we're going to quickly discuss Flask, see how to get it installed, and make our first simple web service.

We briefly talked last time about how we'd be working with Flask over the next several episodes, and we referred to what we'd be building as a “web service.”

Let's elaborate on Flask more now so we can get a little more comfortable and understand its role in our upcoming tasks.

Introduction to Flask

Technically, Flask is considered a microframework for Python. A microframework is just a term that describes a very minimal web application framework. A web application framework is just software that supports the development of web apps, including web services.

As previously discussed, we'll be using Flask to host a web service. Our web service will be handling HTTP requests that come in asking for predictions of images of cats and dogs from our fine-tuned VGG16 network.

Before we start creating a web service, we first need to get Flask onto our machines.

Installing Flask

To install Flask, you'll first need an environment where Python is installed.

Installation is pretty straightforward. If you're using a Linux environment, then assuming you already have Python installed, from the terminal you can simply run the following command.

sudo pip install Flask

If you're using a Windows environment, then from a command line window launched with admin privileges, you can run the following command.

pip install Flask

If you're using Anaconda, then from the terminal, you can run the following command.

conda install -c anaconda flask

Creating a sample Flask app

Now that we have Flask installed, let's test it out to make sure it works.

To do this, we're going to write a simple web service that gives the user the message “Flask is running!” when we browse to the web service.

First, we need to create a Python file for where we'll write our code.

Create a file called sample_app.py, and place it inside of a new directory called flask_apps.

Now, let's open this file to begin writing the code.

First, we import the Flask class.

from flask import Flask

Next, we define a variable called app.

app = Flask(__name__)

This line is creating an instance of the Flask class, and the __name__ argument is the name of the application's module. The Flask documentation tells us to use this name like this if we're simply using a single module like we are here.

Next, we have an app decorator @app.route that tells Flask what URL a user has to browse to in order for the function underneath it to be called. The argument we pass to the route() function is also called an endpoint.

@app.route('/sample')

So, when someone browses to this sample endpoint, only then will they get what we define next.

Now, to the function.

def running():
    return 'Flask is running!'

I've named this function running(), and all it does is return the string “Flask is running!” when the user browses to the sample endpoint.

That's it for what we'll include in our program! This is our web service.

Starting the Flask app

Now, we need to tell Flask which program we want to run, and then we need to start Flask. Be sure to first cd into the directory for which your app is located before you run the following commands.

If you're running this from a Linux machine, then from the terminal, type the following command.

export FLASK_APP=sample_app.py

If you're running this from a Windows machine, then from the command prompt launched with admin privileges, run the following command.

C:\path\to\app set FLASK_APP=sample_app.py

If you'd like to run this from Powershell, then instead, run the following command.

$env:FLASK_APP="sample_app.py"

Next, both for Linux and Windows users, run the following command.

flask run --host=0.0.0.0

This command starts Flask, and the host parameter specified in this way makes the web service visible and publicly available to all computers on your network. Otherwise, if you don't specify the host parameter like this, then you can only access the service from your local machine. Just make sure you trust the users on your network before you specify the parameter in this way.

After running this command, you should receive indication in your terminal that Flask is running on port 5000, which is the default port for Flask, so let's see what happens when we browse to our service.

If you're going to be browsing from the same machine where Flask is running, then you don't have to worry about what IP address is assigned to your machine, and you can simply browse to http://localhost:5000/sample.

Remember, sample is the name of the endpoint we created in our service.

If you're browsing from a remote machine, then you should the IP address where Flask is running for localhost in the address.

When we browse to the page, we should receive a message on the page that says “Flask is running!”

From the Flask terminal, we can also see some logging information for the request that came through, including the IP address that browsed to the web service, the date and time for which the request occurred, what type of request it was, which endpoint was accessed, and the HTTP response code.

Make sure you're up and running with Flask and that you can do everything we demoed in this episode. If you happen to get any errors when you attempt to start Flask, then be sure to first check the Flask documentation to see if your error can be resolved by the steps mentioned there.

In the next episode, we'll create another simple web service with Flask, but this time, we'll actually be passing data to the service, and having it return data back to us.

To pass data to Flask, we'll also be building a front end application in HTML and Javascript. This will prepare us for the work we'll need to do to create the service and the front end application we'll create to handle our predictions from VGG16 in a later episode.

Let me know in the comments if you're all set and ready to go, and I'll see ya in the next one!

quiz

expand_more chevron_left
deeplizard logo DEEPLIZARD Message notifications

Quiz Results

resources

expand_more chevron_left
Here, we're going to quickly discuss Flask, see how to get it installed, and make our first simple web service. We briefly talked last time about how we'd be working with flask over the next several videos, and we referred to what we'd be building as a web service. So let's elaborate on flask now and get a little more familiar so we can understand its role in our upcoming tasks. Note, in the video, we show how to export the FLASK_APP variable in a Linux terminal so that Flask knows which application to work with. To do this, we simply ran the following command: export FLASK_APP=sample_app.py To do the equivalent in Windows (which isn't shown in the video), run the following command from the command prompt launched with admin privileges: C:\path\to\app set FLASK_APP=sample_app.py If you run into issues with starting Flask, check this out: http://flask.pocoo.org/docs/1.0/quickstart/#what-to-do-if-the-server-does-not-start 🕒🦎 VIDEO SECTIONS 🦎🕒 00:00 Welcome to DEEPLIZARD - Go to deeplizard.com for learning resources 00:30 Help deeplizard add video timestamps - See example in the description 06:49 Collective Intelligence and the DEEPLIZARD HIVEMIND 💥🦎 DEEPLIZARD COMMUNITY RESOURCES 🦎💥 👋 Hey, we're Chris and Mandy, the creators of deeplizard! 👀 CHECK OUT OUR VLOG: 🔗 https://youtube.com/deeplizardvlog 💪 CHECK OUT OUR FITNESS CHANNEL: 🔗 https://www.youtube.com/channel/UCdCxHNCexDrAx78VfAuyKiA 🧠 Use code DEEPLIZARD at checkout to receive 15% off your first Neurohacker order: 🔗 https://neurohacker.com/shop?rfsn=6488344.d171c6 ❤️🦎 Special thanks to the following polymaths of the deeplizard hivemind: Mano Prime 👀 Follow deeplizard: Our vlog: https://youtube.com/deeplizardvlog Fitness: https://www.youtube.com/channel/UCdCxHNCexDrAx78VfAuyKiA Facebook: https://facebook.com/deeplizard Instagram: https://instagram.com/deeplizard Twitter: https://twitter.com/deeplizard Patreon: https://patreon.com/deeplizard YouTube: https://youtube.com/deeplizard 🎓 Deep Learning with deeplizard: AI Art for Beginners - https://deeplizard.com/course/sdcpailzrd Deep Learning Dictionary - https://deeplizard.com/course/ddcpailzrd Deep Learning Fundamentals - https://deeplizard.com/course/dlcpailzrd Learn TensorFlow - https://deeplizard.com/course/tfcpailzrd Learn PyTorch - https://deeplizard.com/course/ptcpailzrd Natural Language Processing - https://deeplizard.com/course/txtcpailzrd Reinforcement Learning - https://deeplizard.com/course/rlcpailzrd Generative Adversarial Networks - https://deeplizard.com/course/gacpailzrd Stable Diffusion Masterclass - https://deeplizard.com/course/dicpailzrd 🎓 Other Courses: DL Fundamentals Classic - https://deeplizard.com/learn/video/gZmobeGL0Yg Deep Learning Deployment - https://deeplizard.com/learn/video/SI1hVGvbbZ4 Data Science - https://deeplizard.com/learn/video/d11chG7Z-xk Trading - https://deeplizard.com/learn/video/ZpfCK_uHL9Y 🛒 Check out products deeplizard recommends on Amazon: 🔗 https://amazon.com/shop/deeplizard 📕 Get a FREE 30-day Audible trial and 2 FREE audio books using deeplizard's link: 🔗 https://amzn.to/2yoqWRn 🎵 deeplizard uses music by Kevin MacLeod 🔗 https://youtube.com/channel/UCSZXFhRIx6b0dFX3xS8L1yQ ❤️ Please use the knowledge gained from deeplizard content for good, not evil.

updates

expand_more chevron_left
deeplizard logo DEEPLIZARD Message notifications

Update history for this page

Did you know you that deeplizard content is regularly updated and maintained?

  • Updated
  • Maintained

Spot something that needs to be updated? Don't hesitate to let us know. We'll fix it!


All relevant updates for the content on this page are listed below.