IniziaInizia gratis

Adding a new endpoint version

A customer of your penguin classification API has asked for an endpoint that pre-processes data from a space-delimited text string instead of a JSON dictionary. You need to add a "v2" schema and endpoint to your app to accept the new input format.

The FastAPI instance named app and the PenguinV1 class have been pre-loaded.

Questo esercizio fa parte del corso

Deploying AI into Production with FastAPI

Visualizza il corso

Istruzioni dell'esercizio

  • Add a PenguinV2 Pydantic model that accepts a parameter data as a string.
  • Add a v2 penguin classifier endpoint at /v2/penguin_classifier.
  • Use the v2 model as input for the v2 endpoint.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# Add v2 model
class ____(BaseModel):
    data: str

@app.post("/v1/penguin_classifier")
def classify_penguin_v1(penguin: PenguinV1):
    values = list(penguin.model_dump().values())
    result = classifier.predict([values])[0]
    return result

# Add v2 endpoint
@app.post("____")
# Use v2 model
def classify_penguin_v2(penguin: ____):
    values = penguin.data.split()
    result = classifier.predict([values])[0]
    return result
Modifica ed esegui il codice