| import os |
| import json |
| from PIL import Image |
| from datasets import Dataset, DatasetDict, Features, Sequence, Value |
| from datasets import Image as ImageData |
| from io import BytesIO |
|
|
| from datasets import disable_caching |
| disable_caching() |
|
|
| def generate_examples(json_path): |
| with open(json_path, 'r', encoding='utf-8') as f: |
| |
| for line in f: |
| |
| |
| try: |
| data = json.loads(line) |
| |
| raw_path = data['images'][0] |
| |
| with Image.open(raw_path, "r") as img: |
| |
| if img.mode != 'RGB': |
| img = img.convert('RGB') |
| |
| resized_img = img |
| |
| |
| |
| filename = os.path.basename(raw_path) |
| img_id = os.path.splitext(filename)[0] |
| yield { |
| "images": [resized_img], |
| "problem": data["query"], |
| "answer": data["response"], |
| "id": img_id |
| } |
| |
| |
| |
| except Exception as e: |
| data = json.loads(line) |
| print(data) |
| print(f"Error processing {raw_path}: {str(e)}") |
| continue |
|
|
| def main(): |
| train_json_path="./magic_mirror_data_train_r1.jsonl" |
| test_json_path="./magic_mirror_data_test_r1.jsonl" |
|
|
| base_dir = "./" |
| train_parquet_path =base_dir + "magic_mirror_data_train_r1.parquet" |
| test_parquet_path =base_dir + "magic_mirror_data_test_r1.parquet" |
| |
| train_json_path = "./magic_mirror_data_train_resample_r1.jsonl" |
| train_parquet_path = "./magic_mirror_data_train_resample_r1.parquet" |
|
|
| |
| test_ds = Dataset.from_generator(generate_examples, gen_kwargs={"json_path": test_json_path}) |
| test_ds.cast_column("images", Sequence(ImageData())).to_parquet(test_parquet_path) |
| |
| train_ds = Dataset.from_generator(generate_examples, gen_kwargs={"json_path": train_json_path}) |
| train_ds.cast_column("images", Sequence(ImageData())).to_parquet(train_parquet_path) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|