Spaces:
Running

oddadmix's picture
tiny-voca-persian demo
ca681c0 verified
Raw History Blame Contribute Delete
2.81 kB
import os, time
os.environ.setdefault("TV_FA_G2P_REVISION", "3f6994c82e52628fe948102a7009d281fc9aa99d") # the G2P revision the model was trained with
os.environ.setdefault("TV_FA_G2P_DEVICE", "cpu")
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)
MODEL = load_model(hf_hub_download("oddadmix/tiny-voca-persian", "tiny-voca-persian.pt", token=os.environ.get("HF_TOKEN")))
VOICES = {"Female": "fa_female", "Male": "fa_male"}
EXAMPLES = [
["سلام، وقت بخیر. سفارش شما با موفقیت ثبت شد و تا دو روز آینده به دستتان می‌رسد.", "Female"],
["جلسه‌ی فردا ساعت ده صبح در دفتر مرکزی برگزار می‌شود. لطفاً گزارش هفته‌ی گذشته را همراه داشته باشید.", "Male"],
["این هفته روی همه‌ی محصولات تخفیف ویژه داریم و ارسال برای خریدهای امروز رایگان است.", "Female"],
["ایران کشوری با تاریخ کهن و فرهنگی غنی است که هر سال میلیون‌ها گردشگر را به خود جذب می‌کند.", "Male"],
]
speak_text(MODEL, "سلام", "fa_female") # warm-up: loads the G2P
def synth(text, voice, speed):
text = (text or "").strip()[:1000]
if not text:
raise gr.Error("Write some Persian 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.diacritized) 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-persian") as demo:
gr.Markdown("# tiny-voca-persian\nPersian (Farsi) text-to-speech, two voices, 4.5M parameters, trained from scratch. "
"Write ordinary Persian text; numbers are read out in words.")
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 (G2P romanization)", lines=3)
gr.Examples(EXAMPLES, [text, voice])
go.click(synth, [text, voice, speed], [out, info, phones])
demo.queue(max_size=16).launch()