def inference()

in src/src/predictor.py [0:0]


def inference():
    """Performed an inference on incoming data.
    In this sample server, we take data as application/json,
    print it out to confirm that the server received it.
    """
    content_type = flask.request.content_type
    if flask.request.content_type != "application/json":
        msg = "I just take json, and I am fed with {}".format(content_type)
    else:
        msg = "I am fed with json. Therefore, I am happy"

    
    data = flask.request.data.decode("utf-8")
    data = io.StringIO(data)
    data = json.loads(data.read())
    
    account_id = boto3.client("sts").get_caller_identity()["Account"]
    region = boto3.Session().region_name
    
    bucket_name = f"photo-to-sketch-{account_id}"
    dict_style = {"1":"style/1.jpeg","2":"style/2.jpeg","3":"style/3.jpeg","4":"style/4.jpeg"}
    effect_type = dict_style[data["effectType"]]
    
    #Style image
    style_image_object = read_image_from_s3(bucket_name, effect_type)
    style_image = load_img(style_image_object)
    print("Style image loaded!")
    
    # Content image
    input_image = data['image']
    content_image_object = Image.open(BytesIO(base64.b64decode(input_image)))
    content_image = load_img(content_image_object)
    print("Content image loaded!")
    
    stylized_image = TensorflowService.predict(content_image,style_image)
    stylized_image = tensor_to_image(stylized_image)  
    
    #Encode the response to base64 
    buffered = BytesIO()
    stylized_image.save(buffered, format="JPEG")
    img_str = base64.b64encode(buffered.getvalue())
    print("Stylized image generated!")
    
    return flask.Response(
        response=json.dumps({"image": img_str.decode("utf-8")}),
        status=200,
        mimetype="text/plain",
    )