File size: 2,473 Bytes
691fc0b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #!/usr/bin/env python3
import argparse
from pathlib import Path
import json
from encryption_utils import derive_key, decrypt_text
def main():
parser = argparse.ArgumentParser(description="Batch decrypt BCV3 JSONL")
parser.add_argument("--input", required=True, help="Path to train.jsonl (or archived bcv3_encrypted.jsonl)")
parser.add_argument("--key-file", required=True, help="Path to a text file containing passphrase")
parser.add_argument("--output-dir", required=True, help="Directory to write per-sample JSON")
parser.add_argument("--keep-encrypted", action="store_true", help="Keep encrypted fields in output")
args = parser.parse_args()
key_str = Path(args.key_file).read_text(encoding="utf-8").strip()
key = derive_key(key_str)
out_dir = Path(args.output_dir)
out_dir.mkdir(parents=True, exist_ok=True)
count = 0
total = 0
with Path(args.input).open("r", encoding="utf-8") as f:
for line in f:
if not line.strip():
continue
total += 1
obj = json.loads(line)
# Detect format: archived bcv3_encrypted.jsonl vs current train.jsonl
if "encrypted_data" in obj:
enc = obj.get("encrypted_data", {})
question = decrypt_text(enc.get("question", {}), key)
answer = decrypt_text(enc.get("answer", {}), key)
elif "encrypted_question" in obj:
enc_q = json.loads(obj.get("encrypted_question", "{}"))
enc_a = json.loads(obj.get("encrypted_answer", "{}"))
question = decrypt_text(enc_q, key)
answer = decrypt_text(enc_a, key)
else:
print(f"Warning: Unrecognized format in record {total}, skipping...")
continue
out_obj = obj.copy()
out_obj["question"] = question
out_obj["answer"] = answer
if not args.keep_encrypted:
out_obj.pop("encrypted_data", None)
out_obj.pop("encrypted_question", None)
out_obj.pop("encrypted_answer", None)
out_obj.pop("trajectory", None)
out_path = out_dir / f"{obj.get('id')}.json"
out_path.write_text(json.dumps(out_obj, ensure_ascii=False, indent=2), encoding="utf-8")
count += 1
print(f"Decrypted {count}/{total} samples and wrote to {out_dir}")
if __name__ == "__main__":
main()
|