fex(kb):知识库数据清洗针对pdf含有大量图片ocr清洗超时问题优化;perf(chat):问答回复点击doc移动端无法预览问题优化
This commit is contained in:
1 parent
0890c79070
commit
900a13722c
8 files changed
+170
-18
No files matched your search
@@ -8,12 +8,37 @@ from edkb_system.contracts import Block, Context
|
||||
|
||||
_PAGE_IMAGE_RENDER_SCALE = 0.75
|
||||
_PAGE_IMAGE_JPEG_QUALITY = 82
|
||||
_TEXT_HEAVY_PAGE_TEXT_LIMIT = 40
|
||||
_DEFAULT_PAGE_TEXT_LIMIT = 120
|
||||
_MIN_SCAN_IMAGE_BYTES = 4 * 1024
|
||||
_MIN_DEFAULT_IMAGE_BYTES = 16 * 1024
|
||||
_MIN_TEXT_HEAVY_IMAGE_BYTES = 24 * 1024
|
||||
|
||||
|
||||
def _is_scanned(text_chars: int, pages: int) -> bool:
|
||||
return text_chars < int(300 * max(pages, 1) / 10)
|
||||
|
||||
|
||||
def _is_text_heavy_pdf(text_chars: int, pages: int) -> bool:
|
||||
return text_chars >= max(800, 180 * max(pages, 1))
|
||||
|
||||
|
||||
def _should_keep_embedded_image(
|
||||
*,
|
||||
data_size: int,
|
||||
page_text_chars: int,
|
||||
scan_likely: bool,
|
||||
text_heavy_pdf: bool,
|
||||
) -> bool:
|
||||
if data_size <= 0:
|
||||
return False
|
||||
if scan_likely:
|
||||
return data_size >= _MIN_SCAN_IMAGE_BYTES
|
||||
if text_heavy_pdf:
|
||||
return page_text_chars <= _TEXT_HEAVY_PAGE_TEXT_LIMIT and data_size >= _MIN_TEXT_HEAVY_IMAGE_BYTES
|
||||
return page_text_chars <= 80 or data_size >= _MIN_DEFAULT_IMAGE_BYTES
|
||||
|
||||
|
||||
def _to_data_uri(blob: bytes, content_type: str = "image/png") -> str:
|
||||
b64 = base64.b64encode(blob).decode("ascii")
|
||||
return f"data:{content_type};base64,{b64}"
|
||||
@@ -62,32 +87,45 @@ def _page_image_stats(
|
||||
def _should_use_page_image_mode(
|
||||
*,
|
||||
scan_likely: bool,
|
||||
text_heavy_pdf: bool,
|
||||
text_chars: int,
|
||||
image_count: int,
|
||||
largest_ratio: float,
|
||||
total_ratio: float,
|
||||
) -> bool:
|
||||
if text_heavy_pdf and text_chars > _TEXT_HEAVY_PAGE_TEXT_LIMIT:
|
||||
return False
|
||||
if scan_likely:
|
||||
if image_count == 0:
|
||||
return True
|
||||
if largest_ratio >= 0.55 or total_ratio >= 0.65 or image_count >= 3:
|
||||
return True
|
||||
if text_chars <= 120 and (
|
||||
if text_chars <= _DEFAULT_PAGE_TEXT_LIMIT and (
|
||||
largest_ratio >= 0.72 or (image_count >= 5 and total_ratio >= 0.45)
|
||||
):
|
||||
return True
|
||||
if text_heavy_pdf:
|
||||
return text_chars <= _TEXT_HEAVY_PAGE_TEXT_LIMIT and (
|
||||
largest_ratio >= 0.88 or total_ratio >= 0.95 or image_count >= 8
|
||||
)
|
||||
return False
|
||||
|
||||
|
||||
def _analysis_candidate_pages(
|
||||
page_payloads: list[dict], scan_likely: bool
|
||||
page_payloads: list[dict], scan_likely: bool, text_heavy_pdf: bool
|
||||
) -> list[int]:
|
||||
if scan_likely:
|
||||
return [int(payload["page"]) for payload in page_payloads]
|
||||
if text_heavy_pdf:
|
||||
return [
|
||||
int(payload["page"])
|
||||
for payload in page_payloads
|
||||
if len(str(payload.get("text") or "")) <= _TEXT_HEAVY_PAGE_TEXT_LIMIT
|
||||
]
|
||||
return [
|
||||
int(payload["page"])
|
||||
for payload in page_payloads
|
||||
if len(str(payload.get("text") or "")) <= 120
|
||||
if len(str(payload.get("text") or "")) <= _DEFAULT_PAGE_TEXT_LIMIT
|
||||
]
|
||||
|
||||
|
||||
@@ -193,21 +231,56 @@ def run(input_data: dict, context: Context) -> list[Block]:
|
||||
images: list[dict] = []
|
||||
if text:
|
||||
text_chars += len(text)
|
||||
try:
|
||||
page_width = float(getattr(getattr(p, "mediabox", None), "width", 1000.0) or 1000.0)
|
||||
page_height = float(getattr(getattr(p, "mediabox", None), "height", 1000.0) or 1000.0)
|
||||
except Exception:
|
||||
page_width = 1000.0
|
||||
page_height = 1000.0
|
||||
|
||||
try:
|
||||
for img_idx, img in enumerate(getattr(p, "images", []) or [], start=1):
|
||||
data = getattr(img, "data", None)
|
||||
if not data:
|
||||
continue
|
||||
if not _should_keep_embedded_image(
|
||||
data_size=len(data),
|
||||
page_text_chars=len(text),
|
||||
scan_likely=False,
|
||||
text_heavy_pdf=False,
|
||||
):
|
||||
continue
|
||||
ext = (getattr(img, "image_format", "") or "").lower()
|
||||
ct = f"image/{ext}" if ext else "image/png"
|
||||
images.append({"id": img_idx, "data": data, "content_type": ct})
|
||||
except Exception:
|
||||
pass
|
||||
page_payloads.append({"page": page_idx, "text": text, "images": images})
|
||||
page_payloads.append(
|
||||
{
|
||||
"page": page_idx,
|
||||
"text": text,
|
||||
"images": images,
|
||||
"page_width": page_width,
|
||||
"page_height": page_height,
|
||||
}
|
||||
)
|
||||
|
||||
scan_likely = _is_scanned(text_chars, context.pages)
|
||||
candidate_pages = _analysis_candidate_pages(page_payloads, scan_likely)
|
||||
text_heavy_pdf = _is_text_heavy_pdf(text_chars, context.pages)
|
||||
for payload in page_payloads:
|
||||
payload["images"] = [
|
||||
item
|
||||
for item in payload["images"]
|
||||
if _should_keep_embedded_image(
|
||||
data_size=len(item.get("data") or b""),
|
||||
page_text_chars=len(str(payload.get("text") or "")),
|
||||
scan_likely=scan_likely,
|
||||
text_heavy_pdf=text_heavy_pdf,
|
||||
)
|
||||
]
|
||||
candidate_pages = _analysis_candidate_pages(
|
||||
page_payloads, scan_likely, text_heavy_pdf
|
||||
)
|
||||
image_boxes_by_page: dict[int, list[tuple[float, float, float, float]]] = {}
|
||||
pdfium_doc = None
|
||||
if candidate_pages:
|
||||
@@ -227,9 +300,14 @@ def run(input_data: dict, context: Context) -> list[Block]:
|
||||
text = str(payload["text"] or "")
|
||||
page_images = list(payload["images"])
|
||||
image_boxes = image_boxes_by_page.get(page_idx, [])
|
||||
stats = _page_image_stats(1000.0, 1000.0, image_boxes)
|
||||
stats = _page_image_stats(
|
||||
float(payload.get("page_width") or 1000.0),
|
||||
float(payload.get("page_height") or 1000.0),
|
||||
image_boxes,
|
||||
)
|
||||
use_page_image_mode = _should_use_page_image_mode(
|
||||
scan_likely=scan_likely,
|
||||
text_heavy_pdf=text_heavy_pdf,
|
||||
text_chars=len(text),
|
||||
image_count=len(page_images) or int(stats["count"]),
|
||||
largest_ratio=float(stats["largest_ratio"]),
|
||||
@@ -292,6 +370,8 @@ def run(input_data: dict, context: Context) -> list[Block]:
|
||||
if scan_likely:
|
||||
context.warnings.append("pdf likely scanned; OCR recommended")
|
||||
context.extras["scan_likely"] = True
|
||||
if text_heavy_pdf:
|
||||
context.extras["text_heavy_pdf"] = True
|
||||
if page_image_mode_pages:
|
||||
context.extras["pdf_page_image_mode_pages"] = page_image_mode_pages
|
||||
context.warnings.append(
|
||||
|
||||
Reference in new issue
Block a user