import json
import re
from tqdm import tqdm
# Read the original JSON data from the file
with open('/root/Monkey_Data/train_monkey.json', 'r') as file:
original_json = json.load(file)
# Desired JSON format
desired_json = []
# Regex pattern to match the entire
... string
pattern = r"
.*?"
#count = 0
# Extract and transform the data
for item in tqdm(original_json, desc="Processing"):
new_conversations = []
for conv in item["conversations"]:
if conv["from"] == "user":
new_value = re.sub(pattern, "\n", conv["value"])
new_conversations.append({
"from": "human",
"value": new_value
})
elif conv["from"] == "assistant":
new_conversations.append({
"from": "gpt",
"value": conv["value"]
})
desired_json.append({
"id": item["id"],
"image": item["id"],
"conversations": new_conversations
})
#count += 1
#if count > 10:
# break
# Optionally, write the transformed JSON to a new file
with open('/root/Monkey_Data/transformed_train_monkey.json', 'w') as outfile:
json.dump(desired_json, outfile, indent=4, ensure_ascii=False)