oddadmix's picture
Tags and attribution to n4ze3m/tiny-voca
64e3e2a verified
Raw History Blame Contribute Delete
5.93 kB
"""tiny-voca-arabic demo: Modern Standard Arabic text -> speech on the CPU with ONNX Runtime.
Text pipeline: numbers to words -> CATT diacritization -> rule-based MSA phonemes -> ONNX model.
Long text is read sentence by sentence and the audio is joined.
"""
import json
import os
import re
import time
os.environ.setdefault("CATT_THREADS", "2")
import gradio as gr
import numpy as np
import onnxruntime as ort
from tiny_voca_ar.numbers import expand_numbers
from tiny_voca_ar.text import NothingToPronounce, phonemize
from tiny_voca_ar import arabic_frontend
HERE = os.path.dirname(os.path.abspath(__file__))
META = json.load(open(os.path.join(HERE, "model.json")))
RATE, MAX_TOKENS = META["sample_rate"], META["max_tokens"] - 12
options = ort.SessionOptions()
options.intra_op_num_threads = int(os.environ.get("THREADS", "2"))
SESSION = ort.InferenceSession(os.path.join(HERE, "model.onnx"), options, providers=["CPUExecutionProvider"])
arabic_frontend.diacritize(["مرحبا"]) # load the diacritizer (downloads its ONNX files on first start)
SENTENCE_END = re.compile(r"(?<=[.!?؟…])\s+(?=\S)")
CLAUSE_END = re.compile(r"(?<=[،,;؛:])\s+")
MAX_CHARS = 3000
def _run(phones, speed, pitch_shift, pitch_range):
one = lambda v: np.array([v], dtype=np.float32)
audio, _ = SESSION.run(None, {
"tokens": np.array([phones.ids], dtype=np.int64), "stress": np.array([phones.stress], dtype=np.int64),
"voice": np.array([0], dtype=np.int64), "speed": one(speed), "pitch_shift": one(pitch_shift),
"pitch_range": one(pitch_range)})
return audio[0]
def _pieces(text):
"""Split a sentence that is too long for one call: at commas first, then in halves between words."""
parts = [p for p in CLAUSE_END.split(text) if p.strip()]
if len(parts) > 1:
return parts
words = text.split()
if len(words) < 2:
return []
half = len(words) // 2
return [" ".join(words[:half]), " ".join(words[half:])]
def speak(text, speed=1.0, pitch_shift=0.0, pitch_range=1.0):
text = (text or "").strip()[:MAX_CHARS]
if not text:
raise gr.Error("اكتب نصًا أولًا. Type some text first.")
if text[-1] not in ".!?؟…":
text += "."
start = time.perf_counter()
waves, diacritized = [], []
gap = np.zeros(int(0.12 * RATE), dtype=np.float32)
def read(piece):
try:
phones = phonemize(piece)
except NothingToPronounce:
return
if len(phones) <= MAX_TOKENS:
waves.extend([_run(phones, speed, pitch_shift * 0.058, pitch_range), gap])
diacritized.append(" ".join(phones.diacritized))
return
for part in _pieces(piece):
read(part)
for sentence in SENTENCE_END.split(expand_numbers(text)):
read(sentence)
if not waves:
raise gr.Error("لا يوجد نص عربي للقراءة. There is no Arabic text to read.")
audio = np.concatenate(waves[:-1])
peak = float(np.abs(audio).max())
if peak > 0.99:
audio = audio * (0.99 / peak)
took = time.perf_counter() - start
seconds = len(audio) / RATE
info = f"{seconds:.1f} s of audio in {took:.2f} s on the CPU ({seconds / took:.0f}× real time, including diacritization)"
return (RATE, (audio * 32767).astype(np.int16)), "\n".join(diacritized), info
EXAMPLES = [
["مرحبًا بكم، هذا نموذج صغير لتحويل النص إلى كلام باللغة العربية الفصحى."],
["تقع مدينة القاهرة على ضفاف نهر النيل، وهي من أكبر المدن في العالم العربي."],
["هل يمكنك أن تخبرني عن حالة الطقس غدًا؟ أريد أن أخرج في نزهة مع أصدقائي."],
["يبلغ عدد سكان المدينة 2,500,000 نسمة، ونسبة الشباب فيها 35%."],
["العلم نور، والجهل ظلام. ومن جدّ وجد، ومن زرع حصد."],
]
with gr.Blocks(title="tiny-voca-arabic") as demo:
gr.Markdown(
"# tiny-voca-arabic\n"
"A **4.5M-parameter** Modern Standard Arabic text-to-speech model, trained from scratch, "
"running here on the CPU with ONNX Runtime. Text is diacritized automatically "
"(you can also add your own tashkeel), converted to phonemes with MSA rules, then spoken. "
"[Model](https://huggingface.co/oddadmix/tiny-voca-arabic) · "
"[Dataset](https://huggingface.co/datasets/oddadmix/msa-duplex-assistant-tts) · "
"Based on [tiny-voca](https://huggingface.co/n4ze3m/tiny-voca) by Muhammed Nazeem (@n4ze3m): "
"same architecture and training code, trained from scratch on Arabic.")
with gr.Row():
with gr.Column():
text = gr.Textbox(label="النص (Text)", lines=5, rtl=True, value=EXAMPLES[0][0], max_length=MAX_CHARS)
with gr.Accordion("Settings", open=False):
speed = gr.Slider(0.6, 1.6, value=1.0, step=0.05, label="Speed")
pitch = gr.Slider(-4, 4, value=0, step=0.5, label="Pitch shift (semitones)")
prange = gr.Slider(0.0, 2.0, value=1.0, step=0.1, label="Pitch range (intonation)")
button = gr.Button("تحدّث · Speak", variant="primary")
with gr.Column():
audio = gr.Audio(label="Speech", type="numpy", autoplay=True)
info = gr.Markdown()
shown = gr.Textbox(label="Diacritized text the model read", rtl=True, lines=3, interactive=False)
gr.Examples(EXAMPLES, inputs=[text])
gr.Markdown("The voice is synthetic. Please tell listeners when audio they hear was generated.")
button.click(speak, [text, speed, pitch, prange], [audio, shown, info], api_name="speak")
text.submit(speak, [text, speed, pitch, prange], [audio, shown, info])
if __name__ == "__main__":
demo.queue(default_concurrency_limit=2).launch()