Get startedGet started for free

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.

This exercise is part of the course

Deploying AI into Production with FastAPI

View Course

Exercise instructions

  • 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.
  • Check if model_id is 0.
  • Raise an HTTPException with a 404 status code indicating that the model was not found if the model_id is 0.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

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):
    # Check if the passed model id is 0
    if model_id == ____:
      	# Raise the right status code for not found
        raise HTTPException(status_code=____, detail="Model not found")
    model_info = get_model_details(id)  
    # Return the model id and info in the dict
    return {"model_id": ____, "model_name": model_info}
Edit and Run Code