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, )