Skip to content

Commit

Permalink
Implement Unhandled Events
Browse files Browse the repository at this point in the history
  • Loading branch information
davidvonthenen committed Apr 5, 2024
1 parent 5dee108 commit a6ca9af
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 71 deletions.
3 changes: 2 additions & 1 deletion deepgram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
MetadataResponse,
SpeechStartedResponse,
UtteranceEndResponse,
ErrorResponse,
CloseResponse,
UnhandledResponse,
ErrorResponse,
)

# prerecorded
Expand Down
3 changes: 2 additions & 1 deletion deepgram/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
MetadataResponse,
SpeechStartedResponse,
UtteranceEndResponse,
ErrorResponse,
CloseResponse,
ErrorResponse,
UnhandledResponse,
)

# prerecorded
Expand Down
3 changes: 2 additions & 1 deletion deepgram/clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
MetadataResponse,
SpeechStartedResponse,
UtteranceEndResponse,
ErrorResponse,
CloseResponse,
ErrorResponse,
UnhandledResponse,
)

# prerecorded
Expand Down
3 changes: 3 additions & 0 deletions deepgram/clients/listen.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@

# responses
from .live import (
OpenResponse,
LiveResultResponse,
MetadataResponse,
SpeechStartedResponse,
UtteranceEndResponse,
CloseResponse,
ErrorResponse,
UnhandledResponse,
)


Expand Down
3 changes: 2 additions & 1 deletion deepgram/clients/live/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
MetadataResponse,
SpeechStartedResponse,
UtteranceEndResponse,
ErrorResponse,
CloseResponse,
ErrorResponse,
UnhandledResponse,
)

from ...options import DeepgramClientOptions
15 changes: 12 additions & 3 deletions deepgram/clients/live/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
MetadataResponse as MetadataResponseLatest,
SpeechStartedResponse as SpeechStartedResponseLatest,
UtteranceEndResponse as UtteranceEndResponseLatest,
ErrorResponse as ErrorResponseLatest,
CloseResponse as CloseResponseLatest,
ErrorResponse as ErrorResponseLatest,
UnhandledResponse as UnhandledResponseLatest,
)

"""
Expand Down Expand Up @@ -72,6 +73,14 @@ class UtteranceEndResponse(UtteranceEndResponseLatest):
pass


class CloseResponse(CloseResponseLatest):
"""
pass through for CloseResponse based on API version
"""

pass


class ErrorResponse(ErrorResponseLatest):
"""
pass through for ErrorResponse based on API version
Expand All @@ -80,9 +89,9 @@ class ErrorResponse(ErrorResponseLatest):
pass


class CloseResponse(CloseResponseLatest):
class UnhandledResponse(UnhandledResponseLatest):
"""
pass through for CloseResponse based on API version
pass through for UnhandledResponse based on API version
"""

pass
Expand Down
1 change: 1 addition & 0 deletions deepgram/clients/live/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ class LiveTranscriptionEvents(str, Enum):
UtteranceEnd: str = "UtteranceEnd"
SpeechStarted: str = "SpeechStarted"
Error: str = "Error"
Unhandled: str = "Unhandled"
Warning: str = "Warning"
3 changes: 2 additions & 1 deletion deepgram/clients/live/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
MetadataResponse,
SpeechStartedResponse,
UtteranceEndResponse,
ErrorResponse,
CloseResponse,
ErrorResponse,
UnhandledResponse,
)
48 changes: 28 additions & 20 deletions deepgram/clients/live/v1/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
MetadataResponse,
SpeechStartedResponse,
UtteranceEndResponse,
ErrorResponse,
CloseResponse,
ErrorResponse,
UnhandledResponse,
)
from .options import LiveOptions

Expand Down Expand Up @@ -244,14 +245,6 @@ async def _listening(self) -> None:
utterance_end=result,
**dict(self.kwargs),
)
case LiveTranscriptionEvents.Error.value:
result = ErrorResponse.from_json(message)
self.logger.verbose("LiveTranscriptionEvents: %s", result)
await self._emit(
LiveTranscriptionEvents.Error,
error=result,
**dict(self.kwargs),
)
case LiveTranscriptionEvents.Close.value:
result = CloseResponse.from_json(message)
self.logger.verbose("CloseResponse: %s", result)
Expand All @@ -260,18 +253,29 @@ async def _listening(self) -> None:
close=result,
**dict(self.kwargs),
)
case LiveTranscriptionEvents.Error.value:
result = ErrorResponse.from_json(message)
self.logger.verbose("LiveTranscriptionEvents: %s", result)
await self._emit(
LiveTranscriptionEvents.Error,
error=result,
**dict(self.kwargs),
)
case _:
self.logger.warning(
"Unknown Message: response_type: %s, data: %s",
response_type,
data,
)
error = ErrorResponse(
type="UnhandledMessage",
description="Unknown message type",
message=f"Unhandle message type: {response_type}",
unhandled = UnhandledResponse(
type=LiveTranscriptionEvents.Unhandled.value,
raw=message,
)
self._emit(
LiveTranscriptionEvents.Unhandled,
unhandled=unhandled,
**dict(self.kwargs),
)
await self._emit(LiveTranscriptionEvents.Error, error=error)

except websockets.exceptions.ConnectionClosedOK as e:
self.logger.notice(f"_listening({e.code}) exiting gracefully")
Expand Down Expand Up @@ -495,13 +499,17 @@ async def _signal_exit(self) -> None:
except Exception as e:
self.logger.error(f"_signal_exit - Exception: {str(e)}")

await asyncio.sleep(0.5)

# push close event
await self._emit(
LiveTranscriptionEvents.Close,
CloseResponse(type=LiveTranscriptionEvents.Close.value),
)
try:
await self._emit(
LiveTranscriptionEvents.Close,
CloseResponse(type=LiveTranscriptionEvents.Close.value),
)
except Exception as e:
self.logger.error(f"_signal_exit - Exception: {str(e)}")

# wait for task to send
await asyncio.sleep(0.5)

# signal exit
self._exit_event.set()
Expand Down
48 changes: 28 additions & 20 deletions deepgram/clients/live/v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
MetadataResponse,
SpeechStartedResponse,
UtteranceEndResponse,
ErrorResponse,
CloseResponse,
ErrorResponse,
UnhandledResponse,
)
from .options import LiveOptions

Expand Down Expand Up @@ -244,14 +245,6 @@ def _listening(self) -> None:
utterance_end=result,
**dict(self.kwargs),
)
case LiveTranscriptionEvents.Error.value:
result = ErrorResponse.from_json(message)
self.logger.verbose("ErrorResponse: %s", result)
self._emit(
LiveTranscriptionEvents.Error,
error=result,
**dict(self.kwargs),
)
case LiveTranscriptionEvents.Close.value:
result = CloseResponse.from_json(message)
self.logger.verbose("CloseResponse: %s", result)
Expand All @@ -260,18 +253,29 @@ def _listening(self) -> None:
close=result,
**dict(self.kwargs),
)
case LiveTranscriptionEvents.Error.value:
result = ErrorResponse.from_json(message)
self.logger.verbose("ErrorResponse: %s", result)
self._emit(
LiveTranscriptionEvents.Error,
error=result,
**dict(self.kwargs),
)
case _:
self.logger.warning(
"Unknown Message: response_type: %s, data: %s",
response_type,
data,
)
error = ErrorResponse(
type="UnhandledMessage",
description="Unknown message type",
message=f"Unhandle message type: {response_type}",
unhandled = UnhandledResponse(
type=LiveTranscriptionEvents.Unhandled.value,
raw=message,
)
self._emit(
LiveTranscriptionEvents.Unhandled,
unhandled=unhandled,
**dict(self.kwargs),
)
self._emit(LiveTranscriptionEvents.Error, error=error)

except websockets.exceptions.ConnectionClosedOK as e:
self.logger.notice(f"_listening({e.code}) exiting gracefully")
Expand Down Expand Up @@ -483,13 +487,17 @@ def _signal_exit(self) -> None:
except Exception as e:
self.logger.error(f"_signal_exit - Exception: {str(e)}")

time.sleep(0.5)

# push close event
self._emit(
LiveTranscriptionEvents.Close,
CloseResponse(type=LiveTranscriptionEvents.Close.value),
)
try:
self._emit(
LiveTranscriptionEvents.Close,
CloseResponse(type=LiveTranscriptionEvents.Close.value),
)
except Exception as e:
self.logger.error(f"_signal_exit - Exception: {str(e)}")

# wait for task to send
time.sleep(0.5)

# signal exit
self._exit_event.set()
Expand Down
31 changes: 29 additions & 2 deletions deepgram/clients/live/v1/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,29 @@ def __str__(self) -> str:
return self.to_json(indent=4)


# Close Message


@dataclass_json
@dataclass
class CloseResponse:
"""
Close Message from the Deepgram Platform
"""

type: str = ""

def __getitem__(self, key):
_dict = self.to_dict()
return _dict[key]

def __setitem__(self, key, val):
self.__dict__[key] = val

def __str__(self) -> str:
return self.to_json(indent=4)


# Error Message


Expand All @@ -317,14 +340,18 @@ def __str__(self) -> str:
return self.to_json(indent=4)


# Unhandled Message


@dataclass_json
@dataclass
class CloseResponse:
class UnhandledResponse:
"""
Close Message from the Deepgram Platform
Unhandled Message from the Deepgram Platform
"""

type: str = ""
raw: str = ""

def __getitem__(self, key):
_dict = self.to_dict()
Expand Down
8 changes: 4 additions & 4 deletions examples/advanced/streaming/mute-microphone/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ def on_speech_started(self, speech_started, **kwargs):
def on_utterance_end(self, utterance_end, **kwargs):
print(f"\n\n{utterance_end}\n\n")

def on_error(self, error, **kwargs):
print(f"\n\n{error}\n\n")

def on_close(self, close, **kwargs):
print(f"\n\n{close}\n\n")

def on_error(self, error, **kwargs):
print(f"\n\n{error}\n\n")

dg_connection.on(LiveTranscriptionEvents.Open, on_open)
dg_connection.on(LiveTranscriptionEvents.Transcript, on_message)
dg_connection.on(LiveTranscriptionEvents.Metadata, on_metadata)
dg_connection.on(LiveTranscriptionEvents.SpeechStarted, on_speech_started)
dg_connection.on(LiveTranscriptionEvents.UtteranceEnd, on_utterance_end)
dg_connection.on(LiveTranscriptionEvents.Error, on_error)
dg_connection.on(LiveTranscriptionEvents.Close, on_close)
dg_connection.on(LiveTranscriptionEvents.Error, on_error)

options: LiveOptions = LiveOptions(
model="nova-2",
Expand Down
12 changes: 8 additions & 4 deletions examples/streaming/async_http/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,23 @@ async def on_speech_started(self, speech_started, **kwargs):
async def on_utterance_end(self, utterance_end, **kwargs):
print(f"\n\n{utterance_end}\n\n")

async def on_error(self, error, **kwargs):
def on_close(self, close, **kwargs):
print(f"\n\n{close}\n\n")

def on_error(self, error, **kwargs):
print(f"\n\n{error}\n\n")

async def on_close(self, close, **kwargs):
print(f"\n\n{close}\n\n")
def on_unhandled(self, unhandled, **kwargs):
print(f"\n\n{unhandled}\n\n")

dg_connection.on(LiveTranscriptionEvents.Open, on_open)
dg_connection.on(LiveTranscriptionEvents.Transcript, on_message)
dg_connection.on(LiveTranscriptionEvents.Metadata, on_metadata)
dg_connection.on(LiveTranscriptionEvents.SpeechStarted, on_speech_started)
dg_connection.on(LiveTranscriptionEvents.UtteranceEnd, on_utterance_end)
dg_connection.on(LiveTranscriptionEvents.Error, on_error)
dg_connection.on(LiveTranscriptionEvents.Close, on_close)
dg_connection.on(LiveTranscriptionEvents.Error, on_error)
dg_connection.on(LiveTranscriptionEvents.Unhandled, on_unhandled)

# connect to websocket
options: LiveOptions = LiveOptions(
Expand Down
Loading

0 comments on commit a6ca9af

Please sign in to comment.