File size: 3,841 Bytes
857b1b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
import sys
import time
import logging
import librosa
import numpy as np
import torch

from torch.nn import functional as F
from huggingface_hub import hf_hub_download

logger = logging.getLogger(__name__)

# HuggingFace repo
AVAILABLE_MODELS = {
    "dual_emotion": "vyluong/emo_dual_classi"
}

emotion_labels = ['Angry', 'Anxiety', 'Happy', 'Sad', 'Neutral']

EMOTION_META = {
    "Angry": {"emoji": "😑", "color": "#ff4d4f"},
    "Anxiety": {"emoji": "😰", "color": "#faad14"},
    "Happy": {"emoji": "😊", "color": "#52c41a"},
    "Sad": {"emoji": "😒", "color": "#1890ff"},
    "Neutral": {"emoji": "😐", "color": "#d9d9d9"},
}



class EmotionService:

    _models = {}
    
    emotion_labels = emotion_labels
    meta = EMOTION_META

    @classmethod
    def load_dual_model(cls, repo_id, device):

        logger.info(f"Downloading model from HF: {repo_id}")

        model_file = hf_hub_download(
            repo_id=repo_id,
            filename="pytorch_model.bin"
        )

        model_code = hf_hub_download(
            repo_id=repo_id,
            filename="model.py"
        )

        # add model folder to python path
        import importlib.util
        
        spec = importlib.util.spec_from_file_location("hf_model", model_code)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
        
        Dual = module.Dual

        model = Dual()

        state_dict = torch.load(model_file, map_location=device)

        model.load_state_dict(state_dict)

        model.to(device)
        model.eval()

        logger.info("Emotion model loaded successfully")

        return model



    @classmethod
    def get_model(cls, model_name="dual_emotion"):

        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

        if model_name in cls._models:
            return cls._models[model_name]

        repo_id = AVAILABLE_MODELS[model_name]

        model = cls.load_dual_model(repo_id, device)

        cls._models[model_name] = model

        return model


    @classmethod
    def preload_model(cls):

        logger.info("Preloading emotion model...")

        cls.get_model()

        logger.info("Emotion model ready")


    # extract mfcc from segments
    @staticmethod
    def extract_mfcc_segment(
        audio: np.ndarray,
        sr: int,
        start: float,
        end: float,
        duration: float = 5.0,
        n_mfcc: int = 128,
        n_fft: int = 2048,
        hop_length: int = 512
    ):

        start_sample = int(start * sr)
        end_sample = int(end * sr)

        segment = audio[start_sample:end_sample]

        if len(segment) == 0:
            return None

        target_len = int(sr * duration)

        if len(segment) < target_len:
            segment = np.pad(segment,(0,target_len-len(segment)),mode="symmetric")
        else:
            segment = segment[:target_len]

        mfcc = librosa.feature.mfcc(
            y=segment,
            sr=sr,
            n_mfcc=n_mfcc,
            n_fft=n_fft,
            hop_length=hop_length
        )

        return mfcc


    @classmethod
    def predict_from_mfcc(cls, mfcc):

        model = cls.get_model()

        tensor = torch.from_numpy(mfcc).unsqueeze(0).unsqueeze(0).float()

        device = next(model.parameters()).device
        tensor = tensor.to(device)

        with torch.no_grad():

            output = model(tensor)

        probs = F.softmax(output.squeeze(), dim=0).cpu().numpy()

        label = cls.emotion_labels[np.argmax(probs)]

        return label


    # predict from segments
    @classmethod
    def predict_segment(cls, audio, sr, start, end):

        mfcc = cls.extract_mfcc_segment(audio, sr, start, end)

        if mfcc is None:
            return "Neutral"

        return cls.predict_from_mfcc(mfcc)