|
import json |
|
import random |
|
|
|
|
|
file_path = 'robot_comment.json' |
|
|
|
|
|
with open(file_path, 'r', encoding='utf-8') as file: |
|
data = json.load(file) |
|
|
|
|
|
safe_data = [] |
|
unsafe_data = [] |
|
safe_count = 0 |
|
for item in data: |
|
content = item['content'] |
|
audit_status = item['audit_status'] |
|
status = item['status'] |
|
|
|
|
|
if audit_status == 1: |
|
if safe_count < 500: |
|
safe_data.append({ |
|
"prompt": content, |
|
"response": "N/A", |
|
"violated_category_codes": [], |
|
"label": "safe", |
|
"explanation": "" |
|
}) |
|
safe_count += 1 |
|
elif audit_status == -1 or status==0: |
|
unsafe_data.append({ |
|
"prompt": content, |
|
"response": "N/A", |
|
"violated_category_codes": ["S12"], |
|
"label": "unsafe", |
|
"explanation": "This text is not suitable for public display" |
|
}) |
|
|
|
|
|
test_safe = random.sample(safe_data, 50) |
|
test_unsafe = random.sample(unsafe_data, 50) |
|
test_data = test_safe + test_unsafe |
|
|
|
|
|
train_safe = [item for item in safe_data if item not in test_safe] |
|
train_unsafe = [item for item in unsafe_data if item not in test_unsafe] |
|
train_data = train_safe + train_unsafe |
|
|
|
|
|
output_test_path = 'test_data.json' |
|
output_train_path = 'transformed_data.json' |
|
|
|
with open(output_test_path, 'w', encoding='utf-8') as test_output_file: |
|
json.dump(test_data, test_output_file, indent=2) |
|
|
|
with open(output_train_path, 'w', encoding='utf-8') as train_output_file: |
|
json.dump(train_data, train_output_file, indent=2) |