pass5 sha1 script 4c343f57
Browse files
code/pass5/4c343f57/abeja_sha1.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""abeja-cc-ja の各シャードの content SHA-1 を計算して reports/<prefix>/<dir>/<shard>.sha1.parquet に置く(完全一致重複の計画用)。"""
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
import argparse, hashlib, json, os, time
|
| 4 |
+
from concurrent.futures import ProcessPoolExecutor
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def one(task: dict) -> dict:
|
| 9 |
+
import pyarrow as pa, pyarrow.parquet as pq
|
| 10 |
+
from huggingface_hub import hf_hub_download
|
| 11 |
+
t0 = time.perf_counter(); sh = task["shard"]
|
| 12 |
+
for k in range(6):
|
| 13 |
+
try: local = hf_hub_download(task["repo"], task["path"], repo_type="dataset", token=os.environ.get("HF_TOKEN"), local_dir=task["staging"]); break
|
| 14 |
+
except Exception as e: # noqa: BLE001
|
| 15 |
+
if k == 5: return {"shard": sh, "error": repr(e)}
|
| 16 |
+
time.sleep(20 * (k + 1))
|
| 17 |
+
pf = pq.ParquetFile(local); shas = []
|
| 18 |
+
for b in pf.iter_batches(batch_size=4096, columns=["content"]):
|
| 19 |
+
shas.extend(hashlib.sha1((x or "").encode("utf-8")).digest() for x in b.column("content").to_pylist())
|
| 20 |
+
out = Path(task["out"]) / f"{sh}.sha1.parquet"; out.parent.mkdir(parents=True, exist_ok=True)
|
| 21 |
+
pq.write_table(pa.table({"sha1": pa.array(shas, pa.binary(20))}), out); Path(local).unlink(missing_ok=True)
|
| 22 |
+
return {"shard": sh, "rows": len(shas), "seconds": round(time.perf_counter() - t0, 1)}
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def main() -> None:
|
| 26 |
+
p = argparse.ArgumentParser(); p.add_argument("--repo", required=True); p.add_argument("--source-prefix", default="outputs_decontam"); p.add_argument("--dir", required=True)
|
| 27 |
+
p.add_argument("--out", type=Path, default=Path("/work/out")); p.add_argument("--staging", default="/work/staging"); p.add_argument("--limit-shards", type=int, default=None)
|
| 28 |
+
p.add_argument("--upload-repo", default=None); p.add_argument("--upload-prefix", default="reports/sha1_decontam"); p.add_argument("--workers", type=int, default=max(1, int(os.environ.get("CPU_CORES") or os.cpu_count() or 2) - 1))
|
| 29 |
+
a = p.parse_args()
|
| 30 |
+
from huggingface_hub import HfApi
|
| 31 |
+
api = HfApi(token=os.environ.get("HF_TOKEN"))
|
| 32 |
+
shards = sorted({e.path.split("/")[-1] for e in api.list_repo_tree(a.repo, path_in_repo=f"{a.source_prefix}/{a.dir}", repo_type="dataset", recursive=False)})
|
| 33 |
+
if a.limit_shards: shards = shards[: a.limit_shards]
|
| 34 |
+
tasks = [{"repo": a.repo, "path": f"{a.source_prefix}/{a.dir}/{sh}/{sh}.parquet", "shard": sh, "out": str(a.out / a.dir), "staging": a.staging} for sh in shards]
|
| 35 |
+
rows = 0; failed = []
|
| 36 |
+
with ProcessPoolExecutor(max_workers=a.workers) as ex:
|
| 37 |
+
for r in ex.map(one, tasks):
|
| 38 |
+
if "error" in r: failed.append(r["shard"]); print(json.dumps({"event": "error", **r}), flush=True); continue
|
| 39 |
+
rows += r["rows"]; print(json.dumps({"event": "done", **r}), flush=True)
|
| 40 |
+
(a.out / a.dir / "_summary.json").write_text(json.dumps({"dir": a.dir, "shards": len(tasks), "rows": rows, "failed": failed}), encoding="utf-8")
|
| 41 |
+
if a.upload_repo:
|
| 42 |
+
for k in range(6):
|
| 43 |
+
try: api.upload_folder(repo_id=a.upload_repo, repo_type="dataset", folder_path=str(a.out / a.dir), path_in_repo=f"{a.upload_prefix}/{a.dir}", commit_message=f"sha1 {a.dir}"); break
|
| 44 |
+
except Exception as e: # noqa: BLE001
|
| 45 |
+
if k == 5: raise
|
| 46 |
+
time.sleep(60 * (k + 1))
|
| 47 |
+
print(json.dumps({"event": "finish", "dir": a.dir, "rows": rows, "failed": failed}), flush=True)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
main()
|