File size: 5,032 Bytes
f730521 | 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 | """Turn-taking latency: time from the caller finishing to the first reply audio.
Held-out caller turns (test voices, test scenarios, with their noise) are cut out of
the test dialogues and spoken to the endpoint one per fresh session, once with the
controller deciding and once with the fixed-silence baseline ("vad"). Reported per mode:
the latency of the reply that finally answers the turn, in how many turns the caller
heard the model before they had finished (the audible false starts), how many replies
were withdrawn before a single frame played (inaudible), and how often no reply came.
Only turns that end a caller's say are used (the turn's whole audio, pauses included),
so a mid-sentence pause that triggers a reply counts as "started early".
python -m eval.eval_latency --data data/out/dataset --out eval/results/latency --modes controller,vad
"""
import argparse
import asyncio
import json
import random
from pathlib import Path
import aiohttp
import numpy as np
from duplex.spec import CALLER, SR, read_wav, write_json
from eval.ws_client import open_session, percentiles, run_turn
def held_out_turns(data, n, seed):
data = Path(data)
turns = []
for it in json.loads((data / "index_test.json").read_text()):
meta = json.loads((data / "meta" / f"{it['id']}.json").read_text())
cuts = [e["onset"] for e in meta["events"] if e["type"] == "interrupt"]
for e in meta["events"]:
# skip turns that begin as an interruption: their first words overlap the agent
if e["type"] == "caller_turn" and e["start"] not in cuts and 1.0 * SR <= e["end"] - e["start"] <= 25 * SR:
turns.append((it["id"], e["start"], e["end"]))
random.Random(seed).shuffle(turns)
return turns[:n]
async def one(http, args, data, turn, mode, sem):
did, start, end = turn
lead = int(0.3 * SR)
x, _ = read_wav(Path(data) / "audio" / f"{did}.wav", max(0, start - lead), end - max(0, start - lead))
pcm = (x[:, CALLER] * 32767).astype(np.int16)
async with sem:
ws = await open_session(http, args.url, args.system, mode)
try:
r = await run_turn(ws, pcm, until="done_after_audio", max_wait_s=args.max_wait_s)
finally:
await ws.close()
lag = [e.get("lag_ms") for e in r["events"] if e["type"] == "reply.start"]
return {"dialogue": did, "start": start, "end": end, "turn_s": len(pcm) / SR, "latency_ms": r["latency_ms"], "server_lag_ms": lag[0] if lag else None,
"started_early": r["started_early"], "replied": r["t_first_audio"] is not None, "error": r["error"],
"heard_early_ms": r["audio_ms_before_caller_end"], "replies_cut_off": sum(1 for d in r["replies"] if d["reason"] != "done"),
"withdrawn_unheard": sum(1 for e in r["events"] if e["type"] == "reply.withdrawn")}
async def main():
ap = argparse.ArgumentParser()
ap.add_argument("--url", default="http://localhost:8765/ws")
ap.add_argument("--data", required=True)
ap.add_argument("--out", default="eval/results/latency")
ap.add_argument("--modes", default="controller,vad")
ap.add_argument("--turns", type=int, default=100)
ap.add_argument("--concurrency", type=int, default=4)
ap.add_argument("--max-wait-s", type=float, default=30.0)
ap.add_argument("--system", default="You are a helpful assistant on a phone call. Keep answers short.")
ap.add_argument("--seed", type=int, default=0)
args = ap.parse_args()
turns = held_out_turns(args.data, args.turns, args.seed)
summary = {"turns": len(turns)}
async with aiohttp.ClientSession() as http:
for mode in args.modes.split(","):
sem = asyncio.Semaphore(args.concurrency)
rows = await asyncio.gather(*[one(http, args, args.data, t, mode, sem) for t in turns])
write_json(Path(args.out) / f"{mode}.json", rows)
clean = [r for r in rows if r["replied"] and r["heard_early_ms"] == 0 and r["latency_ms"] is not None and r["latency_ms"] >= 0]
summary[mode] = {
# the caller never heard the model before finishing, and the reply came after: the clean turns
"latency_ms_clean_turns": percentiles([r["latency_ms"] for r in clean]),
"turns_where_caller_heard_the_model_early": sum(r["heard_early_ms"] > 0 for r in rows),
"replies_cut_off_after_being_heard": sum(r["replies_cut_off"] for r in rows),
"replies_withdrawn_unheard": sum(r["withdrawn_unheard"] for r in rows),
"no_reply": sum(not r["replied"] for r in rows),
"errors": sum(bool(r["error"]) for r in rows),
"server_lag_ms": percentiles([r["server_lag_ms"] for r in rows]), # how far behind real time the server was when it decided
}
print(mode, json.dumps(summary[mode]))
write_json(Path(args.out) / f"summary_{args.modes.replace(',', '_')}.json", summary)
if __name__ == "__main__":
asyncio.run(main())
|