-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer.py
More file actions
25 lines (20 loc) · 916 Bytes
/
tokenizer.py
File metadata and controls
25 lines (20 loc) · 916 Bytes
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
from tokenizers import Tokenizer, normalizers
from tokenizers.models import BPE
from tokenizers.trainers import BpeTrainer
from tokenizers.pre_tokenizers import ByteLevel
def create_tokenizer(texts):
# Create a Byte-Level BPE Tokenizer
tokenizer = Tokenizer(BPE(unk_token="[UNK]"))
# Apply normalization similar to GPT-2:
tokenizer.normalizer = normalizers.Sequence([
normalizers.NFD(),
normalizers.Lowercase(),
normalizers.StripAccents()
])
# Use ByteLevel pre-tokenization (this handles punctuation and spaces more robustly)
tokenizer.pre_tokenizer = ByteLevel()
# Set trainer with a vocabulary size close to GPT-2 (50257)
trainer = BpeTrainer(special_tokens=["[UNK]", "[PAD]", "[BOS]", "[EOS]"], vocab_size=50257)
# Train the tokenizer on your text iterator
tokenizer.train_from_iterator(texts, trainer)
return tokenizer