MulaiMulai sekarang secara gratis

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.

Latihan ini adalah bagian dari kursus

Deploying AI into Production with FastAPI

Lihat Kursus

Petunjuk latihan

  • 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.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

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 dan Jalankan Kode