Download scripts/phase3_prepare_data.py from Anupam-Rawat-IITB/code-switch-mtp: direct link, hf CLI and curl.
- Browser
- Download file 2.36 kB
-
https://huggingface.co/Anupam-Rawat-IITB/code-switch-mtp/resolve/main/scripts/phase3_prepare_data.py
- Command line
-
hf download hf://Anupam-Rawat-IITB/code-switch-mtp/scripts/phase3_prepare_data.py
-
curl -L -o phase3_prepare_data.py https://huggingface.co/Anupam-Rawat-IITB/code-switch-mtp/resolve/main/scripts/phase3_prepare_data.py
2.36 kB
| """Prepare Phase 3 training and held-out data for one condition. | |
| CPU-only. Writes a packed token-id stream for training and a disjoint set of | |
| held-out document texts for evaluation. Run once per condition before | |
| scripts/phase3_train.py. | |
| """ | |
| import argparse | |
| import logging | |
| import sys | |
| from pathlib import Path | |
| import yaml | |
| sys.path.insert(0, str(Path(__file__).resolve().parents[1])) | |
| from transformers import AutoTokenizer | |
| from src.data.itrans_transliterate import romanize | |
| from src.train.data import iter_english_docs, iter_sangraha_docs, prepare_corpus, save_corpus | |
| logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s") | |
| logging.getLogger("httpx").setLevel(logging.WARNING) | |
| logging.getLogger("huggingface_hub").setLevel(logging.WARNING) | |
| logger = logging.getLogger("phase3_prepare") | |
| def parse_args() -> argparse.Namespace: | |
| p = argparse.ArgumentParser(description=__doc__) | |
| p.add_argument("--config", default="configs/phase3.yaml") | |
| p.add_argument("--condition", required=True, choices=["L2", "L4", "E1"]) | |
| return p.parse_args() | |
| def main() -> None: | |
| args = parse_args() | |
| with open(args.config, encoding="utf-8") as f: | |
| cfg = yaml.safe_load(f) | |
| logger.info("config: %s", cfg) | |
| condition_cfg = cfg["conditions"][args.condition] | |
| tokenizer = AutoTokenizer.from_pretrained(cfg["backbone"]["hf_id"]) | |
| if condition_cfg["source"] == "sangraha": | |
| docs = iter_sangraha_docs(cfg["sangraha_jsonl"]) | |
| else: | |
| docs = iter_english_docs() | |
| transform = romanize if condition_cfg["transform"] == "itrans_romanize" else None | |
| logger.info( | |
| "preparing %s: source=%s transform=%s target_tokens=%d heldout_docs=%d", | |
| args.condition, condition_cfg["source"], condition_cfg["transform"], | |
| cfg["target_train_tokens"], cfg["heldout_docs"], | |
| ) | |
| prepared = prepare_corpus( | |
| docs=docs, | |
| tokenizer=tokenizer, | |
| target_tokens=cfg["target_train_tokens"], | |
| n_heldout_docs=cfg["heldout_docs"], | |
| transform=transform, | |
| ) | |
| paths = save_corpus(prepared, cfg["data_dir"], args.condition) | |
| logger.info( | |
| "%s: %d train tokens from %d docs, %d held-out docs -> %s", | |
| args.condition, prepared.n_train_tokens, prepared.n_docs_consumed, len(prepared.heldout_texts), paths, | |
| ) | |
| if __name__ == "__main__": | |
| main() | |