Spaces:
Sleeping
Sleeping
| 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 |