File size: 1,318 Bytes
a704a0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from transformers import pipeline

# Emotion to tone instruction mapping
emotion_tone_map = {
    "Sadness": "Be comforting, empathetic, and gentle.",
    "Anger": "Stay calm, respectful, and de-escalate.",
    "Love": "Be warm, appreciative, and encouraging.",
    "Surprise": "Be affirming and help clarify what's surprising.",
    "Fear": "Be reassuring and emphasize safety/facts.",
    "Happiness": "Be enthusiastic and congratulatory.",
    "Neutral": "Be informative and straightforward.",
    "Disgust": "Be clinical, non-judgmental, and clarify facts.",
    "Shame": "Be kind, avoid blame, and uplift the user.",
    "Guilt": "Be compassionate and reduce self-blame.",
    "Confusion": "Be extra clear and explain step-by-step.",
    "Desire": "Be supportive and help guide constructively.",
    "Sarcasm": "Stay serious, clarify misunderstandings politely.",
}

emotion_classifier = pipeline("text-classification", model="boltuix/bert-emotion")

def get_emotion_and_tone(text):
    emotions = emotion_classifier(text)
    detected_emotion = emotions[0]["label"].capitalize() if emotions else "Neutral"
    emotion = detected_emotion if detected_emotion in emotion_tone_map else "Neutral"
    tone_instruction = emotion_tone_map.get(emotion, "Be informative and polite.")
    return emotion, tone_instruction