Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle is_final and endpointing together with utterance end + clean u… #364

Merged
merged 7 commits into from
Apr 15, 2024
41 changes: 32 additions & 9 deletions examples/streaming/microphone/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

load_dotenv()

is_finals = []

def main():
try:
Expand All @@ -30,31 +31,47 @@ def main():
dg_connection = deepgram.listen.live.v("1")

def on_open(self, open, **kwargs):
print(f"\n\n{open}\n\n")
print(f"Deepgram Connection Open")

def on_message(self, result, **kwargs):
global is_finals
sentence = result.channel.alternatives[0].transcript
if len(sentence) == 0:
return
print(f"speaker: {sentence}")
if result.is_final:
is_finals.append(sentence)
if result.speech_final:
utterance = ' '.join(is_finals)
print(f"Speech Final: {utterance}")
is_finals = []
else:
# These are useful if you need real time captioning and update what the Interim Results produced
print(f"Is Final: {sentence}")
else:
# These are useful if you need real time captioning of what is being spoken
print(f"Interim Results: {sentence}")

def on_metadata(self, metadata, **kwargs):
print(f"\n\n{metadata}\n\n")
print(f"Deepgram Metadata: {metadata}")

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

def on_utterance_end(self, utterance_end, **kwargs):
print(f"\n\n{utterance_end}\n\n")
global is_finals
if len(is_finals) > 0:
utterance = ' '.join(is_finals)
print(f"Deepgram Utterance End: {utterance}")
is_finals = []

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

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

def on_unhandled(self, unhandled, **kwargs):
print(f"\n\n{unhandled}\n\n")
print(f"Deepgram Unhandled Error: {unhandled}")
davidvonthenen marked this conversation as resolved.
Show resolved Hide resolved

dg_connection.on(LiveTranscriptionEvents.Open, on_open)
dg_connection.on(LiveTranscriptionEvents.Transcript, on_message)
Expand All @@ -67,15 +84,21 @@ def on_unhandled(self, unhandled, **kwargs):

options: LiveOptions = LiveOptions(
model="nova-2",
punctuate=True,
language="en-US",
# Apply smart formatting to the output
smart_format=True,
# Raw audio format deatils
encoding="linear16",
channels=1,
sample_rate=16000,
# To get UtteranceEnd, the following must be set:
interim_results=True,
utterance_end_ms="1000",
vad_events=True,
# Time in milliseconds of silence to wait for before finalizing speech
endpointing=300,
# Prevent waiting for additional numbers
# no_delay=True
)

print("\n\nPress Enter to stop recording...\n\n")
Expand Down
Loading