Validate request and response for ML prediction
Building on your work as a data scientist at the coffee company, you now need to create a FastAPI endpoint that validates input request using CoffeeQualityInput data validation model and a QualityPrediction for response validation.
This endpoint will accept coffee data and return a quality prediction along with the confidence score.
The model is already loaded into a function called predict_quality for this exercise.
Deze oefening maakt deel uit van de cursus
Deploying AI into Production with FastAPI
Oefeninstructies
- Define
CoffeeQualityInputwith fieldsaroma(float),flavor(float), andaltitude(int). - Specify the
response_modelto validate the response within the POST request decorator. - Specify the data model to validate input request containing the
coffee_data.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
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