Downloads · 30 days
15
15% of all-time downloads
VanshajR/gpt2-emotion-prefix
gpt2-emotion-prefix is a text generation model from VanshajR. Use it when you need the model to write or continue text. It is set up for transformers. The card lists the license as mit.
Fine-tuned GPT-2 model for emotion-controlled dialogue generation using natural language prefix conditioning.
Downloads · 30 days
15
15% of all-time downloads
All-time downloads
99
Public
Parameters
124M
498 MB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors498 MB · 99%
From the Hugging Face model README
Fine-tuned GPT-2 model for emotion-controlled dialogue generation using natural language prefix conditioning.
gpt2 (124M parameters)"Respond with [EMOTION] emotion: [context]")Evaluated on 6,740 test samples using RoBERTa emotion classifier:
| 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% | - |
Key Achievements:
from transformers import AutoTokenizer, AutoModelForCausalLM
# Load model and tokenizer
tokenizer = AutoTokenizer.from_pretrained("VanshajR/gpt2-emotion-prefix")
model = AutoModelForCausalLM.from_pretrained("VanshajR/gpt2-emotion-prefix")
# Generate emotion-controlled response
emotion = "happy"
context = "How was your day today?"
prefix = f"Respond with {emotion} emotion: {context}"
inputs = tokenizer(prefix, return_tensors="pt")
outputs = model.generate(
**inputs,
max_length=100,
num_return_sequences=1,
temperature=0.8,
top_p=0.9,
do_sample=True,
pad_token_id=tokenizer.eos_token_id
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(response)
# Example: "Respond with happy emotion: How was your day today? It was amazing! I got promoted at work."
🎭 happy | 😢 sad | 😠 angry | 😨 fear | 🤢 disgust | 😲 surprise | 😐 neutral
# For better results:
# 1. Use temperature 0.7-0.9 for natural responses
# 2. Use top_p=0.9 for diversity
# 3. Keep context concise (1-2 sentences)
# 4. Prefix format: "Respond with [emotion] emotion: [context]"
# Example with emotion verification
from transformers import pipeline
# Load emotion classifier for verification
classifier = pipeline("text-classification", model="VanshajR/roberta-emotion-7class")
# Generate with happy emotion
emotion = "happy"
context = "Tell me about your weekend"
prefix = f"Respond with {emotion} emotion: {context}"
inputs = tokenizer(prefix, return_tensors="pt")
outputs = model.generate(**inputs, max_length=80, temperature=0.8, top_p=0.9, do_sample=True)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Verify emotion
predicted = classifier(response)[0]
print(f"Target: {emotion}, Predicted: {predicted['label']}, Confidence: {predicted['score']:.2%}")
"Respond with [emotion] emotion: [dialogue_context]" → [response]This model uses natural language prefix conditioning - a lightweight method that doesn't modify the model architecture:
"How are you?" → "I'm fine.""Respond with happy emotion: How are you?" → "I'm doing great! Everything's wonderful!"Why Prefix > Token:
What affects accuracy:
| 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 |
Our result falls within the published range for similar lightweight methods.
✅ Recommended:
❌ Not Recommended:
@misc{vanshajr2024gpt2emotion,
author = {Vanshaj R},
title = {GPT-2 Emotion-Conditioned Text Generation via Prefix Conditioning},
year = {2024},
publisher = {HuggingFace},
url = {https://huggingface.co/VanshajR/gpt2-emotion-prefix}
}
Part of the Emotion-Controlled Response Generation project: