tiny-voca-hindi / web /index.html
RL10x's picture
Add inference code (Python + C++ engine + web page)
ab1b068 verified
Raw History Blame Contribute Delete
10.7 kB
<!doctype html>
<html lang="hi">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hindi TTS</title>
<style>
:root {
--bg: #f6f5f2; --card: #ffffff; --text: #1d1d1f; --muted: #6b6b70; --line: #e3e1dc;
--accent: #c2410c; --accent-soft: #fdeee6; --accent-text: #ffffff;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #141416; --card: #1e1e21; --text: #ececee; --muted: #9a9aa2; --line: #303035;
--accent: #fb923c; --accent-soft: #3a2417; --accent-text: #1a0f08;
}
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text);
font: 16px/1.5 system-ui, -apple-system, "Segoe UI", "Nirmala UI", sans-serif; }
main { max-width: 760px; margin: 0 auto; padding: 32px 16px 48px; }
h1 { font-size: 26px; margin: 0 0 4px; letter-spacing: -0.01em; }
.sub { color: var(--muted); margin: 0 0 24px; font-size: 14px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 18px; }
textarea { width: 100%; min-height: 140px; resize: vertical; border: 1px solid var(--line); border-radius: 10px;
padding: 12px 14px; font: 19px/1.6 "Nirmala UI", "Noto Sans Devanagari", system-ui, sans-serif;
background: var(--bg); color: var(--text); }
textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.examples { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 0; }
.examples button { border: 1px solid var(--line); background: transparent; color: var(--muted); border-radius: 999px;
padding: 3px 10px; font-size: 13px; cursor: pointer; }
.examples button:hover { color: var(--text); border-color: var(--muted); }
.label { font-size: 13px; color: var(--muted); margin: 18px 0 8px; font-weight: 600; letter-spacing: 0.02em; }
.styles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.styles button { border: 1px solid var(--line); background: var(--bg); color: var(--text); border-radius: 10px;
padding: 8px 6px; cursor: pointer; font-size: 14px; line-height: 1.25; }
.styles button small { display: block; color: var(--muted); font-size: 12px; }
.styles button.on { background: var(--accent-soft); border-color: var(--accent); }
.row { display: flex; gap: 24px; flex-wrap: wrap; align-items: center; }
.row > div { flex: 1; min-width: 220px; }
input[type=range] { width: 100%; accent-color: var(--accent); }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; background: transparent; color: var(--text); padding: 7px 14px; cursor: pointer; font-size: 14px; }
.seg button.on { background: var(--accent-soft); color: var(--text); box-shadow: inset 0 0 0 1px var(--accent); }
.go { margin-top: 20px; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.speak { background: var(--accent); color: var(--accent-text); border: 0; border-radius: 10px; padding: 11px 26px;
font-size: 16px; font-weight: 600; cursor: pointer; }
.speak:disabled { opacity: 0.55; cursor: progress; }
.hint { color: var(--muted); font-size: 13px; }
.out { margin-top: 18px; display: none; }
audio { width: 100%; }
.meta { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; font-size: 13px; color: var(--muted); margin-top: 6px; }
.meta a { color: var(--accent); }
.spoken { margin-top: 8px; font-size: 13px; color: var(--muted); }
.error { color: #dc2626; font-size: 14px; margin-top: 10px; }
.engine { color: var(--muted); font-size: 12px; text-align: center; margin-top: 14px; }
@media (max-width: 520px) { .styles { grid-template-columns: repeat(2, 1fr); } }
</style>
</head>
<body>
<main>
<h1>हिंदी आवाज़ · Hindi TTS</h1>
<p class="sub">Tiny phoneme TTS, Rasa female voice, runs on your CPU. Numbers, dates and money are read out in words.</p>
<div class="card">
<textarea id="text" spellcheck="false">नमस्ते! आज मौसम बहुत अच्छा है, चलो कहीं घूमने चलते हैं।</textarea>
<div class="examples" id="examples"></div>
<div class="label">Style</div>
<div class="styles" id="styles"></div>
<div class="row">
<div>
<div class="label">Speed <span id="speedv">1.00×</span></div>
<input id="speed" type="range" min="0.7" max="1.4" step="0.05" value="1">
</div>
<div>
<div class="label">Model</div>
<div class="seg" id="model">
<button data-m="int8" class="on">8-bit <small id="s_int8"></small></button>
<button data-m="float">Full <small id="s_float"></small></button>
</div>
</div>
</div>
<div class="go">
<button class="speak" id="go">बोलो · Speak</button>
<span class="hint">Ctrl + Enter</span>
</div>
<div class="error" id="err"></div>
<div class="out" id="out">
<audio id="audio" controls></audio>
<div class="meta"><span id="stats"></span><a id="dl" download="hindi_tts.wav">Download WAV</a></div>
<div class="spoken" id="spoken"></div>
</div>
</div>
<p class="engine" id="engine"></p>
</main>
<script src="/hindi_norm.js"></script>
<script>
const STYLE_NAMES = {neutral: ["सामान्य", "Neutral"], conv: ["बातचीत", "Chat"], happy: ["खुश", "Happy"], sad: ["उदास", "Sad"],
anger: ["गुस्सा", "Angry"], fear: ["डर", "Fear"], surprise: ["हैरानी", "Surprise"], disgust: ["घिन", "Disgust"]};
const EXAMPLES = [
["समाचार", "भारत एक विशाल देश है, जहाँ अनेक भाषाएँ बोली जाती हैं और हर क्षेत्र की अपनी अलग संस्कृति है।", "neutral"],
["बातचीत", "अरे यार, तुम कल आए क्यों नहीं? मैं पूरे दिन तुम्हारा इंतज़ार करती रही।", "conv"],
["खुशी", "वाह! आज तो कमाल हो गया, मुझे नौकरी मिल गई है, मैं बहुत खुश हूँ!", "happy"],
["नंबर", "मेरी फ़्लाइट 15/08/2025 को सुबह 7:30 बजे है और टिकट ₹4,999 का था।", "neutral"],
["उदासी", "उसके जाने के बाद यह घर बहुत सूना लगता है, कुछ भी पहले जैसा नहीं रहा।", "sad"],
];
let style = "neutral", model = "int8", lastUrl = null, normalizeInBrowser = false;
const $ = id => document.getElementById(id);
function setStyle(s) {
style = s;
document.querySelectorAll("#styles button").forEach(b => b.classList.toggle("on", b.dataset.s === s));
}
async function init() {
let info = {styles: Object.keys(STYLE_NAMES), models: ["int8", "float"], sizes_mb: {}};
try { info = await (await fetch("/info")).json(); } catch (e) {}
for (const s of info.styles) {
const b = document.createElement("button"); b.dataset.s = s;
const [hi, en] = STYLE_NAMES[s] || [s, ""];
b.innerHTML = `${hi}<small>${en}</small>`; b.onclick = () => setStyle(s); $("styles").appendChild(b);
}
setStyle("neutral");
for (const [name, text, st] of EXAMPLES) {
const b = document.createElement("button"); b.textContent = name;
b.onclick = () => { $("text").value = text; setStyle(st); }; $("examples").appendChild(b);
}
normalizeInBrowser = !!info.normalize_in_browser && typeof window.normalizeHi === "function";
$("engine").textContent = info.engine === "cpp"
? `Engine: C++ / ggml (no Python) · RAM ${info.ram_mb} MB`
: "Engine: Python / PyTorch";
if (info.models.length < 2) document.querySelector('[data-m="float"]').style.display = "none";
if (info.sizes_mb.int8) $("s_int8").textContent = `${info.sizes_mb.int8} MB`;
if (info.sizes_mb.float) $("s_float").textContent = `${info.sizes_mb.float} MB`;
if (!info.models.includes("int8")) { model = "float"; document.querySelector('[data-m="int8"]').disabled = true; }
document.querySelectorAll("#model button").forEach(b => {
b.classList.toggle("on", b.dataset.m === model);
b.onclick = () => { model = b.dataset.m; document.querySelectorAll("#model button").forEach(x => x.classList.toggle("on", x === b)); };
});
}
$("speed").oninput = e => $("speedv").textContent = Number(e.target.value).toFixed(2) + "×";
function hexToText(hex) {
const bytes = new Uint8Array(hex.match(/.{2}/g).map(h => parseInt(h, 16)));
return new TextDecoder().decode(bytes);
}
async function speak() {
const text = $("text").value.trim();
if (!text) return;
$("go").disabled = true; $("err").textContent = ""; $("go").textContent = "बन रहा है…";
try {
// C++ server: numbers, dates, money and English words become Devanagari words here (hindi_norm.js)
const send = normalizeInBrowser ? normalizeHi(text, {english: true}).replace(/।/g, ".") : text;
const r = await fetch("/speak", {method: "POST", headers: {"Content-Type": "application/json"},
body: JSON.stringify({text: send, style, speed: Number($("speed").value), model})});
if (!r.ok) { const j = await r.json().catch(() => ({})); throw new Error(j.detail || r.statusText); }
const blob = await r.blob();
if (lastUrl) URL.revokeObjectURL(lastUrl);
lastUrl = URL.createObjectURL(blob);
$("audio").src = lastUrl; $("dl").href = lastUrl; $("out").style.display = "block";
$("audio").play().catch(() => {});
const secs = Number(r.headers.get("X-Audio-Seconds")), comp = Number(r.headers.get("X-Compute-Seconds"));
$("stats").textContent = `${secs.toFixed(1)} s of audio in ${comp.toFixed(2)} s (${(secs / Math.max(comp, 0.001)).toFixed(0)}× real time) · ${model === "int8" ? "8-bit" : "full"} · ${(STYLE_NAMES[style] || [style])[0]}`;
const spoken = hexToText(r.headers.get("X-Spoken-As") || "");
$("spoken").textContent = spoken && spoken !== text.replace(/।/g, ".") ? "Read as: " + spoken : "";
} catch (e) {
$("err").textContent = "Could not speak this: " + e.message;
} finally {
$("go").disabled = false; $("go").textContent = "बोलो · Speak";
}
}
$("go").onclick = speak;
$("text").addEventListener("keydown", e => { if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) speak(); });
init();
</script>
</body>
</html>