Download ocrpages2ocrdocs_thg.py from divinity-library/scripts: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/datasets/divinity-library/scripts/resolve/main/ocrpages2ocrdocs_thg.py
- Command line
-
hf download hf://datasets/divinity-library/scripts/ocrpages2ocrdocs_thg.py
-
curl -L -o ocrpages2ocrdocs_thg.py https://huggingface.co/datasets/divinity-library/scripts/resolve/main/ocrpages2ocrdocs_thg.py
1.09 kB
| 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) |