Views
No views yet
google/gemma-4-E4B-it for emotion classification on the dair-ai/emotion dataset. The model is prompted to return exactly one label from: sadness, joy, love, anger, fear, or surprise.google/gemma-4-E4B-ittransformers + peft + trldair-ai/emotionsadness, joy, love, anger, fear, surprise16320.051e-482256paged_adamw_8bit| Stage | Accuracy | Macro F1 | Invalid predictions |
|---|---|---|---|
Base google/gemma-4-E4B-it | 0.5825 | 0.4211 | 33 |
| Fine-tuned LoRA adapter | 0.7725 | 0.6977 | 20 |
dair-ai/emotion. It is best suited for experimentation, education, and lightweight downstream NLP workflows.1#!pip install -U transformers accelerate bitsandbytes peft
2import torch
3from transformers import AutoTokenizer, BitsAndBytesConfig
4from peft import AutoPeftModelForCausalLM
5
6model_id = "kingabzpro/gemma4-emotion-lora"
7
8bnb_config = BitsAndBytesConfig(
9 load_in_4bit=True,
10 bnb_4bit_quant_type="nf4",
11 bnb_4bit_compute_dtype=torch.bfloat16,
12 bnb_4bit_use_double_quant=True,
13)
14
15tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=True)
16
17if tokenizer.pad_token is None:
18 tokenizer.pad_token = tokenizer.eos_token
19
20model = AutoPeftModelForCausalLM.from_pretrained(
21 model_id,
22 quantization_config=bnb_config,
23 device_map="cuda:0",
24 dtype=torch.bfloat16,
25)
26
27messages = [
28 {
29 "role": "system",
30 "content": (
31 "You are an emotion classification assistant. "
32 "Read the user's text and answer with exactly one label. "
33 "Only choose from: sadness, joy, love, anger, fear, surprise. "
34 "Return only the label and nothing else."
35 ),
36 },
37 {
38 "role": "user",
39 "content": "Classify the emotion of this text:\n\nThis is the best day of my life!",
40 },
41]
42
43inputs = tokenizer.apply_chat_template(
44 messages,
45 tokenize=True,
46 add_generation_prompt=True,
47 return_dict=True,
48 return_tensors="pt",
49).to(model.device)
50
51input_len = inputs["input_ids"].shape[-1]
52
53with torch.no_grad():
54 outputs = model.generate(**inputs, max_new_tokens=4)
55
56print(tokenizer.decode(outputs[0][input_len:], skip_special_tokens=True).strip())
57#joyCheck out the Kaggle notebook for testing here: Testing Gemma 4 on Human Emotions
fine-tune-gemma-4-on-emotions_final.ipynbhttps://huggingface.co/kingabzpro/gemma4-emotion-lorahttps://huggingface.co/google/gemma-4-E4B-it