File size: 11,696 Bytes
cfaa94b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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;
});