zziang commited on
Commit
218f54e
·
verified ·
1 Parent(s): 4300d6b

whisper lines capped at 18 chars; retake short takes

Browse files
Files changed (1) hide show
  1. kaggle/radio_demo.py +30 -15
kaggle/radio_demo.py CHANGED
@@ -205,8 +205,12 @@ def main() -> None:
205
  for line in lines:
206
  v = voices[line["slot"]]
207
  wav_path = str(BUNDLE / v["wav"])
208
- t0 = time.time()
209
- try:
 
 
 
 
210
  if use_student:
211
  latent, spk = tts.prepare_prompt(wav_path, use_spk_emb=True)
212
  latent = latent.to(device=device, dtype=dtype)
@@ -215,22 +219,33 @@ def main() -> None:
215
  # teacher again to measure length gives a different take -
216
  # 25 frames vs 16 on a whisper line - and a wrong budget
217
  # makes the student drift into a loop or into silence.
218
- frames = (frames_by_index or {}).get(line["index"])
219
- if not frames:
220
- log(f"{tag} line {line['index']} skipped: no teacher frames")
221
- continue
222
- torch.manual_seed(SEED + line["index"])
223
- y = _ar(mm, sampler, line["text"], task_prompt, latent,
224
- v["prompt_text"], spk, frames)
225
- else:
226
- y = tts.synthesize(task="TTS", text=line["text"], language_id="Chinese",
227
- prompt_wav_path=wav_path, prompt_text=v["prompt_text"],
228
- use_spk_emb=True, raw_text=True,
229
- max_decode_steps=max(32, min(400, int(len(line["text"]) * 3.5))))
230
- y = y.cpu().numpy().reshape(-1)
231
  except Exception:
232
  log(f"{tag} line {line['index']} failed\n{traceback.format_exc()}")
233
  continue
 
 
 
 
 
 
 
 
 
 
 
 
234
  wall = time.time() - t0
235
  p = OUT / "lines" / f"{tag}_{line['index']:02d}.wav"
236
  sf.write(str(p), y, SR)
 
205
  for line in lines:
206
  v = voices[line["slot"]]
207
  wav_path = str(BUNDLE / v["wav"])
208
+ frames = (frames_by_index or {}).get(line["index"])
209
+ if use_student and not frames:
210
+ log(f"{tag} line {line['index']} skipped: no teacher frames")
211
+ continue
212
+
213
+ def gen(attempt: int) -> np.ndarray:
214
  if use_student:
215
  latent, spk = tts.prepare_prompt(wav_path, use_spk_emb=True)
216
  latent = latent.to(device=device, dtype=dtype)
 
219
  # teacher again to measure length gives a different take -
220
  # 25 frames vs 16 on a whisper line - and a wrong budget
221
  # makes the student drift into a loop or into silence.
222
+ torch.manual_seed(SEED + 1000 * attempt + line["index"])
223
+ return _ar(mm, sampler, line["text"], task_prompt, latent,
224
+ v["prompt_text"], spk, frames)
225
+ out = tts.synthesize(task="TTS", text=line["text"], language_id="Chinese",
226
+ prompt_wav_path=wav_path, prompt_text=v["prompt_text"],
227
+ use_spk_emb=True, raw_text=True,
228
+ max_decode_steps=max(32, min(400, int(len(line["text"]) * 3.5))))
229
+ return out.cpu().numpy().reshape(-1)
230
+
231
+ t0 = time.time()
232
+ try:
233
+ y = gen(0)
 
234
  except Exception:
235
  log(f"{tag} line {line['index']} failed\n{traceback.format_exc()}")
236
  continue
237
+ # A collapsed take shows up as a fraction of the expected duration:
238
+ # one 24-character whisper line came out as 0.4 s of near-silence in
239
+ # both voices. One retake with a different seed is cheap and fixes
240
+ # most of them; keep whichever is longer.
241
+ if y.size / SR < 0.09 * len(line["text"]):
242
+ log(f"{tag} line {line['index']} short ({y.size/SR:.2f}s), retaking")
243
+ try:
244
+ y2 = gen(1)
245
+ if y2.size > y.size:
246
+ y = y2
247
+ except Exception:
248
+ log(f"{tag} line {line['index']} retake failed (keeping the short take)")
249
  wall = time.time() - t0
250
  p = OUT / "lines" / f"{tag}_{line['index']:02d}.wav"
251
  sf.write(str(p), y, SR)