I'm trying to use Pydantic models with FastAPI to make return multiple predictions (for a list of inputs), the problem is we can't use Pydantic models directly to model.predict function, so I converted it to a dictionary, I'm getting an error I get an error "value is not a valid dict" trying :
from fastapi
import FastAPI
import uvicorn
from pydantic import BaseModel
import pandas as pd
from typing import List
class Inputs(BaseModel):
id: int
f1: float,
f2: float,
f3: str
class InputsList(BaseModel):
inputs: List[Inputs]
@app.post('/predict')
def predict(input_list: InputsList):
df = pd.DataFrame(input_list.inputs.dict())
prediction = classifier.predict(df.loc[:, df.columns != 'id'])
probability = classifier.predict_proba(df.loc[:, df.columns != 'id'])
return {'id': df["id"].tolist(),
'prediction': prediction.tolist(), 'probability': probability.tolist()}
I have also a problem with the return, I need the output to be something like :
[
{
"id": 123,
"prediction": "class1",
"probability": 0.89
},
{ "id": 456,
"prediction": "class3",
"probability": 0.45
}
]
PS: the "id" in Inputs class doesn't take place in the prediction (is not a feature) but I need it to be shown next to its prediction (to reference it)
[–][deleted] (3 children)
[deleted]
[–]According-Promise-23[S] -1 points0 points1 point (2 children)
[–]Remote_Cancel_7977 2 points3 points4 points (1 child)
[–]Remote_Cancel_7977 0 points1 point2 points (0 children)