Merge in RR/image-prediction from tests to master
Squashed commit of the following:
commit 1776e3083c97025e699d579f936dd0cc6e1fe152
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Mon Mar 21 13:54:27 2022 +0100
blacckkkyykykykyk
commit 4c9e6c38bdcea7d81008bf9dfcfcdd19d199da6a
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Mon Mar 21 13:53:40 2022 +0100
add predicting as subprocess, add workaround for keras not working if the model was loaded in different process
commit 530de2ff8979c70aa22f06edf297864787e0cc79
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Mon Mar 21 13:36:23 2022 +0100
refactor
commit 130d0e8b23e0375a6fd240ac8aa00492c341a716
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Mon Mar 21 13:34:54 2022 +0100
add minimal not working example for keras bug in multiprocess process
commit 2589598b052f680fd702df4f60d56a55778474a9
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Mon Mar 21 11:13:45 2022 +0100
test
commit eb6f211f02bc184e7f92d6b4d53c91da34ab9f2f
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Mon Mar 21 11:07:32 2022 +0100
hardcoded test
commit 3e9bfac5cf9b2e09340e2c2c5b24a800925bcd60
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Mon Mar 21 11:01:21 2022 +0100
test
commit 3d9c4d8856522cc2a22b2a7b9ea64d34629eb2c1
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Mon Mar 21 10:57:03 2022 +0100
change test
commit 58ca784d6c56fd63734062d0c40b6b39550cf7d7
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Mon Mar 21 10:21:38 2022 +0100
fix test
commit 6faad5ad5b6ef59bb5ef701b57d4c4addd17de0e
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Mon Mar 21 10:00:28 2022 +0100
add predictor test
commit 3fbca0ac23821568a8afa904a8fb33ab0679f129
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Fri Mar 18 13:04:13 2022 +0100
refactor folder structure
commit 90e3058c7124394a9f229d50278e57194f3d875d
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Fri Mar 18 12:58:02 2022 +0100
add response test
commit 2a2deffd0b461ec5161009b3923623152f4c8f44
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Fri Mar 18 12:56:32 2022 +0100
add test infrastructure
66 lines
1.8 KiB
Python
66 lines
1.8 KiB
Python
import multiprocessing
|
|
from typing import Callable
|
|
|
|
from flask import Flask, request, jsonify
|
|
|
|
from image_prediction.utils import get_logger
|
|
|
|
logger = get_logger()
|
|
|
|
|
|
def make_prediction_server(predict_fn: Callable):
|
|
|
|
app = Flask(__name__)
|
|
|
|
@app.route("/ready", methods=["GET"])
|
|
def ready():
|
|
resp = jsonify("OK")
|
|
resp.status_code = 200
|
|
return resp
|
|
|
|
@app.route("/health", methods=["GET"])
|
|
def healthy():
|
|
resp = jsonify("OK")
|
|
resp.status_code = 200
|
|
return resp
|
|
|
|
@app.route("/", methods=["POST"])
|
|
def predict():
|
|
def predict_fn_wrapper(pdf, return_dict):
|
|
return_dict["result"] = predict_fn(pdf)
|
|
|
|
def process():
|
|
# Tensorflow does not free RAM. Workaround is running model in process.
|
|
# https://stackoverflow.com/questions/39758094/clearing-tensorflow-gpu-memory-after-model-execution
|
|
pdf = request.data
|
|
manager = multiprocessing.Manager()
|
|
return_dict = manager.dict()
|
|
p = multiprocessing.Process(
|
|
target=predict_fn_wrapper,
|
|
args=(
|
|
pdf,
|
|
return_dict,
|
|
),
|
|
)
|
|
p.start()
|
|
p.join()
|
|
try:
|
|
return dict(return_dict)["result"]
|
|
except KeyError:
|
|
raise
|
|
|
|
logger.debug("Running predictor on document...")
|
|
try:
|
|
predictions = process()
|
|
response = jsonify(predictions)
|
|
logger.info("Analysis completed.")
|
|
return response
|
|
except Exception as err:
|
|
logger.error("Analysis failed.")
|
|
logger.exception(err)
|
|
response = jsonify("Analysis failed.")
|
|
response.status_code = 500
|
|
return response
|
|
|
|
return app
|