Skip to content
Tutorials Center
My learning

Topics

Lesson 67 of 72 ยท Projects

Deploying a Machine Learning Model with Flask

Serve a scikit-learn model with Flask. Train and save it with joblib, load it once at startup, build a prediction form and a JSON API, validate inputs, test it and plan for production.

A model that lives in a notebook helps nobody. To make it useful, other people or programs must be able to send it data and get a prediction back. The simplest way to do that is to wrap the trained model in a small web application. Flask is a good fit: the model is just a Python object, and a prediction is just a function call inside a route.

In this project you will train a small scikit-learn classifier, save it to a file with joblib, load it once when the app starts, and serve it in two ways: an HTML form for people and a JSON API for other programs. You will also validate the input, test the app, and learn what changes when you go to production.

You need machine-learning basics for this project (what a model, features and labels are). The scikit-learn lessons explain them. Here we keep the model tiny so the web part stays in focus.

The shape of the project

flowchart LR
    T["train.py<br/>fit model"] -- "joblib.dump" --> F[("model.joblib")]
    F -- "joblib.load<br/>once at startup" --> A["Flask app"]
    U["Browser form"] -- "POST /predict" --> A
    C["Other program"] -- "POST /api/predict (JSON)" --> A
    A -- "prediction + probability" --> U
    A -- "JSON" --> C

Training happens offline, once. The web app only loads the saved result and answers requests.

A typical layout:

TEXT
ml-app/
  train.py          # run once to create model.joblib
  app.py            # Flask app
  model.joblib      # the trained artifact (do not commit large models to Git)
  templates/index.html

Keep training and serving separate. The web process should never retrain on a request: training is slow, needs the whole dataset, and would block every visitor.

Step 1: train and save a model

We use the classic iris data set (measurements of three flower species) that ships with scikit-learn. Training produces a pipeline (scaling plus a classifier) so the preprocessing that the model needs travels with the model. Saving the pipeline, not just the classifier, avoids the common bug where the app forgets to scale inputs.

Python train.py: fit a model and save it with joblib

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

We saved a dictionary containing the pipeline, the feature names and the class names, plus the scikit-learn version. A saved model is only guaranteed to load with the same library versions that created it, so record them and pin the same versions in the server's requirements.txt.

joblib.load (like pickle.load) can run arbitrary code stored in the file. Only load model files that you produced or that come from a source you trust. Never load a file uploaded by a visitor.

Step 2: load once, predict many times

Loading a model can take seconds, so do it once at startup and keep it in a module-level variable (or app.extensions). Do not load it inside the route.

Then write the prediction function so that it is independent of Flask and easy to test. It checks the inputs, builds the feature row in the order the model expects, and returns a clean result.

Python app.py: a form page and a JSON API for the model

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Study how the app protects itself and the model:

  • Validation first. Missing values, text where a number is expected, nan, inf and absurd magnitudes are rejected with a clear 4xx message before the model ever sees them. A model will happily return a "confident" answer for garbage input, so the range check is part of the product, not decoration.
  • Feature order matters. The row is built by looping over FEATURES, the order recorded at training time, never by trusting the order of a JSON object or of form fields.
  • predict_proba gives the model's probabilities, which are more informative than a bare label. Remember they are only as trustworthy as the model's calibration.
  • Two front doors, one function. The form and the API both call predict, so the logic is written and tested once.
  • The form re-displays what the user typed, and Jinja escapes it, so a pasted <script> in a field is harmless (Web Security).

Step 3: test it

Treat the model like any other dependency: test the app around it. Fast tests check validation and response shapes; a small golden test checks that known examples still produce the expected species, which catches a wrong model file or library mix-up.

Python
import pytest

@pytest.fixture()
def client():
    app.config["TESTING"] = True
    return app.test_client()

def test_known_flowers(client):
    assert client.post("/api/predict", json=SETOSA).get_json()["species"] == "setosa"
    assert client.post("/api/predict", json=VIRGINICA).get_json()["species"] == "virginica"

@pytest.mark.parametrize("bad", [{}, {"petal width (cm)": "x"}, {"sepal length (cm)": 1e9}])
def test_rejects_bad_input(client, bad):
    assert client.post("/api/predict", json={**SETOSA, **bad}).status_code in (400, 422)

See Testing Flask Apps for fixtures and the test client.

Step 4: take it to production

  • Run a real server. gunicorn -w 2 app:app instead of flask run (Deployment). Each worker loads its own copy of the model, so memory use is workers multiplied by model size. For large models use fewer workers with threads, or preload the app (--preload) so workers share memory.
  • Pin versions. requirements.txt must pin the same scikit-learn, NumPy and joblib versions used for training, or loading may fail or silently behave differently.
  • Keep the artifact out of the image build if it is large. Store it in object storage and download it at startup, or bake a versioned file into the image with a clear name (model-2026-03-01.joblib) so you can roll back.
  • Do not block on heavy models. If a prediction takes more than a fraction of a second, use a queue and workers (Async Views and Background Tasks), or a dedicated model server.
  • Limit the request. Cap the body size (MAX_CONTENT_LENGTH), rate-limit the endpoint, and require an API key if it is not public (API Authentication).
  • Monitor. Log inputs and outputs (without personal data), watch the distribution of inputs over time, and plan to retrain: a model's accuracy decays when the world changes.
  • Be honest in the UI. Show confidence, state what the model was trained on, and do not use a toy classifier for decisions that affect people.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where should the trained model be loaded in a Flask app?

    Loading is slow. Doing it at startup means every request only pays for the prediction itself.
  2. 2. Why save a whole scikit-learn Pipeline instead of just the classifier?

    A classifier trained on scaled data expects scaled input. Keeping the steps together prevents train/serve mismatch.
  3. 3. Why validate the input before calling predict?

    Predictions on out-of-range or malformed data are meaningless but look confident. Validation protects users and the model.
  4. 4. What is the security risk of joblib.load or pickle.load?

    Pickle-based formats can run arbitrary code during loading. Never load a model file supplied by a visitor.

Summary

  • Train offline, save the whole pipeline (plus feature order, class names and library versions) with joblib, and load it once at startup.
  • Put the prediction logic in a plain function used by both the HTML form and the JSON API, and validate inputs strictly.
  • Build the feature row in the training order, and return probabilities as well as the label.
  • Test validation and a few known predictions; pin the same library versions on the server.
  • In production use gunicorn, mind per-worker memory, protect and rate-limit the endpoint, monitor inputs, and retrain when data drifts.
  • Never load model files from untrusted sources.

Next: show the data behind your models and apps with Charts and Dashboards in Flask.