deepgram-python-sdk
deepgram-python-sdk copied to clipboard
ListenWebSocketClient.start failed
What is the current behavior?
def init_deepgram():
def on_message(self, result, **kwargs):
print("on_message")
sentence = result.channel.alternatives[0].transcript
if len(sentence) == 0:
return
print(f"Deepgram transcription: {sentence}")
def on_error(self, error, **kwargs):
print(f"Deepgram error: {error}")
config = DeepgramClientOptions(
api_key=os.getenv("DEEPGRAM_API_KEY"),
options={"keepalive": "true"},
)
deepgram = DeepgramClient(config)
dg_connection = deepgram.listen.websocket.v("1")
dg_connection.on(LiveTranscriptionEvents.Transcript, on_message)
dg_connection.on(LiveTranscriptionEvents.Error, on_error)
options = LiveOptions(
punctuate=True,
language="en-US",
vad_events=True,
)
return dg_connection.start(options)
init_deepgram()
WebSocketException in AbstractSyncWebSocketClient.start: server rejected WebSocket connection: HTTP 401
ListenWebSocketClient.start failed
Expected behavior
I expect it to open the websocket connection successfully
Please tell us about your environment
OS: Tried on Windows and Windows Subsystem for Linux, failed on both Python version: Python 3.10.12 deepgram-sdk 3.7.7 websockets 13.1
Anything else we should know
I know that I'm populating the API key correctly because I tried printing it out
I found a workaround for the above issue, but found what seems to be another bug
I had to pass in the api key to DeepgramClient. It did NOT work if I pass in api key to DeepgramClientOptions
I have this bug too, although I have supplied the API key to DeepgramClient. Any updates?
code:
# Initialize Deepgram client
config = DeepgramClientOptions(verbose=verboselogs.DEBUG, options={"keepalive": "true"})
deepgram_client = DeepgramClient(api_key=deepgram_api_key, config=config)
# Set up Deepgram parameters
deepgram_options: LiveOptions = LiveOptions(
diarize=True,
language=selected_language,
punctuate=True,
model="nova-3",
encoding="linear16",
sample_rate=samplerate
)
connection = deepgram_client.listen.websocket.v("1")
def on_message(self, result, **kwargs):
alternatives = result.get("channel", {}).get("alternatives", [])
if len(alternatives) > 0 and "words" in alternatives[0]:
transcript = alternatives[0]["transcript"]
words = alternatives[0]["words"]
start = words[0]["start"] if words else None
end = words[-1]["end"] if words else None
speakers = [word['speaker'] for word in words]
transcription: Dict = {
'transcript': transcript,
'timestamp_start': start,
'timestamp_end': end,
'speakers': speakers
}
update_transcriptions(transcription)
def on_error(self, error, **kwargs):
print(f"Handled Error: {error}")
connection.on(LiveTranscriptionEvents.Transcript, on_message)
connection.on(LiveTranscriptionEvents.Error, on_error)
connection.start(options=deepgram_options)
output:
WebSocketException in AbstractSyncWebSocketClient.start: server rejected WebSocket connection: HTTP 400
AbstractSyncWebSocketClient.start LEAVE
ListenWebSocketClient.start failed
ListenWebSocketClient.start LEAVE