Önceden eğitilmiş bir modeli yükleme

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

Matt Eckerle

Software and Data Engineering Leader

Mevcut yapı

FastAPI ile ML tahmin akış şeması

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

Model yüklemedeki zorluk

İstek/yanıt yaşam döngüsünü açıklayan diyagram

API yanıtını bekleyen mutsuz adam

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

Modelleri istekten önce yükle

FastAPI ile ML tahmin akış şeması

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

Modeli yükleme

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

FastAPI lifespan olayı

FastAPI lifespan akışını açıklayan diyagram

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

FastAPI lifespan olayı

from contextlib import asynccontextmanager
@asynccontextmanager
async def lifespan(app: FastAPI):
    # Startup: Load the ML model
    load_model()
    yield
app = FastAPI(lifespan=lifespan)
FastAPI ile Üretime AI Dağıtımı

Sağlık kontrolleri

 

@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 command:

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

Output:

{
  "status": "healthy",
  "model_loaded": true
}
FastAPI ile Üretime AI Dağıtımı

Hadi pratik yapalım!

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

Preparing Video For Download...