File size: 24,194 Bytes
f8f2ee2
 
 
 
 
 
545efe4
 
 
f8f2ee2
 
 
7140878
f8f2ee2
 
bed979a
21231d9
7140878
f8f2ee2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bed979a
 
f8f2ee2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7140878
f8f2ee2
 
7140878
da16507
 
 
 
f8f2ee2
 
 
7140878
f8f2ee2
 
bed979a
f8f2ee2
 
545efe4
 
f8f2ee2
 
 
 
 
 
 
 
 
 
 
 
 
545efe4
f8f2ee2
 
545efe4
f8f2ee2
 
 
 
 
 
545efe4
f8f2ee2
 
 
 
 
 
 
 
 
 
 
7140878
545efe4
f8f2ee2
 
 
545efe4
7140878
545efe4
 
f8f2ee2
 
7140878
545efe4
f8f2ee2
 
545efe4
7140878
f8f2ee2
 
 
 
 
545efe4
7140878
f8f2ee2
 
 
 
 
545efe4
 
f8f2ee2
 
 
21231d9
f8f2ee2
 
 
7140878
f8f2ee2
 
 
 
7140878
f8f2ee2
 
7140878
f8f2ee2
 
7140878
 
f8f2ee2
da16507
 
f8f2ee2
 
 
 
 
 
 
 
 
7140878
 
f8f2ee2
545efe4
 
f8f2ee2
21231d9
f8f2ee2
947815d
21231d9
f8f2ee2
 
 
 
21231d9
f8f2ee2
7140878
f8f2ee2
 
7140878
f8f2ee2
 
 
545efe4
da16507
 
 
 
 
f8f2ee2
 
da16507
 
f8f2ee2
 
545efe4
f8f2ee2
da16507
f8f2ee2
 
 
21231d9
f8f2ee2
7140878
f8f2ee2
7140878
 
f8f2ee2
7140878
f8f2ee2
7140878
 
 
 
 
 
 
f8f2ee2
7140878
 
f8f2ee2
 
 
 
 
 
 
da16507
 
f8f2ee2
 
 
545efe4
21231d9
545efe4
21231d9
f8f2ee2
 
 
 
 
 
7140878
f8f2ee2
7140878
f8f2ee2
 
947815d
 
f8f2ee2
947815d
7140878
947815d
 
 
 
7140878
 
947815d
545efe4
7140878
545efe4
da16507
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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
import math
import os
import tempfile
from dataclasses import dataclass
from typing import List, Optional, Tuple

import gradio as gr
import numpy as np
import soundfile as sf
import torch
import torch.nn.functional as F
from pydantic import BaseModel
from scipy.signal import resample as scipy_resample
from torch import nn
from torch.nn.utils import weight_norm
from huggingface_hub import hf_hub_download


# =========================================================
# AudioVAE model definition (single-file, standalone)
# =========================================================

def WNConv1d(*args, **kwargs):
    return weight_norm(nn.Conv1d(*args, **kwargs))


def WNConvTranspose1d(*args, **kwargs):
    return weight_norm(nn.ConvTranspose1d(*args, **kwargs))


class CausalConv1d(nn.Conv1d):
    def __init__(self, *args, padding: int = 0, output_padding: int = 0, **kwargs):
        super().__init__(*args, **kwargs)
        self.__padding = padding
        self.__output_padding = output_padding

    def forward(self, x):
        x_pad = F.pad(x, (self.__padding * 2 - self.__output_padding, 0))
        return super().forward(x_pad)


class CausalTransposeConv1d(nn.ConvTranspose1d):
    def __init__(self, *args, padding: int = 0, output_padding: int = 0, **kwargs):
        super().__init__(*args, **kwargs)
        self.__padding = padding
        self.__output_padding = output_padding

    def forward(self, x):
        return super().forward(x)[..., : -(self.__padding * 2 - self.__output_padding)]


def WNCausalConv1d(*args, **kwargs):
    return weight_norm(CausalConv1d(*args, **kwargs))


def WNCausalTransposeConv1d(*args, **kwargs):
    return weight_norm(CausalTransposeConv1d(*args, **kwargs))


@torch.jit.script
def snake(x, alpha):
    shape = x.shape
    x = x.reshape(shape[0], shape[1], -1)
    x = x + (alpha + 1e-9).reciprocal() * torch.sin(alpha * x).pow(2)
    x = x.reshape(shape)
    return x


class Snake1d(nn.Module):
    def __init__(self, channels):
        super().__init__()
        self.alpha = nn.Parameter(torch.ones(1, channels, 1))

    def forward(self, x):
        return snake(x, self.alpha)


class CausalResidualUnit(nn.Module):
    def __init__(self, dim: int = 16, dilation: int = 1, kernel: int = 7, groups: int = 1):
        super().__init__()
        pad = ((7 - 1) * dilation) // 2
        self.block = nn.Sequential(
            Snake1d(dim),
            WNCausalConv1d(
                dim,
                dim,
                kernel_size=kernel,
                dilation=dilation,
                padding=pad,
                groups=groups,
            ),
            Snake1d(dim),
            WNCausalConv1d(dim, dim, kernel_size=1),
        )

    def forward(self, x):
        y = self.block(x)
        pad = (x.shape[-1] - y.shape[-1]) // 2
        assert pad == 0
        if pad > 0:
            x = x[..., pad:-pad]
        return x + y


class CausalEncoderBlock(nn.Module):
    def __init__(self, output_dim: int = 16, input_dim=None, stride: int = 1, groups=1):
        super().__init__()
        input_dim = input_dim or output_dim // 2
        self.block = nn.Sequential(
            CausalResidualUnit(input_dim, dilation=1, groups=groups),
            CausalResidualUnit(input_dim, dilation=3, groups=groups),
            CausalResidualUnit(input_dim, dilation=9, groups=groups),
            Snake1d(input_dim),
            WNCausalConv1d(
                input_dim,
                output_dim,
                kernel_size=2 * stride,
                stride=stride,
                padding=math.ceil(stride / 2),
                output_padding=stride % 2,
            ),
        )

    def forward(self, x):
        return self.block(x)


class CausalEncoder(nn.Module):
    def __init__(
        self,
        d_model: int = 64,
        latent_dim: int = 32,
        strides: list = [2, 4, 8, 8],
        depthwise: bool = False,
    ):
        super().__init__()
        self.block = [WNCausalConv1d(1, d_model, kernel_size=7, padding=3)]

        for stride in strides:
            d_model *= 2
            groups = d_model // 2 if depthwise else 1
            self.block += [CausalEncoderBlock(output_dim=d_model, stride=stride, groups=groups)]

        self.fc_mu = WNCausalConv1d(d_model, latent_dim, kernel_size=3, padding=1)
        self.fc_logvar = WNCausalConv1d(d_model, latent_dim, kernel_size=3, padding=1)

        self.block = nn.Sequential(*self.block)
        self.enc_dim = d_model

    def forward(self, x):
        hidden_state = self.block(x)
        return {
            "hidden_state": hidden_state,
            "mu": self.fc_mu(hidden_state),
            "logvar": self.fc_logvar(hidden_state),
        }


class NoiseBlock(nn.Module):
    def __init__(self, dim):
        super().__init__()
        self.linear = WNCausalConv1d(dim, dim, kernel_size=1, bias=False)

    def forward(self, x):
        B, C, T = x.shape
        noise = torch.randn((B, 1, T), device=x.device, dtype=x.dtype)
        h = self.linear(x)
        n = noise * h
        return x + n


class CausalDecoderBlock(nn.Module):
    def __init__(
        self,
        input_dim: int = 16,
        output_dim: int = 8,
        stride: int = 1,
        groups=1,
        use_noise_block: bool = False,
    ):
        super().__init__()
        layers = [
            Snake1d(input_dim),
            WNCausalTransposeConv1d(
                input_dim,
                output_dim,
                kernel_size=2 * stride,
                stride=stride,
                padding=math.ceil(stride / 2),
                output_padding=stride % 2,
            ),
        ]
        if use_noise_block:
            layers.append(NoiseBlock(output_dim))
        layers.extend(
            [
                CausalResidualUnit(output_dim, dilation=1, groups=groups),
                CausalResidualUnit(output_dim, dilation=3, groups=groups),
                CausalResidualUnit(output_dim, dilation=9, groups=groups),
            ]
        )
        self.block = nn.Sequential(*layers)
        self.input_channels = input_dim

    def forward(self, x):
        return self.block(x)


class TransposeLastTwoDim(torch.nn.Module):
    def forward(self, x):
        return torch.transpose(x, -1, -2)


class SampleRateConditionLayer(nn.Module):
    def __init__(
        self,
        input_dim: int,
        sr_bin_buckets: int = None,
        cond_type: str = "scale_bias",
        cond_dim: int = 128,
        out_layer: bool = False,
    ):
        super().__init__()

        self.cond_type, out_layer_in_dim = cond_type, input_dim

        if cond_type == "scale_bias":
            self.scale_embed = nn.Embedding(sr_bin_buckets, input_dim)
            self.bias_embed = nn.Embedding(sr_bin_buckets, input_dim)
            nn.init.ones_(self.scale_embed.weight)
            nn.init.zeros_(self.bias_embed.weight)
        elif cond_type == "scale_bias_init":
            self.scale_embed = nn.Embedding(sr_bin_buckets, input_dim)
            self.bias_embed = nn.Embedding(sr_bin_buckets, input_dim)
            nn.init.normal_(self.scale_embed.weight, mean=1)
            nn.init.normal_(self.bias_embed.weight)
        elif cond_type == "add":
            self.cond_embed = nn.Embedding(sr_bin_buckets, input_dim)
            nn.init.normal_(self.cond_embed.weight)
        elif cond_type == "concat":
            self.cond_embed = nn.Embedding(sr_bin_buckets, cond_dim)
            assert out_layer, "out_layer must be True for concat cond_type"
            out_layer_in_dim = input_dim + cond_dim
        else:
            raise ValueError(f"Invalid cond_type: {cond_type}")

        if out_layer:
            self.out_layer = nn.Sequential(
                Snake1d(out_layer_in_dim),
                WNCausalConv1d(out_layer_in_dim, input_dim, kernel_size=1),
            )
        else:
            self.out_layer = nn.Identity()

    def forward(self, x, sr_cond):
        if self.cond_type in ("scale_bias", "scale_bias_init"):
            x = x * self.scale_embed(sr_cond).unsqueeze(-1) + self.bias_embed(sr_cond).unsqueeze(-1)
        elif self.cond_type == "add":
            x = x + self.cond_embed(sr_cond).unsqueeze(-1)
        elif self.cond_type == "concat":
            x = torch.cat([x, self.cond_embed(sr_cond).unsqueeze(-1).repeat(1, 1, x.shape[-1])], dim=1)

        return self.out_layer(x)


class CausalDecoder(nn.Module):
    def __init__(
        self,
        input_channel,
        channels,
        rates,
        depthwise: bool = False,
        d_out: int = 1,
        use_noise_block: bool = False,
        sr_bin_boundaries: List[int] = None,
        cond_type: str = "scale_bias",
        cond_dim: int = 128,
        cond_out_layer: bool = False,
    ):
        super().__init__()

        if depthwise:
            layers = [
                WNCausalConv1d(input_channel, input_channel, kernel_size=7, padding=3, groups=input_channel),
                WNCausalConv1d(input_channel, channels, kernel_size=1),
            ]
        else:
            layers = [WNCausalConv1d(input_channel, channels, kernel_size=7, padding=3)]

        for i, stride in enumerate(rates):
            input_dim = channels // 2**i
            output_dim = channels // 2 ** (i + 1)
            groups = output_dim if depthwise else 1
            layers += [
                CausalDecoderBlock(
                    input_dim,
                    output_dim,
                    stride,
                    groups=groups,
                    use_noise_block=use_noise_block,
                )
            ]

        layers += [
            Snake1d(output_dim),
            WNCausalConv1d(output_dim, d_out, kernel_size=7, padding=3),
            nn.Tanh(),
        ]

        if sr_bin_boundaries is None:
            self.model = nn.Sequential(*layers)
            self.sr_bin_boundaries = None
        else:
            self.model = nn.ModuleList(layers)
            self.register_buffer("sr_bin_boundaries", torch.tensor(sr_bin_boundaries, dtype=torch.int32))
            self.sr_bin_buckets = len(sr_bin_boundaries) + 1

            cond_layers = []
            for layer in self.model:
                if layer.__class__.__name__ == "CausalDecoderBlock":
                    cond_layers.append(
                        SampleRateConditionLayer(
                            input_dim=layer.input_channels,
                            sr_bin_buckets=self.sr_bin_buckets,
                            cond_type=cond_type,
                            cond_dim=cond_dim,
                            out_layer=cond_out_layer,
                        )
                    )
                else:
                    cond_layers.append(None)
            self.sr_cond_model = nn.ModuleList(cond_layers)

    def get_sr_idx(self, sr):
        return torch.bucketize(sr, self.sr_bin_boundaries)

    def forward(self, x, sr_cond=None):
        if self.sr_bin_boundaries is not None:
            sr_cond = self.get_sr_idx(sr_cond)
            for layer, sr_cond_layer in zip(self.model, self.sr_cond_model):
                if sr_cond_layer is not None:
                    x = sr_cond_layer(x, sr_cond)
                x = layer(x)
            return x
        return self.model(x)


class AudioVAEConfig(BaseModel):
    encoder_dim: int = 128
    encoder_rates: List[int] = [2, 5, 8, 8]
    latent_dim: int = 64
    decoder_dim: int = 2048
    decoder_rates: List[int] = [8, 6, 5, 2, 2, 2]
    depthwise: bool = True
    sample_rate: int = 16000
    out_sample_rate: int = 48000
    use_noise_block: bool = False
    sr_bin_boundaries: Optional[List[int]] = [20000, 30000, 40000]
    cond_type: str = "scale_bias"
    cond_dim: int = 128
    cond_out_layer: bool = False


class AudioVAE(nn.Module):
    def __init__(self, config: AudioVAEConfig = None):
        if config is None:
            config = AudioVAEConfig()

        super().__init__()

        self.encoder_dim = config.encoder_dim
        self.encoder_rates = config.encoder_rates
        self.decoder_dim = config.decoder_dim
        self.decoder_rates = config.decoder_rates
        self.depthwise = config.depthwise
        self.use_noise_block = config.use_noise_block

        latent_dim = config.latent_dim
        if latent_dim is None:
            latent_dim = config.encoder_dim * (2 ** len(config.encoder_rates))

        self.latent_dim = latent_dim
        self.hop_length = int(np.prod(config.encoder_rates))

        self.encoder = CausalEncoder(
            config.encoder_dim,
            latent_dim,
            config.encoder_rates,
            depthwise=config.depthwise,
        )

        self.decoder = CausalDecoder(
            latent_dim,
            config.decoder_dim,
            config.decoder_rates,
            depthwise=config.depthwise,
            use_noise_block=config.use_noise_block,
            sr_bin_boundaries=config.sr_bin_boundaries,
            cond_type=config.cond_type,
            cond_dim=config.cond_dim,
            cond_out_layer=config.cond_out_layer,
        )

        self.sample_rate = config.sample_rate
        self.out_sample_rate = config.out_sample_rate
        self.sr_bin_boundaries = config.sr_bin_boundaries
        self.chunk_size = math.prod(config.encoder_rates)
        self.decode_chunk_size = math.prod(config.decoder_rates)

    def preprocess(self, audio_data, sample_rate):
        if sample_rate is None:
            sample_rate = self.sample_rate
        assert sample_rate == self.sample_rate
        pad_to = self.hop_length
        length = audio_data.shape[-1]
        right_pad = math.ceil(length / pad_to) * pad_to - length
        audio_data = nn.functional.pad(audio_data, (0, right_pad))
        return audio_data

    def decode(self, z: torch.Tensor, sr_cond: torch.Tensor = None):
        if self.sr_bin_boundaries is not None and sr_cond is None:
            sr_cond = torch.tensor([self.out_sample_rate], device=z.device, dtype=torch.int32)
        return self.decoder(z, sr_cond)

    def streaming_decode(self):
        return StreamingVAEDecoder(self)

    def encode(self, audio_data: torch.Tensor, sample_rate: int):
        if audio_data.ndim == 2:
            audio_data = audio_data.unsqueeze(1)
        audio_data = self.preprocess(audio_data, sample_rate)
        return self.encoder(audio_data)["mu"]


class StreamingVAEDecoder:
    def __init__(self, vae: AudioVAE):
        self._vae = vae
        self._states: dict = {}
        self._originals: list = []

    def __enter__(self):
        self._states.clear()
        self._install()
        return self

    def __exit__(self, *exc):
        self._restore()
        self._states.clear()

    def decode_chunk(self, z_chunk: torch.Tensor) -> torch.Tensor:
        return self._vae.decode(z_chunk)

    def _install(self):
        for _, mod in self._vae.decoder.named_modules():
            if isinstance(mod, CausalConv1d):
                pad = mod._CausalConv1d__padding * 2 - mod._CausalConv1d__output_padding
                if pad > 0:
                    self._patch_causal_conv(mod, pad)
            elif isinstance(mod, CausalTransposeConv1d):
                trim = mod._CausalTransposeConv1d__padding * 2 - mod._CausalTransposeConv1d__output_padding
                ctx = (mod.kernel_size[0] - 1) // mod.stride[0]
                if ctx > 0:
                    self._patch_transpose_conv(mod, ctx, trim)

    def _patch_causal_conv(self, mod, pad_size):
        states = self._states
        key = id(mod)
        orig = mod.forward

        def fwd(x, _k=key, _p=pad_size, _m=mod):
            x_pad = torch.cat([states[_k], x], dim=-1) if _k in states else F.pad(x, (_p, 0))
            if x.shape[-1] >= _p:
                states[_k] = x[:, :, -_p:].detach()
            else:
                prev = states.get(_k, torch.zeros(x.shape[0], x.shape[1], _p, device=x.device, dtype=x.dtype))
                states[_k] = torch.cat([prev, x], dim=-1)[:, :, -_p:].detach()
            return nn.Conv1d.forward(_m, x_pad)

        mod.forward = fwd
        self._originals.append((mod, orig))

    def _patch_transpose_conv(self, mod, ctx, trim):
        states = self._states
        key = id(mod)
        orig = mod.forward

        def fwd(x, _k=key, _c=ctx, _t=trim, _m=mod):
            x_full = torch.cat([states[_k], x], dim=-1) if _k in states else F.pad(x, (_c, 0))
            states[_k] = x[:, :, -_c:].detach()
            out = nn.ConvTranspose1d.forward(_m, x_full)
            left = _c * _m.stride[0]
            return out[..., left:-_t] if _t > 0 else out[..., left:]

        mod.forward = fwd
        self._originals.append((mod, orig))

    def _restore(self):
        for mod, orig in self._originals:
            mod.forward = orig
        self._originals.clear()


# =========================================================
# Loading utilities
# =========================================================

REPO_ID = os.environ.get("AUDIOVAE_REPO", "openbmb/VoxCPM2")
WEIGHTS_NAME = os.environ.get("AUDIOVAE_WEIGHTS", "audiovae.pth")
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
TARGET_SR = 16000


@dataclass
class LoadedCodec:
    model: AudioVAE
    device: str

    @property
    def sample_rate(self) -> int:
        return int(self.model.sample_rate)

    @property
    def out_sample_rate(self) -> int:                  # ✅ NEW: expose out_sample_rate
        return int(self.model.out_sample_rate)

    @property
    def hop_length(self) -> int:
        return int(self.model.hop_length)

    def encode(self, wav: torch.Tensor) -> torch.Tensor:
        return self.model.encode(wav, self.sample_rate)

    def decode(self, z: torch.Tensor) -> torch.Tensor:
        return self.model.decode(z)


def _pick_state_dict(obj):
    if isinstance(obj, dict):
        for key in ("state_dict", "model", "vae", "audio_vae", "module"):
            if key in obj and isinstance(obj[key], dict):
                return obj[key]
    return obj


@torch.inference_mode()
def load_codec(repo_id: str = REPO_ID, filename: str = WEIGHTS_NAME, device: str = DEVICE) -> LoadedCodec:
    path = hf_hub_download(repo_id=repo_id, filename=filename)
    ckpt = torch.load(path, map_location="cpu")
    state = _pick_state_dict(ckpt)

    model = AudioVAE()
    missing, unexpected = model.load_state_dict(state, strict=False)

    model.to(device).eval()
    print(f"[load] repo={repo_id} file={filename} device={device}")
    if missing:
        print(f"[load] missing keys: {len(missing)}")
    if unexpected:
        print(f"[load] unexpected keys: {len(unexpected)}")

    return LoadedCodec(model=model, device=device)


codec = load_codec()


# =========================================================
# Audio helpers
# =========================================================

def load_audio_file(path: str) -> Tuple[np.ndarray, int]:
    audio, sr = sf.read(path, dtype="float32")
    if audio.ndim > 1:
        audio = audio.mean(axis=1)
    return audio.astype(np.float32), int(sr)


def resample_audio(audio: np.ndarray, orig_sr: int, target_sr: int) -> np.ndarray:
    if orig_sr == target_sr:
        return audio
    num_samples = int(round(len(audio) * target_sr / orig_sr))
    return scipy_resample(audio, num_samples).astype(np.float32)


def to_tensor(audio: np.ndarray, device: str) -> torch.Tensor:
    return torch.from_numpy(audio).unsqueeze(0).unsqueeze(0).to(device)


def save_wav_temp(wav: np.ndarray, sr: int) -> str:
    fd, path = tempfile.mkstemp(suffix=".wav")
    os.close(fd)
    sf.write(path, wav.astype(np.float32), sr)
    return path


def fmt_stats(kv: dict) -> str:
    lines = ["| Property | Value |", "|---|---|"]
    for k, v in kv.items():
        lines.append(f"| {k} | `{v}` |")
    return "\n".join(lines)


# =========================================================
# Encode / Decode
# =========================================================

def encode_audio(file_path):
    if file_path is None:
        return None, None, "Upload an audio file first."

    audio, sr = load_audio_file(file_path)
    orig_len = len(audio)
    audio = resample_audio(audio, sr, codec.sample_rate)
    wav = to_tensor(audio, codec.device)

    with torch.inference_mode():
        z = codec.encode(wav)  # (B, D, T)

    z_btd = z.transpose(1, 2).contiguous()  # (B, T, D)
    latent = z_btd.squeeze(0).detach().cpu().numpy()

    stats = {
        "Original SR": f"{sr} Hz",
        "Model input SR": f"{codec.sample_rate} Hz",
        "Model output SR": f"{codec.out_sample_rate} Hz",   # ✅ shown for clarity
        "Original samples": f"{orig_len:,}",
        "Resampled samples": f"{len(audio):,}",
        "Latent shape": str(tuple(latent.shape)),
        "Latent dim": f"{latent.shape[-1]}",
        "Frames": f"{latent.shape[0]}",
        "Hop length": f"{codec.hop_length} samples",
        "Approx duration": f"{latent.shape[0] * codec.hop_length / codec.sample_rate:.4f} s",
        "Latent min/max": f"{latent.min():.4f} / {latent.max():.4f}",
        "Latent mean/std": f"{latent.mean():.4f} / {latent.std():.4f}",
    }

    return latent.tolist(), latent.tolist(), fmt_stats(stats)


def decode_audio(latent_list, current_stats):
    if latent_list is None:
        return None, (current_stats or "") + "\n\nNo latent found. Encode first."

    try:
        z = torch.tensor(latent_list, dtype=torch.float32, device=codec.device)
        if z.ndim == 2:
            z = z.unsqueeze(0)  # (B, T, D)
        z = z.transpose(1, 2).contiguous()  # (B, D, T)
    except Exception as e:
        return None, f"Invalid latent: {e}"

    with torch.inference_mode():
        audio = codec.decode(z)

    wav = audio.squeeze().detach().cpu().numpy()
    wav = np.nan_to_num(wav)
    wav = np.clip(wav, -1.0, 1.0)

    # ✅ FIX: use out_sample_rate (48000), NOT sample_rate (16000).
    # The decoder upsamples by prod(decoder_rates) = 8×6×5×2×2×2 = 1920,
    # so the output SR is 48000 Hz, not 16000 Hz.
    out_sr = codec.out_sample_rate

    stats = {
        "Decoded samples": f"{len(wav):,}",
        "Output SR": f"{out_sr} Hz",                        # ✅ 48000
        "Duration": f"{len(wav) / out_sr:.4f} s",           # ✅ correct duration
        "Wave min/max": f"{wav.min():.4f} / {wav.max():.4f}",
    }

    merged = (current_stats or "") + "\n\n### Decode Stats\n" + fmt_stats(stats)
    return (out_sr, wav), merged                             # ✅ tell Gradio correct SR


# =========================================================
# UI
# =========================================================

CSS = """
body, .gradio-container {
    background: #0d0d0d !important;
    color: #eaeaea !important;
}
h1, h2, h3 { color: #00e5a0 !important; }
.gr-button {
    background: #00e5a0 !important;
    color: #000 !important;
    font-weight: 700 !important;
    border: none !important;
}
.gr-box, .gr-panel { background: #151515 !important; border: 1px solid #2a2a2a !important; }
code { background: #1e1e1e; padding: 2px 6px; border-radius: 2px; }
"""

with gr.Blocks(css=CSS, title="AudioVAE Encode / Decode") as demo:
    gr.Markdown(
        f"""
# AudioVAE Encode / Decode
Standalone one-file app for `audiovae.pth`.

**Repo:** `{REPO_ID}`  
**Model input SR:** `{codec.sample_rate} Hz`  
**Model output SR:** `{codec.out_sample_rate} Hz`  
**Hop length:** `{codec.hop_length}`
"""
    )

    latent_state = gr.State()

    with gr.Row():
        audio_in = gr.Audio(type="filepath", label="Input Audio")
        audio_out = gr.Audio(label="Reconstructed Audio", interactive=False)

    with gr.Row():
        encode_btn = gr.Button("Encode")
        decode_btn = gr.Button("Decode")

    stats_out = gr.Markdown(value="Upload an audio file and press Encode.")

    with gr.Accordion("Raw latent preview", open=False):
        latent_preview = gr.JSON(label="Latent JSON")

    encode_btn.click(
        fn=encode_audio,
        inputs=audio_in,
        outputs=[latent_state, latent_preview, stats_out],
    )

    decode_btn.click(
        fn=decode_audio,
        inputs=[latent_state, stats_out],
        outputs=[audio_out, stats_out],
    )


if __name__ == "__main__":
    demo.launch()