File size: 811 Bytes
a063388
 
 
ea8ce67
 
3b277e3
ea8ce67
 
 
 
a063388
 
ea8ce67
a063388
3b277e3
a063388
 
 
 
 
 
 
3b277e3
ea8ce67
 
 
 
a063388
 
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
28
29
30
# emotion_detector.py
import torchaudio
from speechbrain.inference.classifiers import AudioClassifier
import os
import tempfile

# Use a temp directory that's guaranteed to be writable
temp_dir = os.path.join(tempfile.gettempdir(), "emotion_model")

# Load pretrained model into temp directory
classifier = AudioClassifier.from_hparams(
    source="speechbrain/emotion-recognition-wav2vec2-IEMOCAP",
    savedir=temp_dir
)

EMOTION_EMOJIS = {
    "angry": "😠",
    "happy": "πŸ˜„",
    "neutral": "😐",
    "sad": "😒",
    "fearful": "😨"
}

def detect_emotion(audio_path):
    signal, fs = torchaudio.load(audio_path)
    prediction = classifier.classify_file(audio_path)
    emotion = prediction[3]  # Predicted label
    emoji = EMOTION_EMOJIS.get(emotion.lower(), "❓")
    return emotion, emoji