API input
The
payload
parameter type in predict(self, payload)
can vary based on the content type of the request and can be parsed according to the Content-Type
header in the request. The parsing rules are as follows:For header
Content-Type: application/json
, thepayload
parameter will be the parsed JSON body.For
Content-Type: multipart/form-data
/ Content-Type: application/x-www-form-urlencoded
, payload
will be starlette.datastructures.FormData
(key-value pairs where the values are strings for text data, or starlette.datastructures.UploadFile
for file uploads; see Starlette's documentation).For all other
Content-Type
values, payload
will be the raw bytes of the request body.
$ curl https://***.syndic.ai \
-X POST \
-H "Content-Type: application/json" \
-d '{"key": "value"}'
class PythonPredictor:
def __init__(self, config):
pass
def predict(self, payload):
print(payload["key"]) # prints "value"
$ curl https://***.c1.syndic.ai \
-X POST \
-F "[email protected]" \
-F "[email protected]" \
-F "[email protected]"
from PIL import Image
import pickle
class PythonPredictor:
def __init__(self, config):
pass
def predict(self, payload):
text = payload["text"].file.read()
print(text.decode("utf-8")) # prints the contents of text.txt
obj = pickle.load(payload["object"].file)
print(obj["key"]) # prints "value" assuming `object.pkl` is a pickled dictionary {"key": "value"}
img = Image.open(payload["image"].file)
print(img.size) # prints the dimensions of image.png
Last modified 1yr ago