Download tokenization_vortex.py from North-ML1/vortex-alpha: direct link, hf CLI and curl.
- Browser
- Download file 2.69 kB
-
https://huggingface.co/North-ML1/vortex-alpha/resolve/main/tokenization_vortex.py
- Command line
-
hf download hf://North-ML1/vortex-alpha/tokenization_vortex.py
-
curl -L -o tokenization_vortex.py https://huggingface.co/North-ML1/vortex-alpha/resolve/main/tokenization_vortex.py
2.69 kB
| """SentencePiece tokenizer wrapper for Hugging Face AutoTokenizer.""" | |
| from __future__ import annotations | |
| import shutil | |
| from pathlib import Path | |
| from transformers import PreTrainedTokenizer | |
| class VortexTokenizer(PreTrainedTokenizer): | |
| vocab_files_names = {"vocab_file": "tokenizer.model"} | |
| model_input_names = ["input_ids", "attention_mask"] | |
| def __init__( | |
| self, | |
| vocab_file: str, | |
| unk_token: str = "<unk>", | |
| eos_token: str = "</s>", | |
| **kwargs, | |
| ) -> None: | |
| import sentencepiece as spm | |
| self.vocab_file = str(vocab_file) | |
| self.sp_model = spm.SentencePieceProcessor(model_file=self.vocab_file) | |
| super().__init__(unk_token=unk_token, eos_token=eos_token, **kwargs) | |
| def vocab_size(self) -> int: | |
| return int(self.sp_model.vocab_size()) | |
| def get_vocab(self) -> dict[str, int]: | |
| vocab = {self.sp_model.id_to_piece(i): i for i in range(self.vocab_size)} | |
| vocab.update(self.added_tokens_encoder) | |
| return vocab | |
| def _tokenize(self, text: str, **kwargs) -> list[str]: | |
| return list(self.sp_model.encode(text, out_type=str)) | |
| def _convert_token_to_id(self, token: str) -> int: | |
| token_id = int(self.sp_model.piece_to_id(token)) | |
| return self.unk_token_id if token_id < 0 else token_id | |
| def _convert_id_to_token(self, index: int) -> str: | |
| return self.sp_model.id_to_piece(int(index)) | |
| def convert_tokens_to_string(self, tokens: list[str]) -> str: | |
| return self.sp_model.decode_pieces(tokens) | |
| def build_inputs_with_special_tokens( | |
| self, | |
| token_ids_0: list[int], | |
| token_ids_1: list[int] | None = None, | |
| ) -> list[int]: | |
| if token_ids_1 is None: | |
| return list(token_ids_0) | |
| return list(token_ids_0) + list(token_ids_1) | |
| def get_special_tokens_mask( | |
| self, | |
| token_ids_0: list[int], | |
| token_ids_1: list[int] | None = None, | |
| already_has_special_tokens: bool = False, | |
| ) -> list[int]: | |
| if already_has_special_tokens: | |
| return [0] * len(token_ids_0) | |
| total = len(token_ids_0) + (len(token_ids_1) if token_ids_1 is not None else 0) | |
| return [0] * total | |
| def save_vocabulary(self, save_directory: str, filename_prefix: str | None = None) -> tuple[str]: | |
| directory = Path(save_directory) | |
| directory.mkdir(parents=True, exist_ok=True) | |
| name = (filename_prefix + "-" if filename_prefix else "") + "tokenizer.model" | |
| output = directory / name | |
| if Path(self.vocab_file).resolve() != output.resolve(): | |
| shutil.copyfile(self.vocab_file, output) | |
| return (str(output),) | |