Skip to content

Build AI methods with your coding agent, then run them anywhere: as an MCP for chatbots, as a webapp for people, or via API for your software.

Feature/pdf text (#273)

Louis Choquelb3c5cd5f5f78
diff --git a/.vscode/launch.json b/.vscode/launch.json
index 5b9f98dec..e7753f589 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -16,15 +16,15 @@
"justMyCode": false
},
{
- "name": "Debug tests",
+ "name": "Debug test_pipe_ocr_pdf",
"type": "debugpy",
"request": "launch",
"module": "pytest",
"args": [
"-s",
"-vv",
- "-m inference",
- "-k test_pipe_run"
+ "-m ocr",
+ "-k test_pipe_ocr_pdf"
],
"console": "integratedTerminal"
},
diff --git a/pipelex/cogt/ocr/ocr_handle.py b/pipelex/cogt/ocr/ocr_handle.py
index d6f789cdc..f949fe6aa 100644
--- a/pipelex/cogt/ocr/ocr_handle.py
+++ b/pipelex/cogt/ocr/ocr_handle.py
@@ -2,4 +2,5 @@ from pipelex.types import StrEnum
class OcrHandle(StrEnum):
+ BASIC_OCR = "basic/pypdfium2"
MISTRAL_OCR = "mistral/mistral-ocr-latest"
diff --git a/pipelex/cogt/ocr/ocr_platform.py b/pipelex/cogt/ocr/ocr_platform.py
index b80ff9506..5a6baa1f2 100644
--- a/pipelex/cogt/ocr/ocr_platform.py
+++ b/pipelex/cogt/ocr/ocr_platform.py
@@ -2,4 +2,5 @@ from pipelex.types import StrEnum
class OcrPlatform(StrEnum):
+ BASIC = "basic"
MISTRAL = "mistral"
diff --git a/pipelex/cogt/ocr/ocr_worker_factory.py b/pipelex/cogt/ocr/ocr_worker_factory.py
index 4c96a1602..eea412ec3 100644
--- a/pipelex/cogt/ocr/ocr_worker_factory.py
+++ b/pipelex/cogt/ocr/ocr_worker_factory.py
@@ -44,5 +44,13 @@ class OcrWorkerFactory:
ocr_engine=ocr_engine,
reporting_delegate=reporting_delegate,
)
+ case OcrPlatform.BASIC:
+ from pipelex.plugins.pypdfium2.pypdfium2_worker import Pypdfium2Worker
+
+ ocr_worker = Pypdfium2Worker(
+ sdk_instance=None,
+ ocr_engine=ocr_engine,
+ reporting_delegate=reporting_delegate,
+ )
return ocr_worker
diff --git a/pipelex/core/interpreter.py b/pipelex/core/interpreter.py
index 4d8bae7fc..ba20c7049 100644
--- a/pipelex/core/interpreter.py
+++ b/pipelex/core/interpreter.py
@@ -1,5 +1,5 @@
from pathlib import Path
-from typing import Any, Dict, Mapping, Optional, Union
+from typing import Any, Dict, Mapping, Optional, Union, cast
import toml
from pydantic import BaseModel, model_validator
@@ -335,6 +335,7 @@ class PipelexInterpreter(BaseModel):
PipelexInterpreter.add_inputs_to_lines_if_exist(lines, pipe.inputs)
lines.append(f'output = "{PipelexInterpreter.escape_plx_string(pipe.output_concept_string_or_concept_code)}"')
+ lines.append(f'ocr_handle = "{pipe.ocr_handle.value}"')
return "\n".join(lines)
@@ -744,6 +745,9 @@ class PipelexInterpreter(BaseModel):
# Add output
result["output"] = pipe.output_concept_string_or_concept_code
+ # Add required fields
+ result["ocr_handle"] = pipe.ocr_handle
+
# Add optional fields
if pipe.ocr_platform:
result["ocr_platform"] = pipe.ocr_platform
@@ -975,7 +979,7 @@ class PipelexInterpreter(BaseModel):
if isinstance(value, dict):
# Nested dict (like InputRequirementBlueprint)
nested_parts: list[str] = []
- nested_dict: Dict[str, Any] = value
+ nested_dict: Dict[str, Any] = cast(Dict[str, Any], value)
for nested_key, nested_value in nested_dict.items():
if isinstance(nested_value, str):
nested_parts.append(f'{nested_key} = "{PipelexInterpreter.escape_plx_string(nested_value)}"')
@@ -1001,7 +1005,7 @@ class PipelexInterpreter(BaseModel):
for key, value in inputs_dict.items():
if isinstance(value, dict):
nested_parts: list[str] = []
- nested_dict: Dict[str, Any] = value
+ nested_dict: Dict[str, Any] = cast(Dict[str, Any], value)
for nested_key, nested_value in nested_dict.items():
if isinstance(nested_value, str):
nested_parts.append(f'{nested_key} = "{PipelexInterpreter.escape_plx_string(nested_value)}"')
diff --git a/pipelex/core/stuffs/stuff_content.py b/pipelex/core/stuffs/stuff_content.py
index 0449d6c4c..43f54ada9 100644
--- a/pipelex/core/stuffs/stuff_content.py
+++ b/pipelex/core/stuffs/stuff_content.py
@@ -385,7 +385,7 @@ class StructuredContent(StuffContent):
def rendered_html(self) -> str:
dict_dump = clean_model_to_dict(obj=self)
- html: str = json2html.convert( # pyright: ignore[reportAssignmentType]
+ html: str = json2html.convert( # pyright: ignore[reportAssignmentType, reportUnknownVariableType]
json=dict_dump, # pyright: ignore[reportArgumentType]
clubbing=True,
table_attributes="",
@@ -451,7 +451,7 @@ class ListContent(StuffContent, Generic[StuffContentType]):
def rendered_html(self) -> str:
list_dump = [item.smart_dump() for item in self.items]
- html: str = json2html.convert( # pyright: ignore[reportAssignmentType]
+ html: str = json2html.convert( # pyright: ignore[reportAssignmentType, reportUnknownVariableType]
json=list_dump, # pyright: ignore[reportArgumentType]
clubbing=True,
table_attributes="",
diff --git a/pipelex/libraries/pipelines/documents.plx b/pipelex/libraries/pipelines/documents.plx
index c3826d8ad..6f38360ca 100644
--- a/pipelex/libraries/pipelines/documents.plx
+++ b/pipelex/libraries/pipelines/documents.plx
@@ -17,6 +17,7 @@ inputs = { ocr_input = "PDF" }
output = "Page"
page_images = true
page_views = false
+ocr_handle = "mistral/mistral-ocr-latest"
[pipe.ocr_page_contents_and_views_from_pdf]
type = "PipeOcr"
@@ -25,4 +26,5 @@ inputs = { ocr_input = "PDF" }
output = "Page"
page_images = true
page_views = true
+ocr_handle = "mistral/mistral-ocr-latest"
diff --git a/pipelex/pipe_operators/ocr/pipe_ocr.py b/pipelex/pipe_operators/ocr/pipe_ocr.py
index d99feb6fc..4b15742c7 100644
--- a/pipelex/pipe_operators/ocr/pipe_ocr.py
+++ b/pipelex/pipe_operators/ocr/pipe_ocr.py
@@ -6,7 +6,6 @@ from typing_extensions import Self, override
from pipelex import log
from pipelex.cogt.content_generation.content_generator_dry import ContentGeneratorDry
from pipelex.cogt.content_generation.content_generator_protocol import ContentGeneratorProtocol
-from pipelex.cogt.ocr.ocr_engine import OcrEngine
from pipelex.cogt.ocr.ocr_handle import OcrHandle
from pipelex.cogt.ocr.ocr_input import OcrInput
from pipelex.cogt.ocr.ocr_job_components import OcrJobConfig, OcrJobParams
@@ -42,7 +41,7 @@ PIPE_OCR_INPUT_NAME = "ocr_input"
class PipeOcr(PipeOperator):
- ocr_engine: Optional[OcrEngine] = None
+ ocr_handle: OcrHandle
should_caption_images: bool
should_include_images: bool
should_include_page_views: bool
@@ -167,7 +166,6 @@ class PipeOcr(PipeOperator):
else:
raise PipeDefinitionError("PipeOcr should have a non-None image_stuff_name or pdf_stuff_name")
- ocr_handle = OcrHandle.MISTRAL_OCR
ocr_job_params = OcrJobParams(
should_include_images=self.should_include_images,
should_caption_images=self.should_caption_images,
@@ -180,7 +178,7 @@ class PipeOcr(PipeOperator):
)
ocr_output = await content_generator.make_ocr_extract_pages(
ocr_input=ocr_input,
- ocr_handle=ocr_handle,
+ ocr_handle=self.ocr_handle,
job_metadata=job_metadata,
ocr_job_params=ocr_job_params,
ocr_job_config=OcrJobConfig(),
diff --git a/pipelex/pipe_operators/ocr/pipe_ocr_blueprint.py b/pipelex/pipe_operators/ocr/pipe_ocr_blueprint.py
index 4dd378c6a..a9865946f 100644
--- a/pipelex/pipe_operators/ocr/pipe_ocr_blueprint.py
+++ b/pipelex/pipe_operators/ocr/pipe_ocr_blueprint.py
@@ -1,11 +1,13 @@
from typing import Literal, Optional
+from pipelex.cogt.ocr.ocr_handle import OcrHandle
from pipelex.cogt.ocr.ocr_platform import OcrPlatform
from pipelex.core.pipes.pipe_blueprint import PipeBlueprint
class PipeOcrBlueprint(PipeBlueprint):
type: Literal["PipeOcr"] = "PipeOcr"
+ ocr_handle: OcrHandle
ocr_platform: Optional[OcrPlatform] = None
page_images: Optional[bool] = None
page_image_captions: Optional[bool] = None
diff --git a/pipelex/pipe_operators/ocr/pipe_ocr_factory.py b/pipelex/pipe_operators/ocr/pipe_ocr_factory.py
index a2be46d49..158bfe64d 100644
--- a/pipelex/pipe_operators/ocr/pipe_ocr_factory.py
+++ b/pipelex/pipe_operators/ocr/pipe_ocr_factory.py
@@ -2,9 +2,6 @@ from typing import List, Optional
from typing_extensions import override
-from pipelex.cogt.ocr.ocr_engine_factory import OcrEngineFactory
-from pipelex.cogt.ocr.ocr_handle import OcrHandle
-from pipelex.cogt.ocr.ocr_platform import OcrPlatform
from pipelex.config import get_config
from pipelex.core.concepts.concept_factory import ConceptFactory
from pipelex.core.pipes.pipe_factory import PipeFactoryProtocol
@@ -24,11 +21,6 @@ class PipeOcrFactory(PipeFactoryProtocol[PipeOcrBlueprint, PipeOcr]):
blueprint: PipeOcrBlueprint,
concept_codes_from_the_same_domain: Optional[List[str]] = None,
) -> PipeOcr:
- ocr_platform = blueprint.ocr_platform or OcrPlatform.MISTRAL
- match ocr_platform:
- case OcrPlatform.MISTRAL:
- ocr_engine = OcrEngineFactory.make_ocr_engine(ocr_handle=OcrHandle.MISTRAL_OCR)
-
output_domain_and_code = ConceptFactory.make_domain_and_concept_code_from_concept_string_or_concept_code(
domain=domain,
concept_string_or_concept_code=blueprint.output_concept_string_or_concept_code,
@@ -39,7 +31,6 @@ class PipeOcrFactory(PipeFactoryProtocol[PipeOcrBlueprint, PipeOcr]):
domain=domain,
code=pipe_code,
definition=blueprint.definition,
- ocr_engine=ocr_engine,
output=get_concept_provider().get_required_concept(
concept_string=ConceptFactory.construct_concept_string_with_domain(
domain=output_domain_and_code.domain, concept_code=output_domain_and_code.concept_code
@@ -48,6 +39,7 @@ class PipeOcrFactory(PipeFactoryProtocol[PipeOcrBlueprint, PipeOcr]):
inputs=PipeInputSpecFactory.make_from_blueprint(
domain=domain, blueprint=blueprint.inputs or {}, concept_codes_from_the_same_domain=concept_codes_from_the_same_domain
),
+ ocr_handle=blueprint.ocr_handle,
should_include_images=blueprint.page_images or False,
should_caption_images=blueprint.page_image_captions or False,
should_include_page_views=blueprint.page_views or False,
diff --git a/pipelex/plugins/plugin_sdk_registry.py b/pipelex/plugins/plugin_sdk_registry.py
index 59619aa32..6b350d4cb 100644
--- a/pipelex/plugins/plugin_sdk_registry.py
+++ b/pipelex/plugins/plugin_sdk_registry.py
@@ -14,6 +14,7 @@ class PluginSdkHandle(StrEnum):
ANTHROPIC_SDK = "anthropic_sdk"
BEDROCK_ANTHROPIC_SDK = "bedrock_anthropic_sdk"
MISTRAL_SDK = "mistral_sdk"
+ BASIC_SDK = "basic_sdk"
BEDROCK_SDK = "bedrock_sdk"
PERPLEXITY_OPENAI_SDK = "perplexity_openai_sdk"
VERTEXAI_OPENAI_SDK = "vertexai_openai_sdk"
@@ -50,6 +51,8 @@ class PluginSdkHandle(StrEnum):
match ocr_platform:
case OcrPlatform.MISTRAL:
return PluginSdkHandle.MISTRAL_SDK
+ case OcrPlatform.BASIC:
+ return PluginSdkHandle.BASIC_SDK
@staticmethod
def get_for_imgg_engine(imgg_platform: ImggPlatform) -> "PluginSdkHandle":
diff --git a/pipelex/plugins/pypdfium2/pypdfium2_worker.py b/pipelex/plugins/pypdfium2/pypdfium2_worker.py
new file mode 100644
index 000000000..1193c4c3f
--- /dev/null
+++ b/pipelex/plugins/pypdfium2/pypdfium2_worker.py
@@ -0,0 +1,76 @@
+from typing import Any, Dict, Optional
+
+from typing_extensions import override
+
+from pipelex.cogt.ocr.ocr_engine import OcrEngine
+from pipelex.cogt.ocr.ocr_input import OcrInputError
+from pipelex.cogt.ocr.ocr_job import OcrJob
+from pipelex.cogt.ocr.ocr_output import OcrOutput, Page
+from pipelex.cogt.ocr.ocr_worker_abstract import OcrWorkerAbstract
+from pipelex.reporting.reporting_protocol import ReportingProtocol
+from pipelex.tools.misc.path_utils import clarify_path_or_url
+from pipelex.tools.pdf.pypdfium2_renderer import pypdfium2_renderer
+
+
+class Pypdfium2Worker(OcrWorkerAbstract):
+ def __init__(
+ self,
+ sdk_instance: Any,
+ ocr_engine: OcrEngine,
+ reporting_delegate: Optional[ReportingProtocol] = None,
+ ):
+ super().__init__(ocr_engine=ocr_engine, reporting_delegate=reporting_delegate)
+
+ @override
+ async def _ocr_extract_pages(
+ self,
+ ocr_job: OcrJob,
+ ) -> OcrOutput:
+ if ocr_job.ocr_input.image_uri:
+ raise NotImplementedError("Pypdfium2 only extracts text from PDFs, not images")
+
+ elif pdf_uri := ocr_job.ocr_input.pdf_uri:
+ pdf_path, pdf_url = clarify_path_or_url(path_or_uri=pdf_uri) # pyright: ignore
+ ocr_output: OcrOutput
+ if pdf_url:
+ ocr_output = await self.extract_from_pdf_url(
+ pdf_url=pdf_url,
+ )
+ else: # pdf_path must be provided based on validation
+ assert pdf_path is not None # Type narrowing for mypy
+ ocr_output = await self.extract_from_pdf_file(
+ pdf_path=pdf_path,
+ )
+ else:
+ raise OcrInputError("No PDF URI provided in OcrJob")
+ return ocr_output
+
+ async def extract_from_pdf_url(
+ self,
+ pdf_url: str,
+ ) -> OcrOutput:
+ page_texts = await pypdfium2_renderer.get_text_from_pdf_pages_from_uri(pdf_uri=pdf_url)
+ pages: Dict[int, Page] = {}
+ for page_index, page_text in enumerate(page_texts):
+ pages[page_index + 1] = Page(
+ text=page_text,
+ )
+ ocr_output = OcrOutput(
+ pages=pages,
+ )
+ return ocr_output
+
+ async def extract_from_pdf_file(
+ self,
+ pdf_path: str,
+ ) -> OcrOutput:
+ page_texts = await pypdfium2_renderer.get_text_from_pdf_pages(pdf_input=pdf_path)
+ pages: Dict[int, Page] = {}
+ for page_index, page_text in enumerate(page_texts):
+ pages[page_index + 1] = Page(
+ text=page_text,
+ )
+ ocr_output = OcrOutput(
+ pages=pages,
+ )
+ return ocr_output
diff --git a/pipelex/tools/pdf/pypdfium2_renderer.py b/pipelex/tools/pdf/pypdfium2_renderer.py
index f6ec1bdf6..4df3eb70e 100644
--- a/pipelex/tools/pdf/pypdfium2_renderer.py
+++ b/pipelex/tools/pdf/pypdfium2_renderer.py
@@ -37,6 +37,7 @@ class PyPdfium2Renderer:
_pdfium_lock: asyncio.Lock = asyncio.Lock() # shared per process
# ---- internal blocking helper ------------------------------------
+ # TODO: Needs UT
@staticmethod
def _render_pdf_pages_sync(pdf_input: PdfInput, scale: float) -> List[Image.Image]:
pdf_doc = pdfium.PdfDocument(pdf_input)
@@ -56,6 +57,19 @@ class PyPdfium2Renderer:
pdf_doc.close()
return images
+ # TODO: Needs UT
+ @staticmethod
+ def _get_text_from_pdf_pages_sync(pdf_input: PdfInput) -> List[str]:
+ pdf_doc = pdfium.PdfDocument(pdf_input)
+ texts: List[str] = []
+ for index in range(len(pdf_doc)):
+ page = pdf_doc[index]
+ text = page.get_textpage().get_text_bounded() # pyright: ignore[reportUnknownMemberType]
+ texts.append(text) # pyright: ignore[reportUnknownArgumentType]
+ page.close()
+ pdf_doc.close()
+ return texts
+
# ---- public async façade -----------------------------------------
async def render_pdf_pages(self, pdf_input: PdfInput, dpi: int) -> List[Image.Image]:
scale = dpi / PDFIUM2_REFERENCE_DPI
@@ -63,6 +77,11 @@ class PyPdfium2Renderer:
async with self._pdfium_lock:
return await asyncio.to_thread(self._render_pdf_pages_sync, pdf_input, scale)
+ async def get_text_from_pdf_pages(self, pdf_input: PdfInput) -> List[str]:
+ """Extract text from all pages of a PDF."""
+ async with self._pdfium_lock:
+ return await asyncio.to_thread(self._get_text_from_pdf_pages_sync, pdf_input)
+
async def render_pdf_pages_from_uri(self, pdf_uri: str, dpi: int) -> List[Image.Image]:
pdf_path, pdf_url = clarify_path_or_url(path_or_uri=pdf_uri) # pyright: ignore
if pdf_url:
@@ -73,5 +92,16 @@ class PyPdfium2Renderer:
else:
raise PyPdfium2RendererError(f"Invalid PDF URI: {pdf_uri}")
+ async def get_text_from_pdf_pages_from_uri(self, pdf_uri: str) -> List[str]:
+ """Extract text from all pages of a PDF from URI."""
+ pdf_path, pdf_url = clarify_path_or_url(path_or_uri=pdf_uri) # pyright: ignore
+ if pdf_url:
+ pdf_bytes = await fetch_file_from_url_httpx_async(url=pdf_url)
+ return await self.get_text_from_pdf_pages(pdf_input=pdf_bytes)
+ elif pdf_path:
+ return await self.get_text_from_pdf_pages(pdf_input=pdf_path)
+ else:
+ raise PyPdfium2RendererError(f"Invalid PDF URI: {pdf_uri}")
+
pypdfium2_renderer = PyPdfium2Renderer()
diff --git a/tests/integration/pipelex/cogt/test_ocr.py b/tests/integration/pipelex/cogt/test_ocr.py
index 74bc9d478..e2150a8d6 100644
--- a/tests/integration/pipelex/cogt/test_ocr.py
+++ b/tests/integration/pipelex/cogt/test_ocr.py
@@ -16,7 +16,7 @@ from tests.cases import ImageTestCases, PDFTestCases
@pytest.mark.asyncio(loop_scope="class")
class TestOcr:
@pytest.mark.parametrize("file_path", PDFTestCases.DOCUMENT_FILE_PATHS)
- async def test_ocr_pdr_path(self, file_path: str):
+ async def test_ocr_pdf_path(self, file_path: str):
ocr_worker = get_ocr_worker(ocr_handle=OcrHandle.MISTRAL_OCR)
ocr_job = OcrJobFactory.make_ocr_job(
ocr_input=OcrInput(pdf_uri=file_path),
@@ -27,7 +27,7 @@ class TestOcr:
assert ocr_output.pages
@pytest.mark.parametrize("url", PDFTestCases.DOCUMENT_URLS)
- async def test_ocr_url(self, url: str):
+ async def test_ocr_pdf_url(self, url: str):
ocr_worker = get_ocr_worker(ocr_handle=OcrHandle.MISTRAL_OCR)
ocr_job = OcrJobFactory.make_ocr_job(
ocr_input=OcrInput(pdf_uri=url),
diff --git a/tests/integration/pipelex/conftest.py b/tests/integration/pipelex/conftest.py
index 9ecd372e4..8f93653e2 100644
--- a/tests/integration/pipelex/conftest.py
+++ b/tests/integration/pipelex/conftest.py
@@ -4,6 +4,7 @@ from pipelex.cogt.imgg.imgg_handle import ImggHandle
from pipelex.cogt.llm.llm_job_components import LLMJobParams
from pipelex.cogt.llm.llm_models.llm_family import LLMCreator, LLMFamily
from pipelex.cogt.llm.llm_models.llm_platform import LLMPlatform
+from pipelex.cogt.ocr.ocr_handle import OcrHandle
@pytest.fixture(
@@ -229,3 +230,14 @@ def llm_job_params(request: pytest.FixtureRequest) -> LLMJobParams:
def imgg_handle(request: pytest.FixtureRequest) -> ImggHandle:
assert isinstance(request.param, ImggHandle)
return request.param
+
+
+ params=[
+ OcrHandle.BASIC_OCR,
+ # OcrHandle.MISTRAL_OCR,
+ ]
+)
+def ocr_handle(request: pytest.FixtureRequest) -> OcrHandle:
+ assert isinstance(request.param, OcrHandle)
+ return request.param
diff --git a/tests/integration/pipelex/pipes/operator/pipe_ocr/test_pipe_ocr.py b/tests/integration/pipelex/pipes/operator/pipe_ocr/test_pipe_ocr.py
index f28b05703..64bbf79fe 100644
--- a/tests/integration/pipelex/pipes/operator/pipe_ocr/test_pipe_ocr.py
+++ b/tests/integration/pipelex/pipes/operator/pipe_ocr/test_pipe_ocr.py
@@ -3,6 +3,7 @@ from typing import Any
import pytest
from pipelex import pretty_print
+from pipelex.cogt.ocr.ocr_handle import OcrHandle
from pipelex.core.concepts.concept_blueprint import ConceptBlueprint
from pipelex.core.concepts.concept_factory import ConceptFactory
from pipelex.core.concepts.concept_native import NativeConceptEnum
@@ -42,10 +43,14 @@ class TestPipeOCR:
@pytest.mark.parametrize("image_url", PipeOcrTestCases.PIPE_OCR_IMAGE_TEST_CASES)
async def test_pipe_ocr_image(
self,
+ ocr_handle: OcrHandle,
pipe_run_mode: PipeRunMode,
image_url: str,
setup: Any,
):
+ if ocr_handle == OcrHandle.BASIC_OCR:
+ pytest.skip("Basic OCR is not supported for image processing")
+
pipe_ocr_blueprint = PipeOcrBlueprint(
definition="OCR test for image processing",
inputs={"page_scan": InputRequirementBlueprint(concept=NativeConceptEnum.IMAGE.value)},
@@ -54,6 +59,7 @@ class TestPipeOCR:
page_image_captions=False,
page_views=True,
page_views_dpi=72,
+ ocr_handle=ocr_handle,
)
pipe_job = PipeJobFactory.make_pipe_job(
@@ -77,6 +83,7 @@ class TestPipeOCR:
@pytest.mark.parametrize("pdf_url", PipeOcrTestCases.PIPE_OCR_PDF_TEST_CASES)
async def test_pipe_ocr_pdf(
self,
+ ocr_handle: OcrHandle,
pipe_run_mode: PipeRunMode,
pdf_url: str,
):
@@ -84,6 +91,7 @@ class TestPipeOCR:
definition="OCR test for PDF processing",
inputs={PIPE_OCR_INPUT_NAME: InputRequirementBlueprint(concept=NativeConceptEnum.PDF.value)},
output=NativeConceptEnum.TEXT_AND_IMAGES.value,
+ ocr_handle=ocr_handle,
page_images=True,
page_image_captions=False,
page_views=True,
diff --git a/tests/integration/pipelex/tools/pdf/test_pdf_render.py b/tests/integration/pipelex/tools/pdf/test_pdf_render.py
index 7a37d4312..3e893d34c 100644
--- a/tests/integration/pipelex/tools/pdf/test_pdf_render.py
+++ b/tests/integration/pipelex/tools/pdf/test_pdf_render.py
@@ -1,5 +1,6 @@
import pytest
+from pipelex import pretty_print
from pipelex.tools.pdf.pypdfium2_renderer import pypdfium2_renderer
from tests.cases import PDFTestCases
@@ -10,3 +11,18 @@ class TestPdfRender:
async def test_render_pdf_from_path(self, file_path: str):
images = await pypdfium2_renderer.render_pdf_pages(pdf_input=file_path, dpi=72)
assert len(images) > 0
+
+ # pytest -k test_get_text_from_pdf_pages -s -vv
+ @pytest.mark.parametrize("file_path", PDFTestCases.DOCUMENT_FILE_PATHS)
+ async def test_get_text_from_pdf_pages(
+ self,
+ pytestconfig: pytest.Config,
+ file_path: str,
+ ):
+ texts = await pypdfium2_renderer.get_text_from_pdf_pages(pdf_input=file_path)
+ assert len(texts) > 0
+ assert all(isinstance(text, str) for text in texts)
+ if pytestconfig.get_verbosity() >= 2:
+ full_text = "\n".join(texts)
+ print("\n")
+ pretty_print(full_text, title=f"Full text from pdf '{file_path}'")
diff --git a/tests/unit/pipelex/core/interpreter/test_interpreter_serialize.py b/tests/unit/pipelex/core/interpreter/test_interpreter_serialize.py
index d018e1ad1..edadd04b5 100644
--- a/tests/unit/pipelex/core/interpreter/test_interpreter_serialize.py
+++ b/tests/unit/pipelex/core/interpreter/test_interpreter_serialize.py
@@ -2,6 +2,7 @@
from typing import Any, Dict
+from pipelex.cogt.ocr.ocr_handle import OcrHandle
from pipelex.core.concepts.concept_blueprint import ConceptBlueprint, ConceptStructureBlueprint, ConceptStructureBlueprintFieldType
from pipelex.core.interpreter import PipelexInterpreter
from pipelex.core.pipes.pipe_input_spec_blueprint import InputRequirementBlueprint
@@ -287,10 +288,10 @@ class TestSerializePipeBlueprints:
def test_serialize_ocr_pipe_basic(self):
"""Test serializing basic PipeOcr blueprint."""
- pipe = PipeOcrBlueprint(type="PipeOcr", definition="Extract text from PDF", output="Page")
+ pipe = PipeOcrBlueprint(type="PipeOcr", definition="Extract text from PDF", output="Page", ocr_handle=OcrHandle.BASIC_OCR)
result = PipelexInterpreter.serialize_ocr_pipe(pipe, "test_domain")
- expected = {"type": "PipeOcr", "definition": "Extract text from PDF", "output": "Page"}
+ expected = {"type": "PipeOcr", "definition": "Extract text from PDF", "output": "Page", "ocr_handle": "basic/pypdfium2"}
assert result == expected
def test_serialize_sequence_pipe_basic(self):
diff --git a/tests/unit/pipelex/core/interpreter/test_pipelex_interpreter_ocr_plx.py b/tests/unit/pipelex/core/interpreter/test_pipelex_interpreter_ocr_plx.py
index c59a65692..2a558c7ee 100644
--- a/tests/unit/pipelex/core/interpreter/test_pipelex_interpreter_ocr_plx.py
+++ b/tests/unit/pipelex/core/interpreter/test_pipelex_interpreter_ocr_plx.py
@@ -1,5 +1,6 @@
import pytest
+from pipelex.cogt.ocr.ocr_handle import OcrHandle
from pipelex.core.interpreter import PipelexInterpreter
from pipelex.pipe_operators.ocr.pipe_ocr_blueprint import PipeOcrBlueprint
@@ -17,11 +18,13 @@ class TestPipelexInterpreterOcrPLX:
type="PipeOcr",
definition="Extract text from document",
output="Page",
+ ocr_handle=OcrHandle.BASIC_OCR,
),
"""[pipe.extract_text]
type = "PipeOcr"
definition = "Extract text from document"
-output = "Page\"""",
+output = "Page"
+ocr_handle = "basic/pypdfium2\"""",
),
# OCR pipe with inputs
(
@@ -31,12 +34,14 @@ output = "Page\"""",
definition="Extract text from PDF",
inputs={"ocr_input": "PDF"},
output="Page",
+ ocr_handle=OcrHandle.BASIC_OCR,
),
"""[pipe.extract_with_input]
type = "PipeOcr"
definition = "Extract text from PDF"
inputs = { ocr_input = "PDF" }
-output = "Page\"""",
+output = "Page"
+ocr_handle = "basic/pypdfium2\"""",
),
],
)
diff --git a/tests/unit/pipelex/core/test_data/pipes/operators/ocr/pipe_ocr.py b/tests/unit/pipelex/core/test_data/pipes/operators/ocr/pipe_ocr.py
index a3e4a6454..e4e252c37 100644
--- a/tests/unit/pipelex/core/test_data/pipes/operators/ocr/pipe_ocr.py
+++ b/tests/unit/pipelex/core/test_data/pipes/operators/ocr/pipe_ocr.py
@@ -1,5 +1,6 @@
"""PipeOcr test cases."""
+from pipelex.cogt.ocr.ocr_handle import OcrHandle
from pipelex.core.bundles.pipelex_bundle_blueprint import PipelexBundleBlueprint
from pipelex.core.concepts.concept_native import NativeConceptEnum
from pipelex.pipe_operators.ocr.pipe_ocr_blueprint import PipeOcrBlueprint
@@ -13,6 +14,7 @@ definition = "Domain with OCR pipe"
type = "PipeOcr"
definition = "Extract text from document"
output = "Page"
+ocr_handle = "basic/pypdfium2"
""",
PipelexBundleBlueprint(
domain="test_pipes",
@@ -22,6 +24,7 @@ output = "Page"
type="PipeOcr",
definition="Extract text from document",
output=NativeConceptEnum.PAGE.value,
+ ocr_handle=OcrHandle.BASIC_OCR,
),
},
),