zziang commited on
Commit
7700015
·
verified ·
1 Parent(s): 2f66858

three takes for weak lines, keep the best

Browse files
Files changed (1) hide show
  1. kaggle/radio_demo.py +23 -12
kaggle/radio_demo.py CHANGED
@@ -239,18 +239,29 @@ def main() -> None:
239
  # right duration filled with near-silence (peak 0.13 against 0.68
240
  # for every other line). One retake with a different seed is cheap;
241
  # keep whichever take is longer and louder.
242
- ref_peak = float(np.median(peaks)) if len(peaks) >= 2 else 0.0
243
- weak = float(np.abs(y).max()) < (0.45 * ref_peak if ref_peak else 0.20)
244
- short = y.size / SR < 0.09 * len(line["text"])
245
- if short or weak:
246
- why = "short" if short else "near-silent"
247
- log(f"{tag} line {line['index']} {why} ({y.size/SR:.2f}s peak {np.abs(y).max():.2f}), retaking")
248
- try:
249
- y2 = gen(1)
250
- if (y2.size, np.abs(y2).max()) > (y.size, np.abs(y).max()):
251
- y = y2
252
- except Exception:
253
- log(f"{tag} line {line['index']} retake failed (keeping the first take)")
 
 
 
 
 
 
 
 
 
 
 
254
  peaks.append(float(np.abs(y).max()))
255
  wall = time.time() - t0
256
  p = OUT / "lines" / f"{tag}_{line['index']:02d}.wav"
 
239
  # right duration filled with near-silence (peak 0.13 against 0.68
240
  # for every other line). One retake with a different seed is cheap;
241
  # keep whichever take is longer and louder.
242
+ def usable(a: np.ndarray) -> bool:
243
+ ref_peak = float(np.median(peaks)) if len(peaks) >= 2 else 0.0
244
+ floor = 0.45 * ref_peak if ref_peak else 0.20
245
+ return (a.size / SR >= 0.09 * len(line["text"])
246
+ and float(np.abs(a).max()) >= max(floor, 0.20))
247
+
248
+ if not usable(y):
249
+ log(f"{tag} line {line['index']} weak ({y.size/SR:.2f}s peak {np.abs(y).max():.2f})")
250
+ # Whisper takes are stochastic: two of six in one run came out
251
+ # as 0.4 s or as near-silence at the right length. Three tries,
252
+ # keep the best by (valid, peak, duration).
253
+ for attempt in (1, 2, 3):
254
+ try:
255
+ cand = gen(attempt)
256
+ except Exception:
257
+ continue
258
+ if usable(cand):
259
+ y = cand
260
+ log(f"{tag} line {line['index']} take {attempt} accepted "
261
+ f"({y.size/SR:.2f}s peak {np.abs(y).max():.2f})")
262
+ break
263
+ if (cand.size, float(np.abs(cand).max())) > (y.size, float(np.abs(y).max())):
264
+ y = cand
265
  peaks.append(float(np.abs(y).max()))
266
  wall = time.time() - t0
267
  p = OUT / "lines" / f"{tag}_{line['index']:02d}.wav"