File size: 26,613 Bytes
0c61122
 
 
 
a635d96
0c61122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
896a01d
0c61122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a635d96
896a01d
0c61122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a635d96
 
0c61122
 
 
 
 
 
 
 
 
 
a635d96
0c61122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
896a01d
 
 
 
0c61122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a635d96
0c61122
 
 
 
 
 
a635d96
 
 
0c61122
0a23660
a635d96
0c61122
 
 
 
 
 
 
 
 
 
a635d96
 
0c61122
 
 
 
a635d96
0c61122
a635d96
 
896a01d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a635d96
 
 
 
0a23660
a635d96
 
 
 
0a23660
a635d96
 
 
0a23660
a635d96
0c61122
 
 
a635d96
 
 
 
 
896a01d
 
 
 
 
 
 
 
 
 
 
 
 
 
0c61122
896a01d
a635d96
 
 
 
0a23660
 
 
 
 
 
a635d96
 
 
 
896a01d
 
a635d96
0c61122
a635d96
0c61122
 
a635d96
0c61122
a635d96
0c61122
 
 
 
 
a635d96
 
 
 
 
0c61122
 
0a23660
0c61122
 
 
 
 
 
 
 
 
 
 
 
 
896a01d
 
 
0c61122
 
896a01d
0c61122
 
 
 
 
a635d96
0c61122
a635d96
896a01d
a635d96
0c61122
896a01d
0c61122
896a01d
 
0c61122
a635d96
0c61122
 
 
 
 
 
0a23660
a635d96
 
0c61122
 
 
 
 
a635d96
 
 
 
 
0a23660
0c61122
0a23660
 
a635d96
 
 
 
0c61122
a635d96
 
 
 
 
0a23660
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a635d96
 
 
0c61122
 
 
 
 
 
896a01d
0c61122
 
 
 
 
 
 
 
 
a635d96
0c61122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
896a01d
0c61122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
896a01d
0c61122
 
 
 
 
 
a635d96
0c61122
 
 
 
 
896a01d
0c61122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a635d96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
896a01d
 
 
 
 
 
 
 
 
 
 
 
0c61122
 
 
 
 
 
a635d96
0c61122
 
 
a635d96
0c61122
 
a635d96
0c61122
a635d96
0c61122
a635d96
896a01d
 
a635d96
0c61122
 
 
 
 
 
 
 
 
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
import os
import sys
import re
import time
import json
import base64
import hashlib
import tempfile
import subprocess
import inspect
from typing import Iterator, Iterable, Optional, Tuple, Any, List

import spaces
import gradio as gr
import torch
import numpy as np
from huggingface_hub import hf_hub_download
from scipy.io.wavfile import write

# ---------------------------------------------------------
# 1) Клануем і падключаем coqui-ai-TTS (fork з падтрымкай BE)
# ---------------------------------------------------------
REPO_URL = "https://github.com/tuteishygpt/coqui-ai-TTS.git"
REPO_DIR = "coqui-ai-TTS"

if not os.path.exists(REPO_DIR):
    subprocess.run(["git", "clone", REPO_URL, REPO_DIR], check=True)

repo_root = os.path.abspath(REPO_DIR)
if repo_root not in sys.path:
    sys.path.insert(0, repo_root)

from TTS.tts.configs.xtts_config import XttsConfig
from TTS.tts.models.xtts import Xtts
from TTS.tts.layers.xtts.tokenizer import VoiceBpeTokenizer, split_sentence

# ---------------------------------------------------------
# 2) Файлы мадэлі
# ---------------------------------------------------------
repo_id = "archivartaunik/BE_XTTS_V2_10ep250k"
model_dir = "./model"
os.makedirs(model_dir, exist_ok=True)

checkpoint_file = os.path.join(model_dir, "model.pth")
config_file = os.path.join(model_dir, "config.json")
vocab_file = os.path.join(model_dir, "vocab.json")
default_voice_file = os.path.join(model_dir, "voice.wav")

if not os.path.exists(checkpoint_file):
    hf_hub_download(repo_id, filename="model.pth", local_dir=model_dir)
if not os.path.exists(config_file):
    hf_hub_download(repo_id, filename="config.json", local_dir=model_dir)
if not os.path.exists(vocab_file):
    hf_hub_download(repo_id, filename="vocab.json", local_dir=model_dir)
if not os.path.exists(default_voice_file):
    hf_hub_download(repo_id, filename="voice.wav", local_dir=model_dir)

# ---------------------------------------------------------
# 3) Загрузка мадэлі і токенайзера
# ---------------------------------------------------------
config = XttsConfig()
config.load_json(config_file)
XTTS_MODEL = Xtts.init_from_config(config)
XTTS_MODEL.load_checkpoint(
    config,
    checkpoint_path=checkpoint_file,
    vocab_path=vocab_file,
    use_deepspeed=False,
)

device = "cuda:0" if torch.cuda.is_available() else "cpu"
XTTS_MODEL.to(device).eval()
sampling_rate = int(XTTS_MODEL.config.audio["sample_rate"])

tokenizer = VoiceBpeTokenizer(vocab_file=vocab_file)
XTTS_MODEL.tokenizer = tokenizer

# =========================================================
# 4) Streaming-канфіг (мінімальная затрымка)
# =========================================================
MIN_BUFFER_S = 0.03        # ~30 мс — хутчэйшы старт
FADE_S       = 0.004       # карацейшы cross-fade
TOKENS_PER_STEP = 1        # крок прэфікса ў fallback
ENABLE_TEXT_SPLITTING = True  # падзел тэксту на сказы/чанкі

def _seconds_to_samples(sec: float, sr: int) -> int:
    return max(1, int(sec * sr))

def _to_np_audio(x) -> np.ndarray:
    """Гарантавана вяртае 1D np.float32 і пераносіць з CUDA на CPU пры патрэбе."""
    if isinstance(x, dict) and "wav" in x:
        x = x["wav"]
    if isinstance(x, torch.Tensor):
        if x.dtype != torch.float32:
            x = x.float()
        x = x.detach().cpu().contiguous().view(-1)
        return x.numpy()
    x = np.asarray(x)
    if x.ndim > 1:
        x = x.reshape(-1)
    if x.dtype != np.float32:
        x = x.astype(np.float32, copy=False)
    return x

def _crossfade_concat(a: np.ndarray, b: np.ndarray, sr: int, fade_s: float) -> np.ndarray:
    """Плыўнае зліццё без клікаў."""
    if a.size == 0:
        return b.astype(np.float32, copy=False)
    if b.size == 0:
        return a.astype(np.float32, copy=False)
    a = a.astype(np.float32, copy=False)
    b = b.astype(np.float32, copy=False)
    fade_n = min(_seconds_to_samples(fade_s, sr), a.size, b.size)
    if fade_n <= 1:
        return np.concatenate([a, b], axis=0)
    fade_out = np.linspace(1.0, 0.0, fade_n, endpoint=True, dtype=np.float32)
    fade_in  = 1.0 - fade_out
    head = a[:-fade_n]
    tail = (a[-fade_n:] * fade_out) + (b[:fade_n] * fade_in)
    rest = b[fade_n:]
    return np.concatenate([head, tail, rest], axis=0)

def _bpe_prefixes(text: str, lang: str, step_tokens: int):
    """Генерацыя прэфіксаў па BPE; калі encode недаступны — fallback на словы/прабелы."""
    try:
        ids = tokenizer.encode(text, lang=lang)
        n = len(ids)
        for k in range(step_tokens, n + 1, step_tokens):
            yield tokenizer.decode(ids[:k], lang=lang)
        if n % step_tokens != 0:
            yield tokenizer.decode(ids, lang=lang)
        return
    except Exception:
        pass
    pseudo_tokens = re.findall(r"\S+|\s+", text)
    acc = ""
    for i in range(0, len(pseudo_tokens), step_tokens):
        acc = "".join(pseudo_tokens[: i + step_tokens])
        yield acc
    if acc.strip() != text.strip():
        yield text

def _native_stream(
    model: Xtts,
    text: str,
    language: str,
    gpt_cond_latent: Any,
    speaker_embedding: Any,
    **gen_kwargs,
) -> Iterator[np.ndarray]:
    """Натыўны паток з model.inference_stream(...), калі ён ёсць у форку."""
    sig = inspect.signature(model.inference_stream)
    call_kwargs = dict(
        text=text,
        language=language,
        gpt_cond_latent=gpt_cond_latent,
        speaker_embedding=speaker_embedding,
    )
    for k in ("temperature", "length_penalty", "repetition_penalty", "top_k", "top_p", "stream_chunk_size_s"):
        if k in gen_kwargs and k in sig.parameters:
            call_kwargs[k] = gen_kwargs[k]

    generator = model.inference_stream(**call_kwargs)
    for out in generator:
        yield _to_np_audio(out)

def _fallback_incremental(
    model: Xtts,
    text: str,
    language: str,
    gpt_cond_latent: Any,
    speaker_embedding: Any,
    tokens_per_step: int,
    **gen_kwargs,
) -> Iterator[np.ndarray]:
    """Fallback: павялічваем прэфікс па токенах і выдаём толькі «новую» аўдыя-частку."""
    emitted = 0
    for prefix in _bpe_prefixes(text, language, tokens_per_step):
        with torch.no_grad():
            out = model.inference(
                text=prefix,
                language=language,
                gpt_cond_latent=gpt_cond_latent,
                speaker_embedding=speaker_embedding,
                temperature=gen_kwargs.get("temperature", 0.1),
                length_penalty=1.0,
                repetition_penalty=10.0,
                top_k=gen_kwargs.get("top_k", 10),
                top_p=gen_kwargs.get("top_p", 0.3),
            )
        wav = _to_np_audio(out)
        new_part = wav[emitted:]
        emitted = wav.size
        if new_part.size:
            yield new_part

class NewTTSGenerationMixin:
    """Дадаем Xtts.generate()/sample_stream()."""

    @torch.inference_mode()
    def generate(
        self: Xtts,
        text: Optional[str] = None,
        *,
        do_stream: bool = False,
        language: str = "be",
        gpt_cond_latent: Any = None,
        speaker_embedding: Any = None,
        min_buffer_s: float = MIN_BUFFER_S,
        tokens_per_step: int = TOKENS_PER_STEP,
        **gen_kwargs,
    ):
        assert isinstance(text, str) and text.strip(), "text is required"
        if not do_stream:
            out = self.inference(
                text=text,
                language=language,
                gpt_cond_latent=gpt_cond_latent,
                speaker_embedding=speaker_embedding,
                temperature=gen_kwargs.get("temperature", 0.1),
                length_penalty=1.0,
                repetition_penalty=10.0,
                top_k=10,
                top_p=0.3,
            )
            return _to_np_audio(out)

        return self.sample_stream(
            text=text,
            language=language,
            gpt_cond_latent=gpt_cond_latent,
            speaker_embedding=speaker_embedding,
            min_buffer_s=min_buffer_s,
            tokens_per_step=tokens_per_step,
            **gen_kwargs,
        )

    @torch.inference_mode()
    def sample_stream(
        self: Xtts,
        *,
        text: str,
        language: str,
        gpt_cond_latent: Any,
        speaker_embedding: Any,
        min_buffer_s: float = MIN_BUFFER_S,
        tokens_per_step: int = TOKENS_PER_STEP,
        **gen_kwargs,
    ) -> Iterator[np.ndarray]:
        local_kwargs = dict(gen_kwargs)
        local_kwargs.setdefault("stream_chunk_size_s", float(min_buffer_s))

        if hasattr(self, "inference_stream"):
            for chunk in _native_stream(
                self,
                text,
                language,
                gpt_cond_latent,
                speaker_embedding,
                **local_kwargs,
            ):
                yield chunk
            return

        for chunk in _fallback_incremental(
            self,
            text,
            language,
            gpt_cond_latent,
            speaker_embedding,
            tokens_per_step,
            **gen_kwargs,
        ):
            yield chunk

def init_stream_support():
    Xtts.generate = NewTTSGenerationMixin.generate
    Xtts.sample_stream = NewTTSGenerationMixin.sample_stream

init_stream_support()

# ---------------------------------------------------------
# 5) Кэш латэнтаў голасу (скарачае старт-латэнтнасць)
# ---------------------------------------------------------
LATENT_CACHE: dict[str, Tuple[Any, Any]] = {}

def _latents_for(path: str) -> Tuple[Any, Any]:
    if path and os.path.exists(path):
        key = f"{path}:{os.path.getmtime(path)}:{os.path.getsize(path)}"
    else:
        key = "default_voice"
    h = hashlib.md5(key.encode("utf-8")).hexdigest()
    if h not in LATENT_CACHE:
        g, s = XTTS_MODEL.get_conditioning_latents(
            audio_path=path,
            gpt_cond_len=XTTS_MODEL.config.gpt_cond_len,
            max_ref_length=XTTS_MODEL.config.max_ref_len,
            sound_norm_refs=XTTS_MODEL.config.sound_norm_refs,
        )
        LATENT_CACHE[h] = (g, s)
    return LATENT_CACHE[h]

# ---------------------------------------------------------
# 6) Хэлперы: буферы + base64
# ---------------------------------------------------------
def _merge_for_file(chunks: List[np.ndarray]) -> np.ndarray:
    if not chunks:
        return np.zeros((0,), dtype=np.float32)
    out = chunks[0]
    for i in range(1, len(chunks)):
        out = _crossfade_concat(out, chunks[i], sampling_rate, FADE_S)
    return out

def _chunker(chunks: Iterable[np.ndarray], sr: int, target_s: float) -> Iterable[np.ndarray]:
    """Мінімальная групоўка да ~target_s (30 мс) — баланс затрымкі/гладкасці."""
    target_samples = _seconds_to_samples(target_s, sr)
    buf = np.zeros((0,), dtype=np.float32)
    for c in chunks:
        c = _to_np_audio(c)
        if c.size == 0:
            continue
        buf = c if buf.size == 0 else _crossfade_concat(buf, c, sr, FADE_S)
        if buf.size >= target_samples:
            yield buf
            buf = np.zeros((0,), dtype=np.float32)
    if buf.size:
        yield buf

def _pcm_f32_to_b64(x: np.ndarray) -> str:
    if x.dtype != np.float32:
        x = x.astype(np.float32, copy=False)
    return base64.b64encode(x.tobytes()).decode("ascii")

# ---------------------------------------------------------
# 7) Асноўная функцыя TTS — стрим + фінальны файл + фінальнае аўдыя + серверныя метрыкі
# ---------------------------------------------------------
@spaces.GPU(duration=60)
def text_to_speech(belarusian_story, speaker_audio_file=None):
    """
    Выхады:
      1) stream_pipe (hidden Textbox) — base64(PCM float32) па кроках, у фінале "__STOP__" (EOS)
      2) final_file (File)           — шлях да WAV у фінале
      3) final_audio (Audio)         — той жа шлях, каб прайграваць у UI
      4) log_pipe   (hidden Textbox) — JSON з сервернымі метрыкамі
    """
    t0 = time.perf_counter()  # пачатак сервернай апрацоўкі (ўжо пасля чаргі)

    if not belarusian_story or str(belarusian_story).strip() == "":
        raise gr.Error("Увядзі хоць нейкі тэкст 🙂")

    # Голас па змаўчанні
    if not speaker_audio_file or (
        not isinstance(speaker_audio_file, str)
        and getattr(speaker_audio_file, "name", "") == ""
    ):
        speaker_audio_file = default_voice_file

    # Conditioning latents (з замерам часу)
    t_lat0 = time.perf_counter()
    try:
        gpt_cond_latent, speaker_embedding = _latents_for(speaker_audio_file)
    except Exception as e:
        raise gr.Error(f"Памылка пры атрыманні латэнтаў голасу: {e}")
    t_lat1 = time.perf_counter()

    # Падзел тэксту (з замерам часу)
    t_split0 = time.perf_counter()
    text_in = str(belarusian_story).strip()
    lang_short = "be"
    chunk_limit = getattr(XTTS_MODEL.tokenizer, "char_limits", {}).get(lang_short, 250)

    if ENABLE_TEXT_SPLITTING:
        try:
            texts = split_sentence(
                text_in,
                lang=lang_short,
                text_split_length=chunk_limit,
            )
            texts = [s.strip() for s in texts if s and s.strip()]
            if not texts:
                texts = [text_in]
        except Exception as e:
            print(f"Warning: памылка пры падзеле тэксту: {e}")
            texts = [text_in]
    else:
        texts = [text_in]
    t_split1 = time.perf_counter()

    # Будзем назапашваць серверныя метрыкі
    server_metrics = {
        "zerogpu_queue_s": None,                # рэальны час чаргі недаступны на серверы
        "latents_s": (t_lat1 - t_lat0),
        "text_split_s": (t_split1 - t_split0),
        "gen_init_to_first_chunk_s": None,      # запоўнім ніжэй пры першым чанку
        "until_first_chunk_total_s": None,      # t_first_chunk - t0
        "server_unaccounted_before_first_chunk_s": None,  # будзе падлічана ў момант 1-га чанка
        "file_write_s": None,                   # у фінале
    }

    # Адразу вышлем пачатковы JSON
    yield ("", None, None, json.dumps(server_metrics))

    full_audio_chunks: List[np.ndarray] = []

    # Генерацыя і стрим па чанках
    first_chunk_seen = False
    t_gen0 = time.perf_counter()
    for idx, part in enumerate(texts):
        # ініцыялізацыя генератара
        gen = XTTS_MODEL.generate(
            text=part,
            do_stream=True,
            language=lang_short,
            gpt_cond_latent=gpt_cond_latent,
            speaker_embedding=speaker_embedding,
            min_buffer_s=MIN_BUFFER_S,
            tokens_per_step=TOKENS_PER_STEP,
            temperature=0.1,
            length_penalty=1.0,
            repetition_penalty=10.0,
            top_k=10,
            top_p=0.3,
        )

        for buf in _chunker(gen, sampling_rate, MIN_BUFFER_S):
            if not first_chunk_seen:
                t_first_chunk = time.perf_counter()
                server_metrics["gen_init_to_first_chunk_s"] = (t_first_chunk - t_gen0)
                server_metrics["until_first_chunk_total_s"] = (t_first_chunk - t0)

                # іншая серверная апрацоўка = усё да 1-га чанка - (latents + split + init→1-ы чанк)
                known = server_metrics["latents_s"] + server_metrics["text_split_s"] + server_metrics["gen_init_to_first_chunk_s"]
                other = server_metrics["until_first_chunk_total_s"] - known
                server_metrics["server_unaccounted_before_first_chunk_s"] = max(0.0, other)

                first_chunk_seen = True
                yield (_pcm_f32_to_b64(buf), None, None, json.dumps(server_metrics))
            else:
                yield (_pcm_f32_to_b64(buf), None, None, None)
            full_audio_chunks.append(buf)

    # Фінал: WAV + апошняе абнаўленне лагу
    if not full_audio_chunks:
        yield ("__STOP__", None, None, json.dumps(server_metrics))
        return

    t_w0 = time.perf_counter()
    full_audio = _merge_for_file(full_audio_chunks)
    tmp = None
    try:
        tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
        write(tmp.name, sampling_rate, full_audio.astype(np.float32))
    except Exception as e:
        raise gr.Error(f"Памылка пры запісе фінальнага WAV: {e}")
    finally:
        t_w1 = time.perf_counter()
        server_metrics["file_write_s"] = (t_w1 - t_w0)

    yield ("__STOP__", tmp.name, tmp.name, json.dumps(server_metrics))

# ---------------------------------------------------------
# 8) UI: логі ў СЕКУНДАХ, Клік=0 + «ацэнка чаргі ZeroGPU + сеткі»
# ---------------------------------------------------------
examples = [
    ["Прывітанне! Гэта праверка жывога струменя беларускага TTS.", "Nestarka.wav"],
]

with gr.Blocks() as demo:
    gr.Markdown("## Belarusian TTS — Streaming па токенах (WebAudio) + фінальны файл")

    with gr.Row():
        inp_text = gr.Textbox(lines=5, label="Тэкст на беларускай мове")
        inp_voice = gr.Audio(type="filepath", label="Прыклад голасу (7+ сек)", interactive=True)

    with gr.Row():
        play_btn = gr.Button("▶️ Play (stream)")
        stop_btn = gr.Button("⏹ Stop (stream)")
        run_btn = gr.Button("Згенераваць")
        gr.Markdown(f"**Sample rate:** {sampling_rate} Hz")

    # Панэль лагавання
    log_panel = gr.HTML(
        value='<div id="wa-log" style="font-family:system-ui;font-size:12px;white-space:pre-line">[лог пусты]</div>',
        label="Лагі плэера",
    )

    # Схаваныя каналы
    stream_pipe = gr.Textbox(value="", visible=False, label="stream_pipe")
    log_pipe    = gr.Textbox(value="", visible=False, label="log_pipe")

    # Фінальны файл і аўдыя
    final_file  = gr.File(label="Згенераваны WAV (спампаваць)")
    final_audio = gr.Audio(label="Фінальнае аўдыя", type="filepath", interactive=False, elem_id="final-audio")

    # Кнопка для прайгравання фінальнага аўдыя
    play_final_btn = gr.Button("▶️ Play Final")

    # --- JS: ініт + reset + лагі ў СЕКУНДАХ, Клік = 0.000 s ---
    INIT_RESET_AND_PLAY_JS = f"""
() => {{
  const sampleRate = {sampling_rate};
  const AC = window.AudioContext || window.webkitAudioContext;
  if (!AC) return;

  function toSec(ms) {{ return (ms/1000); }}
  function fmtS(x) {{ return (x===null||x===undefined) ? "n/a" : x.toFixed(3) + " s"; }}

  function logUpdate() {{
    const el = document.getElementById('wa-log');
    if (!el || !window.__wa || !window.__wa.meta) return;
    const m = window.__wa.meta;
    const lines = [];

    // Клік = 0.000 s
    lines.push("Клік (Згенераваць): 0.000 s");

    // Калі ёсць першы чанк/аўдыя — паказваем у секундах адносна кліку
    let click_to_first_chunk_s = null;
    if (m.t_first_push_ms) {{
      click_to_first_chunk_s = toSec(m.t_first_push_ms - m.t_click_ms);
      lines.push("Першы чанк прыйшоў:   " + click_to_first_chunk_s.toFixed(3) + " s");
      if (m.t_first_audio_ms) {{
        lines.push("Пачатак прайгравання: " + (toSec(m.t_first_audio_ms - m.t_click_ms)).toFixed(3) + " s");
        lines.push("Затрымка (чанк→аўдыя): " + (toSec(m.t_first_audio_ms - m.t_first_push_ms)).toFixed(3) + " s");
      }}
    }}

    // Серверныя метрыкі (ужо ў СЕКУНДАХ у JSON)
    const s = (m.server || {{}});
    lines.push("");
    lines.push("— Серверныя метрыкі —");
    lines.push("Latents (умоўны голас):  " + fmtS(s.latents_s));
    lines.push("Падзел тэксту:           " + fmtS(s.text_split_s));
    lines.push("Ініт→1-ы чанк:           " + fmtS(s.gen_init_to_first_chunk_s));
    lines.push("Усё да 1-га чанка:       " + fmtS(s.until_first_chunk_total_s));
    lines.push("Іншая серверная апрац.:  " + fmtS(s.server_unaccounted_before_first_chunk_s));
    lines.push("Запіс WAV:               " + fmtS(s.file_write_s));

    // ----- АЦЭНКА ЧАРГІ -----
    // Ацэньваем «ZeroGPU чарга + сетка» як розніцу:
    // (клік→першы чанк па кліенце) - (усё да 1-га чанка па серверы)
    if (click_to_first_chunk_s !== null && s.until_first_chunk_total_s !== null) {{
      let est_queue_net = click_to_first_chunk_s - s.until_first_chunk_total_s;
      if (!isFinite(est_queue_net) || est_queue_net < 0) est_queue_net = 0;
      lines.push("");
      lines.push("Ацэнка чаргі ZeroGPU + сеткі: " + est_queue_net.toFixed(3) + " s");
    }} else {{
      lines.push("");
      lines.push("Ацэнка чаргі ZeroGPU + сеткі: n/a");
    }}

    lines.push("");
    lines.push("Статус стриму: " + (window.__wa.playing ? "playing" : "stopped"));
    el.textContent = lines.join("\\n");
    try {{ console.log(lines.join("\\n")); }} catch (e) {{}}
  }}

  if (!window.__wa) {{
    const ctx = new AC({{ sampleRate }});
    const bufferSize = 1024;
    const node = ctx.createScriptProcessor(bufferSize, 0, 1);
    let queue = [];
    let playing = false;
    let eos = false;

    const meta = {{
      t_click_ms: performance.now(),
      t_first_push_ms: null,
      t_first_audio_ms: null,
      server: null, // серверныя метрыкі (секунды)
    }};

    node.onaudioprocess = (e) => {{
      const out = e.outputBuffer.getChannelData(0);
      let i = 0;
      while (i < out.length) {{
        if (queue.length === 0 || !playing) {{ out[i++] = 0.0; continue; }}
        let cur = queue[0];
        const take = Math.min(cur.length, out.length - i);
        if (meta.t_first_audio_ms === null) {{
          meta.t_first_audio_ms = performance.now();
          logUpdate();
        }}
        out.set(cur.subarray(0, take), i);
        i += take;
        if (take === cur.length) queue.shift();
        else queue[0] = cur.subarray(take);
      }}
      if (eos && queue.length === 0 && playing) {{
        playing = false;
        logUpdate();
      }}
    }};
    node.connect(ctx.destination);

    window.__wa = {{
      ctx, node,
      get playing() {{ return playing; }},
      get eos() {{ return eos; }},
      set eos(v) {{ eos = v; }},
      meta,
      push: (f32) => {{ queue.push(f32); }},
      start: async () => {{ try {{ await ctx.resume(); }} catch(e){{}} playing = true; logUpdate(); }},
      stop: () => {{ playing = false; logUpdate(); }},
      reset: () => {{ playing = false; eos = false; queue = []; meta.t_first_push_ms = null; meta.t_first_audio_ms = null; logUpdate(); }},
      updateLog: logUpdate,
    }};
  }} else {{
    window.__wa.reset();
    window.__wa.meta.t_click_ms = performance.now();
  }}

  window.__wa.start();  // аўта-старт стримінгу
}}
"""

    STOP_JS = "() => { if (window.__wa) window.__wa.stop(); }"
    PLAY_JS = "() => { if (window.__wa) window.__wa.start(); }"

    # Base64 -> Float32 + лагі ў СЕКУНДАХ; "__STOP__" — EOS (не стоп адразу)
    PUSH_JS = """
(b64) => {
  if (!window.__wa || !b64) return;
  const meta = window.__wa.meta || {};
  if (b64 === "__STOP__") {
    window.__wa.eos = true;
    window.__wa.updateLog && window.__wa.updateLog();
    return;
  }
  if (!meta.t_first_push_ms) {
    meta.t_first_push_ms = performance.now();
    window.__wa.updateLog && window.__wa.updateLog();
  }
  const bin = atob(b64);
  const len = bin.length;
  const buf = new ArrayBuffer(len);
  const view = new Uint8Array(buf);
  for (let i=0;i<len;i++) view[i] = bin.charCodeAt(i);
  const f32 = new Float32Array(buf);
  window.__wa.push(f32);
}
"""

    # Серверныя метрыкі (JSON) -> у meta.server і перамалёўваем лог
    LOG_JS = """
(js) => {
  if (!window.__wa) return;
  try {
    if (js) {
      const obj = JSON.parse(js);
      // значэнні ўжо ў СЕКУНДАХ на серверы
      window.__wa.meta.server = obj;
      window.__wa.updateLog && window.__wa.updateLog();
    }
  } catch (e) {}
}
"""

    # JS: Play final gr.Audio
    PLAY_FINAL_JS = """
() => {
  const host = document.getElementById('final-audio');
  if (!host) return;
  const audio = host.querySelector('audio');
  if (audio) {
    try { audio.play(); } catch(e) {}
  }
}
"""

    # Ручныя кнопкі стрим-плэера
    play_btn.click(fn=None, inputs=[], outputs=[], js=PLAY_JS)
    stop_btn.click(fn=None, inputs=[], outputs=[], js=STOP_JS)

    # Аўта-ініт+reset+play перад стартам сервера
    run_btn.click(fn=None, inputs=[], outputs=[], js=INIT_RESET_AND_PLAY_JS)

    # Стрымінг: server -> (stream, file, audio, log_json)
    run_btn.click(
        fn=text_to_speech,
        inputs=[inp_text, inp_voice],
        outputs=[stream_pipe, final_file, final_audio, log_pipe],
    )

    # Паўздарожныя падзеі
    stream_pipe.change(fn=None, inputs=[stream_pipe], outputs=[], js=PUSH_JS)
    log_pipe.change(fn=None, inputs=[log_pipe], outputs=[], js=LOG_JS)

    # Кнопка "Play Final"
    play_final_btn.click(fn=None, inputs=[], outputs=[], js=PLAY_FINAL_JS)

    # Прыклады
    gr.Examples(
        examples=examples,
        inputs=[inp_text, inp_voice],
        fn=None,
        cache_examples=False,
    )

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