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:
@@ -6,6 +6,7 @@ import shutil
|
||||
import signal
|
||||
import threading
|
||||
import time
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
@@ -30,10 +31,21 @@ class Worker:
|
||||
settings.work_dir.mkdir(parents=True, exist_ok=True)
|
||||
if settings.ocr_workers < 1 or settings.lease_seconds <= settings.ocr_timeout:
|
||||
raise ValueError("OCR workers must be positive and lease must exceed OCR timeout")
|
||||
if settings.ocr_backend not in ("http", "pdf_text"):
|
||||
raise ValueError("IDP_OCR_BACKEND must be http or pdf_text")
|
||||
if settings.ocr_backend == "pdf_text":
|
||||
log.warning("IDP_OCR_BACKEND=pdf_text: bundle dựng từ text layer của PDF, không gọi model")
|
||||
|
||||
def job_dir(self, job_id: str) -> Path:
|
||||
return self.settings.work_dir / job_id
|
||||
|
||||
def cleanup_source(self, job: dict):
|
||||
"""Xoá file đã upload khi không còn job nào đang dùng nó."""
|
||||
if job["source_kind"] != "upload":
|
||||
return
|
||||
if not self.store.upload_in_use(job["file_path"], job["id"]):
|
||||
Path(job["file_path"]).unlink(missing_ok=True)
|
||||
|
||||
def prepare(self, job: dict):
|
||||
job_dir = self.job_dir(job["id"])
|
||||
downloaded = job_dir / "downloaded.bin"
|
||||
@@ -41,7 +53,8 @@ class Worker:
|
||||
try:
|
||||
if job["attempts"] > self.settings.max_job_attempts:
|
||||
raise ValueError("Preparation lease expired too many times")
|
||||
fetch_pdf(self.settings, job["file_path"], downloaded)
|
||||
settings = replace(self.settings, storage="local") if job["source_kind"] == "upload" else self.settings
|
||||
fetch_pdf(settings, job["file_path"], downloaded)
|
||||
decrypted = prepare_pdf(downloaded, source, self.settings.voffice_decrypt_mode,
|
||||
self.settings.voffice_des_key_hex)
|
||||
with pymupdf.open(source) as pdf:
|
||||
@@ -53,6 +66,8 @@ class Worker:
|
||||
except Exception as exc:
|
||||
log.exception("Preparation failed for %s", job["id"])
|
||||
self.store.prepare_failed(job["id"], job["attempts"], str(exc), self.settings.max_job_attempts)
|
||||
if job["attempts"] >= self.settings.max_job_attempts:
|
||||
self.cleanup_source(job)
|
||||
|
||||
def process_page(self, page: dict):
|
||||
job_id, page_no, attempts = page["job_id"], page["page_no"], page["attempts"]
|
||||
@@ -99,7 +114,8 @@ class Worker:
|
||||
bundle = {
|
||||
"schema": "dla-document-bundle", "version": "1.0",
|
||||
"document_id": document_id,
|
||||
"source": {"filename": Path(job["file_path"]).name, "media_type": "application/pdf",
|
||||
"source": {"filename": job["filename"] or Path(job["file_path"]).name,
|
||||
"media_type": "application/pdf",
|
||||
"page_count": len(pages), "render_dpi": 200},
|
||||
"outline": build_outline(pages), "pages": pages,
|
||||
}
|
||||
@@ -107,6 +123,7 @@ class Worker:
|
||||
has_failures = any(p["status"] == "failed" for p in pages)
|
||||
if self.store.finalized(job_id, job["finalize_attempts"], result_json, has_failures):
|
||||
shutil.rmtree(self.job_dir(job_id), ignore_errors=True)
|
||||
self.cleanup_source(job)
|
||||
log.info("Finalized %s for callback", job_id)
|
||||
except Exception as exc:
|
||||
log.exception("Finalization failed for %s", job_id)
|
||||
|
||||
Reference in New Issue
Block a user