Mulai sekarangMulai gratis

POST endpoint for model registration

While the GET endpoint you created earlier allows users to retrieve information about existing models, you now need a way for authorized team members to register new models or update information about existing ones.

You need to create a POST endpoint that allows team members to register new models or update existing ones. This endpoint will store model information on the server.

Latihan ini merupakan bagian dari kursus

Deploying AI into Production with FastAPI

Lihat Kursus

Instruksi latihan

  • Create a POST request endpoint at "/register-model".
  • Define the function parameter model_info to accept and validate the incoming model information using the ModelInfo Pydantic model.
  • Convert the ModelInfo object to a dictionary and store it in the model_db.

Latihan interaktif langsung praktik

Cobalah latihan ini dengan melengkapi kode contoh ini.

model_db = {}

class ModelInfo(BaseModel):
    model_id: int
    model_name: str
    description: str

# Create the POST request endpoint
@app.____("/register-model", status_code=201)
# Pass the model info from the request as function parameter 
def register_model(____: ModelInfo):
    # Add new model's information dictionary to the model database
    model_db[model_info.model_id] = model_info.____()
    
    return {"message": "Model registered successfully", "model": model_info}, 201
Edit dan Jalankan Kode