File size: 1,745 Bytes
73b6bc8 |
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 |
import json
from tqdm import tqdm
import os
# GAIA
data_path = "data/mat_train.json"
with open(data_path, "r") as f:
dataset = json.load(f)
def _convert(image_path_map, conversations):
output = []
for turn in conversations:
role = turn["role"]
content = turn["content"]
turn_new = dict()
turn_new["from"] = role
pid = 1
keys = sorted(list(image_path_map.keys()))
for k in keys:
v = image_path_map[k]
if k in content:
content = content.replace(k, f"Picture {pid}: <img>{v}</img>\n")
content = content.replace(f"</img>\n\n", "</img>\n")
pid += 1
turn_new["value"] = content
output.append(turn_new)
return output
for item in tqdm(dataset):
#print(item["image"])
#print(item.keys())
conversations = item["conversations"]
#print(len(conversations), conversations[1])
image_path_map = dict()
if "image" not in item:
pass
elif type(item["image"]) == str:
image_path_map["<image>"] = item["image"]
item['image'] = f"{os.getcwd()}/data/{item['image']}"
else:
for k, v in item["image"].items():
image_path_map[k] = v
item["image"][k] = f"{os.getcwd()}/data/{v}"
item["conversations"] = _convert(image_path_map, conversations)
from datetime import datetime
import json
now = "20241209_1731"
print("write to", f"data/train_{now}.json")
with open(f"data/train_{now}.json", "w") as f:
json.dump(dataset, f, indent=4, ensure_ascii=False)
import random
with open(f"data/train_{now}_subset.json", "w") as f:
random.shuffle(dataset)
json.dump(dataset[:1000], f, indent=4, ensure_ascii=False) |