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

Update examples latest namespaces #432

Merged
merged 2 commits into from
Jul 11, 2024
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
6 changes: 4 additions & 2 deletions deepgram/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
# speech-to-text
from .clients import LiveClient, AsyncLiveClient # backward compat
from .clients import (
ListenRESTClient,
AsyncListenRESTClient,
ListenWebSocketClient,
AsyncListenWebSocketClient,
)
Expand All @@ -62,6 +60,10 @@
from .clients import (
PreRecordedClient,
AsyncPreRecordedClient,
) # backward compat
from .clients import (
ListenRESTClient,
AsyncListenRESTClient,
davidvonthenen marked this conversation as resolved.
Show resolved Hide resolved
)
from .clients import (
ListenRESTOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from deepgram.utils import verboselogs
import traceback
davidvonthenen marked this conversation as resolved.
Show resolved Hide resolved

from deepgram import ClientOptionsFromEnv, PrerecordedOptions, PreRecordedClient
from deepgram import ClientOptionsFromEnv, PrerecordedOptions, ListenRESTClient

load_dotenv()

Expand All @@ -19,13 +19,13 @@

def main():
try:
# STEP 1 Create a Deepgram PreRecordedClient using a specific config
# STEP 1 Create a Deepgram ListenRESTClient using a specific config
# config: ClientOptionsFromEnv = ClientOptionsFromEnv(
# verbose=verboselogs.NOTICE,
# )
# asyncClient: PreRecordedClient = PreRecordedClient(config)
# asyncClient: ListenRESTClient = ListenRESTClient(config)
# OR just use the default config
asyncClient: PreRecordedClient = PreRecordedClient(ClientOptionsFromEnv())
asyncClient: ListenRESTClient = ListenRESTClient(ClientOptionsFromEnv())

# STEP 2 Call the transcribe_url method on the prerecorded class
options: PrerecordedOptions = PrerecordedOptions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import threading

from deepgram import (
LiveClient,
ListenWebSocketClient,
ClientOptionsFromEnv,
LiveTranscriptionEvents,
LiveOptions,
Expand All @@ -23,13 +23,15 @@

def main():
try:
# STEP 1 Create a Deepgram LiveClient using a specific config
# STEP 1 Create a Deepgram ListenWebSocketClient using a specific config
# config: ClientOptionsFromEnv = ClientOptionsFromEnv(
# verbose=verboselogs.DEBUG, options={"keepalive": "true"}
# )
# liveClient: LiveClient = LiveClient("", config)
# liveClient: ListenWebSocketClient = ListenWebSocketClient("", config)
# OR just use the default config
liveClient: LiveClient = LiveClient(ClientOptionsFromEnv())
liveClient: ListenWebSocketClient = ListenWebSocketClient(
ClientOptionsFromEnv()
)

def on_message(self, result, **kwargs):
sentence = result.channel.alternatives[0].transcript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from deepgram import (
ClientOptionsFromEnv,
LiveTranscriptionEvents,
LiveClient,
ListenWebSocketClient,
LiveOptions,
Microphone,
LiveResultResponse,
Expand All @@ -24,8 +24,8 @@


# more complex example
class MyLiveClient(LiveClient):
def __init__(self, config: LiveClient):
class MyLiveClient(ListenWebSocketClient):
def __init__(self, config: ListenWebSocketClient):
super().__init__(config)
super().on(LiveTranscriptionEvents.Transcript, self.on_message)
super().on(LiveTranscriptionEvents.Metadata, self.on_metadata)
Expand Down
4 changes: 2 additions & 2 deletions examples/speech-to-text/rest/async_url/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
deepgram: DeepgramClient = DeepgramClient(API_KEY)


# STEP 2 Call the transcribe_url method on the prerecorded class
# STEP 2 Call the transcribe_url method on the rest class
async def transcribe_url():
url_response = await deepgram.listen.asyncprerecorded.v("1").transcribe_url(
url_response = await deepgram.listen.asyncrest.v("1").transcribe_url(
AUDIO_URL, options
)
return url_response
Expand Down
6 changes: 3 additions & 3 deletions examples/speech-to-text/rest/callback/callback/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def main():

deepgram: DeepgramClient = DeepgramClient("", config)

# STEP 2 Call the transcribe_file method on the prerecorded class
# STEP 2 Call the transcribe_file method on the rest class
with open(AUDIO_FILE, "rb") as file:
buffer_data = file.read()

Expand All @@ -51,11 +51,11 @@ def main():
utterances=True,
)

response = deepgram.listen.prerecorded.v("1").transcribe_file_callback(
response = deepgram.listen.rest.v("1").transcribe_file_callback(
payload, CALL_BACK_URL, options=options
)
# For URL hosted audio files, comment out the above and uncomment the below
# response = deepgram.listen.prerecorded.v("1").transcribe_url_callback(
# response = deepgram.listen.rest.v("1").transcribe_url_callback(
# payload, CALL_BACK_URL, options=options
# )
print(response.to_json(indent=4))
Expand Down
4 changes: 2 additions & 2 deletions examples/speech-to-text/rest/file/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def main():
# OR use defaults
# deepgram: DeepgramClient = DeepgramClient()

# STEP 2 Call the transcribe_file method on the prerecorded class
# STEP 2 Call the transcribe_file method on the rest class
with open(AUDIO_FILE, "rb") as file:
buffer_data = file.read()

Expand All @@ -48,7 +48,7 @@ def main():
)

before = datetime.now()
response = deepgram.listen.prerecorded.v("1").transcribe_file(
response = deepgram.listen.rest.v("1").transcribe_file(
payload, options, timeout=httpx.Timeout(300.0, connect=10.0)
)
after = datetime.now()
Expand Down
4 changes: 2 additions & 2 deletions examples/speech-to-text/rest/intent/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def main():
# OR use defaults
deepgram: DeepgramClient = DeepgramClient()

# STEP 2 Call the transcribe_file method on the prerecorded class
# STEP 2 Call the transcribe_file method on the rest class
with open(AUDIO_FILE, "rb") as file:
buffer_data = file.read()

Expand All @@ -47,7 +47,7 @@ def main():
)

before = datetime.now()
response = deepgram.listen.prerecorded.v("1").transcribe_file(payload, options)
response = deepgram.listen.rest.v("1").transcribe_file(payload, options)
after = datetime.now()

print(response.to_json(indent=4))
Expand Down
4 changes: 2 additions & 2 deletions examples/speech-to-text/rest/legacy_dict_url/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ def main():
# STEP 1 Create a Deepgram client using the API key from environment variables
deepgram = DeepgramClient("", ClientOptionsFromEnv())

# STEP 2 Call the transcribe_url method on the prerecorded class
# STEP 2 Call the transcribe_url method on the rest class
options = {
"mode": "nova-2",
"smart_format": True,
}
response = deepgram.listen.prerecorded.v("1").transcribe_url(AUDIO_URL, options)
response = deepgram.listen.rest.v("1").transcribe_url(AUDIO_URL, options)
print(f"response: {response}\n\n")
# print(f"metadata: {response['metadata']}\n\n")
# print(
Expand Down
4 changes: 2 additions & 2 deletions examples/speech-to-text/rest/sentiment/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def main():
# OR use defaults
deepgram: DeepgramClient = DeepgramClient()

# STEP 2 Call the transcribe_file method on the prerecorded class
# STEP 2 Call the transcribe_file method on the rest class
with open(AUDIO_FILE, "rb") as file:
buffer_data = file.read()

Expand All @@ -47,7 +47,7 @@ def main():
)

before = datetime.now()
response = deepgram.listen.prerecorded.v("1").transcribe_file(payload, options)
response = deepgram.listen.rest.v("1").transcribe_file(payload, options)
after = datetime.now()

print(response.to_json(indent=4))
Expand Down
25 changes: 10 additions & 15 deletions examples/speech-to-text/rest/stream_file/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,16 @@ def main():
# OR use defaults
# deepgram = DeepgramClient()

# STEP 2 Call the transcribe_file method on the prerecorded class
stream = open(AUDIO_FILE, "rb")

payload: StreamSource = {
"stream": stream,
}

options = PrerecordedOptions(
model="nova-2",
)

response = deepgram.listen.prerecorded.v("1").transcribe_file(payload, options)
print(response.to_json(indent=4))

stream.close()
# STEP 2 Call the transcribe_file method on the rest class
with open(AUDIO_FILE, "rb") as stream:
payload: StreamSource = {
"stream": stream,
}
options = PrerecordedOptions(
model="nova-2",
)
response = deepgram.listen.rest.v("1").transcribe_file(payload, options)
print(response.to_json(indent=4))

except Exception as e:
print(f"Exception: {e}")
Expand Down
4 changes: 2 additions & 2 deletions examples/speech-to-text/rest/summary/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def main():
# OR use defaults
deepgram: DeepgramClient = DeepgramClient()

# STEP 2 Call the transcribe_file method on the prerecorded class
# STEP 2 Call the transcribe_file method on the rest class
with open(AUDIO_FILE, "rb") as file:
buffer_data = file.read()

Expand All @@ -47,7 +47,7 @@ def main():
)

before = datetime.now()
response = deepgram.listen.prerecorded.v("1").transcribe_file(payload, options)
response = deepgram.listen.rest.v("1").transcribe_file(payload, options)
after = datetime.now()

print(response.to_json(indent=4))
Expand Down
4 changes: 2 additions & 2 deletions examples/speech-to-text/rest/topic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def main():
# OR use defaults
deepgram: DeepgramClient = DeepgramClient()

# STEP 2 Call the transcribe_file method on the prerecorded class
# STEP 2 Call the transcribe_file method on the rest class
with open(AUDIO_FILE, "rb") as file:
buffer_data = file.read()

Expand All @@ -47,7 +47,7 @@ def main():
)

before = datetime.now()
response = deepgram.listen.prerecorded.v("1").transcribe_file(payload, options)
response = deepgram.listen.rest.v("1").transcribe_file(payload, options)
after = datetime.now()

print(response.to_json(indent=4))
Expand Down
4 changes: 2 additions & 2 deletions examples/speech-to-text/rest/url/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ def main():
# STEP 1 Create a Deepgram client using the API key from environment variables
deepgram: DeepgramClient = DeepgramClient("", ClientOptionsFromEnv())

# STEP 2 Call the transcribe_url method on the prerecorded class
# STEP 2 Call the transcribe_url method on the rest class
options: PrerecordedOptions = PrerecordedOptions(
model="nova-2",
smart_format=True,
)
response = deepgram.listen.prerecorded.v("1").transcribe_url(AUDIO_URL, options)
response = deepgram.listen.rest.v("1").transcribe_url(AUDIO_URL, options)
print(f"response: {response}\n\n")
# print(f"metadata: {response['metadata']}\n\n")
# print(
Expand Down
2 changes: 1 addition & 1 deletion examples/speech-to-text/websocket/async_http/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async def main():
lambda: asyncio.create_task(shutdown(signal, loop, dg_connection)),
)

dg_connection = deepgram.listen.asynclive.v("1")
dg_connection = deepgram.listen.asyncwebsocket.v("1")

async def on_open(self, open, **kwargs):
print(f"\n\n{open}\n\n")
Expand Down
10 changes: 5 additions & 5 deletions examples/speech-to-text/websocket/async_microphone/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ async def main():
# otherwise, use default config
# deepgram: DeepgramClient = DeepgramClient()

dg_connection = deepgram.listen.asynclive.v("1")
dg_connection = deepgram.listen.asyncwebsocket.v("1")

async def on_open(self, open, **kwargs):
print(f"Connection Open")
print("Connection Open")

async def on_message(self, result, **kwargs):
global is_finals
Expand Down Expand Up @@ -75,18 +75,18 @@ async def on_metadata(self, metadata, **kwargs):
print(f"Metadata: {metadata}")

async def on_speech_started(self, speech_started, **kwargs):
print(f"Speech Started")
print("Speech Started")

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

async def on_close(self, close, **kwargs):
print(f"Connection Closed")
print("Connection Closed")

async def on_error(self, error, **kwargs):
print(f"Handled Error: {error}")
Expand Down
2 changes: 1 addition & 1 deletion examples/speech-to-text/websocket/http/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def main():
deepgram: DeepgramClient = DeepgramClient()

# Create a websocket connection to Deepgram
dg_connection = deepgram.listen.live.v("1")
dg_connection = deepgram.listen.websocket.v("1")

def on_open(self, open, **kwargs):
print(f"\n\n{open}\n\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def main():
# otherwise, use default config
deepgram = DeepgramClient()

dg_connection = deepgram.listen.live.v("1")
dg_connection = deepgram.listen.websocket.v("1")

def on_open(self, open, **kwargs):
print(f"\n\n{open}\n\n")
Expand Down
10 changes: 5 additions & 5 deletions examples/speech-to-text/websocket/microphone/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ def main():
# otherwise, use default config
deepgram: DeepgramClient = DeepgramClient()

dg_connection = deepgram.listen.live.v("1")
dg_connection = deepgram.listen.websocket.v("1")

def on_open(self, open, **kwargs):
print(f"Connection Open")
print("Connection Open")

def on_message(self, result, **kwargs):
global is_finals
Expand Down Expand Up @@ -63,18 +63,18 @@ def on_metadata(self, metadata, **kwargs):
print(f"Metadata: {metadata}")

def on_speech_started(self, speech_started, **kwargs):
print(f"Speech Started")
print("Speech Started")

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

def on_close(self, close, **kwargs):
print(f"Connection Closed")
print("Connection Closed")

def on_error(self, error, **kwargs):
print(f"Handled Error: {error}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async def main():
model="aura-asteria-en",
)

response = await deepgram.asyncspeak.v("1").save(
response = await deepgram.speak.asyncrest.v("1").save(
filename, SPEAK_OPTIONS, options
)
print(response.to_json(indent=4))
Expand Down
2 changes: 1 addition & 1 deletion examples/text-to-speech/rest/file/hello_world/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def main():
model="aura-asteria-en",
)

response = deepgram.speak.v("1").save(filename, SPEAK_OPTIONS, options)
response = deepgram.speak.rest.v("1").save(filename, SPEAK_OPTIONS, options)
print(response.to_json(indent=4))

except Exception as e:
Expand Down
Loading
Loading