Đóng gói model và xây dựng API
Nội dung đang được chuẩn bị.
Lưu và load model
Serialize model ra file (pickle, joblib, ONNX, safetensors) và load lại đúng cách khi phục vụ.
Thiết kế input/output schema
Định nghĩa rõ schema bằng Pydantic để bắt lỗi sớm và tự động sinh API documentation.
FastAPI cho ML model
Tạo endpoint dự đoán, xử lý request/response, validation và async.
from fastapi import FastAPIfrom pydantic import BaseModel
app = FastAPI()
class PredictRequest(BaseModel): text: str
@app.post("/predict")async def predict(req: PredictRequest): result = model.predict(req.text) return {"prediction": result}Error handling
Xử lý lỗi đúng cách: input không hợp lệ, model lỗi, timeout — trả về HTTP status code phù hợp.
Docker cơ bản
Đóng gói API + dependencies vào Docker image để chạy giống nhau ở mọi môi trường.
Deploy API đơn giản
Deploy bản demo lên cloud (Render, Railway, hoặc VPS đơn giản).
sơ đồ: từ model tới service
Mục tiêu sau module
Sau module này bạn có thể đóng gói model thành API, chạy local bằng Docker, và deploy bản demo.