开始使用免费开始使用

用于模型信息的 GET 端点

您隶属于一个机器学习团队,已经开发了多个模型,分别用于情感分析、商品分类、客户流失预测等不同任务。您正在部署这些模型,需要创建一个端点来提供每个模型的基本信息。

您的任务是在路由 /model-info/{model_id} 上实现一个 GET 端点,用于检索并返回这些核心的模型信息。

本练习是课程的一部分

使用 FastAPI 在生产环境中部署 AI

查看课程

练习说明

  • "/model-info/{model_id}" 创建一个 GET 端点,返回指定模型的信息。
  • 该端点应接受一个作为路径参数的 model_id
  • model_id0 时,抛出 HTTPException,并返回状态码 404,表示未找到该模型。

交互式实操练习

通过完成这段示例代码来试试这个练习。

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}
编辑并运行代码