Datasets:
Download src/render_sample.py from SlayerLab/polish-dynaword: direct link, hf CLI and curl.
- Browser
- Download file 2.27 kB
-
https://huggingface.co/datasets/SlayerLab/polish-dynaword/resolve/2ae87973217935bf33958d0e51cc72587ed08071/src/render_sample.py
- Command line
-
hf download hf://datasets/SlayerLab/polish-dynaword@2ae87973217935bf33958d0e51cc72587ed08071/src/render_sample.py
-
curl -L -o render_sample.py https://huggingface.co/datasets/SlayerLab/polish-dynaword/resolve/2ae87973217935bf33958d0e51cc72587ed08071/src/render_sample.py
2.27 kB
| """Render a few parquet rows as readable markdown for a PR description. | |
| Usage: python3 src/render_sample.py data/<key>/<key>.parquet [-n 3] [--max-chars 800] | |
| Each document becomes a fenced block: metadata as `key: value` lines, a blank | |
| line, then the text with real newlines, truncated with `[…]`. | |
| """ | |
| import argparse | |
| import random | |
| import re | |
| import pyarrow.parquet as pq | |
| def sample_rows(path, n, seed=0): | |
| """n rows from distinct random row groups (seeded), so a 2B-token shard isn't read whole.""" | |
| pf = pq.ParquetFile(path) | |
| if pf.num_row_groups == 0 or n <= 0: | |
| return [] | |
| rng = random.Random(seed) | |
| groups = rng.sample(range(pf.num_row_groups), min(n, pf.num_row_groups)) | |
| per_group = -(-n // len(groups)) # ceil: few row groups -> several rows each | |
| rows = [] | |
| for g in sorted(groups): | |
| batch = pf.read_row_group(g).to_pylist() | |
| rows += rng.sample(batch, min(per_group, len(batch))) | |
| return rows[:n] | |
| def truncate(text, max_chars): | |
| text = text.strip() | |
| if len(text) <= max_chars: | |
| return text | |
| cut = text[:max_chars] | |
| space = max(cut.rfind(" "), cut.rfind("\n")) | |
| return (cut[:space] if space > 0 else cut).rstrip() + " […]" | |
| def fmt_value(v): | |
| if isinstance(v, (list, tuple)): | |
| return ", ".join(fmt_value(x) for x in v) | |
| if hasattr(v, "isoformat"): | |
| return v.isoformat() | |
| return str(v) | |
| def render_doc(row, max_chars): | |
| meta = [f"{k}: {fmt_value(v)}" for k, v in row.items() if k != "text"] | |
| body = "\n".join(meta) + "\n\n" + truncate(row.get("text") or "", max_chars) | |
| longest = max((len(m) for m in re.findall(r"`+", body)), default=0) | |
| fence = "`" * max(3, longest + 1) | |
| return f"{fence}\n{body}\n{fence}" | |
| def render(path, n=3, max_chars=800, seed=0): | |
| return "\n\n".join(render_doc(r, max_chars) for r in sample_rows(path, n, seed)) | |
| if __name__ == "__main__": | |
| ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) | |
| ap.add_argument("parquet") | |
| ap.add_argument("-n", type=int, default=3) | |
| ap.add_argument("--max-chars", type=int, default=800) | |
| ap.add_argument("--seed", type=int, default=0) | |
| a = ap.parse_args() | |
| print(render(a.parquet, a.n, a.max_chars, a.seed)) | |