FastAPI prediction with a pre-trained model

Deploying AI into Production with FastAPI

Matt Eckerle

Software and Data Engineering Leader

Setting up the environment

Required libraries:  

  • FastAPI: framework for building APIs with Python
  • uvicorn: fast ASGI server that runs Python web apps
  • joblib: for loading the model

 

from fastapi import FastAPI
import uvicorn
import joblib
# Create the FastAPI app instance
app = FastAPI()
Deploying AI into Production with FastAPI

Loading the pre-trained penguin classifier

  • Trained on the Palmer Penguins dataset
  • Predicts penguin species based on 4 features: culmen length, culmen depth, flipper length, and body mass
  • Output: Adelie, Chinstrap, or Gentoo
import joblib

# Load the pre-trained model
model = joblib.load('penguin_classifier.pkl')
# Check data type of model to verify model loading
print(type(model))
<class 'sklearn.pipeline.Pipeline'>
1 https://huggingface.co/SIH/penguin-classifier-sklearn
Deploying AI into Production with FastAPI

Uvicorn

  • ASGI (Asynchronous Server Gateway Interface) server
  • Built by and for Python
uvicorn main:app \
        --host 0.0.0.0 \
        --port 8080
import uvicorn
uvicorn.run(app, 
            host="0.0.0.0", 
            port=8080)

Uvicorn logo

Deploying AI into Production with FastAPI

Creating the prediction endpoint

# FastAPI prediction endpoint
@app.post("/predict")
def predict(culmen_length_mm, culmen_depth_mm, 
            flipper_length_mm, body_mass_g):

    features = [[culmen_length_mm, culmen_depth_mm,
                 flipper_length_mm, body_mass_g]]

    prediction = model.predict(features)[0]
    return {"predicted_species": prediction}
Deploying AI into Production with FastAPI

Running the application

if __name__ == "__main__":
    uvicorn.run(
      app, 
      host="0.0.0.0", 
      port=8080)

Save all the code in a python file - your_api_script.py

$ python3 your_api_script.py

Uvicorn startup logs

Deploying AI into Production with FastAPI

Testing the API

curl \

-X POST "http://localhost:8080/predict" \
-H "Content-Type: application/json" \
-d '{"culmen_length_mm": 39.1, "culmen_depth_mm": 18.7, "flipper_length_mm": 181, "body_mass_g": 3750}'
{
    "prediction": "Adelie",
    "confidence": 0.87
}
Deploying AI into Production with FastAPI

Let's practice!

Deploying AI into Production with FastAPI

Preparing Video For Download...