Model Serving Templates
FastAPI/Flask model serving endpoints, batched inference, A/B testing, and canary deployment configurations.
📄 Product Preview
Try the interactive reader and demo tools below, or get the full product with all content unlocked.
📖 Interactive Reader (Free Preview) ⚙ Try Demo Tools 📦 Download Free Sample📁 File Structure 18 files
📖 Documentation Preview README excerpt
Model Serving Templates
Production-ready templates for serving machine-learning models over HTTP with
dynamic request micro-batching. Ships both a FastAPI (ASGI) and a Flask
(WSGI) implementation behind one identical API, plus a pluggable model loader
(pickle / joblib / ONNX), Prometheus metrics, runnable examples, tests, and a
slim Docker setup with a stable + canary topology.
The headline feature is the batcher: instances arriving in many concurrent
requests are transparently merged into single predict() calls, then fanned
back out to each caller — often a 10×+ throughput win with a bounded latency
cost you control.
Features
- FastAPI app (
src/model_serving/app.py) withPOST /predict,
GET /healthz, GET /metrics, and lifespan-based model loading.
- Dynamic micro-batcher (
batching.py) — asyncio-based, with a size or
latency flush policy; pure stdlib and fully unit-tested.
- Pluggable loaders (
loaders.py) — pickle, joblib, and ONNX, with lazy
imports and a register_loader() hook for custom formats, plus an LRU
ModelCache for multi-version serving.
- Pydantic v2 schemas (
schemas.py) — request/response validation and
env-driven ServingSettings.
- Metrics + logging (
middleware.py) — a dependency-free Prometheus
exposition (counter + histogram) and an ASGI latency middleware.
- Flask alternative (
flask_app.py) — same API for Gunicorn/WSGI shops,
using a thread-based batcher.
- Runnable examples, tests, and Docker — train-and-serve a real sklearn
model, an httpx client, pytest suites, a slim Dockerfile, and a compose file.
Project structure
model-serving-templates/
src/model_serving/
__init__.py - package exports (create_app, DynamicBatcher, ...)
app.py - FastAPI app factory + /predict, /healthz, /metrics
batching.py - asyncio DynamicBatcher (size/latency flush)
loaders.py - ModelHandle + pickle/joblib/onnx loaders + ModelCache
schemas.py - Pydantic request/response models + ServingSettings
middleware.py - Prometheus metrics registry + ASGI MetricsMiddleware
flask_app.py - Flask/WSGI app + ThreadedBatcher
examples/
serve_sklearn.py - train a sklearn model and boot the FastAPI app
client_request.py - httpx client + concurrency burst to exercise batching
guides/
serving-architecture.md - request lifecycle, loading, A/B & canary
batching-and-scaling.md - tuning the batcher, autoscaling, observability
tests/
test_app.py - FastAPI TestClient end-to-end tests
test_batching.py - stdlib-only batcher tests (also runnable standalone)
docker/
Dockerfile - slim python:3.11 + uvicorn, non-root, healthcheck
docker-compose.yml - stable + canary serving services
README.md LICENSE manifest.json
Quick start
... continues with setup instructions, usage examples, and more.