Natural Language Processing · Advanced level

Advanced level · Module 10: NLP Model Evaluation and Deployment · Lesson 2 of 4

Packaging and Serving an NLP Model

Learners save their model, wrap it in a FastAPI service with input validation and confidence scores, and make it run offline on a CPU laptop.

In this lesson you will

  • Save and load NLP models reliably
  • Serve a model through a FastAPI endpoint

Key concepts

joblib / save_pretrainedFastAPI endpointinput validationconfidence and 'unsure' responsesoffline installation

The lesson

Save scikit-learn pipelines with joblib.dump and transformers models with save_pretrained, together with the preprocessing code and library versions. Load them once when the service starts, not for each request.

FastAPI turns a Python function into a web endpoint: @app.post('/classify') receives text, validates its length, runs the model and returns the label, the confidence and an 'unsure' flag when confidence is low.

For offline use, package the model files, a requirements.txt and wheel files so that the service can be installed on a computer without internet. Run it with uvicorn on the local network so that staff can use it from their browsers.

Step by step

  1. Save the trained model and preprocessing.
  2. Write a FastAPI app with one endpoint and input validation.
  3. Test it with ten requests, including empty and very long text.
  4. Install and run it on a second computer without internet.

Practice activity

Deploy the level project model as an offline FastAPI service.

Expected result: A service that installs offline, validates input and returns labels with confidence.

Check your understanding

  1. Why load the model once at start-up?

    Show answer

    Loading for every request is slow.

  2. What should the service return when confidence is low?

    Show answer

    An 'unsure' flag so that a person checks the case.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.