paperless_to_openwebui.py aktualisiert

This commit is contained in:
2026-06-25 16:02:51 +02:00
parent 9f467d3204
commit f80324a6f6
+78
View File
@@ -880,6 +880,75 @@ def wait_for_openwebui():
# MAIN
# ============================================================
def main():
def ocr_fix_only_pass():
"""
Geht alle Paperless-Dokumente durch und korrigiert nur den OCR-Text.
Kein Upload nach OpenWebUI das erledigt der nächste reguläre Sync.
"""
log.info("=== OCR FIX ONLY MODE ===")
log.info("Fetching all documents from Paperless...")
docs = []
page = 1
while True:
url = f"{PAPERLESS_URL}/api/documents/?page_size=100&page={page}"
r = requests.get(url, headers=paperless_headers, timeout=120)
r.raise_for_status()
data = r.json()
results = data.get("results", [])
if not results:
break
docs.extend(results)
if not data.get("next"):
break
page += 1
total = len(docs)
log.info("Found %s documents to check.", total)
fixed = 0
skipped = 0
errors = 0
for i, doc in enumerate(docs):
doc_id = doc["id"]
title = doc.get("title", f"doc_{doc_id}")
content = doc.get("content", "")
if not content or len(content.strip()) < 50:
skipped += 1
continue
if not needs_ocr_fix(content):
skipped += 1
if (i + 1) % 100 == 0:
log.info(" Progress: %d/%d (fixed=%d, skipped=%d)",
i + 1, total, fixed, skipped)
continue
log.info("OCR fix needed: #%s '%s'", doc_id, title)
try:
fixed_text = fix_ocr_chunked(content)
if fixed_text and fixed_text != content:
if update_paperless_content(doc_id, fixed_text):
fixed += 1
log.info(" ✓ Fixed #%s (delta: %+d chars)",
doc_id, len(fixed_text) - len(content))
else:
errors += 1
else:
skipped += 1
log.debug(" No changes for #%s", doc_id)
except Exception as e:
log.error(" ✗ Error fixing #%s: %s", doc_id, e)
errors += 1
log.info("=== OCR FIX COMPLETE ===")
log.info(" Total: %d", total)
log.info(" Fixed: %d", fixed)
log.info(" Skipped: %d", skipped)
log.info(" Errors: %d", errors)
global KNOWLEDGE_ID
init_db()
wait_for_openwebui()
@@ -896,6 +965,15 @@ def calc_wait_time(target_time_str):
return (target - n).total_seconds()
if __name__ == "__main__":
OCR_FIX_ONLY = os.environ.get("OCR_FIX_ONLY", "false").lower() == "true"
if OCR_FIX_ONLY:
# Nur OCR fixen, kein Sync nach OpenWebUI
init_db()
ocr_fix_only_pass()
sys.exit(0)
# Normaler Betrieb
RUN_AT = os.getenv("SYNC_TIME", "04:00")
print(f"Container started, task will run daily at {RUN_AT}", flush=True)
while True: