A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
Feature: Switch all indexing to use rich (#12193)
Trenton H20a9cd40e80b
diff --git a/src/documents/management/commands/document_index.py b/src/documents/management/commands/document_index.pyindex 1fa4f5a70..05efe870c 100644--- a/src/documents/management/commands/document_index.py+++ b/src/documents/management/commands/document_index.py@@ -1,22 +1,25 @@-from django.core.management import BaseCommandfrom django.db import transaction-from documents.management.commands.mixins import ProgressBarMixin+from documents.management.commands.base import PaperlessCommandfrom documents.tasks import index_optimizefrom documents.tasks import index_reindex-class Command(ProgressBarMixin, BaseCommand):+class Command(PaperlessCommand):help = "Manages the document index."def add_arguments(self, parser):+ super().add_arguments(parser)parser.add_argument("command", choices=["reindex", "optimize"])- self.add_argument_progress_bar_mixin(parser)def handle(self, *args, **options):- self.handle_progress_bar_mixin(**options)with transaction.atomic():if options["command"] == "reindex":- index_reindex(progress_bar_disable=self.no_progress_bar)+ index_reindex(+ iter_wrapper=lambda docs: self.track(+ docs,+ description="Indexing documents...",+ ),+ )elif options["command"] == "optimize":index_optimize()diff --git a/src/documents/management/commands/document_llmindex.py b/src/documents/management/commands/document_llmindex.pyindex d2df02ed9..6af1c7c9f 100644--- a/src/documents/management/commands/document_llmindex.py+++ b/src/documents/management/commands/document_llmindex.py@@ -1,22 +1,22 @@-from django.core.management import BaseCommand-from django.db import transaction+from typing import Any-from documents.management.commands.mixins import ProgressBarMixin+from documents.management.commands.base import PaperlessCommandfrom documents.tasks import llmindex_index-class Command(ProgressBarMixin, BaseCommand):+class Command(PaperlessCommand):help = "Manages the LLM-based vector index for Paperless."- def add_arguments(self, parser):+ def add_arguments(self, parser: Any) -> None:+ super().add_arguments(parser)parser.add_argument("command", choices=["rebuild", "update"])- self.add_argument_progress_bar_mixin(parser)- def handle(self, *args, **options):- self.handle_progress_bar_mixin(**options)- with transaction.atomic():- llmindex_index(- progress_bar_disable=self.no_progress_bar,- rebuild=options["command"] == "rebuild",- scheduled=False,- )+ def handle(self, *args: Any, **options: Any) -> None:+ llmindex_index(+ rebuild=options["command"] == "rebuild",+ scheduled=False,+ iter_wrapper=lambda docs: self.track(+ docs,+ description="Indexing documents...",+ ),+ )diff --git a/src/documents/tasks.py b/src/documents/tasks.pyindex c6a158334..1c4ce2d8f 100644--- a/src/documents/tasks.py+++ b/src/documents/tasks.py@@ -4,11 +4,13 @@ import loggingimport shutilimport uuidimport zipfile+from collections.abc import Callable+from collections.abc import Iterablefrom pathlib import Pathfrom tempfile import TemporaryDirectoryfrom tempfile import mkstemp+from typing import TypeVar-import tqdmfrom celery import Taskfrom celery import shared_taskfrom celery import states@@ -66,11 +68,19 @@ from paperless_ai.indexing import llm_index_add_or_update_documentfrom paperless_ai.indexing import llm_index_remove_documentfrom paperless_ai.indexing import update_llm_index+_T = TypeVar("_T")+IterWrapper = Callable[[Iterable[_T]], Iterable[_T]]++if settings.AUDIT_LOG_ENABLED:from auditlog.models import LogEntrylogger = logging.getLogger("paperless.tasks")+def _identity(iterable: Iterable[_T]) -> Iterable[_T]:+ return iterable++@shared_taskdef index_optimize() -> None:ix = index.open_index()@@ -78,13 +88,13 @@ def index_optimize() -> None:writer.commit(optimize=True)-def index_reindex(*, progress_bar_disable=False) -> None:+def index_reindex(*, iter_wrapper: IterWrapper[Document] = _identity) -> None:documents = Document.objects.all()ix = index.open_index(recreate=True)with AsyncWriter(ix) as writer:- for document in tqdm.tqdm(documents, disable=progress_bar_disable):+ for document in iter_wrapper(documents):index.update_document(writer, document)@@ -265,7 +275,6 @@ def bulk_update_documents(document_ids) -> None:ai_config = AIConfig()if ai_config.llm_index_enabled:update_llm_index(- progress_bar_disable=True,rebuild=False,)@@ -606,7 +615,7 @@ def update_document_parent_tags(tag: Tag, new_parent: Tag) -> None:@shared_taskdef llmindex_index(*,- progress_bar_disable=True,+ iter_wrapper: IterWrapper[Document] = _identity,rebuild=False,scheduled=True,auto=False,@@ -629,7 +638,7 @@ def llmindex_index(try:result = update_llm_index(- progress_bar_disable=progress_bar_disable,+ iter_wrapper=iter_wrapper,rebuild=rebuild,)task.status = states.SUCCESSdiff --git a/src/paperless/views.py b/src/paperless/views.pyindex f9aa68297..05a0997fb 100644--- a/src/paperless/views.py+++ b/src/paperless/views.py@@ -378,7 +378,6 @@ class ApplicationConfigurationViewSet(ModelViewSet):):# AI index was just enabled and vector store file does not existllmindex_index.delay(- progress_bar_disable=True,rebuild=True,scheduled=False,auto=True,diff --git a/src/paperless_ai/indexing.py b/src/paperless_ai/indexing.pyindex 654c56f3b..53e4a9796 100644--- a/src/paperless_ai/indexing.py+++ b/src/paperless_ai/indexing.py@@ -1,11 +1,13 @@import loggingimport shutil+from collections.abc import Callable+from collections.abc import Iterablefrom datetime import timedeltafrom pathlib import Path+from typing import TypeVarimport faissimport llama_index.core.settings as llama_settings-import tqdmfrom celery import statesfrom django.conf import settingsfrom django.utils import timezone@@ -29,6 +31,14 @@ from paperless_ai.embedding import build_llm_index_textfrom paperless_ai.embedding import get_embedding_dimfrom paperless_ai.embedding import get_embedding_model+_T = TypeVar("_T")+IterWrapper = Callable[[Iterable[_T]], Iterable[_T]]+++def _identity(iterable: Iterable[_T]) -> Iterable[_T]:+ return iterable++logger = logging.getLogger("paperless_ai.indexing")@@ -156,7 +166,11 @@ def vector_store_file_exists():return Path(settings.LLM_INDEX_DIR / "default__vector_store.json").exists()-def update_llm_index(*, progress_bar_disable=False, rebuild=False) -> str:+def update_llm_index(+ *,+ iter_wrapper: IterWrapper[Document] = _identity,+ rebuild=False,+) -> str:"""Rebuild or update the LLM index."""@@ -176,7 +190,7 @@ def update_llm_index(*, progress_bar_disable=False, rebuild=False) -> str:embed_model = get_embedding_model()llama_settings.Settings.embed_model = embed_modelstorage_context = get_or_create_storage_context(rebuild=True)- for document in tqdm.tqdm(documents, disable=progress_bar_disable):+ for document in iter_wrapper(documents):document_nodes = build_document_node(document)nodes.extend(document_nodes)@@ -184,7 +198,7 @@ def update_llm_index(*, progress_bar_disable=False, rebuild=False) -> str:nodes=nodes,storage_context=storage_context,embed_model=embed_model,- show_progress=not progress_bar_disable,+ show_progress=False,)msg = "LLM index rebuilt successfully."else:@@ -196,7 +210,7 @@ def update_llm_index(*, progress_bar_disable=False, rebuild=False) -> str:for node in index.docstore.get_nodes(all_node_ids)}- for document in tqdm.tqdm(documents, disable=progress_bar_disable):+ for document in iter_wrapper(documents):doc_id = str(document.id)document_modified = document.modified.isoformat()