Skip to main content
In this lesson, we explore the basics of Flask and demonstrate how to serve a machine learning model using Flask. This guide combines content from a Jupyter Notebook and a standalone Flask application file, providing a comprehensive introduction to model deployment with Flask.

Installing and Verifying Flask

Before building the application, ensure that Flask is installed. Run the following commands to install Flask, check its version, and inspect the directory structure of your Flask app:
These commands not only install Flask but also verify that the essential files exist within the flask_app/ directory, an important part of your model deployment workflow. If Flask is already installed, you may see output indicating that the requirements are already satisfied, for example:
You can also re-run the version command from within the Notebook:
This command displays your Python version, Flask version (3.1.0), and Werkzeug version (3.1.3). Next, verify the structure of your Flask app. Running:
should produce an output similar to:

Creating the Flask Application

Before starting the Flask server, initialize the app by loading any required environment variables and your machine learning model. In this example, we use the MobileNetV3 Large pre-trained model. It is essential that the model is loaded before the application processes any requests. Below is an example of the initial setup with logging and error handling:
Make sure that all required modules are imported and logging is correctly configured. The model must be loaded before any request is processed to avoid runtime errors.

Creating Endpoints

Prediction Endpoint

The /predict endpoint handles POST requests. It accepts a JSON payload that contains an image encoded in Base64. This endpoint decodes the image, preprocesses it, performs inference using the model, and returns the prediction in JSON format.
This endpoint performs the following steps:
  • Parses the request payload and verifies the presence of an "image" key.
  • Decodes the Base64-encoded image and converts it into an RGB image.
  • Applies image preprocessing before passing the tensor to the model.
  • Retrieves and returns the prediction using Flask’s jsonify method.

Health Endpoint

The /health endpoint is a simple GET endpoint used to verify that the server is running correctly. It returns a JSON response with a health status.

Testing the Flask Application

Running the App Directly

To start the Flask application, run the following command from your terminal:
This command initializes the app, loads the model, and starts a development server, typically accessible at http://127.0.0.1:5000. Example terminal output:
Do not use the Flask development server in a production environment. For production deployments, consider using a WSGI server such as Gunicorn.

Sending Test Requests

You can use the Python requests library to test your endpoints. Begin by creating a Base64-encoded string from an image (for example, “dog-1.jpg”):
Next, test the prediction endpoint:
A successful prediction response might look like:
And the health check output:

Testing Error Handling

Test error handling by sending requests without the required payload or using an incorrect key:
The first case should return a 500 error (e.g., failure to decode JSON), while the second returns a 400 status with a message indicating that no image was provided.

Running the App with Gunicorn

For production deployments, use a robust WSGI server like Gunicorn. Start the Gunicorn server with the following command:
Test the application on port 8080:
Terminal logs should display messages similar to:

Interpreting the Model Prediction

To convert the numeric prediction (e.g., 207) into a human-readable class label, use a mapping file (labels.json) available from Hugging Face. The labels file can be downloaded from: Imagenet 1K Labels After downloading the file, use the following code to interpret the prediction:
If, for instance, the prediction corresponds to a golden retriever, the output should confirm the image class as “golden retriever”—an ideal match if your input image depicts a golden retriever puppy.
This concludes our introduction to Flask and model deployment. With Flask, you can quickly set up HTTP endpoints to serve machine learning models, complete with robust error handling and logging. Happy coding!

Additional Resources

Watch Video

Practice Lab