Home > Back-end >  FastAPI array of JSON in Request Body for Machine Learning prediction
FastAPI array of JSON in Request Body for Machine Learning prediction

Time:04-13

I’m working with FastAPI for Model inference in Machine Learning, so I need to have as inputs an array of JSON like this:

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

The output (result of prediction) should look like this :

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

How to implement this with FastAPI in Python?

CodePudding user response:

You can declare a request JSON body using a Pydantic model (let's say Item), as described 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. Example below:

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"}]

Update

You can convert the Pydantic model into a dictionary using the .dict() method. You can then remove the Id key from the dictionary, as shown here, and finally, send the data to predict() function, as described in this answer. Example below:

import pandas as pd

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