pulnip's picture
Upload folder using huggingface_hub
222d39c verified
raw
history blame
1.23 kB
import json
import re
def sort_pairs_by_ai_image(input_path, output_path):
# '..._<์ˆซ์ž1>_<์ˆซ์ž2>.jpg' ํŒจํ„ด: prefix๋Š” ์ˆซ์ž1 ์ „๊นŒ์ง€ ์ „๋ถ€
pattern = re.compile(r"(.+)_(\d+)_(\d+)\.jpg$")
entries = []
# 1. ์ฝ์–ด์„œ (prefix, num1, num2, entry)๋กœ ์ €์žฅ
with open(input_path, 'r', encoding='utf-8') as infile:
for line in infile:
entry = json.loads(line)
ai_img = entry.get('ai_image', '')
m = pattern.match(ai_img)
if m:
prefix, num1, num2 = m.groups()
entries.append((prefix, int(num1), int(num2), entry))
else:
# ํŒจํ„ด ์•ˆ ๋งž์œผ๋ฉด ๋งจ ๋’ค๋กœ
entries.append((ai_img, float('inf'), float('inf'), entry))
# 2. (prefix, num1, num2) ์ˆœ์„œ๋กœ ์ •๋ ฌ
entries.sort(key=lambda x: (x[0], x[1], x[2]))
# 3. new_pairs.jsonl์— ๊ธฐ๋ก
with open(output_path, 'w', encoding='utf-8') as outfile:
for _, _, _, entry in entries:
outfile.write(json.dumps(entry, ensure_ascii=False) + '\n')
if __name__ == "__main__":
sort_pairs_by_ai_image("pairs.jsonl", "new_pairs.jsonl")
print("โœ… new_pairs.jsonl ์ƒ์„ฑ ์™„๋ฃŒํ–ˆ์–ด! ๐Ÿ˜‰")