SalgrAI_v2.1 / chat_example.py
salgr's picture
Upload 14 files
b87f745 verified
Raw History Blame Contribute Delete
1.15 kB
"""Minimal chat with SalgrAI-v2.1 (transformers). Run: python chat_example.py [path-or-repo]. Commands: /reset, /exit."""
import sys
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
repo = sys.argv[1] if len(sys.argv) > 1 else "."
tok = AutoTokenizer.from_pretrained(repo)
model = AutoModelForCausalLM.from_pretrained(repo, dtype=torch.bfloat16).eval()
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) # 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)