deepgram / deepgram-python-sdk

Official Python SDK for Deepgram's automated speech recognition APIs.
https://developers.deepgram.com
MIT License
214 stars 58 forks source link

LiveTranscription #127

Closed SishaarRao closed 9 months ago

SishaarRao commented 1 year ago

What is the current behavior?

I keep seeing the following error emitted by my server

Task exception was never retrieved
future: <Task finished name='Task-192191' coro=<LiveTranscription._start() done, defined at /Users/sishaar/.pyenv/versions/3.11.4/lib/python3.11/site-packages/deepgram/transcription.py:178> exception=ConnectionClosedOK(Close(code=1000, reason=''), Close(code=1000, reason=''), True)>
Traceback (most recent call last):
  File "/Users/sishaar/.pyenv/versions/3.11.4/lib/python3.11/site-packages/deepgram/transcription.py", line 222, in _start
    await self._socket.send(body)
  File "/Users/sishaar/.pyenv/versions/3.11.4/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 635, in send
    await self.ensure_open()
  File "/Users/sishaar/.pyenv/versions/3.11.4/lib/python3.11/site-packages/websockets/legacy/protocol.py", line 935, in ensure_open
    raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedOK: received 1000 (OK); then sent 1000 (OK)

Here is the file I use to manage my Deepgram connections - am I not registering/handling errors correctly?

class _DeepgramConnectionPool:
  def __init__(self):
    self.connections: LiveTranscription = []
    self.deepgram_hosted = Deepgram('<elided>')
    self.deepgram_onprem = Deepgram({'api_key': '', 'api_url': '<elided>'})

  async def get_connection(self, config: dict = {}):
    # default to use hosted deepgram
    self.deepgram = self.deepgram_hosted
    if config.get("enable_to_use_deepgram_on_premise") == "true":
      self.deepgram = self.deepgram_onprem
    try:
      deepgram_config = config.get('country_code_configs', {})
      model = deepgram_config.get("model", '<elided>')
      tier = 'enhanced' if model == 'nooks' else deepgram_config.get("tier", 'base')
      version = 'v1' if model == 'nooks' else deepgram_config.get('version', 'latest')

      async def _get_connection():
        return await self.deepgram.transcription.live(
          encoding='mulaw',
          model=model,
          tier=tier,
          version=version,
          sample_rate=8000,
          punctuate=True,
          interim_results=True,
          language='en-US',
          times=False,
        )

      connection: LiveTranscription = await Retry(_get_connection, delay_s=0.1, factor=1.2)
      connection.register_handler(LiveTranscriptionEvent.ERROR, lambda error: logger.error('Error during connection', exception=error))
      return connection
    except ConnectionClosedOK:
      logger.log('Deepgram websocket connection closed')
    except Exception as e:
      logger.error('Error creating connection: ', exception=e)
      raise e

DeepgramConnectionPool = _DeepgramConnectionPool()

Later on, I close the connection with the following piece of code

    async def close_deepgram(self):
        self.logger.debug("Closing Deepgram Connection")
        if self.deepgram:
            await self.deepgram.finish()
            self.logger.log('Closed Deepgram Connection')

Please tell us about your environment

We want to make sure the problem isn't specific to your operating system or programming language.

DamienDeepgram commented 1 year ago

Are you using the keepalive message when your not sending any audio we will close the stream unless you send us a keepalive message

https://developers.deepgram.com/reference/streaming#stream-keepalive

dvonthenen commented 10 months ago

The live client is getting reworked. Please check back when PR is merged.

dvonthenen commented 10 months ago

A new release was just posted which should address this problem, please take a look: https://github.com/deepgram/deepgram-python-sdk/releases/tag/v3.0.0-alpha.1

dvonthenen commented 9 months ago

Hi @SishaarRao have you been able to try the latest v3 release?

dvonthenen commented 9 months ago

Closing due to inactivity. If there is still a problem with the latest release, please reopen or file a new issue. thanks!