umpirix/otsone/laya_mlx/tokenizer.py
caelum0x 4847e35451 OpenJev: open-source typed decision service
Typed decision/classification service with an in-process gte-small ONNX
embedding engine and lexical fallback, JS/Python SDKs, adapters, and the
otsone model family. Apache-2.0 (sub-packages keep their MIT licenses).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 00:12:36 +03:00

28 lines
1.1 KiB
Python

"""Load the checkpoint's Rust tokenizer without importing Transformers or torch."""
import json
from pathlib import Path
from tokenizers import Tokenizer as Backend
class Tokenizer:
def __init__(self, path):
path = Path(path)
self.backend = Backend.from_file(str(path / "tokenizer.json"))
self.backend.no_padding()
self.backend.no_truncation()
config = json.loads((path / "tokenizer_config.json").read_text())
for name in ("cls_token", "sep_token", "pad_token", "mask_token"):
value = config.get(name)
if isinstance(value, dict):
value = value.get("content")
token_id = self.backend.token_to_id(value) if isinstance(value, str) else None
if token_id is None:
raise ValueError(f"Tokenizer is missing a valid {name}")
setattr(self, name, value)
setattr(self, name + "_id", token_id)
def __call__(self, text, add_special_tokens=False):
return {"input_ids": self.backend.encode(text, add_special_tokens=add_special_tokens).ids}