File size: 1,786 Bytes
a66ea25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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')