File size: 4,884 Bytes
83a951c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Speechmatics real-time ASR -> Arc Tagger, printed as JSON lines.

Status: tested against the documented message format (offline fixtures in tests/); not yet tested live.

    export SPEECHMATICS_API_KEY=...        # read from the environment only
    pip install websockets                 # + sounddevice for --mic
    python stream_speechmatics.py --wav call.wav --domain talk
    python stream_speechmatics.py --mic --domain meeting --seconds 30

AddPartialTranscript is tagged immediately and replaced by the next partial; AddTranscript freezes the words; EndOfUtterance
(conversation_config.end_of_utterance_silence_trigger) ends the utterance (utterance-level intent, act, safety and role
read-outs). With diarization (default "speaker") the speaker labels are passed to the tagger, which enables speaker roles.

Each output line is {"type": "update", "source": "interim"|"final"|"endpoint", ...summary} (see arctag.asr.summarize);
the last line is {"type": "done", ...}. --result writes the full result (same schema as ArcTagger.tag()).
"""
import argparse, asyncio, json, os, sys

HERE = os.path.dirname(os.path.abspath(__file__)); sys.path.insert(0, HERE)
from arctag import live                                   # noqa: E402
from arctag.asr import SpeechmaticsAdapter, summarize     # noqa: E402

URL = "wss://eu2.rt.speechmatics.com/v2"


def start_msg(a, sr):
    tc = {"language": "en", "enable_partials": True, "max_delay": a.max_delay, "operating_point": a.operating_point,
          "conversation_config": {"end_of_utterance_silence_trigger": a.eou_silence}}
    if a.diarization != "none":
        tc["diarization"] = a.diarization
    return {"message": "StartRecognition", "audio_format": {"type": "raw", "encoding": "pcm_s16le", "sample_rate": sr},
            "transcription_config": tc}


async def connect(url, key):
    import websockets
    hdr = {"Authorization": "Bearer " + key}
    try:
        return await websockets.connect(url, additional_headers=hdr, max_size=None)
    except TypeError:                                     # websockets < 14
        return await websockets.connect(url, extra_headers=hdr, max_size=None)


async def run(a):
    key = live.api_key("SPEECHMATICS_API_KEY")
    from arctag import ArcTagger
    tg = ArcTagger.from_pretrained(a.model_dir)
    st = tg.stream(domain=a.domain, tier=a.tier)
    ad = SpeechmaticsAdapter(st)
    sr, chunks = live.source(a)
    try:
        ws = await connect(a.url, key)
    except Exception as e:
        sys.exit("Speechmatics connect failed: " + live.scrub(e, key))
    await ws.send(json.dumps(start_msg(a, sr)))
    started = asyncio.Event()

    async def send():
        await started.wait()
        n = 0
        async for b in chunks:
            await ws.send(b); n += 1                      # binary frame = AddAudio
        await ws.send(json.dumps({"message": "EndOfStream", "last_seq_no": n}))

    async def recv():
        async for raw in ws:
            if isinstance(raw, bytes):
                continue
            m = json.loads(raw); t = m.get("message")
            if t == "RecognitionStarted":
                started.set(); continue
            if t == "Error":
                live.emit({"type": "error", "message": live.scrub(m.get("reason") or m.get("type") or "", key)})
                break
            if t == "EndOfTranscript":
                break
            src = {"AddPartialTranscript": "interim", "AddTranscript": "final", "EndOfUtterance": "endpoint"}.get(t)
            for up in ad(m):
                if a.no_interim and src == "interim":
                    continue
                live.emit(dict(type="update", source="endpoint" if up.get("utterances") else src, **summarize(up, st)))

    tx = asyncio.create_task(send())
    try:
        await recv()
    finally:
        tx.cancel()
        await ws.close()
    res = st.finalize()
    live.emit({"type": "done", "n_words": len(res["words"]), "redacted_text": res["redacted_text"],
               "latency_ms": st.s.stats()})
    if a.result:
        json.dump(res, open(a.result, "w"), indent=1, default=str)


def main():
    ap = live.add_common_args(argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter))
    ap.add_argument("--url", default=URL, help="real-time endpoint for your region")
    ap.add_argument("--max-delay", type=float, default=1.0, help="seconds before a word is final (lower = faster finals)")
    ap.add_argument("--operating-point", default="enhanced", choices=["standard", "enhanced"])
    ap.add_argument("--eou-silence", type=float, default=0.5, help="seconds of silence for EndOfUtterance")
    ap.add_argument("--diarization", default="speaker", choices=["speaker", "none"])
    try:
        asyncio.run(run(ap.parse_args()))
    except KeyboardInterrupt:
        pass


if __name__ == "__main__":
    main()