用于机器学习预测的请求正文中 JSON 的 FastAPI 数组

FastAPI array of JSON in Request Body for Machine Learning prediction

我正在使用 FastAPI 在机器学习中进行模型推理,因此我需要将 JSON 的数组作为输入,如下所示:

[
  {
    "Id":"value",
    "feature1":"value",
    "feature2":"value",
    "feature3":"value"
  },
  {
    "Id":"value",
    "feature1":"value",
    "feature2":"value",
    "feature3":"value"
  },
  {
    "Id":"value",
    "feature1":"value",
    "feature2":"value",
    "feature3":"value"
  }
]

输出(预测结果)应如下所示:

[
  {
    "Id":"value",
    "prediction":"value"
  },
  {
    "Id":"value",
    "prediction":"value"
  },
  {
    "Id":"value",
    "prediction":"value"
  }
]

如何在 Python 中使用 FastAPI 实现?

您可以使用 Pydantic 模型(假设 Item)声明请求 JSON 正文,如 here, and use List[Item] to accept a JSON array (a Python List), as documented here. In a similar way, you can define a Response model 所述。下面的示例:

from pydantic import BaseModel
from typing import List

class ItemIn(BaseModel):
    Id: str
    feature1: str
    feature2: str
    feature3: str

class ItemOut(BaseModel):
    Id: str
    prediction: str
    
@app.post('/predict', response_model=List[ItemOut])
def predict(items: List[ItemIn]):
    return [{"Id":  "value", "prediction": "value"}, {"Id":  "value", "prediction": "value"}]

更新

您可以将数据发送到 predict() 函数,如 this answer 中所述。下面的示例:

@app.post('/predict', response_model=List[ItemOut])
def predict(items: List[ItemIn]):
    for item in items:
        pred = model.predict([[item.feature1, item.feature2, item.feature3]])[0] 

或者,如 (选项 3)所述,使用以下内容以避免循环遍历项目并多次调用 predict() 函数:

import pandas as pd

@app.post('/predict', response_model=List[ItemOut])
def predict(items: List[ItemIn]):
    df = pd.DataFrame([i.dict() for i in items])
    pred = model.predict(df)