เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

ตรวจสอบ request และ response สำหรับการพยากรณ์ด้วย ML

ต่อจากงานในฐานะนักวิทยาศาสตร์ข้อมูลของบริษัทกาแฟ คราวนี้ต้องสร้าง FastAPI endpoint ที่ตรวจสอบความถูกต้องของ input ด้วยโมเดล CoffeeQualityInput และตรวจสอบ response ด้วย QualityPrediction

Endpoint นี้จะรับข้อมูลกาแฟและส่งคืนผลการพยากรณ์คุณภาพพร้อมคะแนนความเชื่อมั่น

โมเดลถูกโหลดไว้ในฟังก์ชัน predict_quality แล้วสำหรับแบบฝึกหัดนี้

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

การ Deploy AI สู่ Production ด้วย FastAPI

ดูคอร์ส

คำแนะนำการฝึกหัด

  • กำหนด CoffeeQualityInput ให้มีฟิลด์ aroma (float), flavor (float), และ altitude (int)
  • ระบุ response_model เพื่อตรวจสอบความถูกต้องของ response ภายใน POST request decorator
  • ระบุ data model เพื่อตรวจสอบความถูกต้องของข้อมูล input ใน 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 
แก้ไขและรันโค้ด