Spaces:
Running on Zero
Running on Zero
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,
)
|