0Pricing
MLOps Academy · 课时

您的第一个 /predict 端点

加载模型,并通过 HTTP 返回预测结果

您的第一个 /predict 端点 是 CoddyKit 上的免费 MLOps Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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 uvicorn

Create 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 --reload

Try 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 导师)并解锁 MLOps Academy 课程的其余内容,请升级到 CoddyKit PRO。 MLOps Academy 课程共包含 4 节课。

「您的第一个 /predict 端点」这节课中我会学到什么?

加载模型,并通过 HTTP 返回预测结果 你通过在浏览器中直接运行的动手代码来练习 MLOps Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 MLOps Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 MLOps Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「您的第一个 /predict 端点」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 MLOps Academy 课中编写并运行代码吗?

能。每节 MLOps Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 您的第一个 /predict 端点
  2. 使用 Pydantic 验证请求
  3. 在启动时只加载一次模型
  4. 添加 /health 就绪检查
← 返回 MLOps Academy