1

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?

2
  • just inputdata.instances Commented Aug 5, 2020 at 10:15
  • Thank you @AlexNoname! It worked. I remember trying this earlier and it failed. Probably because I had an error somewhere else. Can you add this as an answer so that I can accept it as the best answer? Commented Aug 5, 2020 at 10:25

1 Answer 1

1

To access request body you can just use inputdata.instances

class ItemList(BaseModel):
    instances: List[List[float]]

@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]]
    extracted_list = inputdata.instances
Sign up to request clarification or add additional context in comments.

2 Comments

Hi! I'm trying to do the same thing but I have a problem. I want to predict a list of items, but I don't know how send a list of items to requests.post function since apparently it only accepts json. I'm really new to this whole deploying model thing, so any help is appreciated!
@Tata could you solve your problem? I'm experiencing the exact same thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.