Nhận PDF upload trực tiếp qua POST /parse
API trước đây chỉ nhận đường dẫn HDFS từ Airflow. Thêm endpoint
multipart/form-data nhận thẳng file PDF, vẫn chạy bất đồng bộ: trả 202 kèm
job_id và result_url, worker xử lý nền, client lấy dla-document-bundle ở
GET /jobs/{job_id}/result hoặc qua callback.
- File upload ghi xuống IDP_DATA_DIR/uploads theo sha256 nội dung nên gửi lại
cùng request_id trả cùng job_id; file được xoá khi không còn job nào dùng.
- Thêm cột filename/source_kind để bundle giữ đúng tên file gốc thay vì tên
đã băm.
- IDP_RUN_WORKER=1 chạy worker trong process API cho triển khai một host.
- IDP_OCR_BACKEND=pdf_text dựng bundle từ text layer của PDF để chạy thử toàn
bộ luồng khi chưa serve được model.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -68,6 +68,75 @@ def test_api_idempotency_and_end_to_end(tmp_path, monkeypatch):
|
||||
assert all(page["status"] == "done" for page in bundle["pages"])
|
||||
|
||||
|
||||
def drain(worker, settings):
|
||||
"""Chạy hết job đang chờ: prepare, mọi trang, rồi finalize."""
|
||||
assert worker.coordinator_once()
|
||||
while True:
|
||||
page = worker.store.claim_page(settings.lease_seconds, settings.ocr_workers)
|
||||
if not page:
|
||||
break
|
||||
worker.process_page(page)
|
||||
assert worker.coordinator_once()
|
||||
|
||||
|
||||
def test_upload_pdf_returns_bundle_without_gpu(tmp_path):
|
||||
settings = replace(settings_for(tmp_path), ocr_backend="pdf_text")
|
||||
source = tmp_path / "Giấy chứng nhận.pdf"
|
||||
make_pdf(source, count=2)
|
||||
app = create_app(settings)
|
||||
with TestClient(app) as client:
|
||||
with source.open("rb") as handle:
|
||||
reply = client.post("/parse", files={"file": (source.name, handle, "application/pdf")})
|
||||
assert reply.status_code == 202
|
||||
job_id = reply.json()["job_id"]
|
||||
assert reply.json()["result_url"] == f"/jobs/{job_id}/result"
|
||||
assert client.get(f"/jobs/{job_id}/result").status_code == 409
|
||||
|
||||
drain(Worker(settings), settings)
|
||||
|
||||
assert client.get(f"/jobs/{job_id}").json()["status"] == "completed"
|
||||
bundle = client.get(f"/jobs/{job_id}/result").json()
|
||||
assert bundle["schema"] == "dla-document-bundle"
|
||||
assert bundle["source"] == {"filename": "Giấy chứng nhận.pdf", "media_type": "application/pdf",
|
||||
"page_count": 2, "render_dpi": 200}
|
||||
assert bundle["document_id"].startswith("doc_")
|
||||
page = bundle["pages"][0]
|
||||
assert page["page_id"] == f"{bundle['document_id']}/p001"
|
||||
assert page["width"] > 0 and page["height"] > 0
|
||||
block = page["blocks"][0]
|
||||
assert block["block_id"] == f"{page['page_id']}/b000"
|
||||
assert block["label"] in {"Paragraph", "Image"}
|
||||
assert all(0 <= value <= 1000 for value in block["bbox"])
|
||||
assert block["outline_no"] == "0.1"
|
||||
assert bundle["outline"][0]["block_ids"][0] == block["block_id"]
|
||||
# Upload đã dedupe theo sha256 được dọn sau khi finalize.
|
||||
assert list(settings.upload_dir.glob("*.pdf")) == []
|
||||
|
||||
|
||||
def test_upload_rejects_non_pdf_and_replays_same_request_id(tmp_path):
|
||||
settings = settings_for(tmp_path)
|
||||
source = tmp_path / "input.pdf"
|
||||
make_pdf(source, count=1)
|
||||
request_id = str(uuid4())
|
||||
app = create_app(settings)
|
||||
with TestClient(app) as client:
|
||||
assert client.post("/parse", files={"file": ("note.txt", b"hello", "text/plain")}).status_code == 422
|
||||
assert client.post("/parse", files={"file": ("empty.pdf", b"", "application/pdf")}).status_code == 422
|
||||
|
||||
content = source.read_bytes()
|
||||
first = client.post("/parse", files={"file": ("input.pdf", content, "application/pdf")},
|
||||
data={"request_id": request_id})
|
||||
second = client.post("/parse", files={"file": ("input.pdf", content, "application/pdf")},
|
||||
data={"request_id": request_id})
|
||||
assert second.json()["job_id"] == first.json()["job_id"]
|
||||
assert second.headers["X-Idempotent-Replay"] == "true"
|
||||
conflict = client.post("/parse", files={"file": ("other.pdf", content, "application/pdf")},
|
||||
data={"request_id": request_id})
|
||||
assert conflict.status_code == 409
|
||||
# Job đầu vẫn giữ được file của nó sau khi request xung đột bị từ chối.
|
||||
assert len(list(settings.upload_dir.glob("*.pdf"))) == 1
|
||||
|
||||
|
||||
def test_page_retry_then_partial_failure(tmp_path, monkeypatch):
|
||||
settings = replace(settings_for(tmp_path), max_page_attempts=2)
|
||||
source = tmp_path / "bad.pdf"
|
||||
@@ -96,6 +165,19 @@ def test_page_retry_then_partial_failure(tmp_path, monkeypatch):
|
||||
assert bundle["pages"][0]["status"] == "failed"
|
||||
|
||||
|
||||
def test_pdf_text_backend_labels_pages_without_text(tmp_path):
|
||||
from idp_service.ocr import extract_text_blocks
|
||||
|
||||
blank = tmp_path / "blank.pdf"
|
||||
document = pymupdf.open()
|
||||
document.new_page()
|
||||
document.save(blank)
|
||||
document.close()
|
||||
result = extract_text_blocks(blank, 1)
|
||||
assert result["blocks"] == [{"label": "Image", "bbox": [0, 0, 1000, 1000],
|
||||
"html": "<p>[no extractable text]</p>"}]
|
||||
|
||||
|
||||
def test_rejects_truncated_plain_text_and_invalid_bbox():
|
||||
def response(content, finish="stop"):
|
||||
return {"choices": [{"finish_reason": finish, "message": {"content": content}}]}
|
||||
|
||||
Reference in New Issue
Block a user