Why FastAPI for ML Deployment?
FastAPI is a modern, fast web framework for building APIs with Python 3.7+ based on standard Python type hints. It's particularly well-suited for deploying machine learning models due to its performance and ease of use.
Key Features
- Fast: Very high performance, on par with NodeJS and Go
- Fast to code: Increase development speed by 200-300%
- Fewer bugs: Reduce human-induced errors by 40%
- Intuitive: Great editor support with auto-completion
- Short: Minimize code duplication
Basic FastAPI Application
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}Loading and Using ML Models
FastAPI can easily serve machine learning models. Here's an example using scikit-learn and joblib.
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
import numpy as np
app = FastAPI()
# Load the trained model
model = joblib.load('model.pkl')
class PredictionRequest(BaseModel):
features: list
@app.post("/predict")
async def predict(request: PredictionRequest):
# Make prediction
prediction = model.predict([request.features])
return {"prediction": prediction.tolist()}Model Serialization
Before deploying, you need to serialize your trained model. Popular options include:
- joblib (for scikit-learn models)
- pickle (Python standard library)
- ONNX (framework-agnostic format)
import joblib
# Save model
joblib.dump(model, 'model.pkl')
# Load model
loaded_model = joblib.load('model.pkl')FastAPI provides an excellent platform for deploying machine learning models with high performance and developer-friendly features.