mining: reject probe CER>0.5, drop unusable modulation gate
Browse files- kaggle/voice_mining.py +44 -1
kaggle/voice_mining.py
CHANGED
|
@@ -153,6 +153,29 @@ def spectral_flatness(y: np.ndarray, sr: int) -> float:
|
|
| 153 |
return float(np.median(np.exp(np.log(spec).mean(axis=1)) / spec.mean(axis=1)))
|
| 154 |
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
def repeated_run(text: str) -> float:
|
| 157 |
"""Share of the transcript taken by its longest repeated character run."""
|
| 158 |
if not text:
|
|
@@ -213,13 +236,28 @@ def harvest(asr) -> list[dict]:
|
|
| 213 |
if seg is None:
|
| 214 |
continue
|
| 215 |
flat = spectral_flatness(seg, sr)
|
|
|
|
| 216 |
st["flatness"] = flat
|
|
|
|
| 217 |
if st["snr_db"] < 12 or st["clip_ratio"] > 1e-3 or st["speech_ratio"] < 0.55 or flat > 0.35:
|
| 218 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
text = asr(seg, sr)
|
| 221 |
if not (6 <= len(text) <= 70) or repeated_run(text) > 0.3 or not is_sfw(text):
|
| 222 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
idx = len(per_artist.get(creator, []))
|
| 225 |
tag = hashlib.md5(creator.encode("utf-8")).hexdigest()[:4]
|
|
@@ -298,6 +336,10 @@ def probe(cands: list[dict], asr) -> list[dict]:
|
|
| 298 |
hfs.append(hf_ratio(arr, 44100))
|
| 299 |
row = dict(c, cer=float(np.mean(ces)), cer_each=ces,
|
| 300 |
hf=float(np.mean(hfs)), wall=round(time.time() - t0, 1))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
results.append(row)
|
| 302 |
log(f"[{i+1}/{len(cands)}] {c['cid']} cer={row['cer']:.3f} hf={row['hf']:.4f} "
|
| 303 |
f"snr={c['snr_db']:.1f} {c['prompt_text'][:16]}")
|
|
@@ -313,7 +355,8 @@ def select(results: list[dict]) -> list[dict]:
|
|
| 313 |
best: dict[str, dict] = {}
|
| 314 |
for r in results:
|
| 315 |
best.setdefault(r["artist"], r)
|
| 316 |
-
ranked = sorted(best.values(), key=lambda r: r["cer"])
|
|
|
|
| 317 |
(WORK / "best.json").write_text(json.dumps(ranked, ensure_ascii=False, indent=2))
|
| 318 |
log("\n-- best per artist --")
|
| 319 |
for r in ranked:
|
|
|
|
| 153 |
return float(np.median(np.exp(np.log(spec).mean(axis=1)) / spec.mean(axis=1)))
|
| 154 |
|
| 155 |
|
| 156 |
+
def speech_modulation(y: np.ndarray, sr: int) -> float:
|
| 157 |
+
"""Share of envelope energy in the 2-8 Hz syllabic band.
|
| 158 |
+
|
| 159 |
+
Speech has a syllabic rhythm; rustling, tapping and room tone do not. This
|
| 160 |
+
is the physical counterpart to the transcript: whisper-small happily
|
| 161 |
+
invents a caption over pure noise ("字幕by索兰娅"), so the transcript alone
|
| 162 |
+
cannot tell us whether anyone is talking.
|
| 163 |
+
"""
|
| 164 |
+
hop = int(0.01 * sr)
|
| 165 |
+
if len(y) < hop * 50:
|
| 166 |
+
return 0.0
|
| 167 |
+
n = len(y) // hop
|
| 168 |
+
env = np.sqrt((y[: n * hop].reshape(n, hop) ** 2).mean(axis=1) + 1e-12)
|
| 169 |
+
env = env - env.mean()
|
| 170 |
+
spec = np.abs(np.fft.rfft(env))
|
| 171 |
+
freqs = np.fft.rfftfreq(len(env), 0.01)
|
| 172 |
+
total = spec.sum() + 1e-12
|
| 173 |
+
return float(spec[(freqs >= 2.0) & (freqs <= 8.0)].sum() / total)
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
ASR_ARTIFACTS = ("字幕", "by索", "谢谢观看", "请订阅", "amara", "subtitles", "感谢观看")
|
| 177 |
+
|
| 178 |
+
|
| 179 |
def repeated_run(text: str) -> float:
|
| 180 |
"""Share of the transcript taken by its longest repeated character run."""
|
| 181 |
if not text:
|
|
|
|
| 236 |
if seg is None:
|
| 237 |
continue
|
| 238 |
flat = spectral_flatness(seg, sr)
|
| 239 |
+
mod = speech_modulation(seg, sr)
|
| 240 |
st["flatness"] = flat
|
| 241 |
+
st["modulation"] = mod
|
| 242 |
if st["snr_db"] < 12 or st["clip_ratio"] > 1e-3 or st["speech_ratio"] < 0.55 or flat > 0.35:
|
| 243 |
continue
|
| 244 |
+
# Do not gate on `modulation`. Measured on clips a listener confirmed
|
| 245 |
+
# contain whisper speech versus clips that are pure ASMR noise, the two
|
| 246 |
+
# overlap almost exactly (0.239 vs 0.230), because whispering has weak
|
| 247 |
+
# syllabic rhythm. It is recorded for diagnostics only. The reliable
|
| 248 |
+
# discriminator is the round-trip probe CER: noise-only prompts score
|
| 249 |
+
# above 1.0, a usable whisper prompt scores 0.22.
|
| 250 |
|
| 251 |
text = asr(seg, sr)
|
| 252 |
if not (6 <= len(text) <= 70) or repeated_run(text) > 0.3 or not is_sfw(text):
|
| 253 |
continue
|
| 254 |
+
if any(a in text for a in ASR_ARTIFACTS):
|
| 255 |
+
continue
|
| 256 |
+
# A hallucinated caption over noise tends to be far shorter or longer
|
| 257 |
+
# than anyone can actually say in six seconds.
|
| 258 |
+
rate = len(text) / (len(seg) / sr)
|
| 259 |
+
if not (2.5 <= rate <= 9.0):
|
| 260 |
+
continue
|
| 261 |
|
| 262 |
idx = len(per_artist.get(creator, []))
|
| 263 |
tag = hashlib.md5(creator.encode("utf-8")).hexdigest()[:4]
|
|
|
|
| 336 |
hfs.append(hf_ratio(arr, 44100))
|
| 337 |
row = dict(c, cer=float(np.mean(ces)), cer_each=ces,
|
| 338 |
hf=float(np.mean(hfs)), wall=round(time.time() - t0, 1))
|
| 339 |
+
# A prompt whose round trip scores above 0.5 is not a voice, it is
|
| 340 |
+
# either a bad transcript or a clip with no speech in it. Both show
|
| 341 |
+
# up here as garbled output, and both are unusable downstream.
|
| 342 |
+
row["keep"] = bool(row["cer"] < 0.5)
|
| 343 |
results.append(row)
|
| 344 |
log(f"[{i+1}/{len(cands)}] {c['cid']} cer={row['cer']:.3f} hf={row['hf']:.4f} "
|
| 345 |
f"snr={c['snr_db']:.1f} {c['prompt_text'][:16]}")
|
|
|
|
| 355 |
best: dict[str, dict] = {}
|
| 356 |
for r in results:
|
| 357 |
best.setdefault(r["artist"], r)
|
| 358 |
+
ranked = [r for r in sorted(best.values(), key=lambda r: r["cer"])
|
| 359 |
+
if r.get("keep", True)][:TOP_K]
|
| 360 |
(WORK / "best.json").write_text(json.dumps(ranked, ensure_ascii=False, indent=2))
|
| 361 |
log("\n-- best per artist --")
|
| 362 |
for r in ranked:
|