radio: student reuses teacher frames; parse malformed script JSON
Browse files- kaggle/radio_demo.py +37 -12
kaggle/radio_demo.py
CHANGED
|
@@ -93,10 +93,8 @@ def write_script() -> tuple[list[dict], str]:
|
|
| 93 |
del model
|
| 94 |
torch.cuda.empty_cache()
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
rows = [r for r in rows if isinstance(r, dict) and r.get("text")]
|
| 99 |
-
# Three usable lines is a valid answer; the previous >=4 gate rejected a
|
| 100 |
# correct three-line script and silently used the fallback.
|
| 101 |
if len(rows) >= 3:
|
| 102 |
return rows[:8], "llm"
|
|
@@ -105,6 +103,28 @@ def write_script() -> tuple[list[dict], str]:
|
|
| 105 |
return FALLBACK, "fallback"
|
| 106 |
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
def cast(rows: list[dict]) -> list[dict]:
|
| 109 |
"""Assign a voice to each line and keep the plan explicit in the report."""
|
| 110 |
out = []
|
|
@@ -145,7 +165,7 @@ def main() -> None:
|
|
| 145 |
tts = MingTTS(str(MODEL), device="cuda", text_normalization=False)
|
| 146 |
report = {"script_source": source, "lines": [], "renders": {}}
|
| 147 |
|
| 148 |
-
def render(tag: str, use_student: bool) -> dict:
|
| 149 |
import copy
|
| 150 |
|
| 151 |
mm = tts.model
|
|
@@ -177,12 +197,15 @@ def main() -> None:
|
|
| 177 |
latent, spk = tts.prepare_prompt(wav_path, use_spk_emb=True)
|
| 178 |
latent = latent.to(device=device, dtype=dtype)
|
| 179 |
spk = [x.to(device=device, dtype=dtype) for x in spk]
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
| 186 |
y = _ar(mm, sampler, line["text"], task_prompt, latent,
|
| 187 |
v["prompt_text"], spk, frames)
|
| 188 |
else:
|
|
@@ -198,6 +221,7 @@ def main() -> None:
|
|
| 198 |
p = OUT / "lines" / f"{tag}_{line['index']:02d}.wav"
|
| 199 |
sf.write(str(p), y, SR)
|
| 200 |
rows.append({**line, "seconds": round(y.size / SR, 2), "wall": round(wall, 1),
|
|
|
|
| 201 |
"rtf": round(wall / max(1e-6, y.size / SR), 2),
|
| 202 |
"wav": str(p)})
|
| 203 |
log(f"{tag} {line['index']} {line['style']} {y.size/SR:.2f}s wall {wall:.1f}s")
|
|
@@ -216,7 +240,8 @@ def main() -> None:
|
|
| 216 |
report["renders"]["teacher"] = teacher
|
| 217 |
log(f"teacher segment: {teacher['seconds']}s, {len(teacher['rows'])} lines")
|
| 218 |
|
| 219 |
-
|
|
|
|
| 220 |
report["renders"]["student"] = student
|
| 221 |
log(f"student segment: {student['seconds']}s, {len(student['rows'])} lines")
|
| 222 |
|
|
|
|
| 93 |
del model
|
| 94 |
torch.cuda.empty_cache()
|
| 95 |
|
| 96 |
+
rows = parse_script(raw)
|
| 97 |
+
# Three usable lines is a valid answer; an earlier >=4 gate rejected a
|
|
|
|
|
|
|
| 98 |
# correct three-line script and silently used the fallback.
|
| 99 |
if len(rows) >= 3:
|
| 100 |
return rows[:8], "llm"
|
|
|
|
| 103 |
return FALLBACK, "fallback"
|
| 104 |
|
| 105 |
|
| 106 |
+
def parse_script(raw: str) -> list[dict]:
|
| 107 |
+
"""Pull style/text pairs out of the model's answer.
|
| 108 |
+
|
| 109 |
+
A 1.5B instruct model reliably writes the fields but not reliably the
|
| 110 |
+
punctuation: one observed answer dropped a closing brace and the whole
|
| 111 |
+
array stopped being JSON. Scanning for the pairs survives that, while
|
| 112 |
+
`json.loads` cannot.
|
| 113 |
+
"""
|
| 114 |
+
pairs = re.findall(r'"style"\s*:\s*"([^"]*)"\s*,\s*"text"\s*:\s*"([^"]*)"', raw)
|
| 115 |
+
if not pairs:
|
| 116 |
+
pairs = [(s, t) for t, s in re.findall(
|
| 117 |
+
r'"text"\s*:\s*"([^"]*)"\s*,\s*"style"\s*:\s*"([^"]*)"', raw)]
|
| 118 |
+
out = [{"style": s or "叙述", "text": t} for s, t in pairs if t.strip()]
|
| 119 |
+
if not out:
|
| 120 |
+
try:
|
| 121 |
+
rows = json.loads(re.search(r"\[.*\]", raw, re.S).group(0))
|
| 122 |
+
out = [r for r in rows if isinstance(r, dict) and r.get("text")]
|
| 123 |
+
except Exception:
|
| 124 |
+
pass
|
| 125 |
+
return out
|
| 126 |
+
|
| 127 |
+
|
| 128 |
def cast(rows: list[dict]) -> list[dict]:
|
| 129 |
"""Assign a voice to each line and keep the plan explicit in the report."""
|
| 130 |
out = []
|
|
|
|
| 165 |
tts = MingTTS(str(MODEL), device="cuda", text_normalization=False)
|
| 166 |
report = {"script_source": source, "lines": [], "renders": {}}
|
| 167 |
|
| 168 |
+
def render(tag: str, use_student: bool, frames_by_index: dict | None = None) -> dict:
|
| 169 |
import copy
|
| 170 |
|
| 171 |
mm = tts.model
|
|
|
|
| 197 |
latent, spk = tts.prepare_prompt(wav_path, use_spk_emb=True)
|
| 198 |
latent = latent.to(device=device, dtype=dtype)
|
| 199 |
spk = [x.to(device=device, dtype=dtype) for x in spk]
|
| 200 |
+
# Reuse the teacher's own frame count. Synthesising the
|
| 201 |
+
# teacher again to measure length gives a different take -
|
| 202 |
+
# 25 frames vs 16 on a whisper line - and a wrong budget
|
| 203 |
+
# makes the student drift into a loop or into silence.
|
| 204 |
+
frames = (frames_by_index or {}).get(line["index"])
|
| 205 |
+
if not frames:
|
| 206 |
+
log(f"{tag} line {line['index']} skipped: no teacher frames")
|
| 207 |
+
continue
|
| 208 |
+
torch.manual_seed(SEED + line["index"])
|
| 209 |
y = _ar(mm, sampler, line["text"], task_prompt, latent,
|
| 210 |
v["prompt_text"], spk, frames)
|
| 211 |
else:
|
|
|
|
| 221 |
p = OUT / "lines" / f"{tag}_{line['index']:02d}.wav"
|
| 222 |
sf.write(str(p), y, SR)
|
| 223 |
rows.append({**line, "seconds": round(y.size / SR, 2), "wall": round(wall, 1),
|
| 224 |
+
"frames": max(1, int(round(y.size / 3528))),
|
| 225 |
"rtf": round(wall / max(1e-6, y.size / SR), 2),
|
| 226 |
"wav": str(p)})
|
| 227 |
log(f"{tag} {line['index']} {line['style']} {y.size/SR:.2f}s wall {wall:.1f}s")
|
|
|
|
| 240 |
report["renders"]["teacher"] = teacher
|
| 241 |
log(f"teacher segment: {teacher['seconds']}s, {len(teacher['rows'])} lines")
|
| 242 |
|
| 243 |
+
frames = {r["index"]: r["frames"] for r in teacher["rows"]}
|
| 244 |
+
student = render("student", use_student=True, frames_by_index=frames)
|
| 245 |
report["renders"]["student"] = student
|
| 246 |
log(f"student segment: {student['seconds']}s, {len(student['rows'])} lines")
|
| 247 |
|