Il primo endpoint /predict
Carichi un modello e restituisca una previsione tramite HTTP.
Il primo endpoint /predict è una lezione MLOps Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento MLOps Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso MLOps Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
From Notebook to API
A model in a notebook helps only you. To serve real users, you wrap it in an API that anyone can call over the network. 🚀
Why FastAPI
FastAPI is a modern Python web framework that is fast to write, validates input for you, and auto-generates interactive docs.
Install It
You need two pieces: FastAPI itself and a server to run it. Install both with pip in one line.
pip install fastapi uvicornCreate the App
Everything starts from one app object. You create a FastAPI instance and attach routes to it.
from fastapi import FastAPI
app = FastAPI()What an Endpoint Is
An endpoint is a URL your app responds to, like /predict. Each one runs a Python function when someone calls it.
Load Your Model First
Before predicting, you load the trained model from disk. Here joblib reads a saved scikit-learn model into memory.
import joblib
model = joblib.load("model.joblib")Declare the Route
You mark a function as a POST endpoint with a decorator. POST fits prediction because you send data in the request body.
@app.post("/predict")
def predict(features: list[float]):
...Run the Prediction
Inside the function you call the model. predict expects a 2D input, so you wrap the row in a list before passing it.
pred = model.predict([features])
result = pred[0]Return JSON
Whatever your function returns, FastAPI serializes to JSON automatically. Return a plain dict and the client gets clean JSON.
return {"prediction": result}Start the Server
You launch the app with uvicorn, pointing it at the module and the app object. The reload flag restarts on code changes.
uvicorn main:app --reloadTry the Auto Docs
Visit /docs in your browser and FastAPI shows an interactive page where you can test /predict without writing any client code. 🎉
Quick Check
You want clients to send feature data to your model. Which HTTP method fits a /predict endpoint?
Recap
You wrapped a model in FastAPI: create the app, load the model, expose a POST /predict that returns JSON, and run it with uvicorn. That is a live API! 🙌
Domande Frequenti
La lezione «Il primo endpoint /predict» è gratuita?
Sì — il testo completo di «Il primo endpoint /predict» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso MLOps Academy, passa a CoddyKit PRO. Il corso MLOps Academy include 4 lezioni in totale.
Cosa imparerò in «Il primo endpoint /predict»?
Carichi un modello e restituisca una previsione tramite HTTP. Eserciti MLOps Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare MLOps Academy?
Non è richiesta alcuna esperienza precedente. MLOps Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.
Quanto tempo richiede la lezione «Il primo endpoint /predict»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione MLOps Academy?
Sì. Ogni lezione MLOps Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Il primo endpoint /predict
- Convalidare le richieste con Pydantic
- Caricare il modello una sola volta all'avvio
- Aggiungere un controllo di readiness /health