Spaces:
Running on Zero
Running on Zero
Download app.py from anwar-dev/bayan-arabic-tts: direct link, hf CLI and curl.
- Browser
- Download file 10.6 kB
-
https://huggingface.co/spaces/anwar-dev/bayan-arabic-tts/resolve/main/app.py
- Command line
-
hf download hf://spaces/anwar-dev/bayan-arabic-tts/app.py
-
curl -L -o app.py https://huggingface.co/spaces/anwar-dev/bayan-arabic-tts/resolve/main/app.py
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) | |
| # --------------------------------------------------------------------------- | |
| 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, | |
| ) | |