الفصل 1: واجهات REST API لتعلم الآلة
FastAPI لخدمة تعلم الآلة
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
import mlflow
import numpy as np
app = FastAPI(title="ML Model API", version="1.0.0")
# Load model at startup
model = None
@app.on_event("startup")
async def load_model():
global model
model = mlflow.sklearn.load_model("models:/churn-model/Production")
class PredictionRequest(BaseModel):
features: list[float] = Field(..., min_length=10, max_length=10)
customer_id: str
class PredictionResponse(BaseModel):
customer_id: str
prediction: int
probability: float
model_version: str
@app.post("/predict", response_model=PredictionResponse)
async def predict(request: PredictionRequest):
try:
features = np.array(request.features).reshape(1, -1)
prediction = model.predict(features)[0]
probability = model.predict_proba(features)[0][1]
return PredictionResponse(
customer_id=request.customer_id,
prediction=int(prediction),
probability=float(probability),
model_version="2.1.0"
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/health")
async def health():
return {"status": "healthy", "model_loaded": model is not None}
النقاط الرئيسية
ملخص
- استخدام Pydantic للتحقق من صحة الطلب/الاستجابة
- تحميل النماذج عند بدء التشغيل وليس لكل طلب
- تنفيذ فحوصات الصحة للتنسيق
- استخدام async لتحسين الإنتاجية
التالي: الفصل 2: أطر خدمة النماذج