File size: 11,925 Bytes
7c6795b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa7ff36
7c6795b
 
aa7ff36
7c6795b
 
 
aa7ff36
7c6795b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa7ff36
 
 
 
 
 
7c6795b
 
 
 
 
 
aa7ff36
 
 
 
 
 
 
 
 
 
 
7c6795b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa7ff36
 
 
 
7c6795b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa7ff36
 
 
 
7c6795b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
"""Generate the tiny-broca training audio with Qwen3-TTS (Brazilian Portuguese).

Every corpus sentence is read by both voices (F1, M1), like the original
tiny-voca dataset. Generation is resumable (sqlite state in the output dir),
peak-normalised to 0.95, and quality-checked per clip with faster-whisper
(int8 on CPU): WER with a number-masking normaliser plus a duration range.

Write a file named STOP inside the output directory to stop gracefully after
the current batch; rerun to continue.

Usage:
    python scripts/generate_audio.py \
        --corpus data/corpus --refs data/pilot/references \
        --out data/audio --device mps
"""

import argparse
import json
import sqlite3
import sys
import time
from pathlib import Path

import numpy as np
import soundfile as sf
import torch
from faster_whisper import WhisperModel
from jiwer import wer
from qwen_tts import Qwen3TTSModel

BASE_MODEL = "Qwen/Qwen3-TTS-12Hz-1.7B-Base"
VOICE_REFS = {"F1": "F1", "M1": "M1"}  # selected Common Voice clones (data/refs)

from tiny_broca.text import spoken_form, spoken_normalize, wer_normalize


def load_corpus(corpus_dir: Path, batches=(1, 2, 3)):
    import pyarrow.parquet as pq

    sentences = []
    for batch in batches:
        for path in sorted(corpus_dir.glob(f"batch{batch}/*.parquet")):
            table = pq.read_table(path)
            for i, (text, source) in enumerate(zip(table.column("text").to_pylist(),
                                                   table.column("source").to_pylist())):
                sentences.append({"batch": batch, "split": path.stem,
                                  "index": i, "text": text, "source": source})
    return sentences


def open_db(path: Path) -> sqlite3.Connection:
    conn = sqlite3.connect(path)
    conn.execute("""CREATE TABLE IF NOT EXISTS clips (
        clip_id TEXT PRIMARY KEY, voice TEXT, batch INTEGER, split TEXT,
        text TEXT, seconds REAL, sr INTEGER, gen_seconds REAL, rtf REAL,
        wer REAL, transcript TEXT, status TEXT)""")
    conn.execute("CREATE INDEX IF NOT EXISTS clips_status ON clips(status)")
    return conn


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--corpus", type=Path, default=Path("data/corpus"))
    ap.add_argument("--refs", type=Path, default=Path("data/refs"))
    ap.add_argument("--out", type=Path, default=Path("data/audio"))
    ap.add_argument("--device", default="mps")
    ap.add_argument("--threads", type=int, default=12)
    ap.add_argument("--batch", type=int, default=8)
    ap.add_argument("--seed", type=int, default=20261117)
    ap.add_argument("--batches", type=int, nargs="+", default=[1, 2, 3], help="Corpus batches to read")
    ap.add_argument("--teacher-text", choices=("raw", "spoken"), default="raw",
                    help="raw: the teacher reads the written sentence (v0); spoken: it reads the front end's "
                         "spoken_form(), so every number, symbol and loanword is said exactly as the student's "
                         "tokens spell it, and quality control compares words without masking numbers")
    ap.add_argument("--max-wer", type=float, default=0.15)
    args = ap.parse_args()

    args.out.mkdir(parents=True, exist_ok=True)
    torch.set_num_threads(args.threads)

    refs = json.loads((args.refs / "voices.json").read_text())
    sentences = load_corpus(args.corpus, args.batches)
    if args.teacher_text == "spoken":
        for s in sentences:
            s["written"], s["text"] = s["text"], spoken_form(s["text"])

    def clip_wer(text, transcript):
        if not transcript:
            return 1.0
        if args.teacher_text == "spoken":
            return wer(spoken_normalize(text), spoken_normalize(transcript))
        return wer(wer_normalize(text), wer_normalize(transcript))
    print(f"corpus: {len(sentences)} sentences", flush=True)

    print("loading whisper small ...", flush=True)
    asr = WhisperModel("small", device="cpu", compute_type="int8", cpu_threads=args.threads)

    def transcribe(path):
        import librosa
        wave, rate = sf.read(str(path), dtype="float32")
        if rate != 16000:
            wave = librosa.resample(wave, orig_sr=rate, target_sr=16000)
        segments, _ = asr.transcribe(wave, language="pt", beam_size=1, vad_filter=False)
        return " ".join(s.text for s in segments)

    print("loading Qwen3-TTS Base ...", flush=True)
    base = Qwen3TTSModel.from_pretrained(BASE_MODEL, device_map=args.device,
                                         dtype=torch.bfloat16, attn_implementation="sdpa")
    prompts = {}
    for voice, ref_tag in VOICE_REFS.items():
        prompts[voice] = base.create_voice_clone_prompt(
            ref_audio=str(args.refs / f"{ref_tag}.wav"),
            ref_text=refs[ref_tag]["text"], x_vector_only_mode=False)
        print(f"voice {voice} <- {ref_tag}.wav", flush=True)

    conn = open_db(args.out / "state.db")
    # Every attempted clip (ok or bad) is skipped: a clip that failed its two
    # attempts is excluded by design and must not be regenerated on a resume.
    done = {r[0] for r in conn.execute("SELECT clip_id FROM clips")}
    jobs = []
    for voice in VOICE_REFS:
        for s in sentences:
            cid = f"b{s['batch']}_{voice}_{s['split']}_{s['index']:05d}"
            if cid not in done:
                jobs.append((voice, cid, s))
    print(f"to generate: {len(jobs)} clips "
          f"({sum(1 for j in jobs if j[0] == 'F1')} F1, "
          f"{sum(1 for j in jobs if j[0] == 'M1')} M1)", flush=True)

    audio_dir = args.out / "audio"
    audio_dir.mkdir(exist_ok=True)
    t_start = time.time()
    n_ok = n_bad = 0
    last_report = 0
    wers, rtfs = [], []

    # Group remaining jobs by voice into batches.
    per_voice = {v: [j for j in jobs if j[0] == v] for v in VOICE_REFS}
    for voice, vjobs in per_voice.items():
        prompt = prompts[voice]
        for b0 in range(0, len(vjobs), args.batch):
            if (args.out / "STOP").exists():
                print("STOP file present, exiting", flush=True)
                return
            chunk = vjobs[b0:b0 + args.batch]
            t0 = time.time()
            wavs, sr = base.generate_voice_clone(
                text=[s["text"] for _, _, s in chunk],
                language=["Portuguese"] * len(chunk),
                voice_clone_prompt=prompt, max_new_tokens=600,
                seed=args.seed + b0)
            gen = time.time() - t0
            for (v, cid, s), wave in zip(chunk, wavs):
                wave = np.asarray(wave, dtype=np.float32)
                seconds = len(wave) / sr
                peak = float(np.abs(wave).max())
                if peak > 1e-9:
                    wave = wave / peak * 0.95
                path = audio_dir / f"{cid}.flac"
                sf.write(path, wave, sr)
                transcript = transcribe(path)
                w = clip_wer(s["text"], transcript)
                ok = (0.5 <= seconds <= 35.0) and (w < args.max_wer) and transcript
                conn.execute("INSERT OR REPLACE INTO clips (clip_id, voice, batch, split, text, seconds, sr, gen_seconds, rtf, "
                             "wer, transcript, status) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)",
                             (cid, voice, s["batch"], s["split"], s["text"],
                              round(seconds, 3), sr, round(gen / len(chunk), 2),
                              round(gen / len(chunk) / max(seconds, 1e-9), 3),
                              round(w, 4), transcript, "ok" if ok else "bad"))
                conn.commit()
                if ok:
                    n_ok += 1
                    wers.append(w)
                    rtfs.append(gen / len(chunk) / max(seconds, 1e-9))
                else:
                    n_bad += 1
                    print(f"  BAD {cid}: {seconds:.1f}s WER {w:.2f}", flush=True)
            done_n = n_ok + n_bad
            if done_n - last_report >= 50 or b0 + args.batch >= len(vjobs):
                last_report = done_n
                (args.out / "progress.json").write_text(json.dumps({
                    "done": done_n, "ok": n_ok, "bad": n_bad,
                    "wer_mean": round(float(np.mean(wers)), 4) if wers else None,
                    "rtf_mean": round(float(np.mean(rtfs)), 3) if rtfs else None,
                    "elapsed_min": round((time.time() - t_start) / 60, 1),
                    "eta_h": round((time.time() - t_start) / max(done_n, 1)
                                   * max(len(jobs) - done_n, 0) / 3600, 2)}))
                print(f"voice {voice} batch {b0 // args.batch + 1}: "
                      f"total {done_n} (bad {n_bad}), "
                      f"WER {np.mean(wers) if wers else 0:.1%}, "
                      f"RTF {np.mean(rtfs) if rtfs else 0:.2f}, "
                      f"ETA {json.loads((args.out / 'progress.json').read_text())['eta_h']}h",
                      flush=True)

    # Clips that failed the gate get one regeneration with a different seed;
    # clips that fail twice are left out and listed in review.jsonl.
    jobs_by_cid = {j[1]: j for j in jobs}
    bad = [r for r in conn.execute('SELECT clip_id, voice FROM clips WHERE status="bad"')
           if r[0] in jobs_by_cid]
    for voice in VOICE_REFS:
        vbad = [(cid, jobs_by_cid[cid][2]) for cid, v in bad if v == voice]
        for b0 in range(0, len(vbad), args.batch):
            if (args.out / "STOP").exists():
                print("STOP file present, exiting", flush=True)
                return
            chunk = vbad[b0:b0 + args.batch]
            t0 = time.time()
            wavs, sr = base.generate_voice_clone(
                text=[s["text"] for _, s in chunk],
                language=["Portuguese"] * len(chunk),
                voice_clone_prompt=prompts[voice], max_new_tokens=600,
                seed=args.seed + 7919 + b0)
            gen = time.time() - t0
            for (cid, s), wave in zip(chunk, wavs):
                wave = np.asarray(wave, dtype=np.float32)
                seconds = len(wave) / sr
                peak = float(np.abs(wave).max())
                if peak > 1e-9:
                    wave = wave / peak * 0.95
                path = audio_dir / f"{cid}.flac"
                sf.write(path, wave, sr)
                transcript = transcribe(path)
                w = clip_wer(s["text"], transcript)
                ok = (0.5 <= seconds <= 35.0) and (w < args.max_wer) and transcript
                conn.execute("INSERT OR REPLACE INTO clips (clip_id, voice, batch, split, text, seconds, sr, gen_seconds, rtf, "
                             "wer, transcript, status) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)",
                             (cid, voice, s["batch"], s["split"], s["text"],
                              round(seconds, 3), sr, round(gen / len(chunk), 2),
                              round(gen / len(chunk) / max(seconds, 1e-9), 3),
                              round(w, 4), transcript, "ok" if ok else "bad"))
                conn.commit()
                print(f"  RETRY {cid}: {seconds:.1f}s WER {w:.2f} -> {'ok' if ok else 'bad'}", flush=True)
    with (args.out / "review.jsonl").open("w") as fh:
        for row in conn.execute(
                'SELECT clip_id, voice, text, seconds, wer, transcript FROM clips WHERE status="bad"'):
            fh.write(json.dumps(dict(zip(
                ("id", "voice", "text", "seconds", "wer", "transcript"), row)), ensure_ascii=False) + "\n")
    hours = sum(r[0] for r in conn.execute(
        'SELECT seconds FROM clips WHERE status="ok"')) / 3600
    conn.close()
    print(json.dumps({"ok": n_ok, "bad": n_bad,
                      "wer_mean": round(float(np.mean(wers)), 4) if wers else None,
                      "rtf_mean": round(float(np.mean(rtfs)), 3) if rtfs else None,
                      "hours_audio": round(hours, 2)}))


if __name__ == "__main__":
    main()