| import json
|
| import os
|
| import random
|
| from copy import deepcopy
|
|
|
|
|
| BASE_DIR = "/root/test/weitiao/data_process_bq/data3"
|
| ABCD_DIR = os.path.join(BASE_DIR, "ABCD")
|
| EXP_DIR = os.path.join(BASE_DIR, "exp")
|
|
|
| INPUT_FILES = {
|
| "A": os.path.join(ABCD_DIR, "instag_A_Safety_Risk_chatml.json"),
|
| "B": os.path.join(ABCD_DIR, "instag_B_Romance_chatml.json"),
|
| "C": os.path.join(ABCD_DIR, "instag_C_Emotional_Support_chatml.json"),
|
| "D": os.path.join(ABCD_DIR, "instag_D_Social_Interaction_chatml.json"),
|
| }
|
|
|
|
|
| TARGETS = {
|
| "1:64": {"A": 312, "B": 6563, "C": 6563, "D": 6562},
|
| "1:32": {"A": 625, "B": 6459, "C": 6458, "D": 6458},
|
| "1:16": {"A": 1250, "B": 6250, "C": 6250, "D": 6250},
|
| "1:8": {"A": 2500, "B": 5834, "C": 5833, "D": 5833},
|
| "1:4": {"A": 5000, "B": 5000, "C": 5000, "D": 5000},
|
| }
|
|
|
| SEED = 20260316
|
|
|
|
|
| def load_data() -> dict:
|
| data = {}
|
| for k, p in INPUT_FILES.items():
|
| if not os.path.exists(p):
|
| raise FileNotFoundError(f"找不到输入文件: {p}")
|
| with open(p, "r", encoding="utf-8") as f:
|
| obj = json.load(f)
|
| if not isinstance(obj, list):
|
| raise ValueError(f"输入文件顶层不是 list: {p}")
|
| data[k] = obj
|
| return data
|
|
|
|
|
| def main():
|
| os.makedirs(EXP_DIR, exist_ok=True)
|
|
|
| data = load_data()
|
| print("原始各类数量:", {k: len(v) for k, v in data.items()})
|
|
|
|
|
| rng = random.Random(SEED)
|
| pools = {}
|
| for k, arr in data.items():
|
| arr_copy = deepcopy(arr)
|
| rng.shuffle(arr_copy)
|
| pools[k] = arr_copy
|
|
|
|
|
| for ratio, cnts in TARGETS.items():
|
| for cls, n in cnts.items():
|
| if n > len(pools[cls]):
|
| raise ValueError(f"{ratio} 目标超出 {cls} 可用数量: need={n}, have={len(pools[cls])}")
|
| if sum(cnts.values()) != 20000:
|
| raise ValueError(f"{ratio} 目标总数不是20000: {sum(cnts.values())}")
|
|
|
|
|
| for ratio, cnts in TARGETS.items():
|
| out_dir = os.path.join(EXP_DIR, f"exp{ratio}")
|
| os.makedirs(out_dir, exist_ok=True)
|
|
|
| merged = []
|
| for cls in ["A", "B", "C", "D"]:
|
| merged.extend(pools[cls][: cnts[cls]])
|
|
|
| out_path = os.path.join(out_dir, "ABCD_2w_chatml.json")
|
| with open(out_path, "w", encoding="utf-8") as f:
|
| json.dump(merged, f, ensure_ascii=False, indent=2)
|
|
|
| meta_path = os.path.join(out_dir, "meta.json")
|
| with open(meta_path, "w", encoding="utf-8") as f:
|
| json.dump(
|
| {
|
| "ratio": ratio,
|
| "target_counts": cnts,
|
| "total": len(merged),
|
| "seed": SEED,
|
| "note": "each class shuffled once, then prefix-sliced for minimal-change scaling",
|
| },
|
| f,
|
| ensure_ascii=False,
|
| indent=2,
|
| )
|
|
|
| print(f"✅ {ratio} -> {out_path} | counts={cnts} | total={len(merged)}")
|
|
|
|
|
| if __name__ == "__main__":
|
| main()
|
|
|
| import json
|
| import os
|
| import random
|
| from copy import deepcopy
|
|
|
|
|
| BASE_DIR = "/root/test/weitiao/data_process_bq/data3"
|
| ABCD_DIR = os.path.join(BASE_DIR, "ABCD")
|
| EXP_DIR = os.path.join(BASE_DIR, "exp")
|
|
|
| INPUT_FILES = {
|
| "A": os.path.join(ABCD_DIR, "instag_A_Safety_Risk_chatml.json"),
|
| "B": os.path.join(ABCD_DIR, "instag_B_Romance_chatml.json"),
|
| "C": os.path.join(ABCD_DIR, "instag_C_Emotional_Support_chatml.json"),
|
| "D": os.path.join(ABCD_DIR, "instag_D_Social_Interaction_chatml.json"),
|
| }
|
|
|
|
|
| TARGETS = {
|
| "1:64": {"A": 312, "B": 6563, "C": 6563, "D": 6562},
|
| "1:32": {"A": 625, "B": 6459, "C": 6458, "D": 6458},
|
| "1:16": {"A": 1250, "B": 6250, "C": 6250, "D": 6250},
|
| "1:8": {"A": 2500, "B": 5834, "C": 5833, "D": 5833},
|
| "1:4": {"A": 5000, "B": 5000, "C": 5000, "D": 5000},
|
| }
|
|
|
| SEED = 20260316
|
|
|
|
|
| def load_data() -> dict:
|
| data = {}
|
| for k, p in INPUT_FILES.items():
|
| if not os.path.exists(p):
|
| raise FileNotFoundError(f"找不到输入文件: {p}")
|
| with open(p, "r", encoding="utf-8") as f:
|
| obj = json.load(f)
|
| if not isinstance(obj, list):
|
| raise ValueError(f"输入文件顶层不是 list: {p}")
|
| data[k] = obj
|
| return data
|
|
|
|
|
| def main():
|
| os.makedirs(EXP_DIR, exist_ok=True)
|
|
|
| data = load_data()
|
| print("原始各类数量:", {k: len(v) for k, v in data.items()})
|
|
|
|
|
| rng = random.Random(SEED)
|
| pools = {}
|
| for k, arr in data.items():
|
| arr_copy = deepcopy(arr)
|
| rng.shuffle(arr_copy)
|
| pools[k] = arr_copy
|
|
|
|
|
| for ratio, cnts in TARGETS.items():
|
| for cls, n in cnts.items():
|
| if n > len(pools[cls]):
|
| raise ValueError(f"{ratio} 目标超出 {cls} 可用数量: need={n}, have={len(pools[cls])}")
|
| if sum(cnts.values()) != 20000:
|
| raise ValueError(f"{ratio} 目标总数不是20000: {sum(cnts.values())}")
|
|
|
|
|
| for ratio, cnts in TARGETS.items():
|
| out_dir = os.path.join(EXP_DIR, f"exp{ratio}")
|
| os.makedirs(out_dir, exist_ok=True)
|
|
|
| merged = []
|
| for cls in ["A", "B", "C", "D"]:
|
| merged.extend(pools[cls][: cnts[cls]])
|
|
|
|
|
|
|
|
|
| out_path = os.path.join(out_dir, "ABCD_2w_chatml.json")
|
| with open(out_path, "w", encoding="utf-8") as f:
|
| json.dump(merged, f, ensure_ascii=False, indent=2)
|
|
|
| meta_path = os.path.join(out_dir, "meta.json")
|
| with open(meta_path, "w", encoding="utf-8") as f:
|
| json.dump(
|
| {
|
| "ratio": ratio,
|
| "target_counts": cnts,
|
| "total": len(merged),
|
| "seed": SEED,
|
| "note": "each class shuffled once, then prefix-sliced for minimal-change scaling",
|
| },
|
| f,
|
| ensure_ascii=False,
|
| indent=2,
|
| )
|
|
|
| print(f"✅ {ratio} -> {out_path} | counts={cnts} | total={len(merged)}")
|
|
|
|
|
| if __name__ == "__main__":
|
| main()
|
|
|
|
|