paperless_to_openwebui.py aktualisiert
This commit is contained in:
@@ -315,7 +315,9 @@ def clean_ocr_text(text: str):
|
|||||||
ch for ch in text
|
ch for ch in text
|
||||||
if ch == "\n" or ch == "\t" or ord(ch) >= 32
|
if ch == "\n" or ch == "\t" or ord(ch) >= 32
|
||||||
)
|
)
|
||||||
text = re.sub(r"\s+", " ", text)
|
# Nur horizontale Whitespaces zusammenfassen, Zeilenumbrüche erhalten!
|
||||||
|
text = re.sub(r"[^\S\n]+", " ", text) # ← geändert
|
||||||
|
text = re.sub(r"\n{3,}", "\n\n", text) # max 2 Leerzeilen
|
||||||
if len(text) > MAX_TEXT_LENGTH:
|
if len(text) > MAX_TEXT_LENGTH:
|
||||||
log.warning("Truncating text (%s chars)", len(text))
|
log.warning("Truncating text (%s chars)", len(text))
|
||||||
text = text[:MAX_TEXT_LENGTH]
|
text = text[:MAX_TEXT_LENGTH]
|
||||||
@@ -679,12 +681,28 @@ def process_single_doc(doc, i: int, total: int):
|
|||||||
with db_lock:
|
with db_lock:
|
||||||
existing = get_synced_document(doc_id)
|
existing = get_synced_document(doc_id)
|
||||||
|
|
||||||
text = download_document_text(doc_id)
|
text = doc.get("content", "")
|
||||||
if not text.strip():
|
if not text.strip():
|
||||||
log.warning("Skipping empty doc=%s", doc_id)
|
log.warning("Skipping empty doc=%s", doc_id)
|
||||||
return "skipped"
|
return "skipped"
|
||||||
|
|
||||||
text = clean_ocr_text(text)
|
text = clean_ocr_text(text)
|
||||||
|
|
||||||
|
# ── OCR Fix ───────────────────────────────────────────────
|
||||||
|
if OCR_FIX_ENABLED and needs_ocr_fix(text):
|
||||||
|
log.info("OCR fix needed for doc=%s '%s'", doc_id, title)
|
||||||
|
fixed_text = fix_ocr_chunked(text)
|
||||||
|
if fixed_text and fixed_text != text:
|
||||||
|
log.info("OCR fix applied for doc=%s (delta: %+d chars)",
|
||||||
|
doc_id, len(fixed_text) - len(text))
|
||||||
|
update_paperless_content(doc_id, fixed_text)
|
||||||
|
text = fixed_text
|
||||||
|
else:
|
||||||
|
log.debug("OCR fix: no changes for doc=%s", doc_id)
|
||||||
|
else:
|
||||||
|
log.debug("OCR fix: not needed for doc=%s", doc_id)
|
||||||
|
# ──────────────────────────────────────────────────────────
|
||||||
|
|
||||||
chunk_docs = build_chunk_documents(doc, text)
|
chunk_docs = build_chunk_documents(doc, text)
|
||||||
content_hash = hashlib.sha256(
|
content_hash = hashlib.sha256(
|
||||||
("".join(chunk_docs)).encode("utf-8")
|
("".join(chunk_docs)).encode("utf-8")
|
||||||
|
|||||||
Reference in New Issue
Block a user