zziang commited on
Commit
bf64381
·
verified ·
1 Parent(s): aee48fb

8 mined voices + clone_voices.py

Browse files
kaggle/clone_voices.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Clone the eight mined voices with Ming and render a sample for each.
2
+
3
+ The point is comparison: the listening page already has the raw 6 s crop, this
4
+ adds what the model actually produces from it, on the same two sentences, so
5
+ timbre can be judged before any voice joins the roster.
6
+
7
+ Reads webfm-eval/mined/manifest.json, writes voices_clone/{slot}_1.wav,
8
+ voices_clone/{slot}_2.wav and voices_clone/metrics.json.
9
+
10
+ Note the prompt transcripts come from adult-roleplay ASMR clips. Only the timbre
11
+ is cloned; the prompt text is conditioning, never spoken in the product. Source
12
+ provenance still needs to hold up in the licence audit before any of these ship.
13
+ """
14
+ import json
15
+ import os
16
+ import sys
17
+ import time
18
+ from pathlib import Path
19
+
20
+ import numpy as np
21
+
22
+ ROOT = Path(os.environ.get("WORK_ROOT", "/kaggle/working"))
23
+ REPO = ROOT / "Ming-3.0-tts"
24
+ MODEL = ROOT / "Ming-3.0-tts-0.5b"
25
+ BUNDLE = ROOT / "webfm-eval"
26
+ OUT = ROOT / "voices_clone"
27
+
28
+ TEXTS = [
29
+ "灯关小一点,我们慢慢来。",
30
+ "睡不着的夜里,时间会走得特别慢。",
31
+ ]
32
+
33
+
34
+ def log(msg: str) -> None:
35
+ print(f"[{time.strftime('%H:%M:%S')}] {msg}", flush=True)
36
+
37
+
38
+ def patch_fp32() -> None:
39
+ for name in ("ming_tts.py", "modeling_bailingmm.py"):
40
+ path = REPO / name
41
+ s = path.read_text()
42
+ s = s.replace("torch_dtype=torch.bfloat16", "torch_dtype=torch.float32")
43
+ s = s.replace(".eval().to(torch.bfloat16).to(device)", ".eval().to(torch.float32).to(device)")
44
+ s = s.replace("dtype=torch.bfloat16", "dtype=torch.float32")
45
+ path.write_text(s)
46
+
47
+
48
+ def hf_ratio(y: np.ndarray, sr: int, cut: int = 8000) -> float:
49
+ spec = np.abs(np.fft.rfft(y)) ** 2
50
+ freqs = np.fft.rfftfreq(len(y), 1 / sr)
51
+ return float(spec[freqs > cut].sum() / (spec.sum() + 1e-12))
52
+
53
+
54
+ def main() -> None:
55
+ import torch
56
+ import torchaudio
57
+
58
+ OUT.mkdir(parents=True, exist_ok=True)
59
+ sys.path.insert(0, str(REPO))
60
+ patch_fp32()
61
+ from ming_tts import MingTTS
62
+
63
+ sys.path.insert(0, str(BUNDLE / "eval"))
64
+ from score import load_t2s, score_row
65
+ table = load_t2s()
66
+
67
+ from transformers import pipeline
68
+ asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-small",
69
+ device=0, torch_dtype=torch.float16)
70
+
71
+ def asr(y: np.ndarray) -> str:
72
+ out = asr_pipe({"array": y.astype(np.float32), "sampling_rate": 44100},
73
+ generate_kwargs={"language": "zh", "task": "transcribe"})
74
+ return (out.get("text", "") if isinstance(out, dict) else str(out)).strip()
75
+
76
+ tts = MingTTS(str(MODEL), device="cuda", text_normalization=False)
77
+ voices = json.loads((BUNDLE / "mined/manifest.json").read_text())["voices"]
78
+ log(f"{len(voices)} voices, {len(TEXTS)} sentences each")
79
+
80
+ rows = []
81
+ for v in voices:
82
+ wav_path = BUNDLE / v["wav"]
83
+ entry = dict(v)
84
+ for i, text in enumerate(TEXTS, 1):
85
+ try:
86
+ t0 = time.time()
87
+ torch.manual_seed(1234)
88
+ wav = tts.synthesize(
89
+ task="TTS", text=text, language_id="Chinese",
90
+ prompt_wav_path=str(wav_path), prompt_text=v["prompt_text"],
91
+ use_spk_emb=True, raw_text=True,
92
+ max_decode_steps=max(32, min(400, int(len(text) * 3.5))),
93
+ )
94
+ arr = wav.cpu().numpy().reshape(-1)
95
+ path = OUT / f"{v['slot']}_{i}.wav"
96
+ torchaudio.save(str(path), torch.from_numpy(arr).unsqueeze(0), 44100)
97
+ s = score_row({"text": text, "hyp_text": asr(arr)}, table)
98
+ entry[f"cer_{i}"] = s["cer_norm"]
99
+ entry[f"asr_{i}"] = s["hyp_text"]
100
+ entry[f"hf_{i}"] = hf_ratio(arr, 44100)
101
+ entry[f"sec_{i}"] = round(arr.size / 44100, 2)
102
+ log(f"{v['slot']} #{i} cer={s['cer_norm']:.3f} hf={entry[f'hf_{i}']:.4f} "
103
+ f"{arr.size/44100:.1f}s {time.time()-t0:.0f}s")
104
+ except Exception as exc:
105
+ entry[f"error_{i}"] = f"{type(exc).__name__}: {exc}"
106
+ log(f"{v['slot']} #{i} failed: {exc}")
107
+ entry["cer"] = float(np.mean([entry[k] for k in entry if k.startswith("cer_")] or [9]))
108
+ entry["hf"] = float(np.mean([entry[k] for k in entry if k.startswith("hf_")] or [0]))
109
+ rows.append(entry)
110
+
111
+ rows.sort(key=lambda r: r["cer"])
112
+ (OUT / "metrics.json").write_text(json.dumps(rows, ensure_ascii=False, indent=1))
113
+ log("\n-- by CER --")
114
+ for r in rows:
115
+ log(f" {r['cer']:.3f} hf={r['hf']:.4f} {r['slot']} {r['artist'][:16]:<16} "
116
+ f"mine_cer={r['mine_cer']:.3f}")
117
+
118
+
119
+ if __name__ == "__main__":
120
+ main()
mined/a01.wav ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:603c440f73dc386af85fcf0d8ddbbddf9769ca30792258b66ce51ddf0e6d285b
3
+ size 159724
mined/a02.wav ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:36ac323b01904e6e8b16b4a84fbb57e138bcb5e4a585e6bf2ae98af708188633
3
+ size 192044
mined/a03.wav ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:38b67bd9c837d96d7a5fa5690cc37331f9a1a506f800dd4209c9ba49ce39dd0a
3
+ size 177644
mined/a04.wav ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3090bdbdddb9ffb2c94d5c3b7f2b51c431d4e7c6a48f83ff335be0c00e349ac3
3
+ size 156524
mined/a05.wav ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:32b53dd91ad3139228e56cd36158e083d4fff1fd40f4ba1d02a8d31011aca45c
3
+ size 191724
mined/a06.wav ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f71d5fff662ec57b57721dece3128174eac2dea6f55815e23d9a1570b99ddc95
3
+ size 192044
mined/a07.wav ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:35be36f5cdb3b2223348772e9ebc19591565d8b578406758be5421bfd2072e19
3
+ size 186924
mined/a08.wav ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3fb35eba47b304c7950a3286393cec7da76deb03c7770ff21094b172a94d813c
3
+ size 192044
mined/manifest.json ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "voices": [
3
+ {
4
+ "slot": "a01",
5
+ "artist": "千千",
6
+ "wav": "mined/a01.wav",
7
+ "prompt_text": "感覺那個地方有個變形風的感覺是",
8
+ "mine_cer": 0.4101449275362319,
9
+ "voicing": 0.721
10
+ },
11
+ {
12
+ "slot": "a02",
13
+ "artist": "Miss哈尼",
14
+ "wav": "mined/a02.wav",
15
+ "prompt_text": "隨時隨地抱抱開心也要抱抱難過也要抱",
16
+ "mine_cer": 0.22939958592132503,
17
+ "voicing": 0.833
18
+ },
19
+ {
20
+ "slot": "a03",
21
+ "artist": "优优",
22
+ "wav": "mined/a03.wav",
23
+ "prompt_text": "你好非常感謝先生購買這次的教練課",
24
+ "mine_cer": 0.2101449275362319,
25
+ "voicing": 0.706
26
+ },
27
+ {
28
+ "slot": "a04",
29
+ "artist": "atinybear",
30
+ "wav": "mined/a04.wav",
31
+ "prompt_text": "好的好的好的 我出去我出去教授我马上就走我马上滚蛋",
32
+ "mine_cer": 0.13395445134575568,
33
+ "voicing": 0.734
34
+ },
35
+ {
36
+ "slot": "a05",
37
+ "artist": "buy1take2",
38
+ "wav": "mined/a05.wav",
39
+ "prompt_text": "但是你還是要像往常一樣以通關為目的的進行遊戲哦",
40
+ "mine_cer": 0.11014492753623188,
41
+ "voicing": 0.859
42
+ },
43
+ {
44
+ "slot": "a06",
45
+ "artist": "vivian",
46
+ "wav": "mined/a06.wav",
47
+ "prompt_text": "你現在老二跟丹丹應該都撞得很難受吧",
48
+ "mine_cer": 0.07681159420289856,
49
+ "voicing": 0.732
50
+ },
51
+ {
52
+ "slot": "a07",
53
+ "artist": "伊芙琳",
54
+ "wav": "mined/a07.wav",
55
+ "prompt_text": "嘿嘿竟然那么老实的尊仗我的吩咐主人果然是无可救药的M呢",
56
+ "mine_cer": 0.07163561076604555,
57
+ "voicing": 0.85
58
+ },
59
+ {
60
+ "slot": "a08",
61
+ "artist": "Rainnight雨",
62
+ "wav": "mined/a08.wav",
63
+ "prompt_text": "先生您是第一次点我们上门按摩的服务吗?",
64
+ "mine_cer": 0.014492753623188406,
65
+ "voicing": 0.888
66
+ }
67
+ ]
68
+ }