indic-shift-262k / tokenization_indic_shift.py
ankur-chariot's picture
Add Indic Shift 262K tokenizer
116187d verified
Raw History Blame Contribute Delete
1.72 kB
"""Indic Shift tokenizer for Hugging Face transformers.
AutoTokenizer loads this file when called with trust_remote_code=True. The tokenizer files and the
text transform come from the indic-shift package (pip install indic-shift), so the ids and the
decoded text are exactly those of indic_shift.load(...). This file only makes the package's
transformers class loadable from this repository.
https://github.com/Chariot-AI/indic-shift
"""
from __future__ import annotations
from typing import Any
_REQUIREMENT = "indic-shift>=1.0,<2"
try:
import indic_shift
from indic_shift.hf import IndicShiftTokenizer as _PackageTokenizer
except ImportError as exc:
raise ImportError(
f"This tokenizer needs the indic-shift package. Install it with: pip install '{_REQUIREMENT}'"
) from exc
if indic_shift.__version__.split(".")[0] != "1":
raise ImportError(
f"This tokenizer needs indic-shift 1.x, found {indic_shift.__version__}. "
f"Install it with: pip install '{_REQUIREMENT}'"
)
__all__ = ["IndicShiftTokenizer"]
class IndicShiftTokenizer(_PackageTokenizer):
"""The package's ``indic_shift.hf.IndicShiftTokenizer``; encoding and decoding are inherited as is."""
def __init__(self, vocab_file: str | None = None, **kwargs: Any) -> None:
# indic_shift.json names the tokenizer ("indic-shift-262k" or "indic-shift-131k"); without it
# the package would fall back to the 262K tokenizer, which is wrong for the 131K repository.
if vocab_file is None and "name" not in kwargs:
raise ValueError("indic_shift.json is missing: it names the Indic Shift tokenizer to load")
super().__init__(vocab_file=vocab_file, **kwargs)