FastAPI에서 다양한 입력 유형 처리

FastAPI로 AI 프로덕션 배포하기

Matt Eckerle

Software and Data Engineering Leader

레스토랑 vs 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...