Download transform_json.py from lhbit20010120/LLaVA_Monkey_Data: direct link, hf CLI and curl.
- Browser
- Download file 1.25 kB
-
https://huggingface.co/datasets/lhbit20010120/LLaVA_Monkey_Data/resolve/main/transform_json.py
- Command line
-
hf download hf://datasets/lhbit20010120/LLaVA_Monkey_Data/transform_json.py
-
curl -L -o transform_json.py https://huggingface.co/datasets/lhbit20010120/LLaVA_Monkey_Data/resolve/main/transform_json.py
1.25 kB
| 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 <img>...</img> string | |
| pattern = r"<img>.*?</img>" | |
| #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, "<image>\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) | |