File size: 6,491 Bytes
8f96165
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f5f788
 
 
 
 
 
 
8f96165
 
 
3f5f788
 
 
 
 
 
8f96165
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import time
import torch
import librosa
import numpy as np
import gradio as gr
import gradio as gr
from .generate_graph import create_behaviour_gantt_plot
from transformers import Wav2Vec2Processor


SAMPLING_RATE = 16_000

class AudioProcessor:
    def __init__(
        self,
        emotion_model,
        segmentation_model,
        device,
        behaviour_model=None,
    ):
        self.emotion_processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
        self.emotion_model = emotion_model
        self.behaviour_model = behaviour_model
        self.device = device
        self.audio_emotion_labels = {
            0: "Neutralità",
            1: "Rabbia",
            2: "Paura",
            3: "Gioia",
            4: "Sorpresa",
            5: "Tristezza",
            6: "Disgusto",
        }
        self.emotion_translation = {
            "neutrality": "Neutralità",
            "anger": "Rabbia",
            "fear": "Paura",
            "joy": "Gioia",
            "surprise": "Sorpresa",
            "sadness": "Tristezza",
            "disgust": "Disgusto"
        }
        self.behaviour_labels = {
            0: "frustrated",
            1: "delighted",
            2: "dysregulated",
        }
        self.behaviour_translation = {
            "frustrated": "frustazione",
            "delighted": "incantato",
            "dysregulated": "disregolazione",    
        }
        self.segmentation_model = segmentation_model

        self._set_emotion_model()
        if self.behaviour_model:
            self._set_behaviour_model()

        self.behaviour_confidence = 0.6

        self.chart_generator = None

    def _set_emotion_model(self):
        self.emotion_model.to(self.device)
        self.emotion_model.eval()

    def _set_behaviour_model(self):
        self.behaviour_model.to(self.device)
        self.behaviour_model.eval()

    def _prepare_transcribed_text(self, chunks):
        formated_timestamps = []
        predictions = []

        for chunk in chunks:
            start = chunk[0] / SAMPLING_RATE
            end = chunk[1] / SAMPLING_RATE
            formated_start = time.strftime('%H:%M:%S', time.gmtime(start))
            formated_end = time.strftime('%H:%M:%S', time.gmtime(end))
            formated_timestamps.append(f"**({formated_start} - {formated_end})**")

            predictions.append(f"**[{chunk[2]}]**")

        transcribed_texts = [chunk[3] for chunk in chunks]
        transcribed_text = "<br/>".join(
            [
                f"{formated_timestamps[i]}: {transcribed_texts[i]} {predictions[i]}" for i in range(len(transcribed_texts))
            ]
        )

        print(f"Transcribed text:\n{transcribed_text}")


        return transcribed_text

    def __call__(self, audio_path: str):
        """
        Predicts the emotion label for a given audio input.

        Args:
            audio (filepath): The audio input path to be processed.

        Returns:
            str: The predicted emotion label.

        """
        try:
            input_frames, _ = librosa.load(
                audio_path, 
                sr=SAMPLING_RATE
            ) 
        except Exception as e:
            gr.Error(f"Error loading audio file: {e}.")

        print("Segmenting audio...")
        out = self.segmentation_model(
            inputs={
                "raw": input_frames,
                "sampling_rate": SAMPLING_RATE,
            }, 
            chunk_length_s=30, 
            stride_length_s=5,
            return_timestamps=True,
        )

        emotion_chunks = []
        behaviour_chunks = []
        timestamps = []
        predicted_labels = []
        all_probabilities = []

        print("Analizing chunks...")
        for chunk in out["chunks"]:                
            # trim audio from timestamps
            start = int(chunk["timestamp"][0] * SAMPLING_RATE)
            end = int(chunk["timestamp"][1] * SAMPLING_RATE if chunk["timestamp"][1] else len(input_frames))

            audio = input_frames[start:end]

            inputs = self.emotion_processor(audio, chunk["text"], return_tensors="pt", sampling_rate=SAMPLING_RATE)

            print(f"Inputs: {inputs}")

            if "input_values" in inputs:
                inputs["input_features"] = inputs.pop("input_values")

            inputs['input_features'] = inputs['input_features'].to(self.device)
            inputs['input_ids'] = inputs['input_ids'].to(self.device)
            inputs['text_attention_mask'] = inputs['text_attention_mask'].to(self.device)

            print("Predicting emotion for chunk...")
            logits = self.emotion_model(**inputs).logits

            logits = logits.detach().cpu()
            softmax = torch.nn.Softmax(dim=1)
            probabilities = softmax(logits).squeeze(0)

            prediction = probabilities.argmax().item()

            predicted_label = self.emotion_processor.config.id2label[prediction]
            label_translation = self.emotion_translation[predicted_label]

            emotion_chunks.append(
                (
                    start, 
                    end,
                    label_translation,
                    chunk["text"], 
                    np.round(probabilities[prediction].item(), 2)
                )
            )

            timestamps.append((start, end))
            predicted_labels.append(label_translation)
            all_probabilities.append(probabilities[prediction].item())


            inputs = self.emotion_processor(audio, return_tensors="pt", sampling_rate=SAMPLING_RATE)
            if "input_values" in inputs:
                inputs["input_features"] = inputs.pop("input_values")
            
            inputs = inputs.input_features.to(self.device)
            print("Predicting behaviour for chunk...")
            logits = self.behaviour_model(inputs).logits
            probabilities = torch.nn.functional.softmax(logits.detach().cpu(), dim=-1).squeeze()
            behaviour_chunks.append(
                (
                    start, 
                    end,
                    chunk["text"],
                    np.round(probabilities[2].item(), 2),
                    label_translation,
                )
            )
        behaviour_gantt = create_behaviour_gantt_plot(behaviour_chunks)

        # transcribed_text = self._prepare_transcribed_text(emotion_chunks)
        
        return (
            behaviour_gantt,
            # transcribed_text,
        )