File size: 4,071 Bytes
aa99569
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# import json

# input_file = "/mnt/hwfile/ai4chem/hao/data_processing/SceMQA-main/chem/chem_multiple_choice.json"
# output_file = "/mnt/hwfile/ai4chem/hao/data_processing/SceMQA-main/test_chem_multiple_choice.jsonl"
# base_image_path = "/mnt/hwfile/ai4chem/hao/data_processing/SceMQA-main/Multiple_Choice/Chemistry/"

# # Read the input JSON file
# with open(input_file, 'r') as infile:
#     data = json.load(infile)

# # Process each entry and write to the new JSONL file
# with open(output_file, 'w') as outfile:
#     for idx, entry in enumerate(data, start=1):
#         new_entry = {
#             "id": str(idx),
#             "images": [base_image_path + entry["ImagePath"] + ".png"],
#             "conversations": [
#                 {"from": "human", "value": "<image>\n" + entry["Question"]},
#                 {"from": "gpt", "value": entry["Answer (final answer highlighted)"]}
#             ]
#         }
#         outfile.write(json.dumps(new_entry, ensure_ascii=False) + "\n")

# print(f"Processing complete. The new JSONL file is saved at {output_file}")

# import json
# import os

# input_file = "/mnt/hwfile/ai4chem/hao/data_processing/SceMQA-main/chem/chem_multiple_choice.json"
# output_file = "/mnt/hwfile/ai4chem/hao/data_processing/SceMQA-main/test_chem_multiple_choice.jsonl"
# base_image_path = "/mnt/hwfile/ai4chem/hao/data_processing/SceMQA-main/Multiple_Choice/"

# # Read the input JSON file
# with open(input_file, 'r') as infile:
#     data = json.load(infile)

# # Process each entry and write to the new JSONL file
# with open(output_file, 'w') as outfile:
#     for idx, entry in enumerate(data, start=1):
#         image_path_png = base_image_path + entry["ImagePath"] + ".png"
#         image_path_jpg = base_image_path + entry["ImagePath"] + ".jpg"
        
#         # Check if the .png file exists, otherwise use the .jpg file
#         if not os.path.exists(image_path_png):
#             image_path = image_path_jpg
#         else:
#             image_path = image_path_png

#         new_entry = {
#             "id": str(idx),
#             "images": [image_path],
#             "conversations": [
#                 {"from": "human", "value": "<image>\n" + entry["Question"]},
#                 {"from": "gpt", "value": entry["Answer (final answer highlighted)"][0]}
#             ]
#         }
#         outfile.write(json.dumps(new_entry, ensure_ascii=False) + "\n")

# print(f"Processing complete. The new JSONL file is saved at {output_file}")


import json
import os
import re

input_file = "/mnt/hwfile/ai4chem/hao/data_processing/SceMQA-main/chem/chem_free_response.json"
output_file = "/mnt/hwfile/ai4chem/hao/data_processing/SceMQA-main/test_chem_free_response.jsonl"
base_image_path = "/mnt/hwfile/ai4chem/hao/data_processing/SceMQA-main/Free_Response/"

# Read the input JSON file
with open(input_file, 'r') as infile:
    data = json.load(infile)

# Process each entry and write to the new JSONL file
with open(output_file, 'w') as outfile:
    for idx, entry in enumerate(data, start=1):
        image_path_png = base_image_path + entry["ImagePath"] + ".png"
        image_path_jpg = base_image_path + entry["ImagePath"] + ".jpg"
        
        # Check if the .png file exists, otherwise use the .jpg file
        if not os.path.exists(image_path_png):
            image_path = image_path_jpg
        else:
            image_path = image_path_png

        # Extract the content inside answer{???} from the Answer attribute
        match = re.search(r'answer\{(.*?)\}', entry["Answer (final answer highlighted)"], re.IGNORECASE)
        answer_content = match.group(1) if match else ""

        new_entry = {
            "id": str(idx),
            "images": [image_path],
            "conversations": [
                {"from": "human", "value": "<image>\n" + entry["Question"]},
                {"from": "gpt", "value": answer_content}
            ]
        }
        outfile.write(json.dumps(new_entry, ensure_ascii=False) + "\n")

print(f"Processing complete. The new JSONL file is saved at {output_file}")