| # turn the data in /fs-computility/niuyazhe/lixueyan/meme/dataset-meme-rewardmodel/allmeme_nocross/allmeme_nocross_train.jsonl | |
| # to the format in /fs-computility/niuyazhe/lixueyan/meme/LLaMA-Factory/data/binary_class_demo.json | |
| import json | |
| import jsonlines | |
| import os | |
| output_path_root = '/fs-computility/niuyazhe/lixueyan/meme/LLaMA-Factory/data/' | |
| with open('/fs-computility/niuyazhe/lixueyan/meme/dataset-meme-rewardmodel/allmeme_nocross/allmeme_nocross_train.jsonl', 'r') as f: | |
| dataset_dict = json.load(f) | |
| for dataset_name, dataset in dataset_dict.items(): | |
| dataset_path = dataset['annotation'] | |
| dataset_name = dataset_path.split('/')[-1].split('.')[0] | |
| # dataset_path is also jsonl file | |
| with open(dataset_path, 'r') as file: | |
| for item in jsonlines.Reader(file): | |
| # check if the image exists | |
| if not os.path.exists(item['image'][0]) or not os.path.exists(item['image'][1]): | |
| print(f"image {item['image'][0]} or {item['image'][1]} does not exist, skip this item") | |
| # skip the whole item | |
| continue | |
| new_item = {} | |
| new_item['messages'] = [ | |
| { | |
| 'role': 'user', | |
| 'content': item['conversations'][0]['value'] | |
| }, | |
| { | |
| 'role': 'assistant', | |
| 'content': '' | |
| } | |
| ] | |
| new_item['images'] = item['image'] | |
| new_item['labels'] = [item['conversations'][1]['value']] | |
| with open(output_path_root + dataset_name + '.json', 'a') as f: | |
| json.dump(new_item, f) | |
| f.write('\n') | |