Skip to content

Commit 83d5f03

Browse files
Merge pull request #386 from dvonthenen/update-example-always-utterance
Update Examples to Always Display UtteranceEnd Even If Not Using to Process Transcription
2 parents 5af50ad + 3a29e00 commit 83d5f03

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

examples/streaming/async_microphone/main.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# We will collect the is_final=true messages here so we can use them when the person finishes speaking
2222
is_finals = []
2323

24+
2425
async def main():
2526
try:
2627
loop = asyncio.get_event_loop()
@@ -44,7 +45,7 @@ async def main():
4445
dg_connection = deepgram.listen.asynclive.v("1")
4546

4647
async def on_open(self, open, **kwargs):
47-
print(f"Deepgram Connection Open")
48+
print(f"Connection Open")
4849

4950
async def on_message(self, result, **kwargs):
5051
global is_finals
@@ -59,7 +60,7 @@ async def on_message(self, result, **kwargs):
5960
# Speech Final means we have detected sufficent silence to consider this end of speech
6061
# Speech final is the lowest latency result as it triggers as soon an the endpointing value has triggered
6162
if result.speech_final:
62-
utterance = ' '.join(is_finals)
63+
utterance = " ".join(is_finals)
6364
print(f"Speech Final: {utterance}")
6465
is_finals = []
6566
else:
@@ -70,26 +71,27 @@ async def on_message(self, result, **kwargs):
7071
print(f"Interim Results: {sentence}")
7172

7273
async def on_metadata(self, metadata, **kwargs):
73-
print(f"Deepgram Metadata: {metadata}")
74+
print(f"Metadata: {metadata}")
7475

7576
async def on_speech_started(self, speech_started, **kwargs):
76-
print(f"Deepgram Speech Started")
77+
print(f"Speech Started")
7778

7879
async def on_utterance_end(self, utterance_end, **kwargs):
80+
print(f"Utterance End")
7981
global is_finals
8082
if len(is_finals) > 0:
81-
utterance = ' '.join(is_finals)
82-
print(f"Deepgram Utterance End: {utterance}")
83+
utterance = " ".join(is_finals)
84+
print(f"Utterance End: {utterance}")
8385
is_finals = []
8486

8587
async def on_close(self, close, **kwargs):
86-
print(f"Deepgram Connection Closed")
88+
print(f"Connection Closed")
8789

8890
async def on_error(self, error, **kwargs):
89-
print(f"Deepgram Handled Error: {error}")
91+
print(f"Handled Error: {error}")
9092

9193
async def on_unhandled(self, unhandled, **kwargs):
92-
print(f"Deepgram Unhandled Websocket Message: {unhandled}")
94+
print(f"Unhandled Websocket Message: {unhandled}")
9395

9496
dg_connection.on(LiveTranscriptionEvents.Open, on_open)
9597
dg_connection.on(LiveTranscriptionEvents.Transcript, on_message)
@@ -115,7 +117,7 @@ async def on_unhandled(self, unhandled, **kwargs):
115117
utterance_end_ms="1000",
116118
vad_events=True,
117119
# Time in milliseconds of silence to wait for before finalizing speech
118-
endpointing=300
120+
endpointing=300,
119121
)
120122

121123
addons = {

examples/streaming/microphone/main.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# We will collect the is_final=true messages here so we can use them when the person finishes speaking
2020
is_finals = []
2121

22+
2223
def main():
2324
try:
2425
# example of setting up a client config. logging values: WARNING, VERBOSE, DEBUG, SPAM
@@ -32,7 +33,7 @@ def main():
3233
dg_connection = deepgram.listen.live.v("1")
3334

3435
def on_open(self, open, **kwargs):
35-
print(f"Deepgram Connection Open")
36+
print(f"Connection Open")
3637

3738
def on_message(self, result, **kwargs):
3839
global is_finals
@@ -47,7 +48,7 @@ def on_message(self, result, **kwargs):
4748
# Speech Final means we have detected sufficent silence to consider this end of speech
4849
# Speech final is the lowest latency result as it triggers as soon an the endpointing value has triggered
4950
if result.speech_final:
50-
utterance = ' '.join(is_finals)
51+
utterance = " ".join(is_finals)
5152
print(f"Speech Final: {utterance}")
5253
is_finals = []
5354
else:
@@ -58,26 +59,27 @@ def on_message(self, result, **kwargs):
5859
print(f"Interim Results: {sentence}")
5960

6061
def on_metadata(self, metadata, **kwargs):
61-
print(f"Deepgram Metadata: {metadata}")
62+
print(f"Metadata: {metadata}")
6263

6364
def on_speech_started(self, speech_started, **kwargs):
64-
print(f"Deepgram Speech Started")
65+
print(f"Speech Started")
6566

6667
def on_utterance_end(self, utterance_end, **kwargs):
68+
print(f"Utterance End")
6769
global is_finals
6870
if len(is_finals) > 0:
69-
utterance = ' '.join(is_finals)
70-
print(f"Deepgram Utterance End: {utterance}")
71+
utterance = " ".join(is_finals)
72+
print(f"Utterance End: {utterance}")
7173
is_finals = []
7274

7375
def on_close(self, close, **kwargs):
74-
print(f"Deepgram Connection Closed")
76+
print(f"Connection Closed")
7577

7678
def on_error(self, error, **kwargs):
77-
print(f"Deepgram Handled Error: {error}")
79+
print(f"Handled Error: {error}")
7880

7981
def on_unhandled(self, unhandled, **kwargs):
80-
print(f"Deepgram Unhandled Websocket Message: {unhandled}")
82+
print(f"Unhandled Websocket Message: {unhandled}")
8183

8284
dg_connection.on(LiveTranscriptionEvents.Open, on_open)
8385
dg_connection.on(LiveTranscriptionEvents.Transcript, on_message)
@@ -102,7 +104,7 @@ def on_unhandled(self, unhandled, **kwargs):
102104
utterance_end_ms="1000",
103105
vad_events=True,
104106
# Time in milliseconds of silence to wait for before finalizing speech
105-
endpointing=300
107+
endpointing=300,
106108
)
107109

108110
addons = {

0 commit comments

Comments
 (0)