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:
@@ -1,6 +1,8 @@
|
||||
"""Render one page, call Chandra, and reject incomplete model output."""
|
||||
import base64
|
||||
import html
|
||||
import json
|
||||
import re
|
||||
from pathlib import Path
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
@@ -57,7 +59,39 @@ def validate_response(response: dict) -> list[dict]:
|
||||
return blocks
|
||||
|
||||
|
||||
def extract_text_blocks(pdf_path: Path, page_no: int) -> dict:
|
||||
"""Backend không cần GPU: lấy block văn bản sẵn có trong PDF số hoá.
|
||||
|
||||
Chỉ dùng để chạy thử service khi chưa serve được model; nhãn luôn là
|
||||
Paragraph nên outline sẽ không có section. Trang không có text trả về một
|
||||
block Image phủ toàn trang.
|
||||
"""
|
||||
with pymupdf.open(pdf_path) as doc:
|
||||
page = doc[page_no - 1]
|
||||
rect = page.rect
|
||||
pixels = (rect * pymupdf.Matrix(200 / 72, 200 / 72)).irect
|
||||
blocks = []
|
||||
for x0, y0, x1, y1, text, *_ in page.get_text("blocks"):
|
||||
text = re.sub(r"\s*\n\s*", " ", text).strip()
|
||||
if not text:
|
||||
continue
|
||||
box = [round(1000 * (x0 - rect.x0) / rect.width), round(1000 * (y0 - rect.y0) / rect.height),
|
||||
round(1000 * (x1 - rect.x0) / rect.width), round(1000 * (y1 - rect.y0) / rect.height)]
|
||||
box = [min(1000, max(0, value)) for value in box]
|
||||
if box[0] >= box[2] or box[1] >= box[3]:
|
||||
continue
|
||||
blocks.append({"label": "Paragraph", "bbox": box,
|
||||
"html": f"<p>{html.escape(text)}</p>"})
|
||||
return {"width": pixels.width, "height": pixels.height,
|
||||
"blocks": blocks or [{"label": "Image", "bbox": [0, 0, 1000, 1000],
|
||||
"html": "<p>[no extractable text]</p>"}]}
|
||||
|
||||
|
||||
def infer_page(settings: Settings, pdf_path: Path, page_no: int, *, penalize_repetition: bool = False) -> dict:
|
||||
if settings.ocr_backend == "pdf_text":
|
||||
return extract_text_blocks(pdf_path, page_no)
|
||||
if settings.ocr_backend != "http":
|
||||
raise ValueError(f"Unknown IDP_OCR_BACKEND: {settings.ocr_backend}")
|
||||
image, width, height = render_page(pdf_path, page_no)
|
||||
data_url = "data:image/png;base64," + base64.b64encode(image).decode("ascii")
|
||||
payload = {
|
||||
|
||||
Reference in New Issue
Block a user