Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Deploying AI into Production with FastAPI

Cursus bekijken

Oefeninstructies

  • Create a GET endpoint at "/model-info/{model_id}" that returns information about a specific model.
  • The endpoint should accept a model_id as a path parameter.
  • Raise an HTTPException with a 404 status code indicating that the model was not found if the model_id is 0.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

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