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%)")