athens / convert_dataset.py
kofdai's picture
Add python application files
8ce09fe
import json
import os
# 元のファイル名
input_filename = 'train.jsonl'
# 新しく生成するファイル名
output_filename = 'train_converted.jsonl'
# ファイルが存在するか確認
if not os.path.exists(input_filename):
print(f"エラー: {input_filename} が見つかりません。")
else:
try:
# 新しいファイルを開く(書き込みモード)
with open(output_filename, 'w', encoding='utf-8') as outfile:
# 元のファイルを一行ずつ読み込む
with open(input_filename, 'r', encoding='utf-8') as infile:
print("変換を開始します...")
# 各行に対して処理を実行
for i, line in enumerate(infile):
# 空行はスキップ
if not line.strip():
continue
# JSONとして読み込む
data = json.loads(line)
# 新しい形式のdictを作成
new_format = {
"messages": [
{"role": "user", "content": data["prompt"]},
{"role": "assistant", "content": data["response"]}
]
}
# 新しい形式をJSON文字列に変換してファイルに書き込む
# ensure_ascii=False で日本語が文字化けしないようにする
outfile.write(json.dumps(new_format, ensure_ascii=False) + '\n')
print(f"変換が完了しました。{output_filename} を確認してください。")
except Exception as e:
print(f"エラーが発生しました: {e}")
print(f"問題が発生したのは {i+1} 行目かもしれません。")