编写自定义预测器
接入您自己的预处理和模型代码
编写自定义预测器 是 CoddyKit 上的免费 MLOps Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 MLOps Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 MLOps Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
When Built-ins Fall Short
The built-in runtimes cover sklearn, PyTorch, and friends. But when your model needs special logic, you write your own. A custom predictor gives you full control. 🛠️
It Is Just a Container
A custom predictor is your own container image that speaks the KServe protocol. You package your code, model, and dependencies into one image and hand it to KServe.
The KServe Python SDK
The kserve Python package gives you a base class to extend. You subclass kserve.Model and fill in how your model loads and predicts.
import kserve
class MyModel(kserve.Model):
def __init__(self, name):
super().__init__(name)
self.ready = FalseImplement load()
The load method reads your model from disk into memory once at startup. When it finishes, you set ready to True so KServe knows it can serve.
def load(self):
self.model = joblib.load("/mnt/models/model.joblib")
self.ready = TrueImplement predict()
The predict method takes the request payload, runs your model, and returns the result as a dict. This is where your real inference logic lives.
def predict(self, payload, headers=None):
rows = payload["instances"]
preds = self.model.predict(rows)
return {"predictions": preds.tolist()}Custom Preprocessing Fits Here
Need to reshape inputs or apply business rules before inference? Put that logic right inside predict or in a preprocess step. The runtime never had to know about it.
Start the Model Server
You wire your class into a ModelServer and start it. KServe's server handles HTTP, health, and the protocol so you only write model code. ModelServer runs the loop.
if __name__ == "__main__":
model = MyModel("custom-model")
model.load()
kserve.ModelServer().start([model])Package It in a Dockerfile
You build a container that installs kserve, copies your code, and runs the server. This Dockerfile produces the image KServe will launch.
FROM python:3.11-slim
RUN pip install kserve joblib scikit-learn
COPY model.py /app/model.py
CMD ["python", "/app/model.py"]Point the Spec at Your Image
In the InferenceService, the predictor uses a plain containers block instead of a model format. You name your image and KServe runs it.
spec:
predictor:
containers:
- name: kserve-container
image: my-registry/custom-model:latestMounting Your Model
You can still set a storageUri, and KServe mounts the downloaded model at a known path your code reads. The container and the storageUri work together.
Same Protocol, Your Logic
Because you implement the standard endpoints, clients call your custom predictor exactly like a built-in one. The protocol stays identical, only the internals are yours.
Quick Check
You subclass kserve.Model for a custom predictor. Which method loads the model into memory?
Recap
You built a custom predictor by subclassing kserve.Model, filling in load and predict, and shipping it as a container. Same protocol, your own logic. Awesome work! 🎉
常见问题解答
「编写自定义预测器」课时是免费的吗?
是的 — 「编写自定义预测器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 MLOps Academy 课程的其余内容,请升级到 CoddyKit PRO。 MLOps Academy 课程共包含 4 节课。
「编写自定义预测器」这节课中我会学到什么?
接入您自己的预处理和模型代码 你通过在浏览器中直接运行的动手代码来练习 MLOps Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 MLOps Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 MLOps Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「编写自定义预测器」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 MLOps Academy 课中编写并运行代码吗?
能。每节 MLOps Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。