generated from deepgram/oss-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c44eb48
commit 7b35fa6
Showing
2 changed files
with
174 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# Copyright 2023-2024 Deepgram SDK contributors. All Rights Reserved. | ||
# Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
# SPDX-License-Identifier: MIT | ||
|
||
from dotenv import load_dotenv | ||
import logging, verboselogs | ||
from time import sleep | ||
|
||
from deepgram import ( | ||
DeepgramClient, | ||
DeepgramClientOptions, | ||
LiveTranscriptionEvents, | ||
LiveOptions, | ||
Microphone, | ||
) | ||
|
||
load_dotenv() | ||
|
||
|
||
def main(): | ||
try: | ||
# example of setting up a client config. logging values: WARNING, VERBOSE, DEBUG, SPAM | ||
# config = DeepgramClientOptions( | ||
# verbose=logging.DEBUG, options={"keepalive": "true"} | ||
# ) | ||
# deepgram: DeepgramClient = DeepgramClient("", config) | ||
# otherwise, use default config | ||
deepgram: DeepgramClient = DeepgramClient() | ||
|
||
# create the microphone | ||
microphone = Microphone() | ||
|
||
dg_connection = deepgram.listen.live.v("1") | ||
|
||
def on_open(self, open, **kwargs): | ||
print(f"\n\n{open}\n\n") | ||
|
||
def on_message(self, result, **kwargs): | ||
sentence = result.channel.alternatives[0].transcript | ||
if len(sentence) == 0: | ||
return | ||
microphone.mute() | ||
print(f"speaker: {sentence}") | ||
microphone.unmute() | ||
|
||
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") | ||
|
||
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") | ||
|
||
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) | ||
|
||
options: LiveOptions = LiveOptions( | ||
model="nova-2", | ||
punctuate=True, | ||
language="en-US", | ||
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, | ||
) | ||
dg_connection.start(options) | ||
|
||
# set the callback on the microphone | ||
microphone.set_callback(dg_connection.send) | ||
|
||
# start microphone | ||
microphone.start() | ||
|
||
# wait until finished | ||
input("Press Enter to mute microphone...\n\n") | ||
|
||
microphone.mute() | ||
|
||
# wait until finished | ||
input("Press Enter to unmute microphone...\n\n") | ||
|
||
microphone.unmute() | ||
|
||
# wait until finished | ||
input("Press Enter to stop recording...\n\n") | ||
|
||
# Wait for the microphone to close | ||
microphone.finish() | ||
|
||
# Indicate that we've finished | ||
dg_connection.finish() | ||
|
||
print("Finished") | ||
# sleep(30) # wait 30 seconds to see if there is any additional socket activity | ||
# print("Really done!") | ||
|
||
except Exception as e: | ||
print(f"Could not open socket: {e}") | ||
return | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |