FastAPIにおける入力タイプの処理

FastAPI を使った AI のプロダクション環境へのデプロイ

Matt Eckerle

Software and Data Engineering Leader

レストランとAPIの比較

レストランとAPIへの入力の比較

FastAPI を使った AI のプロダクション環境へのデプロイ

バリデーションフロー

 

 

  • リクエスト経由でデータを受信
  • Pydanticによる入力データの検証
  • モデルの要件に応じた各データタイプの処理
  • 処理済み入力をモデルへ送信

リクエスト検証のフローチャート

FastAPI を使った AI のプロダクション環境へのデプロイ

コメントモデレーションシステム

 

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

class CommentText(BaseModel): content: str

コメントボックスアイコンの画像

FastAPI を使った AI のプロダクション環境へのデプロイ

浮動小数点数用エンドポイント

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()}
FastAPI を使った AI のプロダクション環境へのデプロイ

テキスト入力用エンドポイント

@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) }

コメントの出力: Sign up for free

{
    "issues": ["free", "sign up"],
    "needs_moderation": 2
}
FastAPI を使った AI のプロダクション環境へのデプロイ

練習しましょう!

FastAPI を使った AI のプロダクション環境へのデプロイ

Preparing Video For Download...