Views
No views yet
1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3from peft import PeftModel
4from unsloth import is_bfloat16_supported
5
6# Precision
7dtype = torch.bfloat16 if is_bfloat16_supported() else torch.float16
8
9# Models
10BASE_MODEL = "unsloth/Meta-Llama-3.1-8B-Instruct-bnb-4bit"
11LORA_MODEL = "sag-uniroma2/FrameLLaMA-3.1-8B-Instruct-FullFN17"
12
13# Load base model
14base_model = AutoModelForCausalLM.from_pretrained(
15 BASE_MODEL,
16 torch_dtype=dtype,
17 device_map="auto"
18)
19
20# Load LoRA
21model = PeftModel.from_pretrained(base_model, LORA_MODEL)
22
23# Load tokenizer
24tokenizer = AutoTokenizer.from_pretrained(LORA_MODEL, use_fast=True)
25
26# Device
27device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
28model = model.to(device)
29
30# 🔥 ===== YOUR SAMPLE HERE =====
31premise = "John drowned Martha."
32hypothesis = "Martha died."
33
34# Prompt
35input_text = f"""
36 Judge if the hypothesis necessarily follows from the premise.
37 Consider the truth value of the premise. If the premise is true, does it necessarily mean that the hypothesis must also be true?
38 Output E if the hypothesis must always be true.
39 Output C if the hypothesis must always be false.
40 Output N if the hypothesis may be either true or false.
41 Do not output anything other than letters E, C, or N.
42
43 Premise: {premise}
44 Hypothesis: {hypothesis}
45 # Output:"""
46
47# Tokenize
48inputs = tokenizer(input_text, return_tensors="pt").to(device)
49
50# Generate
51with torch.no_grad():
52 output_ids = model.generate(
53 **inputs,
54 max_new_tokens=10,
55 do_sample=False
56 )
57
58# Decode only generated part
59input_len = inputs["input_ids"].shape[1]
60generated = output_ids[0][input_len:]
61response = tokenizer.decode(generated, skip_special_tokens=True).strip()
62
63# Print result
64print("Premise:", premise)
65print("Hypothesis:", hypothesis)
66print("Prediction:", response)