import json import os def convert_training_data(input_path, output_path): """ Convert draft training data to a format acceptable for model fine-tuning. Ensure demo images, questions, and answers are included, and query does not include an answer. Args: input_path (str): Path to the input draft JSON file. output_path (str): Path to save the converted JSON file. """ with open(input_path, 'r', encoding='utf-8') as f: raw_data = json.load(f) converted_data = [] for sample in raw_data: id = sample["id"] user_messages = [] assistant_message = sample["conversations"][-1]["value"] # Add instruction user_messages.append({ "type": "text", "text": "Learn from the demos and give only the answer to the final question." }) # Process user content (images, questions, and answers) user_content = sample["conversations"][0]["value"] lines = user_content.split("\n") for line in lines: if line.startswith("Picture"): # Extract image path image_path = line.split("")[1].split("")[0] user_messages.append({"type": "image", "image": image_path}) elif line.startswith("Question"): question_text = line user_messages.append({"type": "text", "text": question_text}) elif line.startswith("Answer"): answer_text = line if answer_text.strip() != "Answer: ": # Append answer only if it's part of the demo user_messages[-1]["text"] += f" {answer_text}\n" # Construct final sample converted_sample = { "id": id, "messages": [ {"role": "user", "content": user_messages}, {"role": "assistant", "content": [{"type": "text", "text": assistant_message}]} ] } converted_data.append(converted_sample) # Save converted data with open(output_path, 'w', encoding='utf-8') as f: json.dump(converted_data, f, ensure_ascii=False, indent=4) print(f"Converted data saved to {output_path}") # Example usage if __name__ == "__main__": input_file = "./draft_training_data.json" # Replace with your draft file path output_file = "./processed_training_data.json" # Replace with your desired output file path convert_training_data(input_file, output_file)