Upload pfd2png_thg.py
Browse files123456789101112131415161718192021222324252627282930313233import tempfile
from pathlib import Path
import fitz # pymupdf
from datasets import Dataset, Image
from huggingface_hub import HfApi, hf_hub_download
REPO = "divinity-library/thg-01"
TARGET = "divinity-library/thg-02"
MAX_PDFS = None # how many PDFs to process
MAX_PAGES = None # pages per PDF, None for all
api = HfApi()
pdf_files = sorted(
f for f in api.list_repo_files(REPO, repo_type="dataset")
if f.lower().endswith(".pdf") and f.startswith("train/")
)[:MAX_PDFS]
out_dir = Path(tempfile.mkdtemp())
rows = []
for name in pdf_files:
pdf_path = Path(hf_hub_download(REPO, name, repo_type="dataset"))
doc = fitz.open(pdf_path)
for i, page in enumerate(doc):
if MAX_PAGES and i >= MAX_PAGES:
break
img_path = out_dir / f"{Path(name).stem}_{i:04d}.png"
page.get_pixmap(dpi=200).save(img_path)
rows.append({"image": str(img_path), "source": name, "page": i + 1})
ds = Dataset.from_list(rows).cast_column("image", Image())
ds.push_to_hub(TARGET, private=True)pfd2png_thg.pyAdded+33