Productionモデルを提供する
ステージ済みのモデルをFastAPIサービスに読み込みます。
「Productionモデルを提供する」はCoddyKit上の無料MLOps Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMLOps Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 MLOps Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
From Registry to API
The promoted model is sitting in the registry. Now you load it into a FastAPI service so real requests can get predictions. 🌐
Reference It by Alias
Build a model URI that points at the champion alias. Your code never hardcodes a version, so promotions just work.
MODEL_URI = "models:/churn-classifier@champion"Load the Model
Use mlflow.pyfunc.load_model to pull the production model down into memory as a callable Python object.
import mlflow.pyfunc
model = mlflow.pyfunc.load_model(MODEL_URI)Load Once at Startup
Loading is slow, so do it once when the app boots, not per request. Use a FastAPI lifespan handler to load before traffic arrives.
from contextlib import asynccontextmanager
@asynccontextmanager
async def lifespan(app):
app.state.model = mlflow.pyfunc.load_model(MODEL_URI)
yieldCreate the App
Wire the lifespan into your FastAPI instance. From here on, the loaded model lives on app.state and is shared by every request.
from fastapi import FastAPI
app = FastAPI(lifespan=lifespan)Validate the Input
Define a Pydantic model for the request body so malformed input is rejected before it ever reaches the model.
from pydantic import BaseModel
class Features(BaseModel):
tenure: int
monthly_charges: floatWrite the Predict Endpoint
Turn the validated input into a row and call the model. The predict method returns the prediction you send back as JSON.
@app.post("/predict")
def predict(f: Features):
pred = app.state.model.predict([[f.tenure, f.monthly_charges]])
return {"prediction": int(pred[0])}Add a Health Check
A tiny /health route lets orchestrators know the service is up and the model finished loading before they route traffic.
@app.get("/health")
def health():
return {"status": "ok"}Run the Server
Launch with uvicorn and your model is live on HTTP. Any client that can POST JSON can now get predictions.
uvicorn serve:app --host 0.0.0.0 --port 8000Quick Check
Why load the model in a lifespan handler instead of inside the predict function?
Updating the Live Model
To ship a new model, re-point the champion alias and restart the service. It loads the new version at startup automatically.
Same Model Everywhere
Because the service pulls from the registry, dev, staging, and prod can all load the exact same artifact by alias. No copy-paste of weights.
Recap: Serving the Production Model
You loaded the champion at startup, validated input, exposed /predict and /health, and ran it with uvicorn. Your model is now a live service. ✅
よくある質問
「Productionモデルを提供する」レッスンは無料ですか?
はい。「Productionモデルを提供する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、MLOps Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MLOps Academyコースには全4レッスンが含まれています。
「Productionモデルを提供する」で何を学びますか?
ステージ済みのモデルをFastAPIサービスに読み込みます。 ブラウザで直接実行するハンズオンコードでMLOps Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
MLOps Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMLOps Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「Productionモデルを提供する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMLOps Academyレッスンでコードを書いて実行できますか?
はい。すべてのMLOps Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 学習してレジストリに記録する
- 最良のモデルをProductionへ昇格させる
- Productionモデルを提供する
- 予測のラウンドトリップを追跡する