import json import os import random import re import tempfile import urllib.request import gradio as gr import numpy as np import spaces from huggingface_hub import snapshot_download REPO = "n4ze3m/tiny-voca" VOICES = ["F2", "M2"] COUNTER = "abderrahmane11-tiny-voca" # Same cookieless counter as the other demos: one server-side call, short timeout, # nothing about the visitor recorded. def bump(kind): try: url = f"https://abacus.jasoncameron.dev/hit/{COUNTER}/{kind}" with urllib.request.urlopen(url, timeout=2) as r: return json.load(r).get("value") except Exception: return None def on_load(): total = bump("views") if not total: return "" return f"{total:,} page load" + ("" if total == 1 else "s") # The phonemizer reads its data files from this variable, and they ship inside the # model repo rather than in the pip package. LOCAL = snapshot_download(REPO) os.environ.setdefault("DIZIONE_DATA", os.path.join(LOCAL, "phonemizer")) from tiny_voca.infer import load_model, match_frontend, speak # noqa: E402 CHECKPOINT = os.path.join(LOCAL, "tiny-voca.pt") match_frontend(CHECKPOINT) MODEL = load_model(CHECKPOINT, "cpu") RATE = MODEL.config.sample_rate def _split(text, limit=8): """The model takes one utterance per call and rejects anything past its token window. A paragraph is split on sentence ends, long sentences on commas, so a visitor gets an answer instead of an error.""" parts = [p.strip() for p in re.split(r"(?<=[.!?])\s+", text.strip()) if p.strip()] out = [] for p in parts: while len(p) > 180: cut = p.rfind(",", 0, 180) cut = cut if cut > 40 else 180 out.append(p[: cut + 1].strip()) p = p[cut + 1 :].strip() if p: out.append(p) return out[:limit] @spaces.GPU(duration=10) def say(text, voice, speed, pitch_shift, pitch_range, seed): bump("speaks") text = (text or "").strip() if not text: raise gr.Error("Type something to say first.") n = int(seed) if n < 0: n = random.randint(0, 2**31 - 1) chunks = _split(text) try: if len(chunks) == 1: wave, phones, _ = speak( MODEL, text, voice, float(speed), float(pitch_shift), n, float(pitch_range) ) audio = wave.numpy().astype(np.float32) phonemes = " ".join(phones.symbols) detail = f"{len(phones.ids)} phonemes, seed {n}\n\n{phonemes}" else: # Same voice and seed across the paragraph, quiet gaps at the sentence # ends the way a reader pauses. pieces = [] for s in chunks: wave, _, _ = speak( MODEL, s, voice, float(speed), float(pitch_shift), n, float(pitch_range) ) pieces.append(wave.numpy().astype(np.float32)) gap = np.zeros(int(RATE * 0.22), dtype=np.float32) audio = np.concatenate([p for pair in zip(pieces, [gap] * len(pieces)) for p in pair][:-1]) detail = f"{len(chunks)} sentences, seed {n}\n\n" + "\n".join(chunks) except Exception as e: raise gr.Error(str(e)) from e # Peak normalise only if it would clip, so the sample stays honest otherwise. peak = float(np.abs(audio).max()) or 1.0 if peak > 0.99: audio = audio / peak * 0.99 seconds = len(audio) / RATE return (RATE, audio), n, f"{seconds:.2f} s of audio, {detail}" @spaces.GPU(duration=10) def compare(text, speed, seed): """Both voices on the same line, which is the decision a visitor actually makes.""" bump("speaks") text = (text or "").strip() if not text: raise gr.Error("Type something to compare first.") n = int(seed) if int(seed) >= 0 else random.randint(0, 2**31 - 1) out = [] for voice in VOICES: try: wave, _, _ = speak(MODEL, text, voice, float(speed), 0.0, n, 1.0) except Exception as e: raise gr.Error(str(e)) from e audio = wave.numpy().astype(np.float32) out.append((RATE, audio)) return out[0], out[1], f"same text, same seed {n}, F2 above and M2 below", gr.update(visible=True) PRESETS = { "Hello": "Hello! This is tiny voca, a small text to speech model that runs on a phone or in a browser.", "Meeting": "The meeting moved to 3:30 pm on March 14th, so we have about 45 minutes left.", "Surprise": "Did you really think I would forget your birthday?", "Story": "She had never seen the sea before, and for a long while she stood at the edge of the water without saying a word.", } DESCRIPTION = """A 4.5 million parameter English text to speech model. Two voices, F2 and M2, 24 kHz output. It runs about 70 times faster than real time on four CPU threads, so a sentence comes back almost instantly. Everything happens on the weights in [n4ze3m/tiny-voca](https://huggingface.co/n4ze3m/tiny-voca). The phonemizer is the author's dizione front end, shipped with the model. Length: one utterance per call. Paste a paragraph and it is read sentence by sentence with a short pause between them, up to eight sentences, and longer text stops there. Other demos on this account: [background remover](https://huggingface.co/spaces/abderrahmane11/bg-remover), [browser transcription](https://huggingface.co/spaces/abderrahmane11/browser-stt), [tiny-voca text to speech](https://huggingface.co/spaces/abderrahmane11/tiny-voca), [FLUX.2 klein inpainting](https://huggingface.co/spaces/abderrahmane11/flux2-klein-inpaint).""" with gr.Blocks(title="tiny-voca text to speech") as demo: gr.Markdown("# tiny-voca text to speech\n" + DESCRIPTION) counter_line = gr.Markdown("") demo.load(on_load, outputs=counter_line) with gr.Row(): with gr.Column(scale=3): with gr.Row(): preset_buttons = [gr.Button(label, size="sm") for label in PRESETS] text = gr.Textbox( label="Text", value="Hello! This is tiny voca, a small text to speech model that runs on a phone or in a browser.", lines=3, ) with gr.Row(): voice = gr.Radio(VOICES, value="F2", label="Voice") speed = gr.Slider(0.7, 1.4, value=1.0, step=0.05, label="Speed") with gr.Accordion("Voice shaping", open=False): pitch_shift = gr.Slider(-0.12, 0.12, value=0.0, step=0.005, label="Pitch shift, 0.058 is one semitone") pitch_range = gr.Slider(0.5, 1.5, value=1.0, step=0.05, label="Pitch movement") seed = gr.Number(value=-1, label="Seed, -1 is random", precision=0) for btn, sentence in zip(preset_buttons, PRESETS.values()): btn.click(lambda s=sentence: s, outputs=[text]) with gr.Row(): run = gr.Button("Speak", variant="primary") run_both = gr.Button("Compare both voices") with gr.Column(scale=2): audio = gr.Audio(label="Audio", type="numpy") info = gr.Textbox(label="Details", lines=4, interactive=False) with gr.Group(visible=False) as compare_box: audio_b = gr.Audio(label="Second voice", type="numpy") info_b = gr.Textbox(label="Comparison", lines=1, interactive=False) run.click(say, api_name="say", inputs=[text, voice, speed, pitch_shift, pitch_range, seed], outputs=[audio, seed, info]) run_both.click(compare, api_name="compare", inputs=[text, speed, seed], outputs=[audio, audio_b, info_b, compare_box]) demo.launch()