Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deepgram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .client import (
LiveResultResponse,
MetadataResponse,
SpeechStartedResponse,
UtteranceEndResponse,
ErrorResponse,
)
Expand Down
1 change: 1 addition & 0 deletions deepgram/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .clients import (
LiveResultResponse,
MetadataResponse,
SpeechStartedResponse,
UtteranceEndResponse,
ErrorResponse,
)
Expand Down
1 change: 1 addition & 0 deletions deepgram/clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .live import (
LiveResultResponse,
MetadataResponse,
SpeechStartedResponse,
UtteranceEndResponse,
ErrorResponse,
)
Expand Down
1 change: 1 addition & 0 deletions deepgram/clients/listen.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from .live import (
LiveResultResponse,
MetadataResponse,
SpeechStartedResponse,
UtteranceEndResponse,
ErrorResponse,
)
Expand Down
1 change: 1 addition & 0 deletions deepgram/clients/live/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .client import (
LiveResultResponse,
MetadataResponse,
SpeechStartedResponse,
UtteranceEndResponse,
ErrorResponse,
)
Expand Down
9 changes: 9 additions & 0 deletions deepgram/clients/live/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .v1.response import (
LiveResultResponse as LiveResultResponseLatest,
MetadataResponse as MetadataResponseLatest,
SpeechStartedResponse as SpeechStartedResponseLatest,
UtteranceEndResponse as UtteranceEndResponseLatest,
ErrorResponse as ErrorResponseLatest,
)
Expand Down Expand Up @@ -45,6 +46,14 @@ class MetadataResponse(MetadataResponseLatest):
pass


class SpeechStartedResponse(SpeechStartedResponseLatest):
"""
pass through for SpeechStartedResponse based on API version
"""

pass


class UtteranceEndResponse(UtteranceEndResponseLatest):
"""
pass through for UtteranceEndResponse based on API version
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 @@ -15,5 +15,6 @@ class LiveTranscriptionEvents(Enum):
Transcript = "Results"
Metadata = "Metadata"
UtteranceEnd = "UtteranceEnd"
SpeechStarted = "SpeechStarted"
Error = "Error"
Warning = "Warning"
1 change: 1 addition & 0 deletions deepgram/clients/live/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .response import (
LiveResultResponse,
MetadataResponse,
SpeechStartedResponse,
UtteranceEndResponse,
ErrorResponse,
)
14 changes: 14 additions & 0 deletions deepgram/clients/live/v1/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .response import (
LiveResultResponse,
MetadataResponse,
SpeechStartedResponse,
UtteranceEndResponse,
ErrorResponse,
)
Expand Down Expand Up @@ -127,6 +128,19 @@ async def _start(self) -> None:
metadata=result,
**dict(self.kwargs),
)
case LiveTranscriptionEvents.SpeechStarted.value:
self.logger.debug(
"response_type: %s, data: %s", response_type, data
)
result = SpeechStartedResponse.from_json(message)
if result is None:
self.logger.error("SpeechStartedResponse.from_json is None")
continue
await self._emit(
LiveTranscriptionEvents.SpeechStarted,
speech_started=result,
**dict(self.kwargs),
)
case LiveTranscriptionEvents.UtteranceEnd.value:
self.logger.debug(
"response_type: %s, data: %s", response_type, data
Expand Down
14 changes: 14 additions & 0 deletions deepgram/clients/live/v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from .response import (
LiveResultResponse,
MetadataResponse,
SpeechStartedResponse,
UtteranceEndResponse,
ErrorResponse,
)
Expand Down Expand Up @@ -159,6 +160,19 @@ def _listening(self) -> None:
metadata=result,
**dict(self.kwargs),
)
case LiveTranscriptionEvents.SpeechStarted.value:
self.logger.debug(
"response_type: %s, data: %s", response_type, data
)
result = SpeechStartedResponse.from_json(message)
if result is None:
self.logger.error("SpeechStartedResponse.from_json is None")
continue
self._emit(
LiveTranscriptionEvents.SpeechStarted,
speech_started=result,
**dict(self.kwargs),
)
case LiveTranscriptionEvents.UtteranceEnd.value:
self.logger.debug(
"response_type: %s, data: %s", response_type, data
Expand Down
1 change: 1 addition & 0 deletions deepgram/clients/live/v1/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class LiveOptions:
tag: Optional[list] = None
tier: Optional[str] = None
utterance_end_ms: Optional[str] = None
vad_events: Optional[bool] = None
version: Optional[str] = None

def __getitem__(self, key):
Expand Down
19 changes: 19 additions & 0 deletions deepgram/clients/live/v1/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,25 @@ def __getitem__(self, key):
return _dict[key]


# Speech Started Message


@dataclass_json
@dataclass
class SpeechStartedResponse:
"""
SpeechStartedResponse Message from the Deepgram Platform
"""

type: Optional[str] = ""
channel: Optional[List[int]] = None
timestamp: Optional[float] = 0

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


# Utterance End Message


Expand Down
6 changes: 5 additions & 1 deletion examples/advanced/streaming/direct-invocation/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def on_message(self, result, **kwargs):
def on_metadata(self, metadata, **kwargs):
print(f"\n\n{metadata}\n\n")

def on_speech_started(self, speech_started, **kwargs):
print(f"\n\n{speech_started}\n\n")

def on_utterance_end(self, utterance_end, **kwargs):
print(f"\n\n{utterance_end}\n\n")

Expand All @@ -47,11 +50,12 @@ def on_error(self, error, **kwargs):

liveClient.on(LiveTranscriptionEvents.Transcript, on_message)
liveClient.on(LiveTranscriptionEvents.Metadata, on_metadata)
liveClient.on(LiveTranscriptionEvents.SpeechStarted, on_speech_started)
liveClient.on(LiveTranscriptionEvents.UtteranceEnd, on_utterance_end)
liveClient.on(LiveTranscriptionEvents.Error, on_error)

# connect to websocket
options = LiveOptions(model="nova", interim_results=False, language="en-US")
options = LiveOptions(model="nova-2", language="en-US")
liveClient.start(options)

lock_exit = threading.Lock()
Expand Down
7 changes: 7 additions & 0 deletions examples/advanced/streaming/microphone-inheritance/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Microphone,
LiveResultResponse,
MetadataResponse,
SpeechStartedResponse,
UtteranceEndResponse,
ErrorResponse,
)
Expand All @@ -27,6 +28,7 @@ def __init__(self, config: LiveClient):
super().__init__(config)
super().on(LiveTranscriptionEvents.Transcript, self.on_message)
super().on(LiveTranscriptionEvents.Metadata, self.on_metadata)
super().on(LiveTranscriptionEvents.SpeechStarted, self.on_speech_started)
super().on(LiveTranscriptionEvents.UtteranceEnd, self.on_utterance_end)
super().on(LiveTranscriptionEvents.Error, self.on_error)
# self.test = "child"
Expand Down Expand Up @@ -54,6 +56,9 @@ def on_message(self, parent, result, **kwargs):
def on_metadata(self, parent, metadata, **kwargs):
print(f"\n\n{metadata}\n\n")

def on_speech_started(self, parent, speech_started, **kwargs):
print(f"\n\n{speech_started}\n\n")

def on_utterance_end(self, parent, utterance_end, **kwargs):
print(f"\n\n{utterance_end}\n\n")

Expand All @@ -73,6 +78,7 @@ def main():
liveClient = MyLiveClient(ClientOptionsFromEnv())

options = LiveOptions(
model="nova-2",
punctuate=True,
language="en-US",
encoding="linear16",
Expand All @@ -81,6 +87,7 @@ def main():
# To get UtteranceEnd, the following must be set:
interim_results=True,
utterance_end_ms="1000",
vad_events=True,
)
liveClient.start(options, addons=dict(myattr="hello"), test="hello")

Expand Down
9 changes: 6 additions & 3 deletions examples/streaming/async_http/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
API_KEY = os.getenv("DG_API_KEY")

options = LiveOptions(
model="nova",
interim_results=False,
model="nova-2",
language="en-US",
)

Expand All @@ -39,14 +38,18 @@ async def on_message(self, result, **kwargs):
async def on_metadata(self, metadata, **kwargs):
print(f"\n\n{metadata}\n\n")

def on_utterance_end(self, utterance_end, **kwargs):
async def on_speech_started(self, speech_started, **kwargs):
print(f"\n\n{speech_started}\n\n")

async def on_utterance_end(self, utterance_end, **kwargs):
print(f"\n\n{utterance_end}\n\n")

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

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)

Expand Down
6 changes: 5 additions & 1 deletion examples/streaming/http/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def on_message(self, result, **kwargs):
def on_metadata(self, metadata, **kwargs):
print(f"\n\n{metadata}\n\n")

def on_speech_started(self, speech_started, **kwargs):
print(f"\n\n{speech_started}\n\n")

def on_utterance_end(self, utterance_end, **kwargs):
print(f"\n\n{utterance_end}\n\n")

Expand All @@ -50,11 +53,12 @@ def on_error(self, error, **kwargs):

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)

# connect to websocket
options = LiveOptions(model="nova", interim_results=False, language="en-US")
options = LiveOptions(model="nova-2", language="en-US")
dg_connection.start(options)

lock_exit = threading.Lock()
Expand Down
6 changes: 6 additions & 0 deletions examples/streaming/microphone/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def on_message(self, result, **kwargs):
def on_metadata(self, metadata, **kwargs):
print(f"\n\n{metadata}\n\n")

def on_speech_started(self, speech_started, **kwargs):
print(f"\n\n{speech_started}\n\n")

def on_utterance_end(self, utterance_end, **kwargs):
print(f"\n\n{utterance_end}\n\n")

Expand All @@ -47,10 +50,12 @@ def on_error(self, error, **kwargs):

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)

options = LiveOptions(
model="nova-2",
punctuate=True,
language="en-US",
encoding="linear16",
Expand All @@ -59,6 +64,7 @@ def on_error(self, error, **kwargs):
# To get UtteranceEnd, the following must be set:
interim_results=True,
utterance_end_ms="1000",
vad_events=True,
)
dg_connection.start(options, addons=dict(myattr="hello"), test="hello")

Expand Down