FastAPI
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python based on standard Python type hints. It is incredibly popular for serving Machine Learning models due to its speed, asynchronous capabilities, and automatic documentation.
Key Features
- Fast: On par with NodeJS and Go (thanks to Starlette and Pydantic).
- Type Checking: Uses Python type hints for data validation and serialization.
- Auto-Docs: Automatically generates interactive API documentation (Swagger UI).
Basic Usage
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
# Define input schema
class InferenceRequest(BaseModel):
text: str
max_length: int = 50
# Define API Endpoint
@app.post("/predict")
def predict(request: InferenceRequest):
# Dummy ML inference
processed_text = request.text.upper()
return {
"status": "success",
"result": processed_text,
"tokens_used": len(processed_text.split())
}
# Run with: uvicorn main:app --reload
Why it is essential for AI
Training a model is only half the battle. To actually use the model in a real product, you need to expose it over an HTTP API so web apps, mobile apps, and other services can send data to it and receive predictions.