ปลายทาง /predict แรกของคุณ
โหลดโมเดลและส่งคืนผลพยากรณ์ผ่าน HTTP
ปลายทาง /predict แรกของคุณ เป็นบทเรียน MLOps Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน MLOps Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส MLOps Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
From Notebook to API
A model in a notebook helps only you. To serve real users, you wrap it in an API that anyone can call over the network. 🚀
Why FastAPI
FastAPI is a modern Python web framework that is fast to write, validates input for you, and auto-generates interactive docs.
Install It
You need two pieces: FastAPI itself and a server to run it. Install both with pip in one line.
pip install fastapi uvicornCreate the App
Everything starts from one app object. You create a FastAPI instance and attach routes to it.
from fastapi import FastAPI
app = FastAPI()What an Endpoint Is
An endpoint is a URL your app responds to, like /predict. Each one runs a Python function when someone calls it.
Load Your Model First
Before predicting, you load the trained model from disk. Here joblib reads a saved scikit-learn model into memory.
import joblib
model = joblib.load("model.joblib")Declare the Route
You mark a function as a POST endpoint with a decorator. POST fits prediction because you send data in the request body.
@app.post("/predict")
def predict(features: list[float]):
...Run the Prediction
Inside the function you call the model. predict expects a 2D input, so you wrap the row in a list before passing it.
pred = model.predict([features])
result = pred[0]Return JSON
Whatever your function returns, FastAPI serializes to JSON automatically. Return a plain dict and the client gets clean JSON.
return {"prediction": result}Start the Server
You launch the app with uvicorn, pointing it at the module and the app object. The reload flag restarts on code changes.
uvicorn main:app --reloadTry the Auto Docs
Visit /docs in your browser and FastAPI shows an interactive page where you can test /predict without writing any client code. 🎉
Quick Check
You want clients to send feature data to your model. Which HTTP method fits a /predict endpoint?
Recap
You wrapped a model in FastAPI: create the app, load the model, expose a POST /predict that returns JSON, and run it with uvicorn. That is a live API! 🙌
คำถามที่พบบ่อย
บทเรียน “ปลายทาง /predict แรกของคุณ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ปลายทาง /predict แรกของคุณ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส MLOps Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส MLOps Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ปลายทาง /predict แรกของคุณ”
โหลดโมเดลและส่งคืนผลพยากรณ์ผ่าน HTTP คุณปฏิบัติ MLOps Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน MLOps Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน MLOps Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “ปลายทาง /predict แรกของคุณ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน MLOps Academy นี้ได้ไหม
ได้ บทเรียน MLOps Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ปลายทาง /predict แรกของคุณ
- ตรวจสอบคำขอด้วย Pydantic
- โหลดโมเดลเพียงครั้งเดียวตอนเริ่มต้น
- เพิ่มการตรวจสอบความพร้อมใช้งานที่ /health