Karroyan commited on
Commit
a66ea25
·
verified ·
1 Parent(s): 322bf9d

Upload data_to_llamafactory.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. data_to_llamafactory.py +39 -0
data_to_llamafactory.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # turn the data in /fs-computility/niuyazhe/lixueyan/meme/dataset-meme-rewardmodel/allmeme_nocross/allmeme_nocross_train.jsonl
2
+ # to the format in /fs-computility/niuyazhe/lixueyan/meme/LLaMA-Factory/data/binary_class_demo.json
3
+
4
+ import json
5
+ import jsonlines
6
+ import os
7
+
8
+ output_path_root = '/fs-computility/niuyazhe/lixueyan/meme/LLaMA-Factory/data/'
9
+
10
+ with open('/fs-computility/niuyazhe/lixueyan/meme/dataset-meme-rewardmodel/allmeme_nocross/allmeme_nocross_train.jsonl', 'r') as f:
11
+ dataset_dict = json.load(f)
12
+ for dataset_name, dataset in dataset_dict.items():
13
+ dataset_path = dataset['annotation']
14
+ dataset_name = dataset_path.split('/')[-1].split('.')[0]
15
+ # dataset_path is also jsonl file
16
+ with open(dataset_path, 'r') as file:
17
+ for item in jsonlines.Reader(file):
18
+ # check if the image exists
19
+ if not os.path.exists(item['image'][0]) or not os.path.exists(item['image'][1]):
20
+ print(f"image {item['image'][0]} or {item['image'][1]} does not exist, skip this item")
21
+ # skip the whole item
22
+ continue
23
+
24
+ new_item = {}
25
+ new_item['messages'] = [
26
+ {
27
+ 'role': 'user',
28
+ 'content': item['conversations'][0]['value']
29
+ },
30
+ {
31
+ 'role': 'assistant',
32
+ 'content': ''
33
+ }
34
+ ]
35
+ new_item['images'] = item['image']
36
+ new_item['labels'] = [item['conversations'][1]['value']]
37
+ with open(output_path_root + dataset_name + '.json', 'a') as f:
38
+ json.dump(new_item, f)
39
+ f.write('\n')