| 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) | |