I have a FastAPI application which makes a prediction based on the HTTP POST data sent to it. The code is as follows
import tensorflow as tf
from typing import List
from fastapi import FastAPI, Response
from pydantic import BaseModel
class ItemList(BaseModel):
instances: List[List[float]]
app = FastAPI()
model = tf.keras.models.load_model('trainedmodel/1')
@app.post('/v1/models/my_model:predict')
def predict(inputdata: ItemList):
# Extract [[1.5,1.65,2,0.5,-2,1.5,0.1,2,0.2]]
result = {
"predictions": [
model.predict(modelInput).flatten().tolist()
]
}
return result
The request type to the application is application/json and the request body contains {"instances":[[1.5,1.65,2,0.5,-2,1.5,0.1,2,0.2]]}
I need to extract [[1.5,1.65,2,0.5,-2,1.5,0.1,2,0.2]] from the request body so that I can input it to the model (as modelInput in the above code). In Flask I used request.json['instances'] to do so. Can someone please let me know how I can do the same in FastAPI?
inputdata.instances