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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import os
import torch
import torchaudio
import numpy as np
import pandas as pd
from pathlib import Path
from typing import Tuple, Dict, List, Optional, Union
from dataclasses import dataclass
from collections import Counter
import zipfile
import shutil

from speechbrain.pretrained import EncoderClassifier
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, confusion_matrix
from imblearn.over_sampling import RandomOverSampler

import warnings
warnings.filterwarnings('ignore')
import torch
import torchaudio
import soundfile as sf

@dataclass
class Config:
    """Configuration for the language identification pipeline"""
    target_sample_rate: int = 16000
    embedding_dim: int = 256
    test_size: float = 0.2  
    random_state: int = 42
    max_iter: int = 1000

    # Language mappings for custom classifier
    label_map: Dict[str, int] = None
    canonical_languages: List[str] = None

    def __post_init__(self):
        # Now includes malay in the custom classifier
        self.label_map = {"iban": 0, "bukar_sadong": 1, "malay": 2}
        self.canonical_languages = ["malay", "english", "mandarin", "tamil"]

class AudioProcessor:
    """Handles audio loading and preprocessing"""

    def __init__(self, target_sr: int = 16000):
        self.target_sr = target_sr

    def load_audio(self, path: str) -> torch.Tensor:
        try:
            signal, sr = torchaudio.load(path)
        except RuntimeError as e:
            print(f"[WARN] torchaudio failed: {e}. Falling back to soundfile.")
            signal, sr = sf.read(path, dtype="float32")
            signal = torch.tensor(signal).T  # (channels, time)

        # Convert to mono
        if signal.shape[0] > 1:
            signal = signal.mean(dim=0, keepdim=True)

        # Resample if needed
        if sr != self.target_sr:
            resampler = torchaudio.transforms.Resample(sr, self.target_sr)
            signal = resampler(signal)

        return signal.to(torch.float32)


class LanguageIdentifier:
    """Main language identification system"""

    def __init__(self, config: Config = None):
        self.config = config or Config()
        self.audio_processor = AudioProcessor(self.config.target_sample_rate)
        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

        # Initialize models
        self.vox_model = None
        self.custom_classifier = None
        self.label_encoder = None

    def load_vox_model(self, model_path: str = None):
        """Load SpeechBrain VoxLingua107 model"""
        source = "speechbrain/lang-id-voxlingua107-ecapa"
        savedir = model_path or "pretrained_models/lang-id-voxlingua107-ecapa"

        self.vox_model = EncoderClassifier.from_hparams(
            source=source,
            savedir=savedir,
            run_opts={"device": self.device}
        )
        self.label_encoder = self.vox_model.hparams.label_encoder
        print(f"VoxLingua107 model loaded on {self.device}")

    def extract_embedding(self, audio: Union[str, torch.Tensor]) -> np.ndarray:
        """Extract embedding from audio using VoxLingua107"""
        if isinstance(audio, str):
            wav = self.audio_processor.load_audio(audio)
        else:
            wav = audio

        # Ensure batch dimension
        if wav.dim() == 1:
            wav = wav.unsqueeze(0)

        wav = wav.to(self.device, dtype=torch.float32)

        with torch.no_grad():
            embedding = self.vox_model.encode_batch(wav)
            if isinstance(embedding, tuple):
                embedding = embedding[0]
            # Flatten to 1D array
            embedding = embedding.view(embedding.size(0), -1).squeeze(0)
            return embedding.cpu().numpy()

    def normalize_language_label(self, raw_label: str) -> Optional[str]:
        """Map VoxLingua107 short codes to canonical language names"""
        label_code = raw_label.strip().lower()

        # Direct mapping from VoxLingua codes to canonical names
        vox_to_canonical = {
            "ms": "malay",
            "en": "english",
            "zh": "mandarin",
            "ta": "tamil"
        }

        return vox_to_canonical.get(label_code)

    def extract_audio_files_from_zip(self, zip_path: str, extract_dir: str) -> List[Path]:
        """Extract and return list of audio files from a zip archive"""
        temp_extract = Path(extract_dir) / Path(zip_path).stem
        if temp_extract.exists():
            shutil.rmtree(temp_extract)
        temp_extract.mkdir(parents=True)

        with zipfile.ZipFile(zip_path, 'r') as z:
            z.extractall(temp_extract)

        # Find all audio files
        audio_files = []
        for ext in ['*.wav', '*.mp3']:
            audio_files.extend(list(temp_extract.rglob(ext)))

        return sorted(audio_files)

    def train_custom_classifier(self, drive_base: str = "/content/drive"):
        """Train custom classifier for Iban/Bukar Sadong/Malay"""
        print("Training custom 3-language classifier...")

        # Temporary extraction directory
        temp_dir = Path("/tmp/training_data")
        if temp_dir.exists():
            shutil.rmtree(temp_dir)
        temp_dir.mkdir(parents=True)

        all_embeddings = []
        all_labels = []
        language_files = {"iban": [], "bukar_sadong": [], "malay": []}

        # Process Iban data (from two sources)
        print("\nProcessing Iban data...")
        iban_zips = [
            f"{drive_base}/MyDrive/language_identification/training_data/github_iban_filter_train.zip",
            f"{drive_base}/MyDrive/language_identification/training_data/gkalaka_iban_filter_train.zip"
        ]

        for zip_path in iban_zips:
            if os.path.exists(zip_path):
                print(f"Extracting {Path(zip_path).name}...")
                audio_files = self.extract_audio_files_from_zip(zip_path, str(temp_dir))
                language_files["iban"].extend(audio_files)
                print(f"Found {len(audio_files)} files")

        # Process Malay data
        print("\nProcessing Malay data...")
        malay_zip = f"{drive_base}/MyDrive/language_identification/training_data/malay_train.zip"
        if os.path.exists(malay_zip):
            audio_files = self.extract_audio_files_from_zip(malay_zip, str(temp_dir))
            language_files["malay"].extend(audio_files)
            print(f"Found {len(audio_files)} Malay files")

        # Process Bukar Sadong data
        print("\nProcessing Bukar Sadong data...")
        bukar_zip = f"{drive_base}/MyDrive/language_identification/training_data/bukar_sadong_train.zip"
        if os.path.exists(bukar_zip):
            audio_files = self.extract_audio_files_from_zip(bukar_zip, str(temp_dir))
            language_files["bukar_sadong"].extend(audio_files)
            print(f"Found {len(audio_files)} Bukar Sadong files")

        # Extract embeddings for each language
        for lang, files in language_files.items():
            print(f"\nExtracting embeddings for {lang}: {len(files)} files")
            for i, audio_file in enumerate(files):
                if i % 100 == 0:
                    print(f"Processing {lang}: {i}/{len(files)}")
                try:
                    emb = self.extract_embedding(str(audio_file))
                    all_embeddings.append(emb)
                    all_labels.append(self.config.label_map[lang])
                except Exception as e:
                    print(f"Error processing {audio_file}: {e}")

        if not all_embeddings:
            raise ValueError("No training data collected")

        X = np.array(all_embeddings)
        y = np.array(all_labels)

        print(f"\nTotal samples collected:")
        print(f"Iban: {np.sum(y == 0)}")
        print(f"Bukar Sadong: {np.sum(y == 1)}")
        print(f"Malay: {np.sum(y == 2)}")

        # Stratified split ensuring 20% from each language
        X_train, X_test, y_train, y_test = train_test_split(
            X, y, test_size=self.config.test_size,
            stratify=y, random_state=self.config.random_state
        )

        print(f"\nTraining set distribution:")
        for i, lang in enumerate(["iban", "bukar_sadong", "malay"]):
            print(f"{lang}: {np.sum(y_train == i)}")

        # Apply oversampling to balance the training set
        # Given the huge imbalance (48 vs 2895), we'll use a moderate sampling strategy
        ros = RandomOverSampler(
            sampling_strategy='not majority',  # Oversample minority classes
            random_state=self.config.random_state
        )
        X_train_balanced, y_train_balanced = ros.fit_resample(X_train, y_train)

        print(f"\nAfter oversampling:")
        for i, lang in enumerate(["iban", "bukar_sadong", "malay"]):
            print(f"{lang}: {np.sum(y_train_balanced == i)}")

        # Train classifier
        self.custom_classifier = LogisticRegression(
            max_iter=self.config.max_iter,
            random_state=self.config.random_state,
            class_weight='balanced'  # Additional balancing
        )
        self.custom_classifier.fit(X_train_balanced, y_train_balanced)

        # Evaluate
        y_pred = self.custom_classifier.predict(X_test)
        print("\n" + "="*60)
        print("Custom Classifier Performance:")
        print("="*60)
        print(classification_report(y_test, y_pred,
                                   target_names=["iban", "bukar_sadong", "malay"]))

        print("\nConfusion Matrix:")
        cm = confusion_matrix(y_test, y_pred)
        print("       Iban  Bukar  Malay")
        for i, row in enumerate(cm):
            label = ["Iban  ", "Bukar ", "Malay "][i]
            print(f"{label} {row}")

        # Cleanup
        shutil.rmtree(temp_dir)

        return self.custom_classifier

    @torch.no_grad()
    def predict_vox(self, audio: Union[str, torch.Tensor]) -> Tuple[str, float, List]:
        """Predict using VoxLingua107 for major languages"""
        if isinstance(audio, str):
            wav = self.audio_processor.load_audio(audio)
        else:
            wav = audio

        if wav.dim() == 1:
            wav = wav.unsqueeze(0)

        wav = wav.to(self.device, dtype=torch.float32)

        # Get predictions
        output = self.vox_model.classify_batch(wav)
        logits = output[0] if isinstance(output, tuple) else output
        logits = logits.squeeze(0).detach().cpu()

        # Convert to probabilities
        if logits.max().item() <= 1.0:  
            probs = logits.exp()
            probs = probs / probs.sum()
        else:
            probs = logits

        # Get top prediction
        top_prob, top_idx = torch.max(probs, dim=0)
        top_prob = float(top_prob.item())

        # Decode label
        try:
            raw_label = self.label_encoder.ind2lab[int(top_idx)]
        except:
            raw_label = self.label_encoder.decode_ndim(int(top_idx))
        raw_label = raw_label.split(":")[0].strip().lower()

        # Get canonical name
        canonical = self.normalize_language_label(raw_label)

        # Get top-5 for debugging
        topk = torch.topk(probs, k=min(5, probs.shape[0]))
        top_results = []
        for prob, idx in zip(topk.values.tolist(), topk.indices.tolist()):
            try:
                label = self.label_encoder.ind2lab[int(idx)]
            except:
                label = self.label_encoder.decode_ndim(int(idx))
            top_results.append((label, float(prob)))

        return canonical if canonical else raw_label, top_prob, top_results

    def predict_custom(self, audio: Union[str, torch.Tensor]) -> Tuple[str, float]:
        """Predict using custom Iban/Bukar Sadong/Malay classifier"""
        emb = self.extract_embedding(audio)
        proba = self.custom_classifier.predict_proba([emb])[0]
        pred_idx = np.argmax(proba)

        inv_label_map = {v: k for k, v in self.config.label_map.items()}
        return inv_label_map[pred_idx], float(proba[pred_idx])

    def predict(self, audio: Union[str, torch.Tensor]) -> Dict:
        """Main prediction method combining both classifiers"""
        # First, get VoxLingua107 prediction
        vox_lang, vox_score, top_results = self.predict_vox(audio)

        # Check if VoxLingua predicted one of the 4 major languages
        major_languages = ["english", "mandarin", "tamil", "malay"]

        # Condition 1: If not a major language, pass to custom classifier
        if vox_lang not in major_languages:
            custom_lang, custom_score = self.predict_custom(audio)
            return {
                'language': custom_lang,
                'confidence': custom_score,
                'source': 'custom_classifier',
                'reason': 'non_major_language',
                'vox_initial': {'language': vox_lang, 'confidence': vox_score},
                'debug': {'vox_top_5': top_results}
            }

        # Condition 2: If VoxLingua predicts Malay, compare with custom classifier
        if vox_lang == "malay":
            custom_lang, custom_score = self.predict_custom(audio)

            # Compare scores and take the higher confidence prediction
            if custom_score > vox_score:
                # Custom classifier has higher confidence
                return {
                    'language': custom_lang,
                    'confidence': custom_score,
                    'source': 'custom_classifier',
                    'reason': 'higher_confidence',
                    'vox_initial': {'language': vox_lang, 'confidence': vox_score},
                    'custom_scores': {
                        'iban': float(self.custom_classifier.predict_proba([self.extract_embedding(audio)])[0][0]),
                        'bukar_sadong': float(self.custom_classifier.predict_proba([self.extract_embedding(audio)])[0][1]),
                        'malay': float(self.custom_classifier.predict_proba([self.extract_embedding(audio)])[0][2])
                    },
                    'debug': {'vox_top_5': top_results}
                }
            else:
                # VoxLingua has higher confidence, keep Malay
                return {
                    'language': 'malay',
                    'confidence': vox_score,
                    'source': 'voxlingua107',
                    'reason': 'higher_confidence',
                    'custom_comparison': {'language': custom_lang, 'confidence': custom_score},
                    'custom_scores': {
                        'iban': float(self.custom_classifier.predict_proba([self.extract_embedding(audio)])[0][0]),
                        'bukar_sadong': float(self.custom_classifier.predict_proba([self.extract_embedding(audio)])[0][1]),
                        'malay': float(self.custom_classifier.predict_proba([self.extract_embedding(audio)])[0][2])
                    },
                    'debug': {'top_5': top_results}
                }

        # For English, Mandarin, Tamil - use VoxLingua result directly
        return {
            'language': vox_lang,
            'confidence': vox_score,
            'source': 'voxlingua107',
            'debug': {'top_5': top_results}
        }


class Evaluator:
    """Evaluate performance on test datasets"""

    def __init__(self, identifier: LanguageIdentifier):
        self.identifier = identifier

    def test_zip_file(self, zip_path: str, true_label: Optional[str] = None,
                      verbose: bool = True) -> Dict:
        """Test on a zip file containing audio files"""
        # Extract files
        extract_dir = Path(f"/tmp/test_{Path(zip_path).stem}")
        if extract_dir.exists():
            shutil.rmtree(extract_dir)
        extract_dir.mkdir(parents=True)

        with zipfile.ZipFile(zip_path, 'r') as z:
            z.extractall(extract_dir)

        # Find all audio files
        audio_files = list(extract_dir.rglob("*.wav"))
        audio_files.extend(list(extract_dir.rglob("*.mp3")))
        audio_files.sort()

        if not audio_files:
            print(f"No audio files found in {zip_path}")
            return {}

        results = []
        source_counts = Counter()
        language_counts = Counter()
        reason_counts = Counter()

        for audio_file in audio_files:
            try:
                pred = self.identifier.predict(str(audio_file))
                results.append(pred)
                source_counts[pred['source']] += 1
                language_counts[pred['language']] += 1
                if 'reason' in pred:
                    reason_counts[pred['reason']] += 1

                if verbose:
                    status = ""
                    if true_label:
                        status = "✓" if pred['language'] == true_label else "✗"

                    # Build detailed output string
                    output_str = f"{audio_file.name:<30}{pred['language']:<12} [{pred['confidence']:.3f}]"

                    # Add source and reason if available
                    if 'reason' in pred:
                        output_str += f" via {pred['source']:<20} (reason: {pred['reason']})"
                    else:
                        output_str += f" via {pred['source']:<20}"

                    # Add comparison info if available
                    if 'custom_comparison' in pred:
                        comp = pred['custom_comparison']
                        output_str += f" [vs {comp['language']}:{comp['confidence']:.3f}]"
                    elif 'vox_initial' in pred:
                        vox = pred['vox_initial']
                        output_str += f" [vox:{vox['language']}:{vox['confidence']:.3f}]"

                    print(f"{output_str} {status}")

            except Exception as e:
                print(f"Error processing {audio_file.name}: {e}")

        # Calculate accuracy if true label provided
        accuracy = None
        if true_label:
            correct = sum(1 for r in results if r['language'] == true_label)
            accuracy = correct / len(results) if results else 0
            print(f"\nAccuracy for '{true_label}': {accuracy:.1%} ({correct}/{len(results)})")

        print(f"\nSource usage: {dict(source_counts)}")
        print(f"Language predictions: {dict(language_counts)}")
        if reason_counts:
            print(f"Decision reasons: {dict(reason_counts)}")

        # Cleanup
        shutil.rmtree(extract_dir)

        return {
            'total': len(results),
            'results': results,
            'source_counts': dict(source_counts),
            'language_counts': dict(language_counts),
            'reason_counts': dict(reason_counts),
            'accuracy': accuracy
        }