Video summary

Creating APIs For Machine Learning Models with FastAPI

Main summary

Key takeaways

Technology

Overview

This tutorial shows how to turn a trained machine learning model into a simple HTTP API using FastAPI so non‑programmers or external applications can use the model. The full demo trains a classifier on MNIST (handwritten digits), serializes it, builds a FastAPI endpoint that accepts images, preprocesses them with Pillow/NumPy, returns predictions as JSON, and includes a minimal HTML+JS front end to upload images and display results.

What the tutorial covers (high-level)

  • Train a classifier on MNIST and serialize it to disk.
  • Build a FastAPI endpoint that accepts uploaded images and returns predictions.
  • Preprocess images with Pillow and NumPy to match the model’s expected input.
  • Provide a minimal browser front end (HTML + JavaScript) that uploads images and shows predictions.

Required packages / tools

  • Python packages: fastapi, uvicorn, pillow, scikit-learn, numpy
  • Built‑in: pickle, io
  • Dataset: MNIST via sklearn.datasets.fetch_openml
  • Example command to run the server:
uvicorn main:app --reload

Training & serialization (train_model.py)

Typical steps performed in the training script:

  1. Load MNIST (mnist_784) with fetch_openml.
  2. Split the data with train_test_split (e.g., test_size=0.2).
  3. Train a RandomForestClassifier (example uses n_jobs=-1 to use all cores).
  4. Evaluate on the test set.
  5. Serialize the fitted model to disk with pickle.dump (e.g., save as mnist_model.pickle).

Files: train_model.py performs the training and writes the serialized model file.

API server (main.py)

Key parts of the FastAPI server:

  • Create the app:
from fastapi import FastAPI
app = FastAPI()
  • Add CORS middleware (for demo use, allow all origins/methods/headers so a local HTML page can call the API; tighten in production).

  • Load the pickled model at startup with pickle.load.

  • Define a POST endpoint (for example /predict_image) that accepts an UploadFile (FastAPI type hints are required for request parsing).

Endpoint flow (typical implementation):

  1. Read the uploaded file bytes:
    • contents = await file.read()
  2. Open the image from bytes using Pillow (PIL.Image) and convert to grayscale ('L').
  3. Optionally invert colors with PIL.ImageOps.invert if your training format expects inverted colors.
  4. Resize to 28×28 (use antialiasing) if necessary.
  5. Convert to a NumPy array and reshape/flatten to match the model’s expected input shape.
  6. Call model.predict(image_array) and return JSON like:
{"prediction": 7}

Notes:

  • Use proper error handling and validation in production.
  • Ensure the same preprocessing steps used during training are applied at inference.

Front end (index.html + JavaScript)

Minimal UI elements:

  • File input: <input type="file" id="imageInput" accept="image/*">
  • A “Classify” button
  • A result display element

JavaScript flow (outline):

  1. Check for a selected file.
  2. Create FormData and append the file.
  3. POST the form data to http://127.0.0.1:8000/predict_image using fetch():
    • method: "POST"
    • body: formData
  4. Await response.json() and update the result element with result.prediction.
  5. Basic error handling: log errors to console and show an alert on failure.

Example fetch:

const response = await fetch("http://127.0.0.1:8000/predict_image", {
  method: "POST",
  body: formData
});
const result = await response.json();

Notes, tips, and caveats

  • Type hinting matters in FastAPI for request parsing and automatic docs.
  • The example uses permissive CORS for demonstration — restrict origins and methods in production.
  • Using pickle is simple but has security and maintenance implications; production systems often use safer/robust model serving patterns (e.g., model servers, containerized inference, signed artifacts).
  • This is a minimal demo. Production deployments require:
    • Proper API design
    • Authentication and authorization
    • Input validation and sanitization
    • Logging, monitoring, and scaling
    • A complete, user-friendly front end
  • The core idea: load a trained model and expose a small endpoint that accepts data and returns model.predict(...).

The “magic” is simply: load trained model + expose a small endpoint to accept data and return model.predict(…).

Files referenced

  • train_model.py — training script that saves the model
  • main.py — FastAPI server
  • index.html — simple front end
  • mnist_model.pickle — serialized model file

Main speaker / source

The content is based on a hands‑on FastAPI + scikit‑learn + Pillow tutorial presented in a YouTube video by an individual walking through the end‑to‑end demo.

Rate this summary

Your feedback will help improve summaries.

Improve this summary

Reprocess with a stronger model when the summary feels incomplete or inaccurate.

Pro

Translate summary in another language

Pro

Ask questions to this video

Chat for follow-up questions, clarifications, and source-backed answers.

Coming soon

Share this summary

Original video