Views
No views yet
| Training N | Test N | Macro F1 | |
|---|---|---|---|
| Original (all labels) | 24,000 | 3,000 | 0.8923 |
| Spot-check (this adapter) | 18,865 | 2,372 | 0.9497 |
| Δ | +0.0574 |
| Parameter | Value |
|---|---|
r | 16 |
lora_alpha | 32 |
lora_dropout | 0.05 |
target_modules | q_proj, k_proj, v_proj, o_proj |
| Epochs | 3 |
| Learning rate | 2 × 10⁻⁴ |
| Precision | bfloat16 |
1from transformers import AutoTokenizer, AutoModelForCausalLM
2from peft import PeftModel
3import torch
4
5BASE_MODEL = "meta-llama/Llama-3.1-8B-Instruct"
6ADAPTER = "Sravanigunnu/llama-3.1-8b-macd-telugu-spotcheck-lora"
7
8tokenizer = AutoTokenizer.from_pretrained(ADAPTER, use_fast=True)
9base = AutoModelForCausalLM.from_pretrained(BASE_MODEL, torch_dtype=torch.bfloat16, device_map="auto")
10model = PeftModel.from_pretrained(base, ADAPTER)
11model.eval()
12
13SYSTEM = (
14 "You are a hate speech classifier for social media content. "
15 "Classify the given text as abusive or non-abusive. "
16 "Reply with only '1' if the text contains hate speech or abuse, "
17 "or '0' if it is non-abusive. Do not explain your answer."
18)
19
20def classify(text: str) -> int:
21 messages = [{"role": "system", "content": SYSTEM},
22 {"role": "user", "content": text}]
23 prompt = tokenizer.apply_chat_template(
24 messages, tokenize=False, add_generation_prompt=True
25 ) + "Label: "
26 base_ids = tokenizer.encode(prompt, add_special_tokens=False)
27 id0 = tokenizer.encode(prompt + "0", add_special_tokens=False)[len(base_ids)]
28 id1 = tokenizer.encode(prompt + "1", add_special_tokens=False)[len(base_ids)]
29 ids = tokenizer(prompt, return_tensors="pt").input_ids.to(model.device)
30 with torch.no_grad():
31 logits = model(ids).logits[0, -1, :]
32 probs = torch.softmax(torch.stack([logits[id0], logits[id1]]), dim=-1)
33 return int(probs.argmax().item())