Live Streaming Audio Transcription
The transcription.live
function creates a websocket connection to the Deepgram API. It tries to create this connection and throws an exception if it could not open the socket, by using the try/except
code block.
Parameters
Additional options can be provided for streaming transcriptions. They are provided as an object of the transcription.live
function. Each of these parameters map to a feature in the Deepgram API. Reference the
features documentation to learn what features may be appropriate for your request.
Initiating a Connection
The transcription.live
function creates a websocket connection to the Deepgram API. It tries to create this connection and throws an exception if it could not open the socket, by using the try/except
code block.
try:
deepgramLive = await deepgram.transcription.live({ 'punctuate': True })
except Exception as e:
print(f'Could not open socket: {e}')
return
Please note that additional options can be passed into transcription.live
, such as interim_results
, language
, etc. See the Paremeters section above for a list of all the options. These options are passed as dictionary objects with key/value pairs.
deepgramLive = await deepgram.transcription.live({ 'punctuate': True, 'interim_results': False, 'language': 'en-GB' })
Events
The following events are fired by the live transcription object:
Event | Description | Data |
---|---|---|
open | The websocket connection to Deepgram has been opened. | The DG live transcription object |
close | The websocket connection to Deepgram has been closed. | WebSocket.CloseEvent |
error | An error occurred with the websocket connection | Error object |
transcript_received | Deepgram has responded with a transcription | Transcription response |
Listening to Events
Use the registerHandler
function to listen for events fired on the object returned from the transcription.live
function.
Listen for the event to close
and print out the message Connection closed with code {c}.
, where {c}
is a string argument.
deepgramLive.registerHandler(deepgramLive.event.CLOSE, lambda c: print(f'Connection closed with code {c}.'))
Listen for any transcripts to be received and print them to the console.
deepgramLive.registerHandler(deepgramLive.event.TRANSCRIPT_RECEIVED, print)
Functions
The object returned by the transcription.live
function provides several functions to make using the Deepgram API easier. They are send
and finish
.
Listen for Open Connection
Create a ClientSession called session
and a ClientResponse object called audio
, which gets the information needed from the response.
The send
function sends raw audio data to the Deepgram API, while reading in the contents of the audio.
deepgramLive.send(await audio.content.readany())
async with aiohttp.ClientSession() as session:
async with session.get(URL) as audio:
while True:
deepgramLive.send(await audio.content.readany())
Closing the Connection
The finish
function closes the Websocket connection to Deepgram.
await deepgramLive.finish()
Updated 13 days ago