Download evaluation/scripts/prepare_training_corpus.py from EllaNAM/rag-assignment-processed-bm25: direct link, hf CLI and curl.
- Browser
- Download file 2.55 kB
-
https://huggingface.co/datasets/EllaNAM/rag-assignment-processed-bm25/resolve/main/evaluation/scripts/prepare_training_corpus.py
- Command line
-
hf download hf://datasets/EllaNAM/rag-assignment-processed-bm25/evaluation/scripts/prepare_training_corpus.py
-
curl -L -o prepare_training_corpus.py https://huggingface.co/datasets/EllaNAM/rag-assignment-processed-bm25/resolve/main/evaluation/scripts/prepare_training_corpus.py
2.55 kB
| #!/usr/bin/env python3 | |
| """Build the reranker evidence corpus with the production gold protection and deduplication.""" | |
| import argparse | |
| from pathlib import Path | |
| from datasets import load_dataset | |
| from rag_preprocess.cleaning import clean_batch | |
| from rag_preprocess.dedup import load_protected_gold_pids, safe_deduplicate, write_pid_remap | |
| from rag_preprocess.jsonl_io import _write_jsonl_atomic | |
| def iter_evidence(cleaned, remap): | |
| """Use original title runs for positions and keep the same evidence as search preprocessing.""" | |
| previous = None | |
| position = 0 | |
| for batch in cleaned.iter(batch_size=2048): | |
| for pid, title, original, clean in zip( | |
| batch["pid"], batch["title"], batch["original_text"], batch["clean_text"], strict=True | |
| ): | |
| position = position + 1 if title == previous else 0 | |
| previous = title | |
| if str(pid) not in remap: | |
| yield { | |
| "pid": pid, "title": title, "original_text": original, | |
| "lexical_text": clean, "position_idx": position, | |
| } | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--raw-corpus", type=Path, required=True) | |
| parser.add_argument("--qrels", type=Path, required=True) | |
| parser.add_argument("--output", type=Path, required=True) | |
| parser.add_argument("--work-dir", type=Path, required=True) | |
| parser.add_argument("--num-proc", type=int, default=4) | |
| args = parser.parse_args() | |
| if args.output.exists(): | |
| raise FileExistsError(args.output) | |
| args.work_dir.mkdir(parents=True, exist_ok=True) | |
| raw = load_dataset("json", data_files=str(args.raw_corpus), split="train", cache_dir=str(args.work_dir)) | |
| cleaned = raw.map(clean_batch, batched=True, batch_size=2048, num_proc=args.num_proc, | |
| remove_columns=["text"], desc="Cleaning corpus for deduplication") | |
| print(f"Checking duplicates and gold PIDs across {len(cleaned):,} passages", flush=True) | |
| retained, remap, stats = safe_deduplicate(cleaned, load_protected_gold_pids(args.qrels)) | |
| print(stats, flush=True) | |
| write_pid_remap(remap, args.output.with_name("pid_remap.json")) | |
| count = _write_jsonl_atomic(iter_evidence(cleaned, remap), args.output) | |
| if count != len(retained): | |
| raise ValueError(f"Expected {len(retained)} rows, wrote {count}") | |
| print(f"Saved {count:,} passages to {args.output}", flush=True) | |
| if __name__ == "__main__": | |
| main() | |