FastAPI'de farklı girdi türlerini işleme

FastAPI ile Üretime AI Dağıtımı

Matt Eckerle

Software and Data Engineering Leader

Restoran vs API

Bir restoranın bir API'ye gelen girdilerle karşılaştırılması

FastAPI ile Üretime AI Dağıtımı

Doğrulama akışı

 

 

  • İstekle gelen veriler
  • Girdi doğrulaması Pydantic ile yapılır
  • Modele göre farklı veri türlerini işle
  • İşlenen girdi modele gönderilir

İstek doğrulamasını açıklayan bir akış şeması

FastAPI ile Üretime AI Dağıtımı

Yorum moderasyon sistemi

 

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

class CommentText(BaseModel): content: str

Yorum kutusu simgelerinin bir görüntüsü

FastAPI ile Üretime AI Dağıtımı

Ondalıklı sayılar için endpoint

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 ile Üretime AI Dağıtımı

Metinsel girdi için endpoint

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

Yorum için çıktı: Sign up for free

{
    "issues": ["free", "sign up"],
    "needs_moderation": 2
}
FastAPI ile Üretime AI Dağıtımı

Hadi pratik yapalım!

FastAPI ile Üretime AI Dağıtımı

Preparing Video For Download...