사전 학습된 모델 로드

FastAPI로 AI 프로덕션 배포하기

Matt Eckerle

Software and Data Engineering Leader

현재 구조

FastAPI를 사용한 ML 예측 흐름도

FastAPI로 AI 프로덕션 배포하기

모델 로드의 문제점

요청/응답 생명주기를 설명하는 다이어그램

API 응답을 기다리는 불만스러운 남성

FastAPI로 AI 프로덕션 배포하기

요청 전 모델 로드

FastAPI를 사용한 ML 예측 흐름도

FastAPI로 AI 프로덕션 배포하기

모델 로드

from fastapi import FastAPI
sentiment_model = None
def load_model():
    global sentiment_model
    sentiment_model = SentimentAnalyzer("trained_model.joblib")
    print("Model loaded successfully")
load_model()
Model loaded successfully
FastAPI로 AI 프로덕션 배포하기

FastAPI 라이프스팬 이벤트

FastAPI 라이프스팬을 설명하는 흐름도

FastAPI로 AI 프로덕션 배포하기

FastAPI 라이프스팬 이벤트

from contextlib import asynccontextmanager
@asynccontextmanager
async def lifespan(app: FastAPI):
    # Startup: Load the ML model
    load_model()
    yield
app = FastAPI(lifespan=lifespan)
FastAPI로 AI 프로덕션 배포하기

헬스 체크

 

@app.get("/health")
def health_check():
    if sentiment_model is not None:
        return {"status": "healthy", 
                "model_loaded": True}
    return {"status": "unhealthy", 
            "model_loaded": False}

Curl 명령어:

curl -X GET \
  "http://localhost:8080/health" \
  -H "accept: application/json"

출력:

{
  "status": "healthy",
  "model_loaded": true
}
FastAPI로 AI 프로덕션 배포하기

연습해 봅시다!

FastAPI로 AI 프로덕션 배포하기

Preparing Video For Download...