स्ट्रक्चर्ड prediction रेस्पॉन्स लौटाना

FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना

Matt Eckerle

Software and Data Engineering Leader

मॉडलों को डिप्लॉय करने की चुनौतियाँ

FastAPI के साथ ML prediction दिखाता फ्लोचार्ट

 

 

  1. इनपुट डेटा ठीक से स्वीकार करें
  2. आने वाले डेटा को वैलिडेट करें और errors हैंडल करें
  3. प्रेडिक्शन करें
  4. अच्छा-संरचित रेस्पॉन्स लौटाएँ
FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना

रिक्वेस्ट स्ट्रक्चर परिभाषित करना

from pydantic import BaseModel
class PredictionRequest(BaseModel):
    text: str
class PredictionResponse(BaseModel):

text: str
sentiment: str
confidence: float
FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना

प्रेडिक्शन endpoint बनाना

@app.post("/predict")
def predict_sentiment(request: PredictionRequest):
    if sentiment_model is None:
        raise HTTPException(
            status_code=503,
            detail="Model not loaded"
        )

result = sentiment_model(request.text)
return PredictionResponse( text=request.text, sentiment=result[0]["label"], confidence=result[0]["score"] )

इनपुट JSON:

{"text": "This movie was fantastic!"}

 

रेस्पॉन्स:

{
    "text": "This movie was fantastic!",
    "sentiment": "POSITIVE",
    "confidence": 0.95
}
FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना

एरर हैंडलिंग

try:
  result = sentiment_model(request.text)
  return PredictionResponse(
        text=request.text,
        sentiment=result[0]["label"],
        confidence=result[0]["score"]
    )
except Exception:
  raise HTTPException(
        status_code=500,
        detail="Prediction failed"
    )

जब मॉडल प्रेडिक्ट नहीं कर पाए तो रेस्पॉन्स

{
    "detail": "Prediction failed",
    "status_code": 500
}
FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना

एंडपॉइंट का परीक्षण

# Example request
import requests

response = requests.post(
    "http://localhost:8000/predict",
    json={"text": "Great product!"}
)
print(response.json())
{
    "text": "Great product!",
    "sentiment": "POSITIVE",
    "confidence": 0.998
}
FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना

अभ्यास करते हैं!

FastAPI के साथ प्रोडक्शन में AI डिप्लॉय करना

Preparing Video For Download...