feat/kie-extraction #1

Merged
annt839 merged 3 commits from feat/kie-extraction into main 2026-09-24 08:17:45 +00:00
2 changed files with 24 additions and 2 deletions
Showing only changes of commit e9d7c6d391 - Show all commits

View File

@@ -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

View File

@@ -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(
'<div data-label="Stamp" data-bbox="736 86 909 142"></div>'
'<div data-label="QR Code" data-bbox="814 55 896 112"></div>'
'<div data-label="Paragraph" data-bbox="92 85 795 151"><p>Nội dung</p></div>'))
assert [block["label"] for block in blocks] == ["Stamp", "QR Code", "Paragraph"]
assert [block["html"] for block in blocks[:2]] == ["", ""]
try:
validate_response(response('<div data-label="Paragraph" data-bbox="1 1 500 500"></div>'))
except InvalidOutput as exc:
assert "Paragraph" in str(exc)
else:
raise AssertionError("Block chữ rỗng đáng lẽ phải bị từ chối")
def test_rejects_truncated_plain_text_and_invalid_bbox():
def response(content, finish="stop"):
return {"choices": [{"finish_reason": finish, "message": {"content": content}}]}