0Pricing
MLOps Academy · Lekcja

Udostępnianie modelu Production

Wczytaj model z odpowiedniego etapu do usługi FastAPI

Udostępnianie modelu Production to bezpłatna lekcja MLOps Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej MLOps Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs MLOps Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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)
    yield

Create 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: float

Write 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 8000

Quick 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. ✅

Często zadawane pytania

Czy lekcja „Udostępnianie modelu Production” jest bezpłatna?

Tak — pełny tekst „Udostępnianie modelu Production” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu MLOps Academy, przejdź na CoddyKit PRO. Kurs MLOps Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Udostępnianie modelu Production”?

Wczytaj model z odpowiedniego etapu do usługi FastAPI Ćwiczysz MLOps Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć MLOps Academy?

Nie wymagamy żadnego doświadczenia. MLOps Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „Udostępnianie modelu Production”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji MLOps Academy?

Tak. Każda lekcja MLOps Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Trenowanie i rejestrowanie modelu
  2. Promowanie najlepszego modelu do Production
  3. Udostępnianie modelu Production
  4. Śledzenie pełnego cyklu predykcji
← Powrót do MLOps Academy