Spaces:
Running on Zero
Running on Zero
File size: 7,631 Bytes
1ee45b7 6888a3d e0bd7e0 6888a3d 1ee45b7 6888a3d 1ee45b7 6888a3d e0bd7e0 f2a9aa4 6888a3d 1ee45b7 6888a3d f2a9aa4 6888a3d f2a9aa4 6888a3d 3d20d91 f2a9aa4 3d20d91 1ee45b7 6888a3d e0bd7e0 bcdd5af 6888a3d 1ee45b7 6888a3d 1ee45b7 6888a3d 1ee45b7 3d20d91 6888a3d 3d20d91 6888a3d 3d20d91 6888a3d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | 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]
def _render(text, voice, speed, pitch_shift, pitch_range, n):
"""One utterance, or a paragraph read sentence by sentence with the same voice
and seed and a short pause between. Both buttons go through this, so Compare
accepts a paragraph for the same reason Speak does."""
chunks = _split(text)
if len(chunks) == 1:
wave, phones, _ = speak(MODEL, text, voice, float(speed), float(pitch_shift), n, float(pitch_range))
return wave.numpy().astype(np.float32), f"{len(phones.ids)} phonemes\n\n" + " ".join(phones.symbols)
pieces = []
for piece in chunks:
wave, _, _ = speak(MODEL, piece, 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])
return audio, f"{len(chunks)} sentences\n\n" + "\n".join(chunks)
@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)
try:
audio, detail = _render(text, voice, speed, pitch_shift, pitch_range, n)
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, seed {n}\n\n{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:
audio, _ = _render(text, voice, speed, 0.0, 1.0, n)
except Exception as e:
raise gr.Error(str(e)) from e
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()
|