Get startedGet started for free

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.

This exercise is part of the course

Deploying AI into Production with FastAPI

View Course

Exercise instructions

  • Define CoffeeQualityInput with fields aroma (float), flavor (float), and altitude (int).
  • Specify the response_model to validate the response within the POST request decorator.
  • Specify the data model to validate input request containing the coffee_data.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

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 
Edit and Run Code