File size: 25,318 Bytes
a454062
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1872be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a454062
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1872be
a454062
 
 
 
 
 
b1872be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a454062
b1872be
 
a454062
 
 
 
 
 
 
 
 
 
b1872be
 
 
 
a454062
 
 
 
 
 
 
 
 
 
 
 
b1872be
a454062
 
b1872be
 
 
 
a454062
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1872be
 
a454062
b1872be
 
a454062
 
 
b1872be
 
 
 
 
 
 
a454062
b1872be
 
 
a454062
 
 
 
 
 
 
 
 
 
 
b1872be
a454062
 
b1872be
 
 
 
 
 
 
 
 
 
a454062
 
b1872be
a454062
 
 
 
 
 
 
 
 
b1872be
a454062
 
b1872be
 
 
 
 
 
 
 
 
a454062
 
 
 
 
 
 
 
 
b1872be
 
a454062
 
 
 
 
 
b1872be
a454062
b1872be
a454062
 
 
 
 
b1872be
a454062
b1872be
 
a454062
 
 
 
 
b1872be
 
 
 
 
 
 
 
a454062
b1872be
 
 
 
 
 
a454062
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1872be
a454062
 
 
b1872be
a454062
 
 
 
 
 
 
b1872be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a454062
 
 
b1872be
a454062
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
"""The loudkit demo Space: hear twenty voices, speak your own text, clone your own.

ZeroGPU bills GPU time to the *visitor*, not to the owner: an anonymous visitor
gets about two minutes a day, a signed-in free account about five. A demo whose
first click spends that budget is one most people bounce off before they have
heard anything at all. So the Listen tab is twenty pre-rendered files served
straight out of this repo — no GPU, no queue, no quota — and the GPU is spent
only on what a visitor types or records.

The engine and the enroller are both built on `cuda` at module level, which is
what ZeroGPU asks for: CUDA transfers are optimised for start-up placement, and
lazy-loading inside a `@spaces.GPU` function is explicitly discouraged. Each
decorated call then runs in a freshly forked, short-lived process, which is also
why there is no `torch.compile` and no CUDA graph capture here: both pay their
cost once per process and would never amortise.

Cloning is exposed, which the CPU scaffold this replaces deliberately did not do.
The reasoning that kept it out was about consent, not about capability, so the
consent is built into the shape of the tab rather than written beside it: the
microphone is the default path, an upload is secondary and gated on an explicit
confirmation, and neither recording outlives the request that carried it.
"""

from __future__ import annotations

import contextlib
import dataclasses
import hashlib
import json
import os
import tempfile
from pathlib import Path

# Before torch, and before anything that imports torch. The module installs the
# CUDA emulation that lets a module-level `.to("cuda")` succeed on a machine
# that has no GPU attached yet.
import spaces

import gradio as gr
import numpy as np

import loudkit as lk
from loudkit.backends.torch_backend import build_torch_enroller
from loudkit.hub import resolve_enrollment_checkpoint, resolve_voice_encoder

REPO = "loudreader/loudr-1"
DEVICE = "cuda"
HERE = Path(__file__).parent

# The CPU scaffold capped text at 300 characters because CPU synthesis ran at
# roughly a tenth of real time. On a GPU the cap is about the visitor's daily
# quota instead, which is a far looser bound: 1000 characters is ~70 s of speech.
MAX_CHARS = 1_000
MAX_CLONE_CHARS = 400
MAX_PROBE_CHARS = 200

# The enroller refuses anything over 30 s and wants 5 to 10. The prompt is built
# from the first 10 s; the speaker embedding reads whatever else is there, so a
# little past the prompt window is useful and 20 s stays clear of the refusal.
ENROLL_SECONDS = 20.0

DOCS = "https://github.com/loudreader/loudkit"
IDENTITY_CONTRACT = f"{DOCS}/blob/main/docs/reference/IDENTITY-CONTRACT.md"
RESPONSIBLE_USE = f"{DOCS}/blob/main/RESPONSIBLE_USE.md"

ROSTER = json.loads((HERE / "voices.json").read_text(encoding="utf-8"))
BY_NAME = {entry["name"]: entry for entry in ROSTER}
ORDERED = sorted(ROSTER, key=lambda e: (e["language"], e["name"]))
VOICE_CHOICES = [(f"{e['name']} · {e['language']} ({e['gender']})", e["name"]) for e in ORDERED]

# English leads. A visitor who does not read the other nine should not have to
# hunt for the one they do, and alphabetical order put Danish first.
FIRST_LANGUAGE = "en"
_LANG_OF = {e["language_id"]: e["language"] for e in ROSTER}
LANGUAGE_FILTER = [(_LANG_OF[FIRST_LANGUAGE], FIRST_LANGUAGE)] + [
    (name, code) for code, name in sorted(_LANG_OF.items(), key=lambda kv: kv[1])
    if code != FIRST_LANGUAGE
]


def voices_in(language: str) -> list[tuple[str, str]]:
    return [
        (f"{e['name']} ({e['gender']})", e["name"])
        for e in ORDERED
        if e["language_id"] == language
    ]


FIRST_VOICES = voices_in(FIRST_LANGUAGE)
FIRST_VOICE = FIRST_VOICES[0][1]

# Reference clips a visitor can clone without recording anything. Every one was
# donated for building TTS voices, so these need no consent box; the roster
# carries the licence and the donor's own words beside each.
CLONE_EXAMPLES = ["joe", "kathleen", "ines", "gosia"]
EXAMPLE_CHOICES = [("Nothing selected", "")] + [
    (f"{n} · {BY_NAME[n]['language']}", n) for n in CLONE_EXAMPLES
]

# --------------------------------------------------------------------------
# Module-level model placement, per the ZeroGPU contract.
# --------------------------------------------------------------------------

engine = lk.load(REPO, device=DEVICE)

# Voice profiles are numpy, not torch, so they are device-agnostic and cost a
# few hundred kilobytes each. Loading all twenty up front means switching voice
# in the Speak tab never blocks on a download.
PROFILES = {entry["name"]: lk.voice(entry["name"], repo=REPO) for entry in ROSTER}

# Enrollment reads the other half of the release: the speech tokenizer and the
# speaker encoder, which synthesis never touches, plus the utterance voice
# encoder that sits beside both. `lk.enroll()` builds this per call by design;
# a Space would pay the load on every clone, so it is built once here instead.
enroller = build_torch_enroller(
    str(resolve_enrollment_checkpoint(REPO)),
    device=DEVICE,
    voice_encoder_weights=str(resolve_voice_encoder(REPO)),
)

FINGERPRINT = engine.algorithm.fingerprint()

_LANGUAGE_NAMES = {e["language_id"]: e["language"] for e in ROSTER}
LANGUAGE_CHOICES = [("Follow the voice", "")] + [
    (f"{_LANGUAGE_NAMES.get(code, code)} ({code})", code) for code in lk.languages()
]

# --------------------------------------------------------------------------
# Helpers
# --------------------------------------------------------------------------


def _sha256_audio(audio: np.ndarray) -> str:
    """Hash the waveform, not the file.

    `Result.save` appends a C2PA manifest carrying a wall-clock creation time,
    which the library itself calls the one byte range in which two identical
    renders may legitimately differ. Hashing the saved WAV would therefore print
    two different digests for two identical renders and read as a determinism
    failure. The waveform is what the identity contract makes its promise about,
    so the waveform is what gets hashed.
    """
    return hashlib.sha256(np.ascontiguousarray(audio, dtype=np.float32).tobytes()).hexdigest()


def _write(result: lk.Result, *, voice: str, language: str) -> str:
    out = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
    out.close()
    # Provenance on: the manifest carries the fingerprint, the recipe and the
    # seed, which is the machine-readable marking a synthetic-speech demo should
    # be handing out by default.
    result.save(out.name, voice=voice, language=language)
    return out.name


def _stats(result: lk.Result) -> str:
    seconds = len(result.audio) / result.sample_rate
    return (
        f"**{seconds:.1f} s of audio.** {result.timings.describe(seconds)}\n\n"
        f"`algo[{result.algorithm_fingerprint}]` · seed `{result.seed}` · "
        f"speed `{result.speed:g}x` · {result.sample_rate} Hz"
    )


def _estimate(text: str, *, passes: int = 1, overhead: float = 15.0) -> int:
    """Seconds of GPU to ask for.

    Speech runs at roughly 14 characters a second, and the render is asked to
    keep up with better than real time; the overhead covers the process fork and
    the first real CUDA touch. Asking for too much costs queue priority but not
    quota, which is charged on effective duration, so this leans generous.
    """
    audio_seconds = len((text or "").strip()) / 14.0
    return int(min(180.0, overhead + passes * max(4.0, audio_seconds * 0.9)))


def _check(text: str, limit: int) -> str:
    text = (text or "").strip()
    if not text:
        raise gr.Error("Type something to say.")
    if len(text) > limit:
        raise gr.Error(f"Keep it under {limit:,} characters here. The library itself takes 10,000.")
    return text


# --------------------------------------------------------------------------
# Listen. No GPU: these files were rendered ahead of time and ship in the repo.
# --------------------------------------------------------------------------


def listen(name: str):
    entry = BY_NAME[name]
    sample, reference, source = entry["sample"], entry["reference"], entry["source"]

    lines = [
        f"### {entry['name']}. {entry['language']} ({entry['gender']}).",
        "",
        f"> {sample['text']}",
        "",
        f"From *{sample['work']}*, seed `{sample['seed']}`.",
        "",
        f"- Reference recording: {reference['duration_s']:.1f} s, {reference['construction']}.",
        f"- Source: [{source['name']}]({source['url']}), {source['license']}.",
        f"- Consent: {source['consent']}.",
    ]
    similarity = entry.get("speaker_similarity")
    if similarity is not None:
        lines.append(f"- Speaker similarity to the reference: {similarity:.3f}.")
    lines.append(f"- Voice profile: `{entry['profile']['hf_path']}`.")

    return (
        str(HERE / sample["audio"]),
        str(HERE / reference["public_preview"]),
        "\n".join(lines),
    )


ROSTER_TABLE = [
    [
        entry["name"],
        entry["language"],
        entry["gender"],
        entry["source"]["license"],
        f"{entry['speaker_similarity']:.3f}" if entry.get("speaker_similarity") is not None else "",
    ]
    for entry in ORDERED
]


# --------------------------------------------------------------------------
# Speak. GPU.
# --------------------------------------------------------------------------


def _speak_duration(text, name, language, seed, speed):
    return _estimate(text, overhead=15.0)


@spaces.GPU(duration=_speak_duration)
def speak(text: str, name: str, language: str, seed: float, speed: float):
    text = _check(text, MAX_CHARS)
    result = engine.synthesize_long(
        text,
        PROFILES[name],
        seed=int(seed),
        language=language or None,
        speed=float(speed),
    )
    label = language or BY_NAME[name]["language_id"]
    return _write(result, voice=name, language=label), _stats(result)


# --------------------------------------------------------------------------
# Clone. GPU. The microphone is the default path; an upload is gated.
# --------------------------------------------------------------------------


def _clone_duration(example, mic, upload, consent, text, language, seed, speed):
    # Enrollment is a fixed cost on top of the render: two encoders and a
    # tokenizer over at most 20 s of audio.
    return _estimate(text, overhead=30.0)


@spaces.GPU(duration=_clone_duration)
def clone(example, mic, upload, consent: bool, text: str, language: str, seed, speed):
    """Enroll a voice, speak with it, keep nothing.

    Three ways in, and they do not carry the same consent story, so they are
    not collapsed into one input. A shipped example is a clip whose donor
    released it for exactly this. A microphone recording is the visitor's own
    voice, which is consent by construction. An upload is neither, so it is the
    only one gated on a checkbox.
    """
    if example:
        # A file that ships in this repo. It must survive the request.
        source = str(HERE / BY_NAME[example]["reference"]["public_preview"])
        ephemeral = False
        label = example
    else:
        source = mic or upload
        ephemeral = True
        label = "cloned"
        if not source:
            raise gr.Error(
                "Pick an example, record yourself, or upload a clip you are allowed to use."
            )
        if upload and not mic and not consent:
            raise gr.Error(
                "Confirm the uploaded voice is yours, or that you have permission to use it."
            )

    text = _check(text, MAX_CLONE_CHARS)
    if example and not language:
        language = BY_NAME[example]["language_id"]

    try:
        import librosa

        samples, _ = librosa.load(source, sr=24_000, mono=True)
        limit = int(ENROLL_SECONDS * 24_000)
        if samples.size > limit:
            samples = samples[:limit]

        try:
            # The profile stays a local. It is never saved, never returned and
            # never offered for download: the embeddings are the part of a
            # cloned voice that would outlive the request if anything held them.
            profile = enroller.enroll(samples, 24_000, name=label)
        except ValueError as exc:
            # The library's own messages name the bound and describe a good
            # input, which is more useful than anything restated here.
            raise gr.Error(str(exc)) from exc

        # `enroll` writes no language, so every cloned voice would claim English
        # and read its text through the English funnel.
        profile = dataclasses.replace(profile, language=language or "en")

        result = engine.synthesize_long(
            text, profile, seed=int(seed), language=language or None, speed=float(speed)
        )
        return _write(result, voice=label, language=profile.language), _stats(result)
    finally:
        # Nothing the visitor recorded outlives the request that carried it.
        # A shipped example is not the visitor's and is not ours to delete.
        if ephemeral and source:
            with contextlib.suppress(OSError):
                os.unlink(source)


# --------------------------------------------------------------------------
# Determinism probe. GPU. Renders the same text twice at the same seed.
# --------------------------------------------------------------------------


def _probe_duration(text, name, seed):
    return _estimate(text, passes=2, overhead=20.0)


@spaces.GPU(duration=_probe_duration)
def probe(text: str, name: str, seed: float):
    text = _check(text, MAX_PROBE_CHARS)
    profile = PROFILES[name]
    first = engine.synthesize_long(text, profile, seed=int(seed))
    second = engine.synthesize_long(text, profile, seed=int(seed))

    left, right = _sha256_audio(first.audio), _sha256_audio(second.audio)
    verdict = "Identical." if left == right else "Different. Please report this."

    return "\n".join(
        [
            f"**{verdict}**",
            "",
            "```",
            f"render 1  sha256  {left}",
            f"render 2  sha256  {right}",
            f"          algo[{first.algorithm_fingerprint}]  seed {int(seed)}",
            "```",
            "",
            "Identical within this build and this device. loudkit promises a "
            "bit-identical waveform for the same seed, build, backend and input. "
            "It does not promise that your laptop matches this GPU. "
            f"[Read the identity contract]({IDENTITY_CONTRACT}).",
        ]
    )


# --------------------------------------------------------------------------
# Interface
# --------------------------------------------------------------------------

# loudreader.io: cream ground, ink text, black pill buttons at 14px.
CSS = """
#lk-head h1 { font-size: 2.15rem; margin-bottom: .25rem; letter-spacing: -.02em; }
#lk-head p { margin-top: 0; }
.lk-pill {
  display: inline-block; padding: .2rem .75rem; margin: .15rem .35rem .15rem 0;
  border: 1px solid #ded8ce; border-radius: 999px; font-size: .8rem;
  color: #374151; background: #fffdfa;
}
.lk-card { background: #fffdfa; border: 1px solid #e7e1d7; border-radius: 14px; padding: .35rem 1rem; }
footer { display: none !important; }
"""

# Gradio follows the visitor's system theme unless told otherwise, and this
# palette is light-first. Without this the ink-on-cream tokens below land under
# a dark stylesheet and the text turns near-white on a cream ground.
FORCE_LIGHT = """
() => {
  const url = new URL(window.location);
  if (url.searchParams.get('__theme') !== 'light') {
    url.searchParams.set('__theme', 'light');
    window.location.replace(url.href);
  }
}
"""

THEME = gr.themes.Soft(
    primary_hue=gr.themes.colors.gray,
    neutral_hue=gr.themes.colors.stone,
    font=[gr.themes.GoogleFont("Inter"), "system-ui", "sans-serif"],
).set(
    body_background_fill="#f7f5f2",
    body_text_color="#111827",
    body_text_color_subdued="#4b5563",
    block_background_fill="#fffdfa",
    block_border_color="#e7e1d7",
    border_color_primary="#e7e1d7",
    input_background_fill="#ffffff",
    button_primary_background_fill="#111827",
    button_primary_background_fill_hover="#374151",
    button_primary_text_color="#ffffff",
    button_large_radius="14px",
    button_small_radius="14px",
)

with gr.Blocks(title="loudkit", theme=THEME, css=CSS, js=FORCE_LIGHT, fill_width=False) as demo:
    gr.Markdown(
        f"""
# Twenty voices. Ten languages. One engine.

On-device text to speech, running here on ZeroGPU.
[Model](https://huggingface.co/{REPO}) · [Code]({DOCS}) · [Responsible use]({RESPONSIBLE_USE})

<span class="lk-pill">Listening costs no GPU</span>
<span class="lk-pill">Speaking and cloning spend your daily quota</span>
<span class="lk-pill">algo[{FINGERPRINT}]</span>
""",
        elem_id="lk-head",
    )

    with gr.Tabs():
        # ------------- Voices: listen for free, then type your own -------------
        with gr.Tab("Voices"):
            gr.Markdown(
                "Pick a voice and the sample plays at once. Those are rendered "
                "ahead of time and use no GPU. Type your own text underneath."
            )
            with gr.Row():
                with gr.Column(scale=1):
                    with gr.Row():
                        lang_pick = gr.Dropdown(
                            LANGUAGE_FILTER, value=FIRST_LANGUAGE, label="Language"
                        )
                        pick = gr.Dropdown(
                            FIRST_VOICES, value=FIRST_VOICE, label="Voice"
                        )
                    made = gr.Audio(label="loudkit", type="filepath", interactive=False)
                    ref = gr.Audio(
                        label="Reference recording", type="filepath", interactive=False
                    )
                with gr.Column(scale=1):
                    card = gr.Markdown(elem_classes="lk-card")

            with gr.Accordion("The whole roster", open=False):
                gr.Dataframe(
                    value=ROSTER_TABLE,
                    headers=["Voice", "Language", "Gender", "Licence", "Similarity"],
                    interactive=False,
                    wrap=True,
                )

            gr.Markdown("### Say something in this voice.")
            gr.Markdown(
                f"Up to {MAX_CHARS:,} characters here. The library itself takes 10,000. "
                "This part spends your ZeroGPU quota."
            )
            say = gr.Textbox(
                label="Your text",
                placeholder="Hello from loudkit.",
                lines=3,
                # Without an explicit ceiling the box renders at its default
                # maximum, which is twenty rows of empty space.
                max_lines=6,
                max_length=MAX_CHARS,
            )
            with gr.Row():
                say_lang = gr.Dropdown(LANGUAGE_CHOICES, value="", label="Read the text as")
                say_seed = gr.Number(value=7, precision=0, label="Seed")
                say_speed = gr.Slider(
                    lk.MIN_SPEED, lk.MAX_SPEED, value=1.0, step=0.05, label="Speed"
                )
            say_go = gr.Button("Speak", variant="primary")
            say_out = gr.Audio(label="Speech", type="filepath")
            say_stats = gr.Markdown()

            say_go.click(
                speak, [say, pick, say_lang, say_seed, say_speed], [say_out, say_stats]
            )

            def on_language(language):
                choices = voices_in(language)
                name = choices[0][1]
                return (gr.Dropdown(choices=choices, value=name), *listen(name))

            lang_pick.change(on_language, lang_pick, [pick, made, ref, card])
            pick.change(listen, pick, [made, ref, card])
            demo.load(listen, pick, [made, ref, card])

            with gr.Accordion("Determinism check", open=False):
                gr.Markdown(
                    "This renders the same text twice at the same seed and hashes "
                    "both waveforms. The digests must match."
                )
                with gr.Row():
                    probe_text = gr.Textbox(
                        value="The same seed gives the same audio.",
                        label="Text",
                        lines=1,
                        max_lines=2,
                        max_length=MAX_PROBE_CHARS,
                        scale=3,
                    )
                    probe_seed = gr.Number(value=7, precision=0, label="Seed", scale=1)
                probe_go = gr.Button("Render twice")
                probe_out = gr.Markdown()
                probe_go.click(probe, [probe_text, pick, probe_seed], probe_out)

        # ---------------------------- Clone ----------------------------
        with gr.Tab("Clone"):
            gr.Markdown(
                f"""
Clone a voice from a short recording, then speak with it.

- Try one of the shipped examples, or record yourself.
- Clone only your own voice, or a voice you have permission to use.
- Nothing is kept. The recording and the voice embeddings are discarded when the
  request ends, and neither is offered for download.
- See [Responsible use]({RESPONSIBLE_USE}).
"""
            )
            with gr.Row():
                with gr.Column(scale=1):
                    example = gr.Dropdown(
                        EXAMPLE_CHOICES,
                        value=CLONE_EXAMPLES[0],
                        label="Try an example",
                        info="Reference clips donated for building TTS voices.",
                    )
                    example_ref = gr.Audio(
                        label="What gets cloned",
                        type="filepath",
                        interactive=False,
                        show_download_button=False,
                    )
                    gr.Markdown("Or use your own voice. That clears the example.")
                    mic = gr.Audio(
                        sources=["microphone"], type="filepath", label="Record yourself"
                    )
                    with gr.Accordion("Upload a file instead", open=False):
                        upload = gr.Audio(
                            sources=["upload"], type="filepath", label="Audio file"
                        )
                        consent = gr.Checkbox(
                            value=False,
                            label=(
                                "This is my own voice, or I have permission from the "
                                "person who owns it."
                            ),
                        )
                with gr.Column(scale=1):
                    clone_text = gr.Textbox(
                        label="Text to speak",
                        placeholder="Now in my own voice.",
                        lines=3,
                        max_lines=6,
                        max_length=MAX_CLONE_CHARS,
                    )
                    clone_lang = gr.Dropdown(
                        LANGUAGE_CHOICES, value="", label="Language of the text"
                    )
                    with gr.Row():
                        clone_seed = gr.Number(value=7, precision=0, label="Seed")
                        clone_speed = gr.Slider(
                            lk.MIN_SPEED, lk.MAX_SPEED, value=1.0, step=0.05, label="Speed"
                        )
                    clone_go = gr.Button("Clone and speak", variant="primary")
                    clone_out = gr.Audio(
                        label="Speech", type="filepath", show_download_button=False
                    )
                    clone_stats = gr.Markdown()

            def show_example(name):
                if not name:
                    return None
                return str(HERE / BY_NAME[name]["reference"]["public_preview"])

            def clear_example(value):
                # Recording or uploading takes over from the example, so the two
                # cannot both be armed and leave the visitor guessing which won.
                return gr.Dropdown(value="") if value else gr.skip()

            example.change(show_example, example, example_ref)
            demo.load(show_example, example, example_ref)
            mic.change(clear_example, mic, example)
            upload.change(clear_example, upload, example)

            clone_go.click(
                clone,
                [example, mic, upload, consent, clone_text, clone_lang, clone_seed, clone_speed],
                [clone_out, clone_stats],
            )

    gr.Markdown(
        f"""
---
Run the same engine locally, where nothing is queued and nothing is metered.

```bash
pip install "loudkit[torch,audio,enroll,hub]"
```

```python
import loudkit as lk

engine = lk.load("{REPO}")
voice = lk.voice("kathleen", repo="{REPO}")
engine.synthesize_long("Hello from loudkit.", voice, seed=7).save("hello.wav")
```

Output files carry C2PA provenance: the fingerprint, the recipe and the seed.
"""
    )

# The engine holds one set of weights and renders with an internal producer
# thread. One render at a time keeps two requests off the same buffers.
demo.queue(default_concurrency_limit=1, max_size=24)

if __name__ == "__main__":
    demo.launch()