File size: 2,287 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
#!/usr/bin/env python3
import argparse
import json
from pathlib import Path
from encryption_utils import derive_key, decrypt_text

def main():
    parser = argparse.ArgumentParser(description="Decrypt BCV3 encrypted JSONL")
    parser.add_argument("--input", required=True, help="Path to train.jsonl (or archived bcv3_encrypted.jsonl)")
    parser.add_argument("--key", required=True, help="Passphrase string")
    parser.add_argument("--output", required=True, help="Output JSON file")
    parser.add_argument("--keep-encrypted", action="store_true", help="Keep encrypted fields in output")
    args = parser.parse_args()

    key = derive_key(args.key)
    in_path = Path(args.input)
    out_path = Path(args.output)

    outputs = []
    total = 0
    with in_path.open("r", encoding="utf-8") as f:
        for line in f:
            if not line.strip():
                continue
            total += 1
            obj = json.loads(line)
            out_obj = obj.copy()

            # 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["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)

            outputs.append(out_obj)

    out_path.write_text(json.dumps(outputs, ensure_ascii=False, indent=2), encoding="utf-8")
    print(f"Decrypted {len(outputs)}/{total} records and wrote to {out_path}")

if __name__ == "__main__":
    main()