Handling different input types in FastAPI

Deploying AI into Production with FastAPI

Matt Eckerle

Software and Data Engineering Leader

Restaurant vs API

A restaurant being compared to inputs to an API

Deploying AI into Production with FastAPI

Validation flow

 

 

  • Incoming data via request
  • Input data validation happens using Pydantic
  • Process different types of data as per model requirements
  • Processed input sent to the model

A flowchart explaining request validation

Deploying AI into Production with FastAPI

Comment moderation system

 

class CommentMetrics(BaseModel):
    length: int           
    user_karma: int     
    report_count: int

class CommentText(BaseModel): content: str

An image of comment box icons

Deploying AI into Production with FastAPI

Endpoint for floating point numbers

app = FastAPI()

@app.post("/predict")
def predict_score(data: CommentMetrics):
features = np.array([ data.length, data.user_karma, data.report_count ])
model = CommentScorer() prediction = model.predict(features)
return {"prediction": round(prediction, 2), "input": data.dict()}
Deploying AI into Production with FastAPI

Endpoint for textual input

@app.post("/analyze_text")

def analyze(comment: CommentText):
forbidden = ["spam", "hate", "free" "fake", "sign up"]
text_lower = comment.lower()
issues = [word for word in forbidden if word in text_lower]
return { "issues": issues, "needs_moderation": len(issues) }

Output for comment: Sign up for free

{
    "issues": ["free", "sign up"],
    "needs_moderation": 2
}
Deploying AI into Production with FastAPI

Let's practice!

Deploying AI into Production with FastAPI

Preparing Video For Download...