0Pricing
MLOps Academy · บทเรียน

ให้บริการโมเดล Production

โหลดโมเดลที่อยู่ในระยะพร้อมใช้งานเข้าสู่บริการ FastAPI

ให้บริการโมเดล Production เป็นบทเรียน MLOps Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน MLOps Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส MLOps Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

From Registry to API

The promoted model is sitting in the registry. Now you load it into a FastAPI service so real requests can get predictions. 🌐

Reference It by Alias

Build a model URI that points at the champion alias. Your code never hardcodes a version, so promotions just work.

MODEL_URI = "models:/churn-classifier@champion"

Load the Model

Use mlflow.pyfunc.load_model to pull the production model down into memory as a callable Python object.

import mlflow.pyfunc

model = mlflow.pyfunc.load_model(MODEL_URI)

Load Once at Startup

Loading is slow, so do it once when the app boots, not per request. Use a FastAPI lifespan handler to load before traffic arrives.

from contextlib import asynccontextmanager

@asynccontextmanager
async def lifespan(app):
    app.state.model = mlflow.pyfunc.load_model(MODEL_URI)
    yield

Create the App

Wire the lifespan into your FastAPI instance. From here on, the loaded model lives on app.state and is shared by every request.

from fastapi import FastAPI

app = FastAPI(lifespan=lifespan)

Validate the Input

Define a Pydantic model for the request body so malformed input is rejected before it ever reaches the model.

from pydantic import BaseModel

class Features(BaseModel):
    tenure: int
    monthly_charges: float

Write the Predict Endpoint

Turn the validated input into a row and call the model. The predict method returns the prediction you send back as JSON.

@app.post("/predict")
def predict(f: Features):
    pred = app.state.model.predict([[f.tenure, f.monthly_charges]])
    return {"prediction": int(pred[0])}

Add a Health Check

A tiny /health route lets orchestrators know the service is up and the model finished loading before they route traffic.

@app.get("/health")
def health():
    return {"status": "ok"}

Run the Server

Launch with uvicorn and your model is live on HTTP. Any client that can POST JSON can now get predictions.

uvicorn serve:app --host 0.0.0.0 --port 8000

Quick Check

Why load the model in a lifespan handler instead of inside the predict function?

Updating the Live Model

To ship a new model, re-point the champion alias and restart the service. It loads the new version at startup automatically.

Same Model Everywhere

Because the service pulls from the registry, dev, staging, and prod can all load the exact same artifact by alias. No copy-paste of weights.

Recap: Serving the Production Model

You loaded the champion at startup, validated input, exposed /predict and /health, and ran it with uvicorn. Your model is now a live service. ✅

คำถามที่พบบ่อย

บทเรียน “ให้บริการโมเดล Production” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ให้บริการโมเดล Production” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส MLOps Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส MLOps Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ให้บริการโมเดล Production”

โหลดโมเดลที่อยู่ในระยะพร้อมใช้งานเข้าสู่บริการ FastAPI คุณปฏิบัติ MLOps Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน MLOps Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน MLOps Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “ให้บริการโมเดล Production” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน MLOps Academy นี้ได้ไหม

ได้ บทเรียน MLOps Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ฝึกโมเดลและบันทึกเข้ารีจิสทรี
  2. เลื่อนโมเดลที่ดีที่สุดขึ้น Production
  3. ให้บริการโมเดล Production
  4. ติดตามการเดินทางไปกลับของผลพยากรณ์
← กลับไปที่ MLOps Academy