Datasets:
Tasks:
Image-Text-to-Text
Modalities:
Image
Formats:
imagefolder
Languages:
English
Size:
1K - 10K
ArXiv:
License:
File size: 2,519 Bytes
1e9a83f |
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 |
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("<img>")[1].split("</img>")[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)
|