#!/usr/bin/env python3 """Audio8 TTS Preview 0.6B voice gallery for Hugging Face ZeroGPU.""" import json import logging import os import sys import tempfile import time import gradio as gr import requests import soundfile as sf import spaces _DIR = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(_DIR, "src")) import audio8_backend # noqa: E402 import asr_backend # noqa: E402 logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s") SUPPORTED_LANGUAGES = ( "Cantonese, Chinese, Dutch, English, French, German, Italian, " "Japanese, Korean, Polish, Spanish" ) # ── Voices ─────────────────────────────────────────────────────────────────── with open(os.path.join(_DIR, "voices.json"), encoding="utf-8") as _f: VOICES = json.load(_f) LANGUAGES = ["All"] + sorted({v.get("language", "") for v in VOICES if v.get("language")}) GENDERS = ["All", "female", "male", "neutral"] PER_PAGE = 20 logging.info(f"Loaded {len(VOICES):,} reference voices") def _filter(search, lang, gender, accent): s = (search or "").lower() return [ v for v in VOICES if (lang == "All" or v.get("language") == lang) and (gender == "All" or v.get("gender") == gender) and (accent == "All" or v.get("accent") == accent) and (not s or s in v.get("name", "").lower() or s in (v.get("description") or "").lower()) ] def _accents_for(lang): pool = VOICES if lang == "All" else [v for v in VOICES if v.get("language") == lang] return ["All"] + sorted({v.get("accent", "") for v in pool if v.get("accent")}) def _card_html(v): g = v.get("gender", "") badge_cls = {"female": "badge-f", "male": "badge-m"}.get(g, "badge-n") badge_sym = {"female": "♀", "male": "♂"}.get(g, "•") badge = f'{badge_sym}' name = v.get("name", "Unknown") lt, at, ag = v.get("language", "?"), v.get("accent", "?"), v.get("age", "?") desc = (v.get("description") or "")[:100] src = v.get("preview_url", "") return ( f'
{desc}
' if desc else "") + f'' ) _INITIAL_CHUNK = VOICES[:PER_PAGE] _INITIAL_TOTAL_PAGES = max(1, (len(VOICES) + PER_PAGE - 1) // PER_PAGE) # ── Models ─────────────────────────────────────────────────────────────────── audio8_backend.load() asr_backend.load() @spaces.GPU(duration=60, size="large") def _generate_gpu(prompt, ref_audio_path, ref_text, temperature, top_p, top_k, max_new_tok, seed): return audio8_backend.generate( prompt, voice_ref=ref_audio_path, reference_text=ref_text, temperature=temperature, top_p=top_p, top_k=top_k, max_new_tokens=max_new_tok, seed=seed, ) def on_generate(prompt, ref_audio_path, ref_text, temperature, top_p, top_k, max_new_tok, seed, progress=gr.Progress()): """Synthesize speech from text, optionally cloning the reference voice.""" if not (prompt or "").strip(): raise gr.Error("Prompt is empty.") # Validate here, client-side of the ZeroGPU fork boundary, so a real # user-input problem never has to survive that boundary: every exception # a @spaces.GPU-decorated call raises — regardless of its original type — # crosses back as a gradio.exceptions.Error, which is itself a ValueError # subclass (gradio_client.exceptions.AppError(ValueError)). That makes a # genuine worker crash indistinguishable from a validation ValueError # once it reaches us, so anything past this point is treated as a worker # failure eligible for retry, never as a user-facing validation error. if ref_audio_path and not (ref_text or "").strip(): raise gr.Error( "This model needs a transcript of the reference clip to clone it. " "Fill in \"Reference transcript\" (auto-transcription may have failed) " "or clear the reference audio to generate without cloning." ) max_attempts = 6 backoff_schedule = [2, 4, 6, 8, 10] # seconds between attempts; ~30s total window last_err = None for attempt in range(1, max_attempts + 1): try: progress( 0.5, desc="Generating with Audio8 TTS…" if attempt == 1 else f"ZeroGPU allocation hiccup — retrying ({attempt}/{max_attempts})…", ) waveform, sr = _generate_gpu( prompt.strip(), ref_audio_path, ref_text, float(temperature), float(top_p), int(top_k), int(max_new_tok), int(seed), ) except Exception as e: # ZeroGPU occasionally fails to bind a physical GPU to the fresh # worker before our code runs at all (infra-side flakiness — see # spaces/zero/wrappers.py::worker_init in the server logs). The # wrapped exception carries only the original class name, not # its message, so we can't pattern-match the text — just retry, # with growing backoff since the underlying blip can outlast a # couple of quick retries. last_err = e if attempt == max_attempts: raise gr.Error(f"Generation failed after {max_attempts} attempts: {e}") time.sleep(backoff_schedule[attempt - 1]) continue out = tempfile.mktemp(suffix=".wav", prefix="audio8_", dir="/tmp") sf.write(out, waveform, sr) return out # ── CSS ────────────────────────────────────────────────────────────────────── CSS = """ /* The Spaces embed puts this app in an iframe with scrolling="no" and relies on a postMessage height handshake to grow the iframe to fit the page — which doesn't reliably catch up with a tall, dynamic page like this one, leaving content clipped with no way to scroll it into view. Capping the app to the iframe's own viewport and scrolling *inside* that box works regardless of the outer handshake, since scrolling="no" only blocks the iframe's own native scrollbar, not wheel-driven overflow scrolling on an element inside its document. */ html, body { height: 100vh !important; margin: 0; overflow: hidden !important; } .gradio-container { height: 100vh !important; overflow-y: auto !important; } /* card grid */ .card-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; } @media (max-width: 1200px) { .card-grid { grid-template-columns: repeat(3, 1fr); } } @media (max-width: 800px) { .card-grid { grid-template-columns: repeat(2, 1fr); } } /* individual card — scoped inside the Gradio column */ .voice-card { background: #14181f !important; border: 1px solid #26303f !important; border-radius: 10px !important; padding: 14px !important; height: 100% !important; } .voice-card:hover { border-color: #2dd4bf !important; } /* card header line */ .card-header { display: flex; align-items: flex-start; gap: 8px; margin-bottom: 6px; } .badge-f { background: #3d0e2a; color: #e080b0; font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 4px; white-space: nowrap; } .badge-m { background: #0e2a3d; color: #80c0e0; font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 4px; white-space: nowrap; } .badge-n { background: #1e2a1e; color: #a0c8a0; font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 4px; white-space: nowrap; } .card-name { font-size: 13px; font-weight: 600; color: #dde5f0; line-height: 1.35; } /* tags row */ .card-tags { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 4px; } .card-tags span { font-size: 10px; padding: 2px 6px; border-radius: 3px; } .t-lang { background: #123a2e; color: #6fd7b5; } .t-acc { background: #16283a; color: #7fb0d9; } .t-age { background: #2a1e2a; color: #b08cc0; } /* description */ .card-desc { font-size: 11px; color: #6a7590; line-height: 1.4; margin-bottom: 4px; } /* "Use this voice" button override */ .use-btn { background: #2dd4bf !important; color: #04231f !important; border: none !important; font-weight: 700 !important; } .use-btn:hover { background: #5fe4d3 !important; } /* selected voice banner */ .sel-banner { background: #0d1a17; border: 1px solid #204a3f; border-radius: 8px; padding: 10px 14px; margin: 6px 0; } /* pagination */ .pager-row { display: flex; align-items: center; gap: 12px; padding: 8px 0; } """ # ── UI ─────────────────────────────────────────────────────────────────────── with gr.Blocks(title="Audio8 TTS Preview", analytics_enabled=False, css=CSS) as app: gr.Markdown( "# 🗣️ Audio8 TTS Preview 0.6B\n" "A 0.6B-parameter multilingual TTS model with zero-shot voice cloning " "([model card](https://huggingface.co/Audio8/Audio8-TTS-Preview-0.6b)). " f"Browse **{len(VOICES):,} reference voices**, hit ▶ to preview, then " "**Use this voice** to clone it — or upload/record your own reference clip.\n\n" f"⚠️ Generated text should be one of the model's supported languages: " f"**{SUPPORTED_LANGUAGES}**. Reference clips in other languages still work " "as voice-timbre references, but transcription/cloning quality is best " "within these 11." ) # Filters with gr.Row(): search_in = gr.Textbox(placeholder="Search by name or description…", label="Search", scale=3) lang_in = gr.Dropdown(LANGUAGES, value="All", label="Language", scale=2) gender_in = gr.Radio(GENDERS, value="All", label="Gender", scale=2) accent_in = gr.Dropdown(["All"], value="All", label="Accent", scale=2) result_md = gr.Markdown(f"**{len(VOICES):,}** voices found") # ── Fixed card grid (PER_PAGE slots), rendered synchronously with the # first page's data baked in — the whole point is that the initial page # load already contains the full-height gallery. Populating it instead # via app.load() (a websocket round-trip after mount) makes the page's # true height arrive too late for the Spaces iframe's one-shot resize # measurement, leaving the embed clipped with scrolling disabled. ────── card_rows = [] # gr.Column slots (show/hide) card_html = [] # gr.HTML — full card content incl.