File size: 10,596 Bytes
a15d0ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
526f338
a15d0ba
 
 
 
 
 
 
733b0b2
a15d0ba
 
 
 
 
 
 
 
 
 
 
 
 
 
733b0b2
a15d0ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
733b0b2
a15d0ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
733b0b2
a15d0ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
733b0b2
 
 
 
 
 
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
import os

# --- ZeroGPU / NeMo environment guards (must precede any CUDA-touching import) ---
# numba.cuda (pulled in by NeMo) calls cuInit in the parent process, which poisons
# the spaces fork -> "Stuck at ZeroGPU init 10.0%". Disable it.
os.environ.setdefault("NUMBA_DISABLE_CUDA", "1")
os.environ.setdefault("OMP_NUM_THREADS", "2")
os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")

import spaces  # noqa: E402  MUST come before torch / NeMo

import random
from typing import Tuple

import gradio as gr
import numpy as np
import torch
from huggingface_hub import hf_hub_download

from nemo.collections.tts.modules.magpietts_inference.utils import (
    ModelLoadConfig,
    load_magpie_model,
)

# ---------------------------------------------------------------------------
# Model
# ---------------------------------------------------------------------------
REPO_ID = "anwar-dev/Bayan-Arabic-TTS-v1.0"
NEMO_FILENAME = "Bayan-Arabic-TTS-v1.0.nemo"
CODEC_MODEL_PATH = "nvidia/nemo-nano-codec-22khz-1.89kbps-21.5fps"
LANGUAGE = "ar-MSA"
SAMPLE_RATE = 22050

HF_TOKEN = os.environ.get("HF_TOKEN") or None

print(f"Downloading {REPO_ID}/{NEMO_FILENAME} ...")
CHECKPOINT_PATH = hf_hub_download(
    repo_id=REPO_ID,
    filename=NEMO_FILENAME,
    token=HF_TOKEN,
)
print(f"Checkpoint at {CHECKPOINT_PATH}")

# The repo ships a merged, self-contained .nemo (MagpieTTS 357M + LoRA + a 6th
# baked speaker row already fused into baked_context_embedding) so there is no
# separate load_lora_weights step. load_magpie_model applies the tokenizer /
# charset migrations the checkpoint needs and enables the KV cache.
model, _ = load_magpie_model(
    ModelLoadConfig(
        nemo_file=CHECKPOINT_PATH,
        codecmodel_path=CODEC_MODEL_PATH,
        legacy_codebooks=False,
        legacy_text_conditioning=False,
    )
)
model.eval()
print("Bayan-Arabic-TTS-v1.0 loaded and ready.")

SPEAKERS = {
    "ู…ุฑุงุฏ ยท Murad (Arabic voice)": 5,
    "Sofia (base voice)": 4,
    "Leo (base voice)": 3,
    "John (base voice)": 2,
    "Jason (base voice)": 1,
    "Aria (base voice)": 0,
}
DEFAULT_SPEAKER = "ู…ุฑุงุฏ ยท Murad (Arabic voice)"


# ---------------------------------------------------------------------------
# Inference (runs on ZeroGPU)
# ---------------------------------------------------------------------------
@spaces.GPU(duration=30)
def generate_speech(
    text: str,
    speaker: str = DEFAULT_SPEAKER,
    cfg_scale: float = 2.5,
    temperature: float = 0.6,
    topk: int = 80,
    max_decoder_steps: int = 800,
    seed: int = 0,
) -> Tuple[int, np.ndarray]:
    """Synthesize Arabic speech from text with Bayan-Arabic-TTS-v1.0.

    Args:
        text: Arabic text to speak. Fully diacritized (mashkลซl) text gives the
            best accuracy; diacritics are recommended but not required.
        speaker: Which baked voice to use. The first entry is the fine-tuned
            Arabic voice "Murad"; the rest are the base MagpieTTS voices.
        cfg_scale: Classifier-free guidance scale (2.5 default; raise toward 3.0
            if the speech stutters).
        temperature: Sampling temperature (0.6 default; lower to 0.5 to reduce
            stuttering at the cost of expressiveness).
        topk: Top-k sampling cutoff.
        max_decoder_steps: Hard cap on generated codec frames (~37 s of audio).
        seed: RNG seed for reproducible sampling (0 works well with the examples).

    Returns:
        A (sample_rate, waveform) tuple for a Gradio audio component โ€” 22.05 kHz
        mono float32 numpy audio.
    """
    if not text or not text.strip():
        raise gr.Error("Please enter some Arabic text / ุงู„ุฑุฌุงุก ุฅุฏุฎุงู„ ู†ุต ุนุฑุจูŠ.")

    speaker_index = SPEAKERS.get(speaker, 5)
    torch.manual_seed(int(seed))
    random.seed(int(seed))

    model.inference_parameters.cfg_scale = float(cfg_scale)
    model.inference_parameters.temperature = float(temperature)
    model.inference_parameters.topk = int(topk)
    model.inference_parameters.max_decoder_steps = int(max_decoder_steps)

    with torch.inference_mode():
        audio, audio_len = model.do_tts(
            text.strip(),
            language=LANGUAGE,
            apply_TN=False,
            use_cfg=True,
            speaker_index=speaker_index,
        )

    n = int(audio_len[0])
    wav = np.asarray(audio[0, :n].float().cpu().numpy())  # never return a CUDA tensor
    if wav.size == 0:
        raise gr.Error("The model produced no audio โ€” try shorter or diacritized text.")
    return SAMPLE_RATE, wav


# ---------------------------------------------------------------------------
# UI
# ---------------------------------------------------------------------------
EXAMPLES = [
    ["ุชูุนูŽุฏูู‘ ุงู„ู„ูู‘ุบูŽุฉู ุงู„ู’ุนูŽุฑูŽุจููŠูŽู‘ุฉู ู…ูู†ู’ ุฃูŽู‚ู’ุฏูŽู…ู ุงู„ู„ูู‘ุบูŽุงุชู ุงู„ู’ุญูŽูŠูŽู‘ุฉู ูˆูŽุฃูŽูƒู’ุซูŽุฑูู‡ูŽุง ุงูู†ู’ุชูุดูŽุงุฑู‹ุง ูููŠ ุงู„ู’ุนูŽุงู„ูŽู…ู."],
    ["ุฃูŽุนู’ู„ูŽู†ูŽุชู ุงู„ู’ูˆูุฒูŽุงุฑูŽุฉู ุนูŽู†ู’ ุฎูุทูŽู‘ุฉู ุฌูŽุฏููŠุฏูŽุฉู ู„ูุชูŽุทู’ูˆููŠุฑู ุงู„ุชูŽู‘ุนู’ู„ููŠู…ู ูููŠ ุงู„ู’ู…ูŽุฏูŽุงุฑูุณู ุงู„ู’ุญููƒููˆู…ููŠูŽู‘ุฉู ุฎูู„ูŽุงู„ูŽ ุงู„ุณูŽู‘ู†ูŽูˆูŽุงุชู ุงู„ู’ู‚ูŽุงุฏูู…ูŽุฉู."],
    ["ู‡ูŽู„ู’ ูŠูู…ู’ูƒูู†ููƒูŽ ุฃูŽู†ู’ ุชูŽุดู’ุฑูŽุญูŽ ู„ููŠ ูƒูŽูŠู’ููŽ ุชูŽุนู’ู…ูŽู„ู ู‡ูŽุฐูู‡ู ุงู„ู’ุขู„ูŽุฉูุŸ"],
    ["ู‚ูŽุฑูŽุฃู’ุชู ูƒูุชูŽุงุจู‹ุง ุนูŽู†ู’ ุชูŽุงุฑููŠุฎู ุงู„ู’ุฃูŽู†ู’ุฏูŽู„ูุณูุŒ ูˆูŽุฃูŽุนู’ุฌูŽุจูŽู†ููŠ ูƒูŽุซููŠุฑู‹ุง ุญูŽุฏููŠุซูู‡ู ุนูŽู†ู’ ุนูู„ูŽู…ูŽุงุกู ุงู„ู’ู‚ูุฑููˆู†ู ุงู„ู’ูˆูุณู’ุทูŽู‰."],
    ["ูŠูŽุจู’ู„ูุบู ุนูŽุฏูŽุฏู ุณููƒูŽู‘ุงู†ู ุงู„ู’ุนูŽุงู„ูŽู…ู ุงู„ู’ูŠูŽูˆู’ู…ูŽ ู†ูŽุญู’ูˆูŽ ุซูŽู…ูŽุงู†ููŠูŽุฉู ู…ูู„ู’ูŠูŽุงุฑูŽุงุชู ู†ูŽุณูŽู…ูŽุฉูุŒ ูˆูŽู…ูู†ูŽ ุงู„ู’ู…ูุชูŽูˆูŽู‚ูŽู‘ุนู ุฃูŽู†ู’ ูŠูŽุฒู’ุฏูŽุงุฏูŽ ู‡ูŽุฐูŽุง ุงู„ู’ุนูŽุฏูŽุฏู ูููŠ ุงู„ู’ู…ูุณู’ุชูŽู‚ู’ุจูู„ู."],
    ["ุฅูู†ูŽู‘ ุงู„ู’ุนูู„ู’ู…ูŽ ู†ููˆุฑูŒ ูŠูุถููŠุกู ุทูŽุฑููŠู‚ูŽ ุงู„ู’ุฅูู†ู’ุณูŽุงู†ููŠูŽู‘ุฉูุŒ ูˆูŽู…ูŽู†ู’ ุทูŽู„ูŽุจูŽ ุงู„ู’ุนูู„ู’ู…ูŽ ูˆูŽุฌูŽุฏูŽู‡ู ุฅูู†ู’ ุฃูŽุฎู’ู„ูŽุตูŽ ูููŠ ุทูŽู„ูŽุจูู‡ู."],
    ["ุฃูŽุฑู’ุฌููˆ ุฃูŽู†ู’ ุชูุฑูŽุงุฌูุนูŽ ุงู„ุชูŽู‘ู‚ู’ุฑููŠุฑูŽ ู‚ูŽุจู’ู„ูŽ ุงูุฌู’ุชูู…ูŽุงุนู ุงู„ู’ุบูŽุฏูุŒ ููŽุฅูู†ูŽู‘ ุจูŽุนู’ุถูŽ ุงู„ู’ุฃูŽุฑู’ู‚ูŽุงู…ู ุชูŽุญู’ุชูŽุงุฌู ุฅูู„ูŽู‰ ุชูŽุตู’ุญููŠุญู."],
    ["ูููŠ ุตูŽุจูŽุงุญู ูŠูŽูˆู’ู…ู ู…ูุดู’ู…ูุณูุŒ ุฎูŽุฑูŽุฌูŽ ุงู„ู’ุฃูŽุทู’ููŽุงู„ู ุฅูู„ูŽู‰ ุงู„ู’ุญูŽุฏููŠู‚ูŽุฉู ูŠูŽู„ู’ุนูŽุจููˆู†ูŽ ูˆูŽูŠูŽุถู’ุญูŽูƒููˆู†ูŽุŒ ุจูŽูŠู’ู†ูŽู…ูŽุง ูƒูŽุงู†ูŽุชู’ ุฃูู…ูู‘ู‡ูู…ู’ ุชูุนูุฏูู‘ ุทูŽุนูŽุงู…ูŽ ุงู„ู’ุบูŽุฏูŽุงุกู."],
]

CSS = """
#col-container { max-width: 1100px; margin: 0 auto; }
.dark .gradio-container { color: var(--body-text-color); }
"""

with gr.Blocks(title="Bayan Arabic TTS") as demo:
    with gr.Column(elem_id="col-container"):
        gr.Markdown(
            "# ๐ŸŽ™๏ธ Bayan Arabic TTS (ุจูŠุงู†)\n"
            "Arabic text-to-speech with a single male voice โ€” **ยซู…ุฑุงุฏยป / Murad** โ€” "
            "built on [NVIDIA MagpieTTS Multilingual 357M]"
            "(https://huggingface.co/nvidia/magpie_tts_multilingual_357m) with LoRA "
            "and a dedicated speaker row, fine-tuned on 71.6 minutes of Arabic speech.\n\n"
            "Output is 22.05 kHz mono audio. Accuracy is much better with **fully "
            "diacritized (ู…ุดูƒู‘ู„ุฉ)** text; keep utterances under ~37 seconds and write "
            "numbers out as words (text normalization is off). "
            "[Model card](https://huggingface.co/anwar-dev/Bayan-Arabic-TTS-v1.0)"
        )

        with gr.Row():
            with gr.Column(scale=5):
                text_input = gr.Textbox(
                    label="ุงู„ู†ุต ุงู„ุนุฑุจูŠ ยท Arabic text",
                    placeholder="ุงูƒุชุจ ุงู„ู†ุต ุงู„ุนุฑุจูŠ ู‡ู†ุงโ€ฆ (ูŠููุถู‘ู„ ุงู„ุชุดูƒูŠู„ ุงู„ูƒุงู…ู„)",
                    lines=5,
                    rtl=True,
                    text_align="right",
                )
                speaker = gr.Dropdown(
                    choices=list(SPEAKERS.keys()),
                    value=DEFAULT_SPEAKER,
                    label="ุงู„ุตูˆุช ยท Voice",
                )
                run_btn = gr.Button("๐Ÿ”Š ุชูˆู„ูŠุฏ ุงู„ุตูˆุช ยท Generate speech", variant="primary")

            with gr.Column(scale=4):
                audio_output = gr.Audio(
                    label="ุงู„ุตูˆุช ุงู„ู…ูˆู„ู‘ุฏ ยท Generated audio",
                    type="numpy",
                    autoplay=True,
                )

        with gr.Accordion("โš™๏ธ ุฅุนุฏุงุฏุงุช ู…ุชู‚ุฏู…ุฉ ยท Advanced settings", open=False):
            with gr.Row():
                cfg_scale = gr.Slider(1.0, 4.0, value=2.5, step=0.1,
                                      label="CFG scale (2.5 โ€” raise toward 3.0 to fix stutter)")
                temperature = gr.Slider(0.1, 1.5, value=0.6, step=0.05,
                                        label="Temperature (0.6 โ€” lower to 0.5 to reduce stutter)")
            with gr.Row():
                topk = gr.Slider(1, 200, value=80, step=1, label="Top-k")
                max_decoder_steps = gr.Slider(100, 1000, value=800, step=50,
                                              label="Max decoder steps (~37 s of audio at 800)")
                seed = gr.Number(value=0, precision=0, label="Seed")

        gr.Markdown("### ๐Ÿ“ ุฃู…ุซู„ุฉ ุฌุงู‡ุฒุฉ ยท Examples (ุงู„ูุตุญู‰ ุงู„ู…ุดูƒู‘ู„ุฉ)")
        gr.Examples(
            examples=EXAMPLES,
            inputs=[text_input],
            outputs=[audio_output],
            fn=generate_speech,
            cache_examples=True,
            cache_mode="lazy",
        )

        gr.Markdown(
            "_License: [NVIDIA Open Model License]"
            "(https://huggingface.co/nvidia/magpie_tts_multilingual_357m). "
            "Inference code adapted from the model author's `inference.py`._"
        )

    run_btn.click(
        fn=generate_speech,
        inputs=[text_input, speaker, cfg_scale, temperature, topk, max_decoder_steps, seed],
        outputs=[audio_output],
        api_name="generate",
    )
    text_input.submit(
        fn=generate_speech,
        inputs=[text_input, speaker, cfg_scale, temperature, topk, max_decoder_steps, seed],
        outputs=[audio_output],
        api_name="generate_text",
    )

if __name__ == "__main__":
    # Gradio 6 moved `theme` / `css` from the Blocks constructor to launch().
    demo.launch(
        theme=gr.themes.Citrus(),
        css=CSS,
        mcp_server=True,
    )