0
0
MLOpsdevops~5 mins

REST API serving with FastAPI in MLOps - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to share your machine learning model or data processing logic with other programs or users, you need a way to communicate over the internet. FastAPI helps you create a REST API quickly and easily so others can send requests and get responses from your code.
When you want to let other applications send data to your model and get predictions back instantly.
When you need a simple web service to expose your data processing functions to a team.
When you want to test your machine learning model with real inputs from a web client.
When you want to build a backend for a web or mobile app that uses your ML model.
When you want to deploy your model as a service without writing complex server code.
Config File - main.py
main.py
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    feature1: float
    feature2: float

@app.post("/predict")
async def predict(item: Item):
    # Simple example: sum features as prediction
    prediction = item.feature1 + item.feature2
    return {"prediction": prediction}

This Python file creates a FastAPI app that listens for POST requests at the /predict endpoint.

The Item class defines the expected input data with two features.

The predict function receives the input, calculates a simple prediction by adding the features, and returns the result as JSON.

Commands
This command starts the FastAPI server using Uvicorn. The --reload flag restarts the server automatically when you change the code, making development easier.
Terminal
uvicorn main:app --reload
Expected OutputExpected
INFO: Will watch for changes in these directories: ['.'] INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO: Started reloader process [12345] using statreload INFO: Started server process [12346] INFO: Waiting for application startup.
--reload - Automatically reload server on code changes
This command sends a POST request to the /predict endpoint with JSON data containing feature1 and feature2. It tests if the API returns the correct prediction.
Terminal
curl -X POST http://127.0.0.1:8000/predict -H "Content-Type: application/json" -d '{"feature1": 3.5, "feature2": 2.5}'
Expected OutputExpected
{"prediction":6.0}
-X POST - Specify POST request method
-H "Content-Type: application/json" - Set request content type to JSON
-d '{"feature1": 3.5, "feature2": 2.5}' - Send JSON data as request body
This command opens the automatic interactive API documentation generated by FastAPI. It helps you explore and test the API endpoints in a web browser.
Terminal
curl http://127.0.0.1:8000/docs
Expected OutputExpected
<!DOCTYPE html>... (HTML content of Swagger UI page)
Key Concept

If you remember nothing else from this pattern, remember: FastAPI lets you quickly turn Python functions into web APIs that accept JSON input and return JSON output.

Code Example
MLOps
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    feature1: float
    feature2: float

@app.post("/predict")
async def predict(item: Item):
    prediction = item.feature1 + item.feature2
    return {"prediction": prediction}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="127.0.0.1", port=8000, reload=True)
OutputSuccess
Common Mistakes
Not specifying the correct Content-Type header when sending JSON data.
The server won't parse the input correctly and may return an error or unexpected results.
Always include -H "Content-Type: application/json" when sending JSON in curl or other clients.
Forgetting to run the server with the correct module and app name in uvicorn.
Uvicorn will fail to find the app and won't start the server.
Use the format uvicorn main:app where 'main' is the Python filename and 'app' is the FastAPI instance.
Not using async def for endpoint functions in FastAPI.
FastAPI supports async for better performance; using regular def can work but misses async benefits.
Define endpoint functions with async def to enable asynchronous handling.
Summary
Create a FastAPI app with Python functions that accept and return JSON data.
Run the app using uvicorn to start a local web server.
Test the API by sending POST requests with JSON payloads and check the JSON responses.
Use the built-in /docs endpoint to explore and interact with the API easily.