-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtokenizer.py
More file actions
63 lines (50 loc) · 2.38 KB
/
Copy pathtokenizer.py
File metadata and controls
63 lines (50 loc) · 2.38 KB
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
import yaml
from transformers import GPT2Tokenizer
import gpt3_tokenizer
def count_tokens(text: str) -> int:
return gpt3_tokenizer.count_tokens(text)
def tokenize_line(line: str, tokenizer) -> list:
return tokenizer.encode(line, add_special_tokens=False)
def write_chunk_to_file(chunk_tokens: list, chunk_id: int, tokenizer, output_file_pattern: str) -> None:
chunk_name = output_file_pattern.format(chunk_id=chunk_id)
with open(chunk_name, 'w', encoding='utf-8') as chunk_file:
decoded_text = tokenizer.decode(chunk_tokens)
chunk_file.write(decoded_text)
token_count = count_tokens(decoded_text)
if token_count is not None:
print(f"Chunk {chunk_id} Token Count: {token_count}")
else:
print(f"Chunk {chunk_id} Token Count: Could not be determined.")
def split_file_into_chunks(config: dict) -> None:
try:
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
filename = config['input_file_name']
max_tokens = config['max_tokens']
output_file_pattern = config['output_file_pattern']
current_chunk_id = 1
current_chunk_tokens = []
with open(filename, 'r', encoding='utf-8') as file:
for line in file:
line_tokens = tokenize_line(line, tokenizer)
# Check if adding this line exceeds the max token limit
if len(current_chunk_tokens) + len(line_tokens) > max_tokens:
if current_chunk_tokens: # Write non-empty chunk to file
write_chunk_to_file(current_chunk_tokens, current_chunk_id, tokenizer, output_file_pattern)
current_chunk_id += 1
current_chunk_tokens = line_tokens # Start new chunk
else:
current_chunk_tokens.extend(line_tokens)
# Write the last chunk if it has content
if current_chunk_tokens:
write_chunk_to_file(current_chunk_tokens, current_chunk_id, tokenizer, output_file_pattern)
print(f"File '{filename}' split into {current_chunk_id} chunks.")
except Exception as e:
print(f"An error occurred: {e}")
def load_config():
with open('config.yaml', 'r') as file:
return yaml.safe_load(file)
def main():
config = load_config()['tokenizer_config']
split_file_into_chunks(config)
if __name__ == "__main__":
main()