Summary of "Creating APIs For Machine Learning Models with FastAPI"
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:
- Load MNIST (mnist_784) with fetch_openml.
- Split the data with train_test_split (e.g., test_size=0.2).
- Train a RandomForestClassifier (example uses n_jobs=-1 to use all cores).
- Evaluate on the test set.
- 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 anUploadFile(FastAPI type hints are required for request parsing).
Endpoint flow (typical implementation):
- Read the uploaded file bytes:
contents = await file.read()
- Open the image from bytes using Pillow (
PIL.Image) and convert to grayscale ('L'). - Optionally invert colors with
PIL.ImageOps.invertif your training format expects inverted colors. - Resize to 28×28 (use antialiasing) if necessary.
- Convert to a NumPy array and reshape/flatten to match the model’s expected input shape.
- 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):
- Check for a selected file.
- Create
FormDataand append the file. - POST the form data to
http://127.0.0.1:8000/predict_imageusingfetch():- method:
"POST" - body:
formData
- method:
- Await
response.json()and update the result element withresult.prediction. - 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
pickleis 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.
Category
Technology
Share this summary
Is the summary off?
If you think the summary is inaccurate, you can reprocess it with the latest model.