def input_handler()

in tf-distribution-options/code/inference.py [0:0]


def input_handler(data, context):
    """ Pre-process request input before it is sent to TensorFlow Serving REST API

    Args:
        data (obj): the request data, in format of dict or string
        context (Context): an object containing request and configuration details

    Returns:
        (dict): a JSON-serializable dict that contains request body and headers
    """

    if context.request_content_type == 'application/x-image':

        image_as_bytes = io.BytesIO(data.read())
        image = Image.open(image_as_bytes)
        instance = np.expand_dims(image, axis=0)
        return json.dumps({"instances": instance.tolist()})

    else:
        _return_error(415, 'Unsupported content type "{}"'.format(context.request_content_type or 'Unknown'))