multimodalart's picture
multimodalart HF Staff
Upload folder using huggingface_hub
526f338 verified
Raw History Blame Contribute Delete
10.6 kB
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,
)