File size: 11,118 Bytes
d5f8ae0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e824b96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5f8ae0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f141fc2
 
 
 
 
4fcfcbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f141fc2
 
4fcfcbc
 
f141fc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5f8ae0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f141fc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5f8ae0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import re
import os
import numpy as np
import torch
import librosa
import librosa.display
import matplotlib
import matplotlib.pyplot as plt
from PIL import Image
from torchvision import transforms
import whisper

# Force non-interactive backend for server environments
matplotlib.use('Agg')

# ==========================================
# 0. Segmentation CSV Parser
# ==========================================
def parse_segmentation_csv(csv_content: bytes) -> list:
    """
    Parse segmentation CSV to extract PAR speaker intervals.
    CSV format: speaker,start_ms,end_ms
    Returns list of (start_ms, end_ms) tuples for PAR speaker only.
    """
    intervals = []
    try:
        lines = csv_content.decode('utf-8', errors='replace').strip().split('\n')
        for i, line in enumerate(lines):
            if i == 0 and 'speaker' in line.lower():
                continue  # Skip header
            parts = line.strip().split(',')
            if len(parts) >= 3 and parts[0].strip().upper() == 'PAR':
                start_ms = int(parts[1].strip())
                end_ms = int(parts[2].strip())
                intervals.append((start_ms, end_ms))
    except Exception as e:
        print(f"Error parsing segmentation CSV: {e}")
    return intervals



# ==========================================
# 1. Linguistic Feature Extractor
# ==========================================
class LinguisticFeatureExtractor:
    def __init__(self):
        self.patterns = {
            'fillers': re.compile(r'&-([a-z]+)', re.IGNORECASE),
            'repetition': re.compile(r'\[/+\]'),
            'retracing': re.compile(r'\[//\]'),
            'incomplete': re.compile(r'\+[\./]+'),
            'errors': re.compile(r'\[\*.*?\]'),
            'pauses': re.compile(r'\(\.+\)')
        }

    def clean_for_bert(self, raw_text):
        text = re.sub(r'^\*PAR:\s+', '', raw_text)
        text = re.sub(r'\x15\d+_\d+\x15', '', text)
        text = re.sub(r'<|>', '', text)
        text = re.sub(r'\[.*?\]', '', text)
        text = re.sub(r'\(\.+\)', '[PAUSE]', text)
        text = text.replace('_', ' ')
        text = re.sub(r'\s+', ' ', text).strip()
        if text.endswith('[PAUSE]'):
            text = text[:-7].strip()
        return text

    def get_features(self, raw_text):
        stats = {
            'filler_count': len(self.patterns['fillers'].findall(raw_text)),
            'repetition_count': len(self.patterns['repetition'].findall(raw_text)),
            'retracing_count': len(self.patterns['retracing'].findall(raw_text)),
            'incomplete_count': len(self.patterns['incomplete'].findall(raw_text)),
            'error_count': len(self.patterns['errors'].findall(raw_text)),
            'pause_count': len(self.patterns['pauses'].findall(raw_text))
        }
        clean_for_stats = re.sub(r'\[.*?\]', '', raw_text)
        clean_for_stats = re.sub(r'&-([a-z]+)', '', clean_for_stats)
        clean_for_stats = re.sub(r'[^\w\s]', '', clean_for_stats)
        words = clean_for_stats.lower().split()
        stats['word_count'] = len(words)
        return stats

    def get_feature_vector(self, raw_text):
        stats = self.get_features(raw_text)
        n = stats['word_count'] if stats['word_count'] > 0 else 1
        
        # Calculate TTR (Type-Token Ratio)
        clean_for_stats = re.sub(r'\[.*?\]', '', raw_text)
        clean_for_stats = re.sub(r'&-([a-z]+)', '', clean_for_stats)
        clean_for_stats = re.sub(r'[^\w\s]', '', clean_for_stats)
        words = clean_for_stats.lower().split()
        ttr = (len(set(words)) / n) if n > 0 else 0.0

        return np.array([
            ttr,
            stats['filler_count'] / n,
            stats['repetition_count'] / n,
            stats['retracing_count'] / n,
            stats['error_count'] / n,
            stats['pause_count'] / n
        ], dtype=np.float32)

    def extract_key_segments(self, text, max_segments=3):
        """
        Extract sentences with highest linguistic marker density.
        Returns list of {text, marker_count} sorted by marker count.
        """
        # Split into segments using multiple delimiters:
        # - Sentence endings (.?!)
        # - Newlines
        # - Timestamp markers (common in CHA files)
        segments = re.split(r'[.?!\n]+|\x15\d+_\d+\x15', text)
        segments = [s.strip() for s in segments if s.strip()]
        
        # If no segments found, try splitting by long spaces or just use the whole text
        if not segments and text.strip():
            # Split by multiple spaces or use chunks of ~50 words
            words = text.split()
            if len(words) > 15:
                # Create chunks of ~15 words each
                chunk_size = 15
                segments = [' '.join(words[i:i+chunk_size]) for i in range(0, len(words), chunk_size)]
            else:
                segments = [text.strip()]
        
        scored = []
        for sent in segments:
            # Count markers in each segment
            count = 0
            count += len(self.patterns['fillers'].findall(sent))
            count += len(self.patterns['repetition'].findall(sent))
            count += len(self.patterns['retracing'].findall(sent))
            count += len(self.patterns['pauses'].findall(sent))
            count += len(self.patterns['errors'].findall(sent))
            # Also count [PAUSE] tokens from ASR
            count += sent.count('[PAUSE]')
            count += sent.count('[/]')
            
            if len(sent) > 10:  # Skip very short fragments
                scored.append({"text": sent, "marker_count": count})
        
        # Sort by marker count descending
        scored.sort(key=lambda x: x['marker_count'], reverse=True)
        return scored[:max_segments]

# ==========================================
# 2. Audio Processor
# ==========================================
class AudioProcessor:
    def __init__(self):
        self.vit_transforms = transforms.Compose([
            transforms.Resize((224, 224)),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])

    def create_spectrogram_tensor(self, audio_path, intervals=None):
        """
        Generates spectrogram image and transforms it to Tensor.
        """
        try:
            fig = plt.figure(figsize=(2.24, 2.24), dpi=100)
            ax = fig.add_subplot(1, 1, 1)
            fig.subplots_adjust(left=0, right=1, bottom=0, top=1)

            if intervals:
                # Load full audio then slice based on timestamps
                y, sr = librosa.load(audio_path, sr=None)
                clips = []
                for start_ms, end_ms in intervals:
                    start_sample = int(start_ms * sr / 1000)
                    end_sample = int(end_ms * sr / 1000)
                    if end_sample > len(y): end_sample = len(y)
                    if start_sample < len(y):
                        clips.append(y[start_sample:end_sample])
                if clips:
                    y = np.concatenate(clips)
                else:
                    y = np.zeros(int(sr*30))
                
                # Limit to 30s
                if len(y) > 30 * sr:
                    y = y[:30 * sr]
            else:
                y, sr = librosa.load(audio_path, duration=30)

            ms = librosa.feature.melspectrogram(y=y, sr=sr)
            log_ms = librosa.power_to_db(ms, ref=np.max)
            librosa.display.specshow(log_ms, sr=sr, ax=ax)

            # Save to buffer instead of file
            from io import BytesIO
            buf = BytesIO()
            fig.savefig(buf, format='png')
            plt.close(fig)
            buf.seek(0)

            image = Image.open(buf).convert('RGB')
            return self.vit_transforms(image).unsqueeze(0)
            
        except Exception as e:
            print(f"Spectrogram creation failed: {e}")
            return torch.zeros((1, 3, 224, 224))

    def create_spectrogram_base64(self, audio_path, intervals=None):
        """
        Generates spectrogram and returns as base64 string for visualization.
        """
        import base64
        from io import BytesIO
        
        try:
            fig = plt.figure(figsize=(4, 3), dpi=100)
            ax = fig.add_subplot(1, 1, 1)
            
            if intervals:
                y, sr = librosa.load(audio_path, sr=None)
                clips = []
                for start_ms, end_ms in intervals:
                    start_sample = int(start_ms * sr / 1000)
                    end_sample = int(end_ms * sr / 1000)
                    if end_sample > len(y): end_sample = len(y)
                    if start_sample < len(y):
                        clips.append(y[start_sample:end_sample])
                if clips:
                    y = np.concatenate(clips)
                else:
                    y = np.zeros(int(sr*30))
                if len(y) > 30 * sr:
                    y = y[:30 * sr]
            else:
                y, sr = librosa.load(audio_path, duration=30)

            ms = librosa.feature.melspectrogram(y=y, sr=sr)
            log_ms = librosa.power_to_db(ms, ref=np.max)
            
            img = librosa.display.specshow(log_ms, sr=sr, x_axis='time', y_axis='mel', ax=ax)
            fig.colorbar(img, ax=ax, format='%+2.0f dB')
            ax.set_title('Mel-Spectrogram')
            
            buf = BytesIO()
            fig.savefig(buf, format='png', bbox_inches='tight')
            plt.close(fig)
            buf.seek(0)
            
            b64_str = base64.b64encode(buf.read()).decode('utf-8')
            return f"data:image/png;base64,{b64_str}"
            
        except Exception as e:
            print(f"Spectrogram base64 creation failed: {e}")
            return None

# ==========================================
# 3. ASR Helper (Whisper + CHAT Rules)
# ==========================================
def apply_chat_rules(transcription_result):
    """
    Converts Whisper result into CHAT-like format AND inserts [PAUSE] tokens.
    """
    formatted_text = []
    segments = transcription_result.get('segments', [])
    last_end = 0
    
    for seg in segments:
        gap = seg['start'] - last_end
        # Insert [PAUSE] token + CHAT marker
        if gap > 0.8:
            formatted_text.append("[PAUSE] (..)")
        elif gap > 0.3:
            formatted_text.append("[PAUSE] (.)")
            
        text = seg['text'].strip()
        
        # Repetitions (Basic Detection)
        words = text.split()
        processed_words = []
        for i, w in enumerate(words):
            clean_w = re.sub(r'[^a-zA-Z]', '', w.lower())
            if i > 0:
                prev_clean = re.sub(r'[^a-zA-Z]', '', words[i-1].lower())
                if clean_w == prev_clean and clean_w: 
                    processed_words[-1] = f"{words[i-1]} [/]"
            processed_words.append(w)
            
        formatted_text.append(" ".join(processed_words))
        last_end = seg['end']
        
    return " ".join(formatted_text)