ai: cache model

This commit is contained in:
Fam Zheng 2026-01-27 22:34:08 +00:00
parent fe1dccde17
commit 74809305ae

View File

@ -28,6 +28,9 @@ scans_dir = os.path.join(data_dir, 'scans')
os.makedirs(local_model_dir, exist_ok=True) os.makedirs(local_model_dir, exist_ok=True)
# Cache for loaded models to avoid reloading on every request
_model_cache = {}
def get_file_md5(fname): def get_file_md5(fname):
return hashlib.md5(open(fname, 'rb').read()).hexdigest() return hashlib.md5(open(fname, 'rb').read()).hexdigest()
@ -85,7 +88,10 @@ def do_qr_verify():
fd = flask.request.form fd = flask.request.form
model_name = fd.get('model', default_model) model_name = fd.get('model', default_model)
model_path, model_md5 = download_model(model_name) model_path, model_md5 = download_model(model_name)
model, transforms = load_model(model_path) # Cache models by path to avoid reloading on every request
if model_path not in _model_cache:
_model_cache[model_path] = load_model(model_path)
model, transforms = _model_cache[model_path]
frame_image, clarities = find_best_frame(flask.request.files) frame_image, clarities = find_best_frame(flask.request.files)
frame_qrcode, _ = extract_qr(frame_image) frame_qrcode, _ = extract_qr(frame_image)