File size: 1,085 Bytes
f076fda | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | from collections import defaultdict
from datasets import Dataset, load_dataset
SOURCE = "divinity-library/thg-02" # page-level dataset with OCR column
TARGET = "divinity-library/thg-03"
OCR_COLUMN = "markdown_glm_ocr"
PAGE_MARKERS = True # insert <!-- page N --> between pages
# Drop the image column so no images are decoded
ds = load_dataset(SOURCE, split="train").select_columns(["source", "page", OCR_COLUMN])
pages = defaultdict(list)
for row in ds:
pages[row["source"]].append((row["page"], row[OCR_COLUMN]))
docs = []
for source, items in sorted(pages.items()):
items.sort(key=lambda x: x[0])
failed = [p for p, text in items if text == "[OCR ERROR]"]
if failed:
print(f"WARNING {source}: OCR failed on pages {failed}")
parts = [
(f"<!-- page {p} -->\n" if PAGE_MARKERS else "") + text
for p, text in items
]
docs.append({
"source": source,
"num_pages": len(items),
"markdown": "\n\n".join(parts),
})
Dataset.from_list(docs).push_to_hub(TARGET, private=True) |