diff --git a/idp_service/ocr.py b/idp_service/ocr.py index d91d83c..78fcf5f 100644 --- a/idp_service/ocr.py +++ b/idp_service/ocr.py @@ -18,6 +18,8 @@ LABELS = { } LABEL_ALIASES = {"Page-Header": "Header", "Page Header": "Header", "Page-Footer": "Footer", "Page Footer": "Footer"} +# Vùng không có chữ: model trả div rỗng, không phải output hỏng. +VISUAL_LABELS = {"Image", "QR Code", "Barcode", "Signature", "Stamp"} class InvalidOutput(Exception): @@ -54,8 +56,8 @@ def validate_response(response: dict) -> list[dict]: box = block["bbox"] if box is None or len(box) != 4 or not (0 <= box[0] < box[2] <= 1000 and 0 <= box[1] < box[3] <= 1000): raise InvalidOutput(f"Invalid bounding box: {box}") - if not block["html"]: - raise InvalidOutput("Empty layout block") + if not block["html"] and block["label"] not in VISUAL_LABELS: + raise InvalidOutput(f"Empty layout block: {block['label']}") return blocks diff --git a/tests/test_service.py b/tests/test_service.py index f734fb1..9d7c8d5 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -322,6 +322,26 @@ def test_kie_prompt_schema_and_answer_shape(): assert kie.select_pages(20, 5) == [1, 2, 3, 4, 20] +def test_keeps_empty_visual_blocks_but_rejects_empty_text_blocks(): + """Model trả div rỗng cho con dấu, QR, chữ ký; chỉ block chữ mà rỗng mới là output hỏng.""" + def response(content): + return {"choices": [{"finish_reason": "stop", "message": {"content": content}}]} + + blocks = validate_response(response( + '
' + '' + 'Nội dung