開始使用免費開始

驗證 ML 預測的請求與回應

延續你在咖啡公司的資料科學家工作,現在需要建立一個 FastAPI 端點,使用 CoffeeQualityInput 資料驗證模型來驗證輸入請求,並使用 QualityPrediction 來驗證回應。

此端點會接收咖啡資料,並回傳品質預測以及信心水準分數。

本練習中,模型已載入為名為 predict_quality 的函式。

本練習屬於課程

使用 FastAPI 將 AI 佈署到生產環境

檢視課程

練習說明

  • 定義 CoffeeQualityInput,其欄位包含 aromafloat)、flavorfloat),以及 altitudeint)。
  • 在 POST 請求的裝飾器中指定 response_model,用來驗證回應。
  • 指定資料模型來驗證包含 coffee_data 的輸入請求。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

class CoffeeQualityInput(BaseModel):
    ____: ____
    ____: ____
    ____: ____
    
class QualityPrediction(BaseModel):
    quality_score: float 
    confidence: float

# Specify the data model to validate response
@app.post("/predict", response_model=____) 
# Specify the data model to validate input request
def predict(coffee_data: ____):
    prediction = predict_quality(coffee_data)
    return prediction 
編輯並執行程式碼