whisper lines capped at 18 chars; retake short takes
Browse files- kaggle/director_probe.py +12 -7
kaggle/director_probe.py
CHANGED
|
@@ -31,6 +31,10 @@ OUT = ROOT / "director"
|
|
| 31 |
|
| 32 |
SLOTS = ["开场", "叙述", "故事", "叙述", "耳语", "耳语"]
|
| 33 |
MIN_CHARS, MAX_CHARS = 8, 40
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
LATIN_OR_DIGIT = re.compile(r"[A-Za-z0-9]")
|
| 35 |
ALLOWED = re.compile(r"^[\u4e00-\u9fff,。?!、;:…——“”‘’「」《》]+$")
|
| 36 |
|
|
@@ -94,15 +98,15 @@ def parse_script(raw: str) -> list[dict]:
|
|
| 94 |
return out
|
| 95 |
|
| 96 |
|
| 97 |
-
def trim(text: str) -> str:
|
| 98 |
"""Cut an over-long line at its last clause boundary under the limit."""
|
| 99 |
-
if len(text) <=
|
| 100 |
return text
|
| 101 |
-
cut = text[:
|
| 102 |
for i in range(len(cut) - 1, MIN_CHARS - 1, -1):
|
| 103 |
if cut[i] in ",。?!":
|
| 104 |
return cut[:i] + "。"
|
| 105 |
-
return cut[:
|
| 106 |
|
| 107 |
|
| 108 |
def check(lines: list[dict]) -> dict:
|
|
@@ -113,7 +117,7 @@ def check(lines: list[dict]) -> dict:
|
|
| 113 |
t = l.get("text", "")
|
| 114 |
if i < len(SLOTS) and l.get("style") != SLOTS[i]:
|
| 115 |
bad.append(f"L{i}:style={l.get('style')!r}")
|
| 116 |
-
if not (MIN_CHARS <= len(t) <=
|
| 117 |
bad.append(f"L{i}:len={len(t)}")
|
| 118 |
if LATIN_OR_DIGIT.search(t) or not ALLOWED.match(t):
|
| 119 |
bad.append(f"L{i}:chars")
|
|
@@ -182,7 +186,8 @@ def main() -> None:
|
|
| 182 |
# The label is positional anyway (it decides which voice speaks),
|
| 183 |
# so relabel by position and repair over-long lines instead of
|
| 184 |
# throwing the whole script away.
|
| 185 |
-
lines = [{"style": SLOTS[i], "text": trim(l["text"]
|
|
|
|
| 186 |
if any(LATIN_OR_DIGIT.search(l["text"]) or len(l["text"]) < MIN_CHARS for l in lines):
|
| 187 |
continue
|
| 188 |
kept.append({"topic": topic, "lines": lines})
|
|
@@ -209,7 +214,7 @@ def main() -> None:
|
|
| 209 |
best = r
|
| 210 |
if best is None:
|
| 211 |
raise SystemExit("no script generated")
|
| 212 |
-
lines = [{"style": SLOTS[i], "text": trim(l["text"])}
|
| 213 |
for i, l in enumerate(best["lines"][:len(SLOTS)])]
|
| 214 |
doc = {"model": MODEL_ID, "topic": topic, "tries": attempt + 1,
|
| 215 |
"lines": lines}
|
|
|
|
| 31 |
|
| 32 |
SLOTS = ["开场", "叙述", "故事", "叙述", "耳语", "耳语"]
|
| 33 |
MIN_CHARS, MAX_CHARS = 8, 40
|
| 34 |
+
# Whisper slots get a tighter cap. Measured: 8-15 character whisper lines come
|
| 35 |
+
# out clean, a 24 character one collapsed to 0.4 s of near-silence in both the
|
| 36 |
+
# teacher and the student.
|
| 37 |
+
SLOT_MAX = [40, 40, 40, 40, 18, 18]
|
| 38 |
LATIN_OR_DIGIT = re.compile(r"[A-Za-z0-9]")
|
| 39 |
ALLOWED = re.compile(r"^[\u4e00-\u9fff,。?!、;:…——“”‘’「」《》]+$")
|
| 40 |
|
|
|
|
| 98 |
return out
|
| 99 |
|
| 100 |
|
| 101 |
+
def trim(text: str, limit: int = MAX_CHARS) -> str:
|
| 102 |
"""Cut an over-long line at its last clause boundary under the limit."""
|
| 103 |
+
if len(text) <= limit:
|
| 104 |
return text
|
| 105 |
+
cut = text[:limit]
|
| 106 |
for i in range(len(cut) - 1, MIN_CHARS - 1, -1):
|
| 107 |
if cut[i] in ",。?!":
|
| 108 |
return cut[:i] + "。"
|
| 109 |
+
return cut[: limit - 1] + "。"
|
| 110 |
|
| 111 |
|
| 112 |
def check(lines: list[dict]) -> dict:
|
|
|
|
| 117 |
t = l.get("text", "")
|
| 118 |
if i < len(SLOTS) and l.get("style") != SLOTS[i]:
|
| 119 |
bad.append(f"L{i}:style={l.get('style')!r}")
|
| 120 |
+
if not (MIN_CHARS <= len(t) <= SLOT_MAX[min(i, len(SLOT_MAX) - 1)]):
|
| 121 |
bad.append(f"L{i}:len={len(t)}")
|
| 122 |
if LATIN_OR_DIGIT.search(t) or not ALLOWED.match(t):
|
| 123 |
bad.append(f"L{i}:chars")
|
|
|
|
| 186 |
# The label is positional anyway (it decides which voice speaks),
|
| 187 |
# so relabel by position and repair over-long lines instead of
|
| 188 |
# throwing the whole script away.
|
| 189 |
+
lines = [{"style": SLOTS[i], "text": trim(l["text"], SLOT_MAX[i])}
|
| 190 |
+
for i, l in enumerate(r["lines"])]
|
| 191 |
if any(LATIN_OR_DIGIT.search(l["text"]) or len(l["text"]) < MIN_CHARS for l in lines):
|
| 192 |
continue
|
| 193 |
kept.append({"topic": topic, "lines": lines})
|
|
|
|
| 214 |
best = r
|
| 215 |
if best is None:
|
| 216 |
raise SystemExit("no script generated")
|
| 217 |
+
lines = [{"style": SLOTS[i], "text": trim(l["text"], SLOT_MAX[i])}
|
| 218 |
for i, l in enumerate(best["lines"][:len(SLOTS)])]
|
| 219 |
doc = {"model": MODEL_ID, "topic": topic, "tries": attempt + 1,
|
| 220 |
"lines": lines}
|