Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Deploying AI into Production with FastAPI

Cursus bekijken

Oefeninstructies

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

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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
Code bewerken en uitvoeren