Merge in RR/fb_detr_prediction_container from exception_handling to master
Squashed commit of the following:
commit 591339edfc9fd9c1dfb26119b5b35336fde696f1
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Fri Feb 18 11:49:10 2022 +0100
removed debug prints
commit bbbf50c3ba1a3b3f0e0a91fbc64b38a5f3a8d8eb
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Fri Feb 18 11:48:18 2022 +0100
fixed docker.sh base-dockerfile name
commit 82b0453dcfd7f099336185bcb059d49c37336d28
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Fri Feb 18 11:00:56 2022 +0100
dinge
commit bd8778256624b11cab4ed083d41e254120fdc158
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Fri Feb 18 10:58:48 2022 +0100
fixed docker.shs
commit 42cb54fb150d506243bde1b654cc3f7b90ae2069
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Fri Feb 18 10:56:11 2022 +0100
fixed (?) path to submodule in planspec script
commit 981304483870ccce0868a59514234cec8c6b5234
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Fri Feb 18 10:53:38 2022 +0100
added submodule init call to docker-build.sh
commit 6a1745a4d3857f49c460ac1a2479d44e79adf695
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Fri Feb 18 10:27:30 2022 +0100
debug echos
commit 659828d3a9220d4f009f53d7bae0dc44786e831e
Merge: 91af62c 91ee3b7
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Fri Feb 18 10:09:33 2022 +0100
Merge branch 'master' into exception_handling
commit 91ee3b778201b095d279f8e1f9ec6bef7f863edb
Merge: 08b2583 b7ca7cb
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Fri Feb 18 10:09:09 2022 +0100
Merge branch 'master' of ssh://git.iqser.com:2222/rr/fb_detr_prediction_container
commit 91af62c09e852f7393aac8172634c06d2b7f7e75
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Fri Feb 18 09:53:17 2022 +0100
renaming
commit 0e5f861b79f747a4e338c0d9b083d2e4a35d9893
Merge: 08b2583 03274d4
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Fri Feb 18 09:52:10 2022 +0100
Merge branch 'master' of ssh://git.iqser.com:2222/rr/fb_detr_prediction_container into exception_handling
commit 08b2583b0db78cf6c87a2c68f372a91800abde7b
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Fri Feb 18 09:49:49 2022 +0100
renaming
commit e5053345669eb1336d3327ece5c5514c694e1801
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Fri Feb 18 09:49:31 2022 +0100
applied black
commit e051ef6cc4aa95ed9ec8b83c96a1a087edb652b6
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Fri Feb 18 09:48:33 2022 +0100
exception handling
109 lines
2.4 KiB
Python
109 lines
2.4 KiB
Python
import argparse
|
|
import logging
|
|
import os
|
|
|
|
from flask import Flask, request, jsonify
|
|
from pdf2image import pdf2image
|
|
|
|
from fb_detr.locations import DATA_DIR
|
|
from fb_detr.locations import TORCH_HOME
|
|
from fb_detr.predictor import Predictor
|
|
from fb_detr.utils.config import read_config
|
|
|
|
|
|
def suppress_userwarnings():
|
|
import warnings
|
|
|
|
warnings.filterwarnings("ignore")
|
|
|
|
|
|
def parse_args():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--resume")
|
|
parser.add_argument("--warnings", action="store_true", default=False)
|
|
args = parser.parse_args()
|
|
|
|
return args
|
|
|
|
|
|
def load_classes():
|
|
classes = read_config("classes")
|
|
id2class = dict(zip(range(1, len(classes) + 1), classes))
|
|
return id2class
|
|
|
|
|
|
def get_checkpoint():
|
|
return DATA_DIR / read_config("checkpoint")
|
|
|
|
|
|
def set_torch_env():
|
|
os.environ["TORCH_HOME"] = str(TORCH_HOME)
|
|
|
|
|
|
def main(args):
|
|
|
|
if not args.warnings:
|
|
suppress_userwarnings()
|
|
|
|
run_server(args.resume)
|
|
|
|
|
|
def run_server(resume):
|
|
|
|
set_torch_env()
|
|
|
|
def initialize_predictor():
|
|
checkpoint = get_checkpoint() if not resume else resume
|
|
predictor = Predictor(checkpoint, classes=load_classes(), rejection_class=read_config("rejection_class"))
|
|
return predictor
|
|
|
|
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_request():
|
|
def inner():
|
|
|
|
pdf = request.data
|
|
|
|
pages = pdf2image.convert_from_bytes(pdf)
|
|
predictions = predictor.predict(pages)
|
|
|
|
return jsonify(list(predictions))
|
|
|
|
try:
|
|
return inner()
|
|
except Exception as err:
|
|
logging.warning("Analysis failed")
|
|
logging.exception(err)
|
|
resp = jsonify("Analysis failed")
|
|
resp.status_code = 500
|
|
return resp
|
|
|
|
@app.route("/status", methods=["GET"])
|
|
def status():
|
|
response = "OK"
|
|
return jsonify(response)
|
|
|
|
predictor = initialize_predictor()
|
|
|
|
logging.info("Predictor ready.")
|
|
|
|
app.run(host="127.0.0.1", port=5000, debug=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
args = parse_args()
|
|
main(args)
|