Views
No views yet
| Metric | Before (base, zero-shot) | After (LoRA fine-tuned) | Improvement |
|---|---|---|---|
| ROUGE-1 | 0.138 | 0.238 | +72% |
| ROUGE-2 | — | 0.096 | — |
| ROUGE-L | — | 0.184 | — |
| ROUGE-Lsum | — | 0.191 | — |
1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
3from peft import PeftModel
4
5base_model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
6adapter_name = "your-username/tinyllama-xsum-lora" # replace with your repo id
7
8bnb_config = BitsAndBytesConfig(
9 load_in_4bit=True,
10 bnb_4bit_quant_type="nf4",
11 bnb_4bit_compute_dtype=torch.float16,
12)
13
14tokenizer = AutoTokenizer.from_pretrained(base_model_name)
15base_model = AutoModelForCausalLM.from_pretrained(
16 base_model_name,
17 quantization_config=bnb_config,
18 torch_dtype=torch.float16,
19 device_map="auto",
20)
21model = PeftModel.from_pretrained(base_model, adapter_name)
22
23prompt = (
24 "<|system|>\nYou are a helpful assistant that summarizes news articles "
25 "in one short sentence.</s>\n"
26 "<|user|>\nSummarize the following article:\n{your_article_here}</s>\n"
27 "<|assistant|>\n"
28)
29
30inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
31output = model.generate(**inputs, max_new_tokens=60, do_sample=False)
32print(tokenizer.decode(output[0], skip_special_tokens=True))