Created
March 24, 2023 15:49
-
-
Save ploeber/54d3c4856a645890c53afccd90124b4e to your computer and use it in GitHub Desktop.
Realtime Transcription AssemblyAI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# requirements: pyaudio and websockets | |
import pyaudio | |
import websockets | |
import asyncio | |
import base64 | |
import json | |
FRAMES_PER_BUFFER = 3200 | |
FORMAT = pyaudio.paInt16 | |
CHANNELS = 1 | |
RATE = 16000 | |
p = pyaudio.PyAudio() | |
# starts recording | |
stream = p.open( | |
format=FORMAT, | |
channels=CHANNELS, | |
rate=RATE, | |
input=True, | |
frames_per_buffer=FRAMES_PER_BUFFER, | |
) | |
print(f"Input source: {p.get_default_input_device_info()['name']}\n") | |
URL = "wss://api.assemblyai.com/v2/realtime/ws?sample_rate=16000" | |
async def send_receive(): | |
async with websockets.connect( | |
URL, | |
extra_headers=(("Authorization", "API_KEY_ASSEMBLYAI"),), | |
ping_interval=5, | |
ping_timeout=20, | |
) as _ws: | |
await asyncio.sleep(0.1) | |
print("Start real-time transcription with AssemblyAI...\n") | |
session_begins = await _ws.recv() | |
async def send(): | |
while True: | |
try: | |
data = stream.read(FRAMES_PER_BUFFER, exception_on_overflow=False) | |
data = base64.b64encode(data).decode("utf-8") | |
json_data = json.dumps({"audio_data": str(data)}) | |
await _ws.send(json_data) | |
except websockets.exceptions.ConnectionClosedError as e: | |
print(e) | |
assert e.code == 4008 | |
break | |
except Exception as e: | |
assert False, "Not a websocket 4008 error" | |
await asyncio.sleep(0.01) | |
return True | |
async def receive(): | |
while True: | |
try: | |
result_str = await _ws.recv() | |
result = json.loads(result_str) | |
prompt = result["text"] | |
print(prompt, end="\r") # overwrite in same line | |
if prompt and result["message_type"] == "FinalTranscript": | |
print(prompt) | |
except websockets.exceptions.ConnectionClosedError as e: | |
print(e) | |
assert e.code == 4008 | |
break | |
except Exception as e: | |
assert False, "Not a websocket 4008 error" | |
send_result, receive_result = await asyncio.gather(send(), receive()) | |
asyncio.run(send_receive()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am getting the following error while running the file how to resolve it: