-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeepgram.py
67 lines (52 loc) · 2.15 KB
/
deepgram.py
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import asyncio
import json
from typing import Any, AsyncIterator, Callable
import websockets
WSS_URL = "wss://api.deepgram.com/v1/listen?endpointing=500&encoding=linear16&sample_rate=16000&channels=1&interim_results=false"
async def deepgram_stream(
key: str, audio_stream: AsyncIterator[bytes]
) -> AsyncIterator[str]:
"""An AsyncIterator-friendly Deepgram wrapper for streaming inputs/outputs.
Args:
key (str): Your Deepgram API key
audio_stream (AsyncIterator[bytes]): An AsyncIterator-compatible audio iterator, commonly in the form of streaming file or audio input.
Yields:
str: A sentence of text.
"""
extra_headers = {"Authorization": f"Token {key}"}
async with websockets.connect(
WSS_URL,
extra_headers=extra_headers,
) as ws:
async def keep_alive(websocket: websockets.WebSocketClientProtocol):
while True:
keep_alive_msg = json.dumps({"type": "KeepAlive"})
await websocket.send(keep_alive_msg)
await asyncio.sleep(3)
keep_alive_task = asyncio.create_task(keep_alive(websocket=ws))
async def sender(ws):
while True:
data = await anext(audio_stream)
await ws.send(data)
async def receiver(ws):
transcript = ""
async for msg in ws:
msg = json.loads(msg)
if len(msg["channel"]["alternatives"][0]["transcript"]) > 0:
if transcript:
transcript += " "
transcript += msg["channel"]["alternatives"][0]["transcript"]
if msg["speech_final"]:
yield transcript
transcript = ""
sender_task = asyncio.create_task(sender(ws))
async for update in receiver(ws):
if update:
yield update
tasks = [sender_task, keep_alive_task]
for task in tasks:
task.cancel()