File size: 1,936 Bytes
0779f68 |
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 |
import json
# # Original data (replace with actual data loading)
# input_data = [
# {
# "query": "<image>According to the principles...",
# "response": "{\"Normal_Hands\": 0, \"Polydactyly_Extra_Fingers\": 0...}",
# "images": ["/mnt/dolphinfs/.../141126815887.jpg"]
# },
# # ... other entries
# ]
def convert_to_sharegpt(original_data):
output = []
# original_data["response"] = original_data["output"]
# original_data["images"] = "./magic_mirror_data_344k/" + original_data["images"]
messages = [
{
"content": original_data["query"],
"role": "user"
},
{
"content": original_data["response"],
"role": "assistant"
}
]
output.append({
"messages": messages,
"images": original_data["images"][0],
})
return output
# # Convert data
# sharegpt_data = convert_to_sharegpt(input_data)
# # Save to file
# with open("output.json", "w") as f:
# json.dump(sharegpt_data, f, indent=2)
def main(source_json_path="", target_json_path=""):
# 读取并逐行解析JSON文件
data = []
with open(source_json_path, 'r', encoding='utf-8') as file:
i = 0
for line in file:
item = json.loads(line.strip())
# if "output" in item.keys():
re = convert_to_sharegpt(item)
data.append(*re)
# 保存修改后的数据到新文件,逐行写入
with open(target_json_path, 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=2)
# for item in data:
# file.write(json.dumps(item, ensure_ascii=False) + '\n')
if __name__ == "__main__":
train_sjp='./magic_mirror_data_train_r1.jsonl'
train_tjp='./magic_mirror_data_train_full_sharegpt.jsonl'
main(source_json_path=train_sjp, target_json_path=train_tjp)
|