Download tokenizer/bpe.py from Pulsar-Dynamic-Technologies/Adria: direct link, hf CLI and curl.
- Browser
- Download file 2.94 kB
-
https://huggingface.co/Pulsar-Dynamic-Technologies/Adria/resolve/main/tokenizer/bpe.py
- Command line
-
hf download hf://Pulsar-Dynamic-Technologies/Adria/tokenizer/bpe.py
-
curl -L -o bpe.py https://huggingface.co/Pulsar-Dynamic-Technologies/Adria/resolve/main/tokenizer/bpe.py
2.94 kB
| import json | |
| from tokenizers import Tokenizer, Regex | |
| from tokenizers.models import BPE | |
| from tokenizers.pre_tokenizers import ByteLevel, Split, Sequence | |
| from tokenizers.trainers import BpeTrainer | |
| from tokenizers.decoders import ByteLevel as ByteLevelDecoder | |
| from tokenizer.tokenizer_interface import TokenizerInterface | |
| class BPETokenizer(TokenizerInterface): | |
| def __init__(self, special_token_map: dict[str, str] = {}): | |
| # Merge rules + Special tokens + Base values | |
| self.special_token_map = special_token_map | |
| self.tokenizer = Tokenizer(BPE()) | |
| """ | |
| This should split the input into individual digits, and then throw those individual digits alongside the trained text data. | |
| It also SHOULD make BPE not merge numbers together into larger tokens (Like 6 & 4 into 64), so it should be more consistent. | |
| """ | |
| splitter = Split(Regex(r"\d{3}"), behavior="isolated") # I don't know why Pycharm is complaining because this is literally what HF's docs tell me to do | |
| self.tokenizer.pre_tokenizer = Sequence([splitter, ByteLevel()]) | |
| self.tokenizer.decoder = ByteLevelDecoder() # Added this so the output text would be decoded and the special characters would be removed | |
| def train(self, iterator, vocab_size: int) -> None: | |
| """ | |
| special_tokens is a map of {token_name:token_string} | |
| where token_name is used in get_special_token | |
| and token_string is the actual text appearance of the token. | |
| """ | |
| trainer = BpeTrainer(special_tokens=list(self.special_token_map.values()), vocab_size=vocab_size) | |
| self.tokenizer.train_from_iterator(iterator, trainer=trainer) | |
| def save(self, path: str) -> None: | |
| self.tokenizer.save(path, True) | |
| with open(path + ".extra", "w") as f: | |
| # ^ switched to write only because r+ requires an existing file, which would not be there if it were your first time training the tokenizer (it also made it work for me) | |
| f.seek(0) | |
| f.truncate() | |
| json.dump( | |
| self.special_token_map, | |
| fp=f | |
| ) | |
| f.close() | |
| def load(cls, path: str) -> 'TokenizerInterface': | |
| out = BPETokenizer() | |
| out.tokenizer = Tokenizer.from_file(path) | |
| out.tokenizer.decoder = ByteLevelDecoder() | |
| with open(path + ".extra", "r") as f: | |
| extra = json.load(f) | |
| f.close() | |
| out.special_token_map = extra | |
| return out | |
| def encode(self, text: str) -> list[int]: | |
| return self.tokenizer.encode(text).ids | |
| def decode(self, tokens: list[int]) -> str: | |
| return self.tokenizer.decode(tokens, False) | |
| def get_special_token(self, token_name: str) -> int: | |
| return self.tokenizer.encode(self.special_token_map[token_name]).ids[0] | |
| def vocab_size(self) -> int: | |
| return self.tokenizer.get_vocab_size() | |