0
0
ML Pythonml~20 mins

FastAPI for model serving in ML Python - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - FastAPI for model serving
Problem:You have a trained machine learning model and want to make it available so others can send data and get predictions quickly using a web API.
Current Metrics:The model works well locally with 90% accuracy, but there is no API to serve it for real-time predictions.
Issue:No model serving setup exists. Users cannot access the model predictions through a web service.
Your Task
Create a FastAPI web service that loads the trained model and returns predictions for input data. The API should respond within 200 milliseconds for a single prediction request.
Use FastAPI framework only.
Load the model once when the server starts.
Accept JSON input with feature values.
Return JSON output with prediction result.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
ML Python
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
import uvicorn

# Define input data schema
class InputData(BaseModel):
    feature1: float
    feature2: float
    feature3: float

app = FastAPI()

# Load model once at startup
model = joblib.load('model.joblib')

@app.post('/predict')
async def predict(data: InputData):
    features = [[data.feature1, data.feature2, data.feature3]]
    prediction = model.predict(features)[0]
    return {'prediction': int(prediction)}

if __name__ == '__main__':
    uvicorn.run(app, host='127.0.0.1', port=8000)
Created FastAPI app with POST /predict endpoint.
Defined InputData class for input validation.
Loaded the trained model once globally.
Returned prediction as JSON response.
Results Interpretation

Before: Model only runs locally, no API available.

After: FastAPI service provides predictions via HTTP POST requests with low latency.

Serving a machine learning model with FastAPI allows easy, fast, and scalable access to predictions over the web.
Bonus Experiment
Add input validation to reject requests with missing or invalid feature values and return clear error messages.
💡 Hint
Use Pydantic's built-in validation features and FastAPI's automatic error handling to manage invalid inputs.