add Python source: model, tokenizer, checkpoint, data, train, train_more, infer + config
dfeab58 verified Download scripts/tokenizer.py from Fantominsight/Polina-Lumen-NumDetective: direct link, hf CLI and curl.
- Browser
- Download file 3.39 kB
-
https://huggingface.co/Fantominsight/Polina-Lumen-NumDetective/resolve/main/scripts/tokenizer.py
- Command line
-
hf download hf://Fantominsight/Polina-Lumen-NumDetective/scripts/tokenizer.py
-
curl -L -o tokenizer.py https://huggingface.co/Fantominsight/Polina-Lumen-NumDetective/resolve/main/scripts/tokenizer.py
3.39 kB
| import json | |
| import os | |
| from dataclasses import dataclass | |
| from typing import Dict, List, Sequence | |
| TOKENIZER_NAME = "tokenizer.json" | |
| class DigitTokenizer: | |
| num_digits: int = 10 | |
| pad_id: int = 10 | |
| eos_id: int = 11 | |
| pad_token: str = "<pad>" | |
| eos_token: str = "<eos>" | |
| def vocab_size(self) -> int: | |
| return self.num_digits + 2 | |
| def id_to_token(self) -> List[str]: | |
| return [str(d) for d in range(self.num_digits)] + [self.pad_token, self.eos_token] | |
| def token_to_id(self, token: str) -> int: | |
| tokens = self.id_to_token | |
| if token not in tokens: | |
| raise KeyError(f"unknown token: {token!r}") | |
| return tokens.index(token) | |
| def encode(self, digits: Sequence[int], add_eos: bool = True) -> List[int]: | |
| for d in digits: | |
| if not 0 <= int(d) < self.num_digits: | |
| raise ValueError(f"digit out of range: {d}") | |
| ids = [int(d) for d in digits] | |
| if add_eos: | |
| ids.append(self.eos_id) | |
| return ids | |
| def decode(self, ids: Sequence[int], skip_special: bool = True) -> str: | |
| out = [] | |
| for i in ids: | |
| i = int(i) | |
| if i == self.eos_id: | |
| if not skip_special: | |
| out.append(self.eos_token) | |
| break | |
| if i == self.pad_id: | |
| if not skip_special: | |
| out.append(self.pad_token) | |
| continue | |
| if 0 <= i < self.num_digits: | |
| out.append(str(i)) | |
| return "".join(out) | |
| def pad_batch(self, sequences: Sequence[Sequence[int]]) -> List[List[int]]: | |
| max_len = max((len(s) for s in sequences), default=0) | |
| return [list(s) + [self.pad_id] * (max_len - len(s)) for s in sequences] | |
| def from_config(cls, cfg: Dict) -> "DigitTokenizer": | |
| m = cfg["model"] | |
| return cls( | |
| num_digits=int(m["vocab_size"]) - 2, | |
| pad_id=int(m["pad_id"]), | |
| eos_id=int(m["eos_id"]), | |
| ) | |
| def to_dict(self) -> Dict: | |
| return { | |
| "class": "DigitTokenizer", | |
| "num_digits": self.num_digits, | |
| "vocab_size": self.vocab_size, | |
| "pad_id": self.pad_id, | |
| "eos_id": self.eos_id, | |
| "pad_token": self.pad_token, | |
| "eos_token": self.eos_token, | |
| "vocab": {str(i): t for i, t in enumerate(self.id_to_token)}, | |
| "id_to_token": self.id_to_token, | |
| } | |
| def from_dict(cls, d: Dict) -> "DigitTokenizer": | |
| if d.get("class") != "DigitTokenizer": | |
| raise ValueError(f"unexpected tokenizer class: {d.get('class')!r}") | |
| return cls( | |
| num_digits=int(d["num_digits"]), | |
| pad_id=int(d["pad_id"]), | |
| eos_id=int(d["eos_id"]), | |
| pad_token=d.get("pad_token", "<pad>"), | |
| eos_token=d.get("eos_token", "<eos>"), | |
| ) | |
| def save(self, path: str) -> str: | |
| os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True) | |
| with open(path, "w", encoding="utf-8") as f: | |
| json.dump(self.to_dict(), f, ensure_ascii=False, indent=2) | |
| return path | |
| def load(cls, path: str) -> "DigitTokenizer": | |
| with open(path, "r", encoding="utf-8") as f: | |
| return cls.from_dict(json.load(f)) |