Skip to content

SOTA-Class TTS at Compact Scale

Python

Add 0.1B ONNX runtime web UI

chuarunyuan98c43b7ff1e7
diff --git a/onnx_runtime_0_1b_int8/README.md b/onnx_runtime_0_1b_int8/README.md
index 6836efe..ce8ae46 100644
--- a/onnx_runtime_0_1b_int8/README.md
+++ b/onnx_runtime_0_1b_int8/README.md
@@ -71,6 +71,11 @@ scope of this export.
## Local HTTP service
+The service includes the same local browser UI as the 0.6B ONNX runtime. Open
+`http://127.0.0.1:8024` after startup to enter text, select a voice, play or
+download WAV output, register a reference voice, inspect memory, and reload
+the runtime. The developer API remains available at `/docs`.
+
```bash
bash start_server.sh
curl http://127.0.0.1:8024/api/health
diff --git a/onnx_runtime_0_1b_int8/README_zh.md b/onnx_runtime_0_1b_int8/README_zh.md
index 56d1f10..65f6158 100644
--- a/onnx_runtime_0_1b_int8/README_zh.md
+++ b/onnx_runtime_0_1b_int8/README_zh.md
@@ -45,6 +45,10 @@ bash run_infer.sh \
## HTTP 服务
+服务同时提供与 0.6B ONNX Runtime 相同的本地网页界面。启动后打开
+`http://127.0.0.1:8024`,即可输入文本、选择音色、播放或下载 WAV、注册参考音色、
+查看内存状态和重新加载运行时;开发者 API 文档仍在 `/docs`。
+
```bash
bash start_server.sh
curl http://127.0.0.1:8024/api/health
diff --git a/onnx_runtime_0_1b_int8/arktts_runtime/service.py b/onnx_runtime_0_1b_int8/arktts_runtime/service.py
index 21a095f..e4d5288 100644
--- a/onnx_runtime_0_1b_int8/arktts_runtime/service.py
+++ b/onnx_runtime_0_1b_int8/arktts_runtime/service.py
@@ -5,17 +5,21 @@ import gc
import io
import json
import os
+import resource
+import subprocess
import threading
+import time
from pathlib import Path
import numpy as np
import soundfile as sf
from fastapi import FastAPI, File, Form, HTTPException, UploadFile
-from fastapi.responses import Response, StreamingResponse
+from fastapi.responses import HTMLResponse, Response, StreamingResponse
from pydantic import BaseModel, Field
from .registration import MAX_AUDIO_BYTES, VoiceRegistration
from .runtime import ArkTtsRuntime
+from .web_ui import TEST_PAGE
ROOT = Path(__file__).resolve().parents[1]
MODEL_DIR = Path(os.getenv("ARKTTS_MODEL_DIR", str(ROOT / "model"))).expanduser()
@@ -32,12 +36,13 @@ runtime: ArkTtsRuntime | None = None
registration: VoiceRegistration | None = None
request_lock = threading.RLock()
active_stop: threading.Event | None = None
+service_started_at = time.monotonic()
class TtsRequest(BaseModel):
text: str = Field(..., min_length=1, max_length=1000)
voice_name: str = "default"
- max_new_tokens: int = Field(256, ge=1, le=2048)
+ max_new_tokens: int = Field(1024, ge=16, le=2048)
temperature: float = Field(0.7, gt=0.0, le=2.0)
top_p: float = Field(0.9, gt=0.0, le=1.0)
top_k: int = Field(50, ge=1, le=4096)
@@ -51,6 +56,11 @@ class OpenAiSpeechRequest(BaseModel):
response_format: str = "wav"
[email protected]("/", response_class=HTMLResponse)
+def test_page() -> str:
+ return TEST_PAGE
+
+
def _load_runtime() -> ArkTtsRuntime:
global runtime, registration
runtime = ArkTtsRuntime(MODEL_DIR, VOICES_DIR, PRECISION, CODEC_PRECISION, THREADS)
@@ -107,6 +117,47 @@ def registration_status() -> dict:
return registration.status()
+def _linux_memory_mb() -> tuple[float | None, float | None]:
+ try:
+ values: dict[str, float] = {}
+ for line in Path("/proc/self/status").read_text(encoding="utf-8").splitlines():
+ name, separator, value = line.partition(":")
+ if separator and name in {"VmRSS", "VmHWM"}:
+ values[name] = float(value.strip().split()[0]) / 1024
+ return values.get("VmRSS"), values.get("VmHWM")
+ except (OSError, ValueError, IndexError):
+ return None, None
+
+
+def _mac_current_memory_mb() -> float | None:
+ try:
+ result = subprocess.run(
+ ["/bin/ps", "-o", "rss=", "-p", str(os.getpid())],
+ capture_output=True,
+ check=True,
+ text=True,
+ timeout=2,
+ )
+ return float(result.stdout.strip()) / 1024
+ except (OSError, ValueError, subprocess.SubprocessError):
+ return None
+
+
[email protected]("/api/system")
+def system_stats() -> dict:
+ current_mb, peak_mb = _linux_memory_mb()
+ if current_mb is None and os.uname().sysname == "Darwin":
+ current_mb = _mac_current_memory_mb()
+ if peak_mb is None:
+ usage = float(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss)
+ peak_mb = usage / (1024 * 1024) if os.uname().sysname == "Darwin" else usage / 1024
+ return {
+ "memory": {"current_mb": current_mb, "peak_mb": peak_mb},
+ "uptime_seconds": time.monotonic() - service_started_at,
+ "refresh_interval_ms": 2000,
+ }
+
+
@app.post("/api/voices/register")
def register_voice(
audio: UploadFile = File(...),
@@ -236,10 +287,18 @@ def cancel() -> dict[str, bool]:
@app.post("/api/runtime/reload")
def reload_runtime() -> dict:
- global runtime, registration
+ global active_stop, runtime, registration
+ if active_stop is not None:
+ active_stop.set()
+ started = time.monotonic()
with request_lock:
runtime = None
registration = None
gc.collect()
obj = _load_runtime()
- return {"ok": True, "precision": obj.precision, "codec_precision": obj.codec_precision}
+ return {
+ "ok": True,
+ "precision": obj.precision,
+ "codec_precision": obj.codec_precision,
+ "reload_seconds": time.monotonic() - started,
+ }
diff --git a/onnx_runtime_0_1b_int8/arktts_runtime/web_ui.py b/onnx_runtime_0_1b_int8/arktts_runtime/web_ui.py
new file mode 100644
index 0000000..906b328
--- /dev/null
+++ b/onnx_runtime_0_1b_int8/arktts_runtime/web_ui.py
@@ -0,0 +1,262 @@
+from __future__ import annotations
+
+
+TEST_PAGE = """<!doctype html>
+<html lang="zh-CN">
+<head>
+ <meta charset="utf-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+ <title>Audio8 TTS 0.1B INT8 本地测试</title>
+ <style>
+ :root { color-scheme:light; --ink:#18201f; --muted:#65706e; --line:#d7dddb; --paper:#f5f7f6; --accent:#087f5b; --accent-dark:#066447; --danger:#b42318; }
+ * { box-sizing:border-box; }
+ body { margin:0; min-width:320px; background:var(--paper); color:var(--ink); font:15px/1.5 -apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif; letter-spacing:0; }
+ header { border-bottom:1px solid var(--line); background:#fff; }
+ .bar, main { width:min(880px, calc(100% - 32px)); margin:auto; }
+ .bar { min-height:64px; display:flex; align-items:center; justify-content:space-between; gap:16px; }
+ h1 { margin:0; font-size:20px; font-weight:680; }
+ .header-actions, .health { display:flex; align-items:center; gap:8px; }
+ .health { color:var(--muted); font-size:13px; }
+ .icon-button { width:36px; height:36px; padding:0; border:1px solid var(--line); border-radius:6px; background:#fff; color:var(--ink); font:22px/34px -apple-system,BlinkMacSystemFont,sans-serif; }
+ .icon-button:hover { border-color:#aeb8b5; background:#f7f9f8; }
+ .icon-button:disabled { opacity:.45; }
+ .dot { width:9px; height:9px; border-radius:50%; background:#a3aaa8; }
+ .dot.ok { background:#12a371; }
+ main { padding:32px 0 48px; }
+ .meta { display:flex; flex-wrap:wrap; gap:8px 18px; margin-bottom:18px; color:var(--muted); font-size:13px; }
+ .metrics { display:grid; grid-template-columns:repeat(3, 1fr); border-top:1px solid var(--line); border-bottom:1px solid var(--line); margin-bottom:24px; }
+ .metric { min-width:0; padding:15px 0; }
+ .metric + .metric { padding-left:18px; border-left:1px solid var(--line); }
+ .metric span { display:block; color:var(--muted); font-size:12px; }
+ .metric strong { display:block; margin-top:2px; font-size:19px; font-weight:680; font-variant-numeric:tabular-nums; }
+ .memory-track { grid-column:1 / -1; height:3px; margin-top:-3px; overflow:hidden; background:#dfe5e3; }
+ .memory-fill { width:0; height:100%; background:var(--accent); transition:width .35s ease; }
+ .tabs { display:flex; width:100%; margin-bottom:24px; border-bottom:1px solid var(--line); }
+ .tab { height:42px; padding:0 18px; border:0; border-bottom:2px solid transparent; border-radius:0; background:transparent; color:var(--muted); }
+ .tab:hover { background:transparent; color:var(--ink); }
+ .tab.active { border-bottom-color:var(--accent); color:var(--ink); }
+ .view[hidden] { display:none; }
+ label { display:block; margin:0 0 7px; font-size:13px; font-weight:650; }
+ textarea, select, input[type=number], input[type=text], input[type=file] { width:100%; border:1px solid #bfc8c5; border-radius:6px; background:#fff; color:var(--ink); font:inherit; outline:none; }
+ textarea:focus, select:focus, input:focus { border-color:var(--accent); box-shadow:0 0 0 3px rgba(8,127,91,.12); }
+ textarea { min-height:156px; resize:vertical; padding:13px 14px; }
+ select, input[type=number], input[type=text], input[type=file] { height:42px; padding:0 11px; }
+ input[type=file] { padding:8px; }
+ .row { display:grid; grid-template-columns:1.35fr repeat(3, 1fr); gap:14px; margin-top:18px; }
+ .actions { display:flex; align-items:center; gap:10px; margin-top:24px; }
+ button, .download { height:42px; border-radius:6px; padding:0 17px; font:650 14px/40px inherit; cursor:pointer; text-decoration:none; }
+ button { border:1px solid var(--accent); background:var(--accent); color:#fff; }
+ button:hover { background:var(--accent-dark); }
+ button:disabled { cursor:not-allowed; opacity:.55; }
+ .download { display:none; border:1px solid #bfc8c5; background:#fff; color:var(--ink); }
+ .status { min-height:22px; margin:18px 0 10px; color:var(--muted); font-size:13px; }
+ .status.error { color:var(--danger); }
+ audio { display:none; width:100%; margin-top:10px; }
+ audio.ready { display:block; }
+ .register-grid { display:grid; grid-template-columns:1fr 1fr; gap:14px; margin-bottom:18px; }
+ .register-text { min-height:112px; }
+ .check { display:flex; align-items:center; gap:8px; margin:14px 0 0; font-weight:500; }
+ .check input { width:16px; height:16px; accent-color:var(--accent); }
+ .audio-preview { display:none; margin:14px 0 0; }
+ .audio-preview.ready { display:block; }
+ @media (max-width:700px) { .row { grid-template-columns:1fr 1fr; } main { padding-top:24px; } }
+ @media (max-width:440px) { .row, .register-grid { grid-template-columns:1fr; } .metrics { grid-template-columns:1fr 1fr; } .metric:nth-child(3) { grid-column:1 / -1; padding-left:0; border-left:0; border-top:1px solid var(--line); } .actions { align-items:stretch; flex-direction:column; } button, .download { width:100%; text-align:center; } }
+ </style>
+</head>
+<body>
+ <header><div class="bar"><h1>Audio8 TTS 0.1B INT8</h1><div class="header-actions"><div class="health"><span id="dot" class="dot"></span><span id="health">正在加载模型</span></div><button id="reloadRuntime" class="icon-button" type="button" title="重新加载模型" aria-label="重新加载模型">&#8635;</button></div></div></header>
+ <main>
+ <div class="meta"><span id="model">模型:-</span><span id="precision">DualAR:-</span><span id="codecPrecision">Codec:-</span><span id="provider">执行器:-</span></div>
+ <section class="metrics" aria-label="运行状态">
+ <div class="metric"><span>当前内存</span><strong id="memoryCurrent">读取中</strong></div>
+ <div class="metric"><span>峰值内存</span><strong id="memoryPeak">读取中</strong></div>
+ <div class="metric"><span>运行时间</span><strong id="uptime">-</strong></div>
+ <div class="memory-track"><div id="memoryFill" class="memory-fill"></div></div>
+ </section>
+ <div class="tabs" role="tablist"><button id="ttsTab" class="tab active" role="tab">语音合成</button><button id="registerTab" class="tab" role="tab">音频注册</button></div>
+ <section id="ttsView" class="view">
+ <label for="text">合成文本</label>
+ <textarea id="text">你好,这是 Audio8 TTS 0.1B INT8 的本地语音合成测试。</textarea>
+ <div class="row">
+ <div><label for="voice">音色</label><select id="voice"></select></div>
+ <div><label for="temperature">Temperature</label><input id="temperature" type="number" value="0.7" min="0.05" max="2" step="0.05"></div>
+ <div><label for="topP">Top P</label><input id="topP" type="number" value="0.9" min="0.05" max="1" step="0.05"></div>
+ <div><label for="seed">Seed</label><input id="seed" type="number" value="42" min="0" step="1"></div>
+ </div>
+ <div class="actions"><button id="generate" disabled>生成语音</button><a id="download" class="download" download="audio8_0.1b_int8_output.wav">下载 WAV</a></div>
+ <div id="status" class="status"></div>
+ <audio id="audio" controls></audio>
+ </section>
+ <section id="registerView" class="view" hidden>
+ <div class="register-grid">
+ <div><label for="referenceAudio">参考音频</label><input id="referenceAudio" type="file" accept="audio/*,.wav,.flac,.mp3,.m4a"></div>
+ <div><label for="voiceName">音色名称</label><input id="voiceName" type="text" maxlength="64" placeholder="speaker_a"></div>
+ </div>
+ <label for="referenceText">参考音频原文</label>
+ <textarea id="referenceText" class="register-text"></textarea>
+ <audio id="referencePreview" class="audio-preview" controls></audio>
+ <label class="check"><input id="overwriteVoice" type="checkbox">覆盖同名音色</label>
+ <div class="actions"><button id="registerVoice" disabled>注册音色</button></div>
+ <div id="registerStatus" class="status"></div>
+ </section>
+ </main>
+ <script>
+ const $ = id => document.getElementById(id);
+ let audioUrl = null;
+ let referenceUrl = null;
+ let registrationAvailable = false;
+ function selectView(name) {
+ const register = name === 'register';
+ $('ttsView').hidden = register;
+ $('registerView').hidden = !register;
+ $('ttsTab').classList.toggle('active', !register);
+ $('registerTab').classList.toggle('active', register);
+ }
+ async function errorMessage(response) {
+ const text = await response.text();
+ try { return JSON.parse(text).detail || text; } catch (_) { return text || `HTTP ${response.status}`; }
+ }
+ async function refreshVoices(selected) {
+ const response = await fetch('/api/voices');
+ if (!response.ok) throw new Error('读取音色失败');
+ const data = await response.json();
+ const names = data.voices.map(voice => typeof voice === 'string' ? voice : voice.name);
+ $('voice').replaceChildren(...names.map(name => new Option(name, name)));
+ if (selected && names.includes(selected)) $('voice').value = selected;
+ $('generate').disabled = names.length === 0;
+ return names;
+ }
+ function formatUptime(seconds) {
+ const value = Math.max(0, Math.floor(seconds));
+ const hours = Math.floor(value / 3600);
+ const minutes = Math.floor((value % 3600) / 60);
+ const secs = value % 60;
+ return hours ? `${hours}时 ${minutes}分 ${secs}秒` : `${minutes}分 ${secs}秒`;
+ }
+ async function refreshMetrics() {
+ try {
+ const response = await fetch('/api/system', {cache:'no-store'});
+ if (!response.ok) throw new Error(`HTTP ${response.status}`);
+ const data = await response.json();
+ $('memoryCurrent').textContent = data.memory.current_mb == null ? '不可用' : `${(data.memory.current_mb / 1024).toFixed(2)} GB`;
+ $('memoryPeak').textContent = data.memory.peak_mb == null ? '不可用' : `${(data.memory.peak_mb / 1024).toFixed(2)} GB`;
+ $('uptime').textContent = formatUptime(data.uptime_seconds);
+ const scale = Math.max(4096, data.memory.peak_mb || 0);
+ $('memoryFill').style.width = `${Math.min(100, ((data.memory.current_mb || 0) / scale) * 100)}%`;
+ } catch (_) {
+ $('memoryCurrent').textContent = '读取失败';
+ }
+ }
+ async function initialize() {
+ try {
+ const [health, registrationStatus] = await Promise.all([fetch('/api/health'), fetch('/api/registration/status')]);
+ if (!health.ok || !registrationStatus.ok) throw new Error('服务初始化失败');
+ const healthData = await health.json();
+ const registrationData = await registrationStatus.json();
+ $('model').textContent = `模型:${healthData.model || 'Audio8 0.1B'}`;
+ $('precision').textContent = `DualAR:${healthData.precision.toUpperCase()}`;
+ $('codecPrecision').textContent = `Codec:${healthData.codec_precision.toUpperCase()}`;
+ $('provider').textContent = `执行器:${healthData.providers.slow[0]}`;
+ const voiceNames = await refreshVoices();
+ registrationAvailable = registrationData.available;
+ $('registerVoice').disabled = !registrationAvailable;
+ $('registerStatus').textContent = registrationAvailable ? '' : registrationData.reason;
+ $('dot').classList.add('ok');
+ $('health').textContent = '模型已就绪';
+ $('generate').disabled = voiceNames.length === 0;
+ $('status').textContent = voiceNames.length ? '' : '没有可用音色';
+ if (voiceNames.length === 0 && registrationAvailable) selectView('register');
+ } catch (error) {
+ $('health').textContent = '服务不可用';
+ $('status').textContent = error.message;
+ $('status').classList.add('error');
+ }
+ }
+ $('generate').addEventListener('click', async () => {
+ const text = $('text').value.trim();
+ if (!text) { $('status').textContent = '请输入合成文本'; return; }
+ $('generate').disabled = true;
+ $('status').classList.remove('error');
+ $('status').textContent = '正在生成语音...';
+ $('audio').classList.remove('ready');
+ $('download').style.display = 'none';
+ const started = performance.now();
+ try {
+ const response = await fetch('/api/tts', {
+ method:'POST', headers:{'Content-Type':'application/json'},
+ body:JSON.stringify({text, voice_name:$('voice').value, max_new_tokens:1024, temperature:Number($('temperature').value), top_p:Number($('topP').value), seed:Number($('seed').value)})
+ });
+ if (!response.ok) throw new Error(await errorMessage(response));
+ const blob = await response.blob();
+ if (audioUrl) URL.revokeObjectURL(audioUrl);
+ audioUrl = URL.createObjectURL(blob);
+ $('audio').src = audioUrl;
+ $('audio').classList.add('ready');
+ $('download').href = audioUrl;
+ $('download').style.display = 'inline-block';
+ $('status').textContent = `生成完成,用时 ${((performance.now()-started)/1000).toFixed(2)} 秒`;
+ await $('audio').play().catch(() => {});
+ } catch (error) {
+ $('status').textContent = `生成失败:${error.message}`;
+ $('status').classList.add('error');
+ } finally { $('generate').disabled = false; }
+ });
+ $('ttsTab').addEventListener('click', () => selectView('tts'));
+ $('registerTab').addEventListener('click', () => selectView('register'));
+ $('referenceAudio').addEventListener('change', () => {
+ const file = $('referenceAudio').files[0];
+ if (referenceUrl) URL.revokeObjectURL(referenceUrl);
+ if (!file) { $('referencePreview').classList.remove('ready'); return; }
+ referenceUrl = URL.createObjectURL(file);
+ $('referencePreview').src = referenceUrl;
+ $('referencePreview').classList.add('ready');
+ });
+ $('registerVoice').addEventListener('click', async () => {
+ const file = $('referenceAudio').files[0];
+ const name = $('voiceName').value.trim();
+ const text = $('referenceText').value.trim();
+ if (!file || !name || !text) { $('registerStatus').textContent = '请选择音频并填写音色名称和准确原文'; return; }
+ $('registerVoice').disabled = true;
+ $('registerStatus').classList.remove('error');
+ $('registerStatus').textContent = '正在注册音色...';
+ const form = new FormData();
+ form.append('audio', file);
+ form.append('name', name);
+ form.append('text', text);
+ form.append('overwrite', $('overwriteVoice').checked ? 'true' : 'false');
+ try {
+ const response = await fetch('/api/voices/register', {method:'POST', body:form});
+ if (!response.ok) throw new Error(await errorMessage(response));
+ const result = await response.json();
+ await refreshVoices(result.voice.name);
+ $('registerStatus').textContent = `注册完成:${result.voice.name}(${result.voice.shape[1]} 帧)`;
+ } catch (error) {
+ $('registerStatus').textContent = `注册失败:${error.message}`;
+ $('registerStatus').classList.add('error');
+ } finally { $('registerVoice').disabled = !registrationAvailable; }
+ });
+ $('reloadRuntime').addEventListener('click', async () => {
+ const button = $('reloadRuntime');
+ button.disabled = true;
+ $('dot').classList.remove('ok');
+ $('health').textContent = '正在重新加载模型';
+ try {
+ const response = await fetch('/api/runtime/reload', {method:'POST'});
+ const result = await response.json();
+ if (!response.ok) throw new Error(result.detail || `HTTP ${response.status}`);
+ $('precision').textContent = `DualAR:${result.precision.toUpperCase()}`;
+ $('codecPrecision').textContent = `Codec:${result.codec_precision.toUpperCase()}`;
+ await refreshVoices();
+ await refreshMetrics();
+ $('dot').classList.add('ok');
+ $('health').textContent = `模型已重新加载(${result.reload_seconds.toFixed(2)} 秒)`;
+ } catch (error) {
+ $('health').textContent = `重新加载失败:${error.message}`;
+ } finally { button.disabled = false; }
+ });
+ initialize();
+ refreshMetrics();
+ setInterval(refreshMetrics, 2000);
+ </script>
+</body>
+</html>"""
diff --git a/onnx_runtime_0_1b_int8/tests/test_sampling.py b/onnx_runtime_0_1b_int8/tests/test_sampling.py
new file mode 100644
index 0000000..5d5c3e1
--- /dev/null
+++ b/onnx_runtime_0_1b_int8/tests/test_sampling.py
@@ -0,0 +1,18 @@
+from __future__ import annotations
+
+import numpy as np
+
+from arktts_runtime.runtime import _sample
+
+
+def test_sample_uses_gumbel_max_noise() -> None:
+ # This seed distinguishes Gumbel-max from the tempting but incorrect -log(U)
+ # shortcut, which can bias sampling toward EOS and truncate speech.
+ result = _sample(
+ np.asarray([0.0, 1.0], dtype=np.float32),
+ temperature=1.0,
+ top_p=1.0,
+ top_k=2,
+ rng=np.random.default_rng(0),
+ )
+ assert result == 0
diff --git a/onnx_runtime_0_1b_int8/tests/test_service_ui.py b/onnx_runtime_0_1b_int8/tests/test_service_ui.py
new file mode 100644
index 0000000..607cbc8
--- /dev/null
+++ b/onnx_runtime_0_1b_int8/tests/test_service_ui.py
@@ -0,0 +1,20 @@
+from __future__ import annotations
+
+from arktts_runtime.web_ui import TEST_PAGE
+
+
+def test_local_ui_has_complete_tts_controls() -> None:
+ assert '<textarea id="text">' in TEST_PAGE
+ assert 'id="voice"' in TEST_PAGE
+ assert 'id="generate"' in TEST_PAGE
+ assert 'id="audio" controls' in TEST_PAGE
+ assert 'id="download"' in TEST_PAGE
+ assert "fetch('/api/tts'" in TEST_PAGE
+
+
+def test_local_ui_has_registration_and_runtime_controls() -> None:
+ assert 'id="registerView"' in TEST_PAGE
+ assert 'id="referenceAudio"' in TEST_PAGE
+ assert "fetch('/api/voices/register'" in TEST_PAGE
+ assert "fetch('/api/system'" in TEST_PAGE
+ assert "fetch('/api/runtime/reload'" in TEST_PAGE