my-remi-dataset / convert.py
StarThomas1002's picture
Upload folder using huggingface_hub
7b76145 verified
import json
# Load the original JSON file
with open('/home/yiyangai/stephenqs/datasets/mehrankazemi___re_mi/default/0.0.0/re_mi-test-02.json', 'r') as f:
data = json.load(f)
# Function to convert the dataset
def convert_format(data):
converted_data = []
for item in data:
new_item = {
"id": item["id"],
"image": None,
"conversations": [
{
"from": "human",
"content": []
},
{
"from": "gpt",
"content": [
{
"text": item["label"],
"image": None
}
]
}
]
}
# Add the main question with the first image
new_item["conversations"][0]["content"].append({
"text": item["question"],
"image": item.get("image_1", None)
})
# If there are more images, add them with null text
for i in range(2, 7):
img_key = f"image_{i}"
if item.get(img_key):
new_item["conversations"][0]["content"].append({
"text": None,
"image": item[img_key]
})
converted_data.append(new_item)
return converted_data
# Convert the dataset
converted_data = convert_format(data)
# Save the converted data to a new JSON file
with open('converted_dataset-02.json', 'w') as f:
json.dump(converted_data, f, indent=4)