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.
This exercise is part of the course
Deploying AI into Production with FastAPI
Exercise instructions
- Add a
PenguinV2
Pydantic model that accepts a parameterdata
as a string. - Add a v2 penguin classifier endpoint at
/v2/penguin_classifier
. - Use the v2 model as input for the v2 endpoint.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Add v2 model
class ____(BaseModel):
data: str
@app.post("/v1/penguin_classifier")
def classify_penguin_v1(penguin: PenguinV1):
values = list(penguin.dict().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