Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: small fixes #181

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build-gpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: Build docker GPU and push ECR

on:
push:
tags:
- "v*"
branches: [main]

env:
AWS_REGION: eu-west-1
Expand Down
17 changes: 9 additions & 8 deletions libs/megaparse/src/megaparse/parser/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
logger = logging.getLogger("megaparse")


def get_strategy_page(
page: PdfPage,
threshold=0.2,
) -> StrategyEnum:
def get_strategy_page(page: PdfPage, threshold_image_page: float) -> StrategyEnum:
total_page_area = page.get_width() * page.get_height()
total_image_area = 0
images_coords = []
Expand Down Expand Up @@ -43,20 +40,24 @@ def get_strategy_page(
# Get the total area of the images
total_image_area = np.sum(canva)

if total_image_area / total_page_area > threshold:
if total_image_area / total_page_area > threshold_image_page:
return StrategyEnum.HI_RES
return StrategyEnum.FAST


def determine_strategy(file: str | Path | bytes | BinaryIO) -> StrategyEnum:
def determine_strategy(
file: str | Path | bytes | BinaryIO,
threshold_pages_ocr: float = 0.2,
threshold_image_page: float = 0.4,
) -> StrategyEnum:
logger.info("Determining strategy...")
need_ocr = 0
document = pdfium.PdfDocument(file)
for page in document:
strategy = get_strategy_page(page)
strategy = get_strategy_page(page, threshold_image_page=threshold_image_page)
need_ocr += strategy == StrategyEnum.HI_RES

doc_need_ocr = (need_ocr / len(document)) > 0.2
doc_need_ocr = (need_ocr / len(document)) > threshold_pages_ocr
document.close()

if doc_need_ocr:
Expand Down
Binary file added libs/megaparse/tests/pdf/mlbook.pdf
Binary file not shown.
4 changes: 3 additions & 1 deletion libs/megaparse/tests/pdf/test_detect_ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@

def test_strategy_all():
pdf = "/Users/amine/Downloads/RAG Corporate 2024 016.pdf"
strategy = determine_strategy(pdf)
strategy = determine_strategy(
pdf, threshold_pages_ocr=0.2, threshold_image_page=0.3
)
assert strategy == StrategyEnum.HI_RES
28 changes: 28 additions & 0 deletions libs/megaparse/tests/pdf/test_unstructured_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from pathlib import Path

import pypdfium2 as pdfium
import pytest
from megaparse.parser.unstructured_parser import UnstructuredParser
from megaparse_sdk.schema.parser_config import StrategyEnum


@pytest.mark.asyncio
async def test_unstructured_parser():
# scanned pdf
p = Path("./tests/pdf/mlbook.pdf")
processor = UnstructuredParser(strategy=StrategyEnum.FAST)
result = await processor.convert(file_path=p)
assert len(result) > 0


def test_pdfium():
# scanned pdf
p = Path("./tests/pdf/mlbook.pdf")
document = pdfium.PdfDocument(p)

objs = []
for page in document:
for obj in page.get_objects():
objs.append(obj)

document.close()
Loading