"""Minimal chat with SalgrAI-v2-Full (transformers). Run: python chat_example.py [path-or-repo] The vocabulary mask in vocab_mask.py forbids tokens of other scripts (Gemma's vocabulary covers all languages; without the mask the model sometimes emits stray Chinese/Arabic characters). Commands: /reset, /exit.""" import sys import torch from transformers import AutoModelForCausalLM, AutoTokenizer, LogitsProcessorList from vocab_mask import RuAllowed repo = sys.argv[1] if len(sys.argv) > 1 else "." tok = AutoTokenizer.from_pretrained(repo) model = AutoModelForCausalLM.from_pretrained(repo, dtype=torch.bfloat16).eval() mask = LogitsProcessorList([RuAllowed(tok, model.config.vocab_size)]) history = [] while True: try: text = input("you> ").strip() except EOFError: break if text == "/exit": break if text == "/reset": history = [] continue if not text: continue history.append({"role": "user", "content": text}) inputs = tok.apply_chat_template(history, add_generation_prompt=True, return_tensors="pt", return_dict=True) inputs.pop("token_type_ids", None) with torch.no_grad(): out = model.generate(**inputs, logits_processor=mask) # sampling settings come from generation_config.json reply = tok.decode(out[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True).strip() history.append({"role": "assistant", "content": reply}) history = history[-8:] print("ai>", reply)