Download app.py from oddadmix/tiny-voca-arabic-code-switching: direct link, hf CLI and curl.
- Browser
- Download file 3.16 kB
-
https://huggingface.co/spaces/oddadmix/tiny-voca-arabic-code-switching/resolve/main/app.py
- Command line
-
hf download hf://spaces/oddadmix/tiny-voca-arabic-code-switching/app.py
-
curl -L -o app.py https://huggingface.co/spaces/oddadmix/tiny-voca-arabic-code-switching/resolve/main/app.py
3.16 kB
| import os, time | |
| from pathlib import Path | |
| os.environ.setdefault("DIZIONE_DATA", str(Path(__file__).parent / "phonemizer")) # English phonemizer data, before dizione loads | |
| import gradio as gr | |
| import numpy as np | |
| import torch | |
| from huggingface_hub import hf_hub_download | |
| from tiny_voca.infer import load_model, speak_text | |
| torch.set_num_threads(2) | |
| REPO = "oddadmix/tiny-voca-arabic-code-switching" | |
| MODEL = load_model(hf_hub_download(REPO, "tiny-voca-arabic-code-switching.pt", token=os.environ.get("HF_TOKEN"))) | |
| VOICES = {"Female": "arcs_female", "Male": "arcs_male"} | |
| EXAMPLES = [ | |
| ["أهلا بك! تم تأكيد الـ order الخاص بك، وستصلك رسالة على WhatsApp فيها الـ tracking number.", "Female"], | |
| ["اجتماع الـ team سيكون غدا الساعة العاشرة على Zoom، فلا تنس مراجعة الـ slides قبل الـ meeting.", "Male"], | |
| ["قمنا بتدريب الـ model على GPU واحد، والنتائج على الـ validation set كانت ممتازة.", "Female"], | |
| ["أرسلت لك الـ link على الـ email، وسأشارك الـ presentation في الـ Google Drive بعد الـ lunch break.", "Male"], | |
| ["مرحبا بكم في نشرة الأخبار. يشهد الطقس اليوم ارتفاعا في درجات الحرارة، مع رياح خفيفة في المساء.", "Female"], | |
| ] | |
| speak_text(MODEL, "مرحبا بالـ team", "arcs_female") # warm-up: loads the diacritizer and the English phonemizer | |
| def synth(text, voice, speed): | |
| text = (text or "").strip()[:1000] | |
| if not text: | |
| raise gr.Error("Write some text first.") | |
| t = time.time() | |
| with torch.inference_mode(): | |
| wave, spoken = speak_text(MODEL, text, VOICES[voice], speed=speed) | |
| audio = (wave.float().numpy() * 32767).clip(-32768, 32767).astype(np.int16) | |
| secs = len(audio) / 24000 | |
| phones = "\n".join(" ".join(s.symbols) for s in spoken) | |
| return (24000, audio), f"{secs:.1f} s of audio in {time.time() - t:.2f} s on CPU", phones | |
| with gr.Blocks(title="tiny-voca-arabic-code-switching") as demo: | |
| gr.Markdown("# tiny-voca-arabic-code-switching\nArabic (MSA) text-to-speech with English words mixed in, two voices, " | |
| "4.5M parameters, trained from scratch. Write English words in Latin letters, with the Arabic article as " | |
| "`الـ` (e.g. `الـ meeting`, `بالـ email`). Numbers are read in Arabic, except inside English names (`iPhone 15`).") | |
| with gr.Row(): | |
| with gr.Column(): | |
| text = gr.Textbox(label="Text", lines=4, rtl=True, value=EXAMPLES[0][0]) | |
| voice = gr.Radio(list(VOICES), value="Female", label="Voice") | |
| speed = gr.Slider(0.7, 1.4, value=1.0, step=0.05, label="Speed") | |
| go = gr.Button("Speak", variant="primary") | |
| with gr.Column(): | |
| out = gr.Audio(label="Speech", autoplay=True) | |
| info = gr.Markdown() | |
| phones = gr.Textbox(label="Pronunciation (phonemes)", lines=3) | |
| gr.Examples(EXAMPLES, [text, voice]) | |
| go.click(synth, [text, voice, speed], [out, info, phones]) | |
| demo.queue(max_size=16).launch() | |