Spaces:
Running
Running
Download static/js/model.js from alphappp/PromptDialog-demo: direct link, hf CLI and curl.
- Browser
- Download file 11.7 kB
-
https://huggingface.co/spaces/alphappp/PromptDialog-demo/resolve/main/static/js/model.js
- Command line
-
hf download hf://spaces/alphappp/PromptDialog-demo/static/js/model.js
-
curl -L -o model.js https://huggingface.co/spaces/alphappp/PromptDialog-demo/resolve/main/static/js/model.js
11.7 kB
| import { fetchText, createAudio, escapeHtml } from "./audio.js"; | |
| const mixedStyleVoices = { | |
| CosyVoice2: "Seed-TTS-Eval voice", | |
| CosyVoice3: "Seed-TTS-Eval voice", | |
| Ours: "Seed-TTS-Eval voice", | |
| "MiMo-V2.5": "mimo voice", | |
| Qwen3: "qwen voice" | |
| }; | |
| const emotionVoices = { | |
| CosyVoice3: "Seed-TTS-Eval voice", | |
| IndexTTS2: "Seed-TTS-Eval voice", | |
| Ours: "Seed-TTS-Eval voice", | |
| Qwen3: "built-in voice", | |
| "MiMo-V2.5": "built-in voice" | |
| }; | |
| const TASKS = [ | |
| { id: "mixed-style", root: "./static/task/mixed-style", voices: mixedStyleVoices }, | |
| { id: "open-ended-english", root: "./static/task/open-ended-english", voices: mixedStyleVoices }, | |
| { id: "paral", root: "./static/task/paral" }, | |
| { id: "punct", root: "./static/task/punct" }, | |
| { id: "emo", root: "./static/task/emo", voices: emotionVoices }, | |
| { id: "multi", root: "./static/task/multi" }, | |
| { id: "open-ended", root: "./static/task/open-ended" }, | |
| { id: "open-ended-seed", root: "./static/task/open-ended-seed" } | |
| ]; | |
| function parseTaskYaml(source) { | |
| const task = { name: "", description: "", show_reference: true, models: [] }; | |
| let listKey = ""; | |
| source.split(/\r?\n/).forEach((rawLine) => { | |
| const line = rawLine.trim(); | |
| if (!line || line.startsWith("#")) return; | |
| if (line.startsWith("- ") && listKey) { | |
| task[listKey].push(line.slice(2).trim().replace(/^["']|["']$/g, "")); | |
| return; | |
| } | |
| const match = line.match(/^([A-Za-z0-9_-]+):\s*(.*)$/); | |
| if (!match) return; | |
| const key = match[1]; | |
| const value = match[2].trim().replace(/^["']|["']$/g, ""); | |
| if (value === "true" || value === "false") { | |
| task[key] = value === "true"; | |
| listKey = ""; | |
| } else if (Array.isArray(task[key])) { | |
| listKey = key; | |
| } else { | |
| task[key] = value; | |
| listKey = ""; | |
| } | |
| }); | |
| return task; | |
| } | |
| function parseJsonl(source) { | |
| return source | |
| .split(/\r?\n/) | |
| .map((line) => line.trim()) | |
| .filter(Boolean) | |
| .map((line) => JSON.parse(line)); | |
| } | |
| function formatScriptText(text, highlightParalinguistic) { | |
| const escapedText = escapeHtml(text); | |
| if (!highlightParalinguistic) { | |
| return escapedText; | |
| } | |
| return escapedText.replace(/[唉嗯哈呵嘿哼呼咳啧]/g, '<em class="paralinguistic-cue">$&</em>'); | |
| } | |
| function createScriptCell(item, highlightParalinguistic = false) { | |
| const wrapper = document.createElement("div"); | |
| wrapper.className = "script-cell"; | |
| if (Array.isArray(item.variants)) { | |
| const target = document.createElement("p"); | |
| target.innerHTML = formatScriptText(item.target_text, highlightParalinguistic); | |
| wrapper.appendChild(target); | |
| const hasMultipleVariants = item.variants.length > 1; | |
| item.variants.forEach((variant, index) => { | |
| const context = document.createElement("p"); | |
| context.className = "variant-context"; | |
| if (hasMultipleVariants) { | |
| context.innerHTML = `<strong>instruction${index + 1}</strong> <span class="context-cue">${escapeHtml(variant.context)}</span>`; | |
| } else { | |
| context.innerHTML = `<span class="context-cue">${escapeHtml(variant.context)}</span>`; | |
| } | |
| wrapper.appendChild(context); | |
| }); | |
| return wrapper; | |
| } | |
| const texts = Array.isArray(item.target_text) ? item.target_text : [item.target_text]; | |
| const speakers = Array.isArray(item.speaker) ? item.speaker : []; | |
| const contexts = Array.isArray(item.context) ? item.context : [item.context]; | |
| texts.forEach((text, index) => { | |
| const turn = document.createElement("p"); | |
| const speaker = speakers[index]; | |
| const context = contexts[index]; | |
| const cleanContext = context ? String(context).replace(/[,,。.!!??;;::、\s]+$/u, "") : ""; | |
| const contextBadge = cleanContext ? ` <span class="context-cue">${escapeHtml(cleanContext)}</span>` : ""; | |
| const formattedText = formatScriptText(text, highlightParalinguistic); | |
| if (speaker !== undefined && speaker !== "") { | |
| turn.innerHTML = `<strong>[S${Number(speaker) + 1}]</strong> ${formattedText}${contextBadge}`; | |
| } else { | |
| turn.innerHTML = `${formattedText}${contextBadge}`; | |
| } | |
| wrapper.appendChild(turn); | |
| }); | |
| return wrapper; | |
| } | |
| function createReferenceCell(item, modelRoot) { | |
| const wrapper = document.createElement("div"); | |
| wrapper.className = "reference-cell"; | |
| const prompts = Array.isArray(item.prompt_audio) ? item.prompt_audio : []; | |
| prompts.forEach((audioPath, index) => { | |
| const block = document.createElement("div"); | |
| if (prompts.length > 1) { | |
| const label = document.createElement("span"); | |
| label.textContent = `Speaker ${index + 1}`; | |
| block.appendChild(label); | |
| } | |
| block.appendChild(createAudio(`${modelRoot}/${audioPath}`)); | |
| wrapper.appendChild(block); | |
| }); | |
| return wrapper; | |
| } | |
| function createAudioCard(label, content) { | |
| const card = document.createElement("div"); | |
| card.className = "audio-card" + (label.startsWith("Ours") ? " is-ours" : "") + (label === "Reference" ? " is-reference" : ""); | |
| const title = document.createElement("div"); | |
| title.className = "audio-card-title"; | |
| title.textContent = label.replace(/^Ours\b\s*/, (prefix) => | |
| prefix === label ? "PromptTTSD (Ours)" : "PromptTTSD (Ours) " | |
| ); | |
| const body = document.createElement("div"); | |
| body.className = "audio-card-body"; | |
| if (content) { | |
| body.appendChild(content); | |
| } | |
| card.appendChild(title); | |
| card.appendChild(body); | |
| return card; | |
| } | |
| function createVariantAudioCell(modelItem, taskRoot, model) { | |
| const wrapper = document.createElement("div"); | |
| wrapper.className = "variant-audio-list"; | |
| if (!modelItem || !Array.isArray(modelItem.variants)) { | |
| return wrapper; | |
| } | |
| const hasMultipleVariants = modelItem.variants.length > 1; | |
| modelItem.variants.forEach((variant, index) => { | |
| const row = document.createElement("div"); | |
| row.className = "variant-audio-row"; | |
| if (hasMultipleVariants) { | |
| const label = document.createElement("span"); | |
| label.textContent = `instruction${index + 1}`; | |
| row.appendChild(label); | |
| } | |
| row.appendChild(createAudio(`${taskRoot}/${model}/${variant.output_audio}`)); | |
| wrapper.appendChild(row); | |
| }); | |
| return wrapper; | |
| } | |
| function renderTable(task, manifests, taskRoot, voices) { | |
| const wrapper = document.createElement("div"); | |
| wrapper.className = "demo-cases"; | |
| const highlightParalinguistic = task.name === "Paralinguistic Cue Generation"; | |
| const caseItems = []; | |
| const seenCases = new Set(); | |
| task.models.forEach((model) => { | |
| manifests[model].forEach((item) => { | |
| if (seenCases.has(item.utt)) { | |
| return; | |
| } | |
| seenCases.add(item.utt); | |
| caseItems.push(item); | |
| }); | |
| }); | |
| caseItems.forEach((item, index) => { | |
| const caseBlock = document.createElement("article"); | |
| caseBlock.className = "case-block"; | |
| const scriptBlock = document.createElement("div"); | |
| scriptBlock.className = "case-script"; | |
| const scriptTitle = document.createElement("div"); | |
| scriptTitle.className = "case-section-title"; | |
| scriptTitle.textContent = `EXAMPLE ${String(index + 1).padStart(2, "0")} / SCRIPT & STYLE`; | |
| scriptBlock.appendChild(scriptTitle); | |
| scriptBlock.appendChild(createScriptCell(item, highlightParalinguistic)); | |
| caseBlock.appendChild(scriptBlock); | |
| const audioGrid = document.createElement("div"); | |
| audioGrid.className = "audio-grid"; | |
| if (task.show_reference) { | |
| const modelRoot = `${taskRoot}/${task.models[0]}`; | |
| audioGrid.appendChild(createAudioCard("Reference", createReferenceCell(item, modelRoot))); | |
| } | |
| task.models.forEach((model) => { | |
| const modelItem = manifests[model].find((entry) => entry.utt === item.utt); | |
| const content = document.createElement("div"); | |
| content.className = "audio-card-content"; | |
| if (modelItem && Array.isArray(modelItem.variants)) { | |
| content.appendChild(createVariantAudioCell(modelItem, taskRoot, model)); | |
| } else if (modelItem) { | |
| content.appendChild(createAudio(`${taskRoot}/${model}/${modelItem.output_audio}`)); | |
| } | |
| const label = voices?.[model] ? `${model} (${voices[model]})` : model; | |
| audioGrid.appendChild(createAudioCard(label, content)); | |
| }); | |
| caseBlock.appendChild(audioGrid); | |
| wrapper.appendChild(caseBlock); | |
| }); | |
| return wrapper; | |
| } | |
| const TASK_LABELS = { | |
| "mixed-style": "Mixed Style Control", | |
| "open-ended-english": "English Style Control", | |
| paral: "Paralinguistic cues", | |
| punct: "Punctuation", | |
| emo: "Emotion", | |
| multi: "Multi-speaker dialogue", | |
| "open-ended": "Open-ended · ElevenLabs", | |
| "open-ended-seed": "Open-ended · Seed-TTS-Eval" | |
| }; | |
| function selectTask(id) { | |
| if (!TASKS.some((task) => task.id === id)) return; | |
| document.querySelectorAll(".task-directory-link").forEach((link) => { | |
| const active = link.hash === `#task-${id}`; | |
| link.classList.toggle("is-active", active); | |
| if (active) link.setAttribute("aria-current", "true"); | |
| else link.removeAttribute("aria-current"); | |
| }); | |
| document.querySelectorAll(".task-card[id]").forEach((card) => { | |
| card.hidden = card.id !== `task-${id}`; | |
| if (card.hidden) card.querySelectorAll("audio").forEach((audio) => audio.pause()); | |
| }); | |
| } | |
| function renderDirectory(tasks) { | |
| const directory = document.getElementById("task-directory"); | |
| const links = tasks.map(({ id }, index) => { | |
| const link = document.createElement("a"); | |
| link.className = "task-directory-link"; | |
| link.href = `#task-${id}`; | |
| link.innerHTML = `<span class="task-number">${String(index + 1).padStart(2, "0")}</span>${TASK_LABELS[id]}`; | |
| link.addEventListener("click", (event) => { | |
| event.preventDefault(); | |
| history.pushState(null, "", link.hash); | |
| selectTask(id); | |
| }); | |
| return link; | |
| }); | |
| directory.replaceChildren(...links); | |
| } | |
| async function loadTask(config) { | |
| const task = parseTaskYaml(await fetchText(`${config.root}/task.yaml`)); | |
| const manifests = {}; | |
| await Promise.all(task.models.map(async (model) => { | |
| manifests[model] = parseJsonl(await fetchText(`${config.root}/${model}/manifest.jsonl`)); | |
| })); | |
| return { ...config, task, manifests }; | |
| } | |
| function renderTaskCard({ id, root, task, manifests, voices }) { | |
| const card = document.createElement("section"); | |
| card.className = "task-card"; | |
| card.id = `task-${id}`; | |
| const heading = document.createElement("div"); | |
| heading.className = "task-card-heading"; | |
| const title = document.createElement("h3"); | |
| title.textContent = task.name; | |
| const description = document.createElement("p"); | |
| description.textContent = task.description; | |
| const count = new Set(task.models.flatMap((model) => manifests[model].map((item) => item.utt))).size; | |
| const meta = document.createElement("span"); | |
| meta.className = "task-meta"; | |
| meta.textContent = `${count} examples · ${task.models.length} models`; | |
| heading.appendChild(meta); | |
| heading.appendChild(title); | |
| heading.appendChild(description); | |
| card.appendChild(heading); | |
| card.appendChild(renderTable(task, manifests, root, voices)); | |
| return card; | |
| } | |
| async function initDemo() { | |
| const tasks = await Promise.all(TASKS.map(loadTask)); | |
| renderDirectory(tasks); | |
| const list = document.getElementById("task-list"); | |
| list.replaceChildren(...tasks.map(renderTaskCard)); | |
| const syncTask = () => { | |
| const id = location.hash === "#task-mixed-style-en" ? "open-ended-english" : location.hash.replace("#task-", ""); | |
| if (TASKS.some((task) => task.id === id)) selectTask(id); | |
| }; | |
| selectTask(TASKS[0].id); | |
| syncTask(); | |
| window.addEventListener("hashchange", syncTask); | |
| } | |
| initDemo().catch((error) => { | |
| document.getElementById("task-directory").textContent = "Failed to load task metadata."; | |
| document.getElementById("task-list").textContent = error.message; | |
| }); | |