MCPcopy Create free account
hub / github.com/cwxndl/LLM / gen_baike

Function gen_baike

utils/data_preprocess.py:49–111  ·  view source on GitHub ↗
(origin_file)

Source from the content-addressed store, hash-verified

47 return ""
48
49def gen_baike(origin_file):
50 baike_items = []
51 eos_token = "[EOS]"
52 max_len = 512
53 batch_size, batch_cnt = 2000000, 0
54 with open(origin_file, "r", encoding="utf-8") as f:
55 while True:
56 line = f.readline()
57 if not line:
58 break
59
60 item = ujson.loads(line)
61 cur_txt, cur_len = [], 0
62
63 if not item["title"]:
64 continue
65
66 temp_txt = f"{item['title']}:{process_none(item['summary'])}"
67
68 cur_len += len(temp_txt)
69 cur_txt.append(temp_txt)
70
71 for section in item["sections"]:
72
73 # 太长的截断不要了
74 if cur_len > max_len:
75 break
76
77 title = f"{section['title']}:" if section["title"] else ""
78 temp_txt = f"{title}{process_none(section['content'])}"
79
80 cur_len += len(temp_txt)
81 cur_txt.append(temp_txt)
82 temp_txt = normalize("NFKC", "".join(cur_txt))
83
84 if len(temp_txt) > max_len:
85 # 从 max_len 开始找第一个句号,叹号
86 n, i = len(temp_txt), max_len
87 while i < n and temp_txt[i] not in ("。", "!"):
88 i += 1
89 temp_txt = "".join(temp_txt[0 : i + 1])
90
91 # 添加 eos token
92 temp_txt = f"{temp_txt}{eos_token}"
93
94 baike_items.append(temp_txt)
95
96 if len(baike_items) % batch_size == 0:
97
98 chunk_data = split_txt_cropus_to_chunk_data(baike_items)
99 tb = pa.Table.from_arrays([chunk_data], names=["text"])
100
101 file_name = f"/root/autodl-tmp/data/baike/baike_chunk_512_5.6M_{batch_cnt}.parquet"
102 pq.write_table(
103 table=tb,
104 where=file_name,
105 row_group_size=50000,
106 )

Callers

nothing calls this directly

Calls 2

process_noneFunction · 0.85

Tested by

no test coverage detected