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.
Bu egzersiz
Deploying AI into Production with FastAPI
kursunun bir parçasıdırEgzersiz talimatları
- Add a
PenguinV2Pydantic model that accepts a parameterdataas a string. - Add a v2 penguin classifier endpoint at
/v2/penguin_classifier. - Use the v2 model as input for the v2 endpoint.
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
# 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