File size: 7,979 Bytes
3d27cfe | 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | import json
from collections import Counter
INPUT_FILE = "/root/test/weitiao/data_process_bq/data34/set1_extracted/B_Romance_3249_safeguard_set1.jsonl"
# conversations ไธญๅธฆ _safety ็ gpt ่ฝฎ๏ผUnsafe ๅ ๆฏ **ไธฅๆ ผๅคงไบ** ่ฏฅๆฏไพๆถ๏ผๆ ทๆฌ็บง conversation ่ฎฐไธบ Unsafe
UNSAFE_CONV_RATIO_THRESHOLD = 0.3
# โโ ็ป่ฎกๅฎนๅจ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
stats = {
"total_samples": 0,
"conv_labels": Counter(),
"chosen_labels": Counter(),
"rejected_labels": Counter(),
"conv_categories": Counter(),
"chosen_categories": Counter(),
"rejected_categories": Counter(),
"sample_any_unsafe": 0,
"sample_any_controversial": 0,
"sample_all_safe": 0,
"total_conv_turns": 0,
}
unsafe_turn_count = Counter()
controversial_turn_count = Counter()
triple_counter = Counter()
with open(INPUT_FILE, "r", encoding="utf-8") as f:
for line in f:
sample = json.loads(line)
stats["total_samples"] += 1
sample_labels = []
# โโ conversations gpt turn โโโโโโโโโโโโโโโโโโโโโโโโ
n_unsafe_conv = 0
n_controversial_conv = 0
conv_turn_labels = []
for turn in sample["conversations"]:
if turn["from"] != "gpt" or "_safety" not in turn:
continue
safety = turn["_safety"]
label = safety.get("label")
stats["total_conv_turns"] += 1
stats["conv_labels"][label] += 1
for cat in safety.get("categories", []):
stats["conv_categories"][cat] += 1
sample_labels.append(label)
conv_turn_labels.append(label)
if label == "Unsafe":
n_unsafe_conv += 1
elif label == "Controversial":
n_controversial_conv += 1
unsafe_turn_count[n_unsafe_conv] += 1
controversial_turn_count[n_controversial_conv] += 1
# โโ chosen โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
chosen_label = None
if "_safety" in sample.get("chosen", {}):
safety = sample["chosen"]["_safety"]
chosen_label = safety.get("label")
stats["chosen_labels"][chosen_label] += 1
for cat in safety.get("categories", []):
stats["chosen_categories"][cat] += 1
sample_labels.append(chosen_label)
# โโ rejected โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
rejected_label = None
if "_safety" in sample.get("rejected", {}):
safety = sample["rejected"]["_safety"]
rejected_label = safety.get("label")
stats["rejected_labels"][rejected_label] += 1
for cat in safety.get("categories", []):
stats["rejected_categories"][cat] += 1
sample_labels.append(rejected_label)
# โโ ๆ ทๆฌ็บงๆฑๆป โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
if "Unsafe" in sample_labels:
stats["sample_any_unsafe"] += 1
elif "Controversial" in sample_labels:
stats["sample_any_controversial"] += 1
else:
stats["sample_all_safe"] += 1
# โโ ไธๅ
็ป๏ผไธๅ็ฑป๏ผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ไป
็ป่ฎก conversations ้ from=gpt ไธๅซ _safety ็่ฝฎ๏ผUnsafe ่ฝฎๆฐ / ่ฏฅๆป่ฝฎๆฐ > 30% ๅๆดๆกไธบ Unsafe
n_conv_turns = len(conv_turn_labels)
if n_conv_turns == 0:
conv_status = "Safe"
elif (n_unsafe_conv / n_conv_turns) >= UNSAFE_CONV_RATIO_THRESHOLD:
conv_status = "Unsafe"
elif any(l == "Controversial" for l in conv_turn_labels):
conv_status = "Controversial"
else:
conv_status = "Safe"
chosen_status = chosen_label if chosen_label in ("Safe", "Controversial", "Unsafe") else "Safe"
rejected_status = rejected_label if rejected_label in ("Safe", "Controversial", "Unsafe") else "Safe"
triple_counter[(
f"conv:{conv_status}",
f"chosen:{chosen_status}",
f"rejected:{rejected_status}",
)] += 1
# โโ ๆๅฐๆฅๅ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
total = stats["total_samples"]
print(f"{'='*50}")
print(f"ๆปๆ ทๆฌๆฐ: {total}")
print(
f"conversation ็บง Unsafe ๅคๅฎ๏ผgpt ไธๅซ _safety ็่ฝฎไธญ๏ผUnsafe ่ฝฎๅ ๆฏ > "
f"{UNSAFE_CONV_RATIO_THRESHOLD * 100:.0f}%"
)
print(f"{'='*50}")
# ๆ ทๆฌ็บง
print(f"\nโโ ๆ ทๆฌ็บงๅๅธ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ")
print(f" ๅ
จ้จ Safe : {stats['sample_all_safe']:>6} ({stats['sample_all_safe']/total*100:.1f}%)")
print(f" ๅซ Controversial : {stats['sample_any_controversial']:>6} ({stats['sample_any_controversial']/total*100:.1f}%)")
print(f" ๅซ Unsafe : {stats['sample_any_unsafe']:>6} ({stats['sample_any_unsafe']/total*100:.1f}%)")
# ๅไฝ็ฝฎ label / category ๅๅธ
for name, label_counter, cat_counter, turn_total in [
("conversations gpt turn", stats["conv_labels"], stats["conv_categories"], stats["total_conv_turns"]),
("chosen", stats["chosen_labels"], stats["chosen_categories"], total),
("rejected", stats["rejected_labels"], stats["rejected_categories"], total),
]:
print(f"\nโโ {name} (ๅ
ฑ {turn_total} ๆก) โโโโโโโโโโโโโโโโโโโโโ")
print(" Label ๅๅธ:")
for label, cnt in label_counter.most_common():
print(f" {label:<15}: {cnt:>6} ({cnt/turn_total*100:.1f}%)")
print(" Category ๅๅธ (ไธๅซ None):")
for cat, cnt in cat_counter.most_common():
if cat == "None":
continue
print(f" {cat:<40}: {cnt:>6} ({cnt/turn_total*100:.1f}%)")
# conversations unsafe/controversial turn ๆฐ้ๅๅธ
print(f"\nโโ conversations ไธญ Unsafe turn ๆฐ้ๅๅธ โโโโโโโโโโโโ")
print(f" {'Unsafe turns':<15} {'ๆ ทๆฌๆฐ':>8} {'ๅ ๆฏ':>8} {'็ดฏ่ฎกๅ ๆฏ':>10}")
cumulative = 0
for n in sorted(unsafe_turn_count):
cnt = unsafe_turn_count[n]
cumulative += cnt
print(f" {n:<15} {cnt:>8} {cnt/total*100:>7.1f}% {cumulative/total*100:>9.1f}%")
print(f"\nโโ conversations ไธญ Controversial turn ๆฐ้ๅๅธ โโโโโ")
print(f" {'Controversial':<15} {'ๆ ทๆฌๆฐ':>8} {'ๅ ๆฏ':>8} {'็ดฏ่ฎกๅ ๆฏ':>10}")
cumulative = 0
for n in sorted(controversial_turn_count):
cnt = controversial_turn_count[n]
cumulative += cnt
print(f" {n:<15} {cnt:>8} {cnt/total*100:>7.1f}% {cumulative/total*100:>9.1f}%")
# ไธๅ
็ปๅๅธ๏ผ27 ็ง็ปๅ๏ผ่ทณ่ฟ็ฉบ็๏ผ
print(f"\nโโ ไธๅ
็ปๅๅธ (conv / chosen / rejected) โโโโโโโโโโโ")
print(f" {'conv':<16} {'chosen':<16} {'rejected':<16} {'ๆ ทๆฌๆฐ':>8} {'ๅ ๆฏ':>8}")
print(f" {'-'*66}")
all_keys = [
(c, ch, r)
for c in ("conv:Safe", "conv:Controversial", "conv:Unsafe")
for ch in ("chosen:Safe", "chosen:Controversial", "chosen:Unsafe")
for r in ("rejected:Safe", "rejected:Controversial", "rejected:Unsafe")
]
for key in all_keys:
cnt = triple_counter.get(key, 0)
if cnt == 0:
continue
c, ch, r = [k.split(":")[1] for k in key]
print(f" {c:<16} {ch:<16} {r:<16} {cnt:>8} ({cnt/total*100:.1f}%)")
print(f" {'-'*66}")
print(f" {'ๅ่ฎก':<48} {total:>8} (100.0%)") |