Process dataset and generate conversation JSON files.
(df, fold, total_num)
| 37 | return image_paths |
| 38 | |
| 39 | def process_data(df, fold, total_num): |
| 40 | """Process dataset and generate conversation JSON files.""" |
| 41 | conversations = [] |
| 42 | |
| 43 | for index, example in tqdm(df.iterrows(), total=total_num, desc="Processing indices"): |
| 44 | if index >= total_num: |
| 45 | break |
| 46 | |
| 47 | images_list = extract_images(example['image_bytes']) |
| 48 | if len(images_list) > 1: |
| 49 | continue # Skip multiple image cases |
| 50 | |
| 51 | images = save_images(images_list, fold, index) |
| 52 | image_token = "<image>" if images else "" |
| 53 | |
| 54 | question = example['question'] |
| 55 | answer_choices = list(map(str, example['answers'].strip('[]').split(', '))) |
| 56 | random.shuffle(answer_choices) |
| 57 | correct_answer = example['correct_answer'] |
| 58 | |
| 59 | answer = ", ".join(answer_choices[:-1]) + " or " + answer_choices[-1] |
| 60 | prompt = f"{question} Choose between the following options: {answer}" |
| 61 | messages = [ |
| 62 | {"role": "user", "content": f"{image_token} Answer in natural language. {prompt}"}, |
| 63 | {"role": "assistant", "content": correct_answer} |
| 64 | ] |
| 65 | |
| 66 | conversation = {"messages": messages, "images": images} |
| 67 | |
| 68 | conversations.append(conversation) |
| 69 | |
| 70 | with open(f'SAT_{fold}_{total_num}.json', 'w') as f: |
| 71 | json.dump(conversations, f, indent=4) |
| 72 | |
| 73 | |
| 74 | def main(): |
no test coverage detected