Download script/safe_guard_count.py from bingqin111/data_process_bq: direct link, hf CLI and curl.
- Browser
- Download file 7.98 kB
-
https://huggingface.co/datasets/bingqin111/data_process_bq/resolve/main/script/safe_guard_count.py
- Command line
-
hf download hf://datasets/bingqin111/data_process_bq/script/safe_guard_count.py
-
curl -L -o safe_guard_count.py https://huggingface.co/datasets/bingqin111/data_process_bq/resolve/main/script/safe_guard_count.py
7.98 kB
| 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%)") |