"""MANTA 20M whole-word inference.""" from pathlib import Path import torch from huggingface_hub import snapshot_download from tokenizers import Tokenizer from transformers import AutoModelForCausalLM from .decoder import GenerativeKeyboard __version__ = '0.1.0' __all__ = ['MantaKeyboard'] class MantaKeyboard(GenerativeKeyboard): """Whole-word next-word prediction and prefix-constrained completion.""" @classmethod def from_pretrained(cls, model_id, *, revision='v0.1.0', device='cpu', local_files_only=False, beam_size=10, max_steps=64, max_chars=48, context_tokens=128, max_seconds=float('inf')): """Load a local directory or a Hub release using standard Transformers. Search is deterministic in FP32 with fixed token and beam budgets. A finite max_seconds adds a wall-clock deadline to the search. """ folder = Path(model_id) if not folder.is_dir(): folder = Path(snapshot_download( str(model_id), revision=revision, local_files_only=local_files_only, allow_patterns=['config.json', 'model.safetensors', 'tokenizer.json', 'tokenizer_config.json', 'generation_config.json'])) model = AutoModelForCausalLM.from_pretrained( folder, local_files_only=True, trust_remote_code=False, attn_implementation='eager', torch_dtype=torch.float32).to(device) return cls(model, Tokenizer.from_file(str(folder / 'tokenizer.json')), beam_size=beam_size, max_steps=max_steps, max_chars=max_chars, context_tokens=context_tokens, max_seconds=max_seconds)