Bóc 7 trường cơ bản của văn bản sau khi OCR xong

Thêm bước KIE một lượt cho cả tài liệu, chạy sau khi mọi trang đã OCR và trước
khi finalize: document_type, document_number, issued_date, issuing_authority,
title, signer.name, signer.position.

- Prompt trong idp_service/kie.py giống từng byte template lượt 2 của
  IDP-Training-Pipeline (kie_prompts/extract_vi.txt) nên model KIE fine-tune gặp
  đúng định dạng đã train; khác ở chỗ schema cố định, không qua lượt GLM đề xuất
  schema. Quy ước riêng của từng trường nằm trong description của trường đó.
- Kết quả nằm ở "extraction" trong bundle, ở GET /jobs/{id}/extraction và trong
  output.extraction của callback, cạnh ảnh từng trang.
- Bóc trường hỏng không làm hỏng job: fields giữ đủ khoá với giá trị null,
  status là failed/skipped kèm lý do, trạng thái job vẫn do OCR quyết định.
- Bước extract dùng chung hàng đợi và giới hạn IDP_OCR_WORKERS với OCR từng
  trang nên không làm quá tải GPU; retry riêng theo IDP_MAX_KIE_ATTEMPTS.
- Tài liệu dài hơn IDP_KIE_MAX_PAGES chỉ gửi các trang đầu và trang cuối vì
  người ký nằm ở cuối văn bản.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-09-23 07:52:41 +00:00
parent 0e15bc89a8
commit de967a64c2
8 changed files with 517 additions and 37 deletions

View File

@@ -16,6 +16,7 @@ from starlette.concurrency import run_in_threadpool
from .config import Settings
from .db import Store
from .kie import SCHEMA_NAME, empty_answer
CHUNK = 1024 * 1024
@@ -73,7 +74,7 @@ def create_app(settings: Settings | None = None) -> FastAPI:
def public_job(job: dict) -> dict:
result = {key: job[key] for key in (
"id", "request_id", "dag_run_id", "batch_id", "status", "file_path", "filename", "source_kind",
"total_pages", "completed_pages", "failed_pages", "error",
"total_pages", "completed_pages", "failed_pages", "error", "extract_status",
"callback_state", "callback_error", "created_at", "updated_at")}
result["result_available"] = job["result_json"] is not None
return result
@@ -168,6 +169,18 @@ def create_app(settings: Settings | None = None) -> FastAPI:
raise HTTPException(status_code=404, detail="Job not found")
return {"job_id": job_id, "pages": store.get_pages(job_id)}
@app.get("/jobs/{job_id}/extraction", dependencies=[Depends(authorize)])
def job_extraction(job_id: str):
"""Chỉ các trường đã bóc; có ngay khi bước extract xong, không cần đợi finalize."""
job = store.get_job(job_id)
if not job:
raise HTTPException(status_code=404, detail="Job not found")
if job["extract_status"] in ("pending", "running"):
raise HTTPException(status_code=409, detail="Extraction is not ready")
return {"job_id": job_id, "schema": SCHEMA_NAME, "status": job["extract_status"],
"fields": json.loads(job["extract_json"]) if job["extract_json"] else empty_answer(),
"error": job["extract_error"]}
@app.get("/jobs/{job_id}/result", dependencies=[Depends(authorize)])
def job_result(job_id: str):
job = store.get_job(job_id)