-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c3d8b1d
commit 5f6079f
Showing
6 changed files
with
154 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: Integration test with benchmark | ||
|
||
on: [push] | ||
|
||
env: | ||
TESSDATA_PREFIX: "/usr/share/tesseract-ocr/5/tessdata" | ||
TORCH_DEVICE: "cpu" | ||
OCR_ENGINE: "tesseract" # So we don't have to install ghostscript, which takes a long time | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python 3.12 | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: 3.12 | ||
- name: Install system dependencies | ||
run: cat scripts/install/apt-requirements.txt | xargs sudo apt-get install -y | ||
- name: Install tesseract 5 | ||
run: bash scripts/install/tesseract_5_install.sh | ||
- name: Install python dependencies | ||
run: | | ||
pip install poetry | ||
poetry install | ||
- name: Download benchmark data | ||
run: | | ||
wget https://drive.google.com/uc?export=download&id=1ktVDYPEeyHlKLaF56FnHjI5VjVnYa1xL -O benchmark_data.zip | ||
unzip benchmark_data.zip | ||
- name: Run benchmark test | ||
run: | | ||
poetry run python benchmark.py benchmark_data/pdfs benchmark_data/references report.json | ||
poetry run python scripts/verify_benchmark_scores.py report.json | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import base64 | ||
import json | ||
import os | ||
import zlib | ||
from typing import List | ||
|
||
from marker.schema import Page | ||
from marker.settings import settings | ||
from PIL import Image | ||
import io | ||
|
||
|
||
def dump_nougat_debug_data(doc, images, converted_spans): | ||
if not settings.DEBUG or not settings.DEBUG_DATA_FOLDER: | ||
return | ||
|
||
# We attempted one conversion per image | ||
assert len(converted_spans) == len(images) | ||
|
||
data_lines = [] | ||
for idx, (image, converted_span) in enumerate(zip(images, converted_spans)): | ||
if converted_span is None: | ||
continue | ||
# Image is a BytesIO object | ||
pil_image = Image.open(image) | ||
img_bytes = io.BytesIO() | ||
pil_image.save(img_bytes, format="WEBP", lossless=True) | ||
b64_image = base64.b64encode(img_bytes.getvalue()).decode("utf-8") | ||
data_lines.append({ | ||
"image": b64_image, | ||
"text": converted_span.text, | ||
"bbox": converted_span.bbox | ||
}) | ||
|
||
# Remove extension from doc name | ||
doc_base = os.path.basename(doc.name).rsplit(".", 1)[0] | ||
|
||
debug_file = os.path.join(settings.DEBUG_DATA_FOLDER, f"{doc_base}_equations.json") | ||
with open(debug_file, "w+") as f: | ||
json.dump(data_lines, f, indent=4) | ||
|
||
|
||
def dump_bbox_debug_data(doc, blocks: List[Page]): | ||
if not settings.DEBUG or not settings.DEBUG_DATA_FOLDER: | ||
return | ||
|
||
# Remove extension from doc name | ||
doc_base = os.path.basename(doc.name).rsplit(".", 1)[0] | ||
|
||
debug_file = os.path.join(settings.DEBUG_DATA_FOLDER, f"{doc_base}_bbox.json") | ||
debug_data = [] | ||
for idx, page_blocks in enumerate(blocks): | ||
page = doc[idx] | ||
|
||
pix = page.get_pixmap(dpi=settings.NOUGAT_DPI, annots=False, clip=page_blocks.bbox) | ||
png = pix.pil_tobytes(format="PNG") | ||
png_image = Image.open(io.BytesIO(png)) | ||
width, height = png_image.size | ||
max_dimension = 6000 | ||
if width > max_dimension or height > max_dimension: | ||
scaling_factor = min(max_dimension / width, max_dimension / height) | ||
png_image = png_image.resize((int(width * scaling_factor), int(height * scaling_factor)), Image.ANTIALIAS) | ||
|
||
img_bytes = io.BytesIO() | ||
png_image.save(img_bytes, format="WEBP", lossless=True, quality=100) | ||
b64_image = base64.b64encode(img_bytes.getvalue()).decode("utf-8") | ||
|
||
page_data = page_blocks.model_dump() | ||
page_data["image"] = b64_image | ||
debug_data.append(page_data) | ||
|
||
with open(debug_file, "w+") as f: | ||
json.dump(debug_data, f, indent=4) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import json | ||
import argparse | ||
|
||
|
||
def verify_scores(file_path): | ||
with open(file_path, 'r') as file: | ||
data = json.load(file) | ||
|
||
multicolcnn_score = data["marker"]["files"]["multicolcnn.pdf"]["score"] | ||
switch_trans_score = data["marker"]["files"]["switch_trans.pdf"]["score"] | ||
|
||
if multicolcnn_score <= 0.4 or switch_trans_score <= 0.4: | ||
raise ValueError("One or more scores are below the required threshold of 0.4") | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="Verify benchmark scores") | ||
parser.add_argument("file_path", type=str, help="Path to the json file") | ||
args = parser.parse_args() | ||
verify_scores(args.file_path) |