시작하기무료로 시작하기

비동기 감성 분석 엔드포인트 만들기

소셜 미디어 분석 플랫폼을 개발하고 있으며, 리뷰의 감성을 분석해야 합니다. 높은 트래픽을 효율적으로 처리하려면 async 엔드포인트를 구현해야 합니다. 감성 분석 모델은 이미 로드되어 sentiment_model로 사용할 수 있습니다.

이 연습은 강의의 일부입니다

FastAPI로 AI 프로덕션 배포하기

강의 보기

연습 안내

  • FastAPI 앱에서 비동기 POST 엔드포인트 /analyze를 생성하세요.
  • 다른 작업을 차단하지 않고 sentiment_model을 비동기적으로 호출하는 키워드를 추가하세요.
  • 이벤트 루프를 차단하지 않도록 리뷰 텍스트를 전달하여 sentiment_model을 별도의 스레드에서 실행하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Review(BaseModel):
    text: str

# Create async endpoint at /analyze route
@app.post("____")
# Write an asynchronous function to process review's text
____ def analyze_review(review: Review):
    # Run the model in a separate thread to avoid any event loop blockage
    result = ____ asyncio.____(sentiment_model, ____)
    return {"sentiment": result[0]["label"]}
코드 편집 및 실행