Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
2from peft import PeftModel
3import torch
4
5# Load base model with 4-bit quantization
6bnb_config = BitsAndBytesConfig(
7 load_in_4bit=True,
8 bnb_4bit_quant_type="nf4",
9 bnb_4bit_compute_dtype=torch.float16,
10 bnb_4bit_use_double_quant=True,
11)
12
13base_model = AutoModelForCausalLM.from_pretrained(
14 "TinyLlama/TinyLlama-1.1B-Chat-v1.0",
15 quantization_config=bnb_config,
16 device_map="auto",
17)
18tokenizer = AutoTokenizer.from_pretrained("kellenmurerwa/AgriSmart-TinyLlama-LoRA")
19
20# Load LoRA adapter
21model = PeftModel.from_pretrained(base_model, "kellenmurerwa/AgriSmart-TinyLlama-LoRA")
22
23# Ask a question
24question = "What is the best fertilizer for rice crops?"
25prompt = f"### Instruction:
26{question}
27
28### Response:
29"
30inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
31
32with torch.no_grad():
33 outputs = model.generate(**inputs, max_new_tokens=256, temperature=0.7, top_p=0.9)
34
35response = tokenizer.decode(outputs[0], skip_special_tokens=True)
36print(response.split("### Response:
37")[-1].strip())| Parameter | Value |
|---|---|
| Base Model | TinyLlama-1.1B-Chat-v1.0 |
| Dataset | KisanVaani agriculture-qa-english-only (22,615 samples) |
| LoRA Rank | 16 |
| LoRA Alpha | 32 |
| Target Modules | q_proj, v_proj |
| Trainable Parameters | ~2.25M (0.2% of total) |
| Learning Rate | 5e-5 |
| Epochs | 2 |
| Quantization | 4-bit NF4 |
| Metric | Score |
|---|---|
| BLEU | 0.1810 |
| ROUGE-1 | 0.5129 |
| ROUGE-2 | 0.3268 |
| ROUGE-L | 0.4826 |
| Perplexity | 2.2583 |