0Pricing
MLOps Academy · Aula

Disponibilize o modelo de Production

Carregue o modelo em estágio no seu serviço FastAPI.

Disponibilize o modelo de Production é uma aula grátis de MLOps Academy no CoddyKit. Esta é a aula 3 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de MLOps Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de MLOps Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

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

Perguntas Frequentes

A aula “Disponibilize o modelo de Production” é grátis?

Sim — o texto completo de “Disponibilize o modelo de Production” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de MLOps Academy, atualize para CoddyKit PRO. O curso de MLOps Academy inclui 4 aulas no total.

O que vou aprender em “Disponibilize o modelo de Production”?

Carregue o modelo em estágio no seu serviço FastAPI. Você pratica MLOps Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar MLOps Academy?

Nenhuma experiência prévia é necessária. MLOps Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 3 de 4.

Quanto tempo leva a aula “Disponibilize o modelo de Production”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de MLOps Academy?

Sim. Cada aula de MLOps Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Treine e registre no registro
  2. Promova o melhor modelo para Production
  3. Disponibilize o modelo de Production
  4. Rastreie uma ida e volta de previsão
← Voltar para MLOps Academy