| # 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}") |