Source code for data.split_wildguardmix

#!/usr/bin/env python3
"""
Split wildguardmix.jsonl into four safety-categorized files based on harm labels.

Classification (4 groups):
- Group A (safe_prompts_safe_response):
    prompt_harm_label == "unharmful" AND response_harm_label == "unharmful"
- Group B (safe_prompts_harmful_response):
    prompt_harm_label == "unharmful" AND response_harm_label == "harmful"
- Group C (harmful_prompt_safe_response):
    prompt_harm_label == "harmful" AND response_harm_label == "unharmful"
- Group D (harmful_prompt_harmful_response):
    prompt_harm_label == "harmful" AND response_harm_label == "harmful"

Samples with unknown/missing labels are discarded.
All original fields including metadata are preserved in the output files.
"""

import json
import os
import sys
from collections import Counter
from typing import Dict, List, Any

[docs] def classify_sample(item: Dict[str, Any]) -> str: """ Classify a sample into one of four groups. Returns: "A" for safe_prompts_safe_response, "B" for safe_prompts_harmful_response, "C" for harmful_prompt_safe_response, "D" for harmful_prompt_harmful_response, None if unknown/invalid """ prompt_harm = item.get("prompt_harm_label", "") response_harm = item.get("response_harm_label", "") # Handle None values if prompt_harm is None: prompt_harm = "" if response_harm is None: response_harm = "" prompt_harm = prompt_harm.lower() response_harm = response_harm.lower() # Discard unknown or missing labels if not prompt_harm or not response_harm: return None if prompt_harm == "unharmful": if response_harm == "unharmful": return "A" elif response_harm == "harmful": return "B" else: return None elif prompt_harm == "harmful": if response_harm == "unharmful": return "C" elif response_harm == "harmful": return "D" else: return None else: return None
[docs] def main(): # Use the actual data directory on the scratch disk input_dir = "/scratch/aszalay1/tianze/cpt_data/wildguardmix" input_path = os.path.join(input_dir, "wildguardmix.jsonl") if not os.path.exists(input_path): print(f"ERROR: Input file not found: {input_path}", file=sys.stderr) sys.exit(1) # Output paths in same directory outputs = { "A": os.path.join(input_dir, "wildguardmix_safe_prompts_safe_response.jsonl"), "B": os.path.join(input_dir, "wildguardmix_safe_prompts_harmful_response.jsonl"), "C": os.path.join(input_dir, "wildguardmix_harmful_prompt_safe_response.jsonl"), "D": os.path.join(input_dir, "wildguardmix_harmful_prompt_harmful_response.jsonl"), } # Ensure we don't accidentally overwrite by checking if outputs already exist for key, out_path in outputs.items(): if os.path.exists(out_path): response = input(f"Output file {out_path} already exists. Overwrite? (y/N): ") if response.lower() != "y": print(f"Aborted.") sys.exit(1) counters = Counter({"A": 0, "B": 0, "C": 0, "D": 0, "discarded": 0}) file_handles = {key: open(path, "w", encoding="utf-8") for key, path in outputs.items()} print(f"Reading {input_path}...") total_lines = 0 try: with open(input_path, "r", encoding="utf-8") as f: for line in f: total_lines += 1 if not line.strip(): continue try: item = json.loads(line) except json.JSONDecodeError as e: print(f"WARNING: Skipping invalid JSON at line {total_lines}: {e}", file=sys.stderr) counters["discarded"] += 1 continue group = classify_sample(item) if group is None: counters["discarded"] += 1 else: file_handles[group].write(json.dumps(item, ensure_ascii=False) + "\n") counters[group] += 1 # Progress update every 10k lines if total_lines % 10000 == 0: print(f" Processed {total_lines} lines...", end="\r", file=sys.stderr) finally: # Close all output files for fh in file_handles.values(): fh.close() print(f"\n✅ Splitting complete!") print(f"Total lines processed: {total_lines}") print(f"Group A (safe_prompts_safe_response): {counters['A']} samples -> {outputs['A']}") print(f"Group B (safe_prompts_harmful_response): {counters['B']} samples -> {outputs['B']}") print(f"Group C (harmful_prompt_safe_response): {counters['C']} samples -> {outputs['C']}") print(f"Group D (harmful_prompt_harmful_response): {counters['D']} samples -> {outputs['D']}") print(f"Discarded (unknown/missing labels): {counters['discarded']} samples") return 0
if __name__ == "__main__": sys.exit(main())