File size: 1,800 Bytes
2cfd9e8 |
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 |
import json
import random
# 文件路径
file_path = 'robot_comment.json'
# 读取JSON数据
with open(file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
# 分开存储safe和unsafe数据
safe_data = []
unsafe_data = []
safe_count = 0
for item in data:
content = item['content']
audit_status = item['audit_status']
status = item['status']
# 根据audit_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"
})
# 随机抽取50条safe和50条unsafe作为测试集
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) |