كتابة متنبئ مخصص
أضف شيفرة المعالجة المسبقة والنموذج الخاصة بك.
كتابة متنبئ مخصص درس مجاني في MLOps Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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! 🎉
الأسئلة الشائعة
هل درس «كتابة متنبئ مخصص» مجاني؟
نعم — نص درس «كتابة متنبئ مخصص» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة MLOps Academy، انتقل إلى CoddyKit PRO. تتضمن دورة MLOps Academy 4 دروس في المجموع.
ماذا ستتعلم في «كتابة متنبئ مخصص»؟
أضف شيفرة المعالجة المسبقة والنموذج الخاصة بك. تتمرن على MLOps Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ MLOps Academy؟
لا تُشترط خبرة سابقة. MLOps Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «كتابة متنبئ مخصص»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس MLOps Academy هذا؟
نعم. كل درس في MLOps Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.