Views
No views yet
gpt2 (124M parameters)"Respond with [EMOTION] emotion: [context]")| Model | Emotion Accuracy | Improvement vs Baseline |
|---|---|---|
| Prefix-Small (This Model) | 38.2% ⭐ | +9.8pp |
| Token-Small | 30.8% | +2.5pp |
| Prefix-Medium (355M) | 35.6% | +7.3pp |
| Baseline (no conditioning) | 28.3% | - |
| Random Baseline | 14.3% | - |
1from transformers import AutoTokenizer, AutoModelForCausalLM
2
3# Load model and tokenizer
4tokenizer = AutoTokenizer.from_pretrained("VanshajR/gpt2-emotion-prefix")
5model = AutoModelForCausalLM.from_pretrained("VanshajR/gpt2-emotion-prefix")
6
7# Generate emotion-controlled response
8emotion = "happy"
9context = "How was your day today?"
10prefix = f"Respond with {emotion} emotion: {context}"
11
12inputs = tokenizer(prefix, return_tensors="pt")
13outputs = model.generate(
14 **inputs,
15 max_length=100,
16 num_return_sequences=1,
17 temperature=0.8,
18 top_p=0.9,
19 do_sample=True,
20 pad_token_id=tokenizer.eos_token_id
21)
22
23response = tokenizer.decode(outputs[0], skip_special_tokens=True)
24print(response)
25# Example: "Respond with happy emotion: How was your day today? It was amazing! I got promoted at work."1# For better results:
2# 1. Use temperature 0.7-0.9 for natural responses
3# 2. Use top_p=0.9 for diversity
4# 3. Keep context concise (1-2 sentences)
5# 4. Prefix format: "Respond with [emotion] emotion: [context]"
6
7# Example with emotion verification
8from transformers import pipeline
9
10# Load emotion classifier for verification
11classifier = pipeline("text-classification", model="VanshajR/roberta-emotion-7class")
12
13# Generate with happy emotion
14emotion = "happy"
15context = "Tell me about your weekend"
16prefix = f"Respond with {emotion} emotion: {context}"
17
18inputs = tokenizer(prefix, return_tensors="pt")
19outputs = model.generate(**inputs, max_length=80, temperature=0.8, top_p=0.9, do_sample=True)
20response = tokenizer.decode(outputs[0], skip_special_tokens=True)
21
22# Verify emotion
23predicted = classifier(response)[0]
24print(f"Target: {emotion}, Predicted: {predicted['label']}, Confidence: {predicted['score']:.2%}")"Respond with [emotion] emotion: [dialogue_context]" → [response]"How are you?" → "I'm fine.""Respond with happy emotion: How are you?" → "I'm doing great! Everything's wonderful!"| Paper | Model Size | Emotion Acc. | Dataset |
|---|---|---|---|
| Colombo et al. (2019) | 117M | 35-42% | DailyDialog |
| Zhou et al. (2018) | 256M | ~45% | DailyDialog |
| Rashkin et al. (2019) | 345M-1.5B | 40-48% | EmpatheticDialogues |
| This Work | 124M | 38.2% | DailyDialog |
1@misc{vanshajr2024gpt2emotion,
2 author = {Vanshaj R},
3 title = {GPT-2 Emotion-Conditioned Text Generation via Prefix Conditioning},
4 year = {2024},
5 publisher = {HuggingFace},
6 url = {https://huggingface.co/VanshajR/gpt2-emotion-prefix}
7}