|
| 1 | +"""A/B listen test for the sentence splitter change (PR #415 adaptation). |
| 2 | +
|
| 3 | +Run once against the old image with --label old, rebuild, run again with |
| 4 | +--label new. Files land in examples/ab_split/<label>/ with matching names, |
| 5 | +so any player sorted by name gives direct A/B pairs. |
| 6 | +
|
| 7 | + uv run ab_split_test.py --label old |
| 8 | +""" |
| 9 | + |
| 10 | +import argparse |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +import requests |
| 14 | + |
| 15 | +URL = "http://localhost:8880/v1/audio/speech" |
| 16 | + |
| 17 | +CASES = [ |
| 18 | + ( |
| 19 | + "01_abbrev_mid", |
| 20 | + "af_heart", |
| 21 | + "This, that, the other thing, etc. was on the list. We need bread, milk, eggs, etc. before the store closes.", |
| 22 | + ), |
| 23 | + ( |
| 24 | + "02_abbrev_end", |
| 25 | + "af_heart", |
| 26 | + "Bring pencils, paper, erasers, etc. Then we can start the exam.", |
| 27 | + ), |
| 28 | + ( |
| 29 | + "03_ie_eg", |
| 30 | + "af_heart", |
| 31 | + "Use the primary color, i.e. red, for warnings. Some fruits, e.g. apples and pears, keep well.", |
| 32 | + ), |
| 33 | + ( |
| 34 | + "04_decimals", |
| 35 | + "af_heart", |
| 36 | + "You have 4.2 messages on average. The rate rose 3.5 percent, reaching 12.8 million this year.", |
| 37 | + ), |
| 38 | + ( |
| 39 | + "05_semicolons", |
| 40 | + "af_heart", |
| 41 | + "The plan was simple; the execution was not. We packed three things: rope, water, and a map.", |
| 42 | + ), |
| 43 | + ( |
| 44 | + "06_titles", |
| 45 | + "af_heart", |
| 46 | + "Dr. Smith met Mr. Jones at 5 p.m. on Main St. to discuss the results.", |
| 47 | + ), |
| 48 | + ( |
| 49 | + "07_dash_continuation", |
| 50 | + "af_heart", |
| 51 | + "The Marbles. --- In the heart of a vibrant city in Spain stood an ancient temple, a masterpiece of design.", |
| 52 | + ), |
| 53 | + ( |
| 54 | + "08_runon_clauses", |
| 55 | + "af_heart", |
| 56 | + "When the committee finally assembled after weeks of delays, cancellations, rescheduled flights, " |
| 57 | + "missing paperwork, and two separate hotel mixups, the chairman, visibly exhausted but determined " |
| 58 | + "to proceed, opened the meeting with a lengthy statement about procedure, precedent, patience, " |
| 59 | + "and the pressing need to conclude before the holidays, which everyone privately doubted was possible.", |
| 60 | + ), |
| 61 | + ( |
| 62 | + "09_tags", |
| 63 | + "af_heart", |
| 64 | + "First a normal sentence. [pause:1.5s] Then after a pause, [voice:am_adam] a different voice finishes the thought.", |
| 65 | + ), |
| 66 | + ( |
| 67 | + "10_zh_commas", |
| 68 | + "zf_xiaobei", |
| 69 | + "他走进房间,放下行李,看了看窗外,然后叹了口气。天色渐暗,远处的灯光一盏盏亮起,街道安静得出奇。", |
| 70 | + ), |
| 71 | + ( |
| 72 | + "11_zh_terminators", |
| 73 | + "zf_xiaobei", |
| 74 | + "这是第一句。这真的可能吗?当然可能!我们明天就出发。", |
| 75 | + ), |
| 76 | +] |
| 77 | + |
| 78 | +NEWLINE_CASE = ( |
| 79 | + "12_newlines_nonorm", |
| 80 | + "af_heart", |
| 81 | + "Chapter One\n\nThe rain had not stopped for three days. Rivers crept up their banks.", |
| 82 | +) |
| 83 | + |
| 84 | + |
| 85 | +def synth(label_dir: Path, name: str, voice: str, text: str, normalize: bool = True): |
| 86 | + payload = { |
| 87 | + "model": "kokoro", |
| 88 | + "input": text, |
| 89 | + "voice": voice, |
| 90 | + "response_format": "mp3", |
| 91 | + "stream": False, |
| 92 | + } |
| 93 | + if not normalize: |
| 94 | + payload["normalization_options"] = {"normalize": False} |
| 95 | + r = requests.post(URL, json=payload, timeout=120) |
| 96 | + r.raise_for_status() |
| 97 | + out = label_dir / f"{name}.mp3" |
| 98 | + out.write_bytes(r.content) |
| 99 | + print(f" {out.name} ({len(r.content) / 1024:.0f} KB)") |
| 100 | + |
| 101 | + |
| 102 | +def main(): |
| 103 | + parser = argparse.ArgumentParser() |
| 104 | + parser.add_argument("--label", required=True, choices=["old", "new"]) |
| 105 | + args = parser.parse_args() |
| 106 | + |
| 107 | + label_dir = Path(__file__).parent / "ab_split" / args.label |
| 108 | + label_dir.mkdir(parents=True, exist_ok=True) |
| 109 | + print(f"Writing {label_dir}") |
| 110 | + |
| 111 | + for name, voice, text in CASES: |
| 112 | + synth(label_dir, name, voice, text) |
| 113 | + name, voice, text = NEWLINE_CASE |
| 114 | + synth(label_dir, name, voice, text, normalize=False) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + main() |
0 commit comments