import torchfrom peft import PeftModelfrom transformers import AutoModelForCausalLM, AutoTokenizer base = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"tok = AutoTokenizer.from_pretrained(base)model = AutoModelForCausalLM.from_pretrained(base, torch_dtype=torch.float16, device_map="auto")model = PeftModel.from_pretrained(model, "Falcot922/tinyllama-sentiment-distilled").eval() prompt = ("Analyze the sentiment of this movie review and respond with exactly one word: " "positive or negative.\n\nReview: {text}\nSentiment:")text = prompt.format(text="A wonderful, moving film.")n = len(tok(text)["input_ids"])pos, neg = (tok(text + w)["input_ids"][n] for w in (" positive", " negative")) # first token of each answerwith torch.no_grad(): last = model(**tok(text, return_tensors="pt").to(model.device)).logits[0, -1]print("positive" if last[pos] > last[neg] else "negative")