TrueMICL / clock /generate_clock.py
ShuoChen99's picture
Upload folder using huggingface_hub
1e9a83f verified
import json
import random
import itertools
# 设置随机种子
SEED = 42
random.seed(SEED)
# 1. 生成所有可能的数字组合 (1-12) 的排列(排除重复)
digits = list(range(1, 13))
combinations = list(itertools.permutations(digits, 2)) # 共 132 组
random.shuffle(combinations) # 打乱顺序
# 计算结果的函数
def calculate_result(num1, num2, op):
num1, num2 = int(num1), int(num2)
if op == "+":
return num1 + num2
elif op == "-":
return num1 - num2
elif op == "x":
return num1 * num2
# 2. 选取不同的组
support_combinations = combinations[:16] # 16 组,用于 support.json
training_shots_combinations = combinations[16:32] # 16 组,用于 training_data 的 shots
remaining_combinations = combinations[32:] # 剩余 100 组(但可生成 300 个 samples)
# 3. 生成 query 数据 (query.json)
operators = ["+", "-", "x"]
all_remaining_samples = [(num1, num2, op) for num1, num2 in remaining_combinations for op in operators]
query_samples = random.sample(all_remaining_samples, 100) # 直接选 100 个样本
query_data = []
for num1, num2, op in query_samples:
query_data.append({
"id": f"clock_query_{num1}_{num2}_{op}",
"image": [f"clock/img/clock_{num1}_{num2}.png"],
"question": "What is the mathematical result of the blue number and the red number?",
"answer": calculate_result(num1, num2, op),
"operator": op
})
with open("query.json", "w") as f:
json.dump(query_data, f, ensure_ascii=False, indent=4)
# 4. 生成 support 数据 (support.json)
support_data = []
for num1, num2 in support_combinations:
support_data.append({
"id": f"clock_support_{num1}_{num2}",
"image": [f"clock/img/clock_{num1}_{num2}.png"],
"question": "What is the mathematical result of the blue number and the red number?",
"answer": [calculate_result(num1, num2, op) for op in operators]
})
with open("support.json", "w") as f:
json.dump(support_data, f, ensure_ascii=False, indent=4)
# 5. 生成 processed_training_data_ck.json
remaining_training_samples = list(set(all_remaining_samples) - set(query_samples)) # 剩余 200 个
selected_training_queries = random.sample(remaining_training_samples, 50) # 选 50 组作为训练 query
training_data = []
for num1, num2, op in selected_training_queries:
# 选 4 个 shots,确保运算符一致
selected_shots = random.sample(training_shots_combinations, 4)
user_messages = [
{"type": "text", "text": "Learn from the demos and give only the answer to the final question."}
]
for shot_num1, shot_num2 in selected_shots:
user_messages.append({
"type": "image",
"image": f"clock/img/clock_{shot_num1}_{shot_num2}.png"
})
user_messages.append({
"type": "text",
"text": f"Question: What is the mathematical result of the blue number and the red number? Answer: {calculate_result(shot_num1, shot_num2, op)}"
})
# 添加 query
user_messages.append({
"type": "image",
"image": f"clock/img/clock_{num1}_{num2}.png"
})
user_messages.append({
"type": "text",
"text": "Question: What is the mathematical result of the blue number and the red number? Answer: "
})
assistant_messages = [
{"type": "text", "text": str(calculate_result(num1, num2, op))}
]
training_data.append({
"id": f"clock_query_{num1}_{num2}_{op}",
"messages": [
{"role": "user", "content": user_messages},
{"role": "assistant", "content": assistant_messages}
]
})
with open("processed_training_data_ck.json", "w") as f:
json.dump(training_data, f, ensure_ascii=False, indent=4)
print("所有数据已成功生成!")