GET endpoint for model information
You're part of a machine learning team that has developed several machine learning models, each designed for different tasks such as sentiment analysis, product categorization, and customer churn prediction. You're working on deploying these models, and you need to create an endpoint that provides basic information about each model.
Your task is to implement a GET endpoint at route /model-info/{model_id} that retrieves and returns this essential model information.
Questo esercizio fa parte del corso
Deploying AI into Production with FastAPI
Istruzioni dell'esercizio
- Create a
GETendpoint at"/model-info/{model_id}"that returns information about a specific model. - The endpoint should accept a
model_idas a path parameter. - Raise an
HTTPExceptionwith a404status code indicating that the model was not found if themodel_idis0.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
from fastapi import FastAPI, HTTPException
app = FastAPI()
# Add model_id as a path parameter in the route
@app.get("/model-info/{____}")
# Pass on the model id as an argument
async def get_model_info(____: int):
if model_id == 0:
# Raise the right status code for not found
raise HTTPException(status_code=____, detail="Model not found")
model_info = get_model_details(id)
return {"model_id": model_id, "model_name": model_info}