From 2d1e2fed2d6f475d51a89dc08059506a6c468399 Mon Sep 17 00:00:00 2001 From: jkroll-deepgram Date: Fri, 11 Aug 2023 10:17:41 -0500 Subject: [PATCH 01/13] Add setup section to readme to install test packages --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d137276c..8f2947a9 100644 --- a/README.md +++ b/README.md @@ -146,11 +146,20 @@ In the `sample-projects` folder, there are examples from four different Python w ## Testing +### Setup + +``` +pip install pytest +pip install pytest-cov +``` + +### Run All Tests + ``` pytest --api-key tests/ ``` -## Test Coverage Report +### Test Coverage Report ``` pytest --cov=deepgram --api-key tests/ From 15fefcd2cfe274560fa69f76dc143230899c0962 Mon Sep 17 00:00:00 2001 From: Saarth Shah <69341449+SaarthShah@users.noreply.github.com> Date: Mon, 14 Aug 2023 14:20:03 -0400 Subject: [PATCH 02/13] added tests for billing and projects --- tests/test_billing.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 tests/test_billing.py diff --git a/tests/test_billing.py b/tests/test_billing.py new file mode 100644 index 00000000..fcecfc09 --- /dev/null +++ b/tests/test_billing.py @@ -0,0 +1,33 @@ +import pytest +import pytest_asyncio # Required to ensure pytest-asyncio is installed +from deepgram import Deepgram + + +api_key = pytest.api_key +assert api_key, "Pass Deepgram API key as an argument: `pytest --api-key tests/`" + +deepgram = Deepgram(api_key) + + +@pytest.mark.asyncio # Requires pytest-asyncio to be installed +async def test_projects_and_billing(): + """ + Test to ensure that projects and billing are working as expected. + """ + projects = await deepgram.projects.list() + assert projects['projects'] is not None + if len(projects['projects']) > 0: + project = projects['projects'][0] + project_id = project['project_id'] + response = await deepgram.billing.list_balance(project_id) + assert 'balances' in response + balances = response['balances'] + assert type(balances) == list + balance_id = balances[0]['balance_id'] + amount = balances[0]['amount'] + units = balances[0]['units'] + purchase_order_id = balances[0]['purchase_order_id'] + assert type(balance_id) == str + assert type(amount) == float + assert type(units) == str + assert type(purchase_order_id) == str \ No newline at end of file From 5737507b3618a4a696e7d736a4bad3a73697555a Mon Sep 17 00:00:00 2001 From: Sandra Rodgers Date: Wed, 23 Aug 2023 16:00:33 -0500 Subject: [PATCH 03/13] add examples folder for local testing and quickstarts --- README.md | 21 +++++- deepgram/transcription.py | 50 ++++++------- examples/README.md | 54 ++++++++++++++ examples/prerecorded/main.py | 72 +++++++++++++++++++ examples/prerecorded/requirements.txt | 1 + examples/streaming/main.py | 54 ++++++++++++++ examples/streaming/requirements.txt | 3 + .../prerecorded-audio/Quickstart/README.md | 13 ++++ .../prerecorded-audio/Quickstart/main.py | 71 ++++++++++++++++++ .../Quickstart/requirements.txt | 2 + .../streaming-audio/Quickstart/README.md | 13 ++++ .../streaming-audio/Quickstart/main.py | 54 ++++++++++++++ .../Quickstart/requirements.txt | 3 + 13 files changed, 385 insertions(+), 26 deletions(-) create mode 100644 examples/README.md create mode 100644 examples/prerecorded/main.py create mode 100644 examples/prerecorded/requirements.txt create mode 100644 examples/streaming/main.py create mode 100644 examples/streaming/requirements.txt create mode 100644 sample-projects/prerecorded-audio/Quickstart/README.md create mode 100644 sample-projects/prerecorded-audio/Quickstart/main.py create mode 100644 sample-projects/prerecorded-audio/Quickstart/requirements.txt create mode 100644 sample-projects/streaming-audio/Quickstart/README.md create mode 100644 sample-projects/streaming-audio/Quickstart/main.py create mode 100644 sample-projects/streaming-audio/Quickstart/requirements.txt diff --git a/README.md b/README.md index 8f2947a9..9f845dfe 100644 --- a/README.md +++ b/README.md @@ -135,15 +135,25 @@ response = await dg_client.transcription.prerecorded(source, punctuate=True, key To run the sample code, you may want to create a virtual environment to isolate your Python projects, but it's not required. You can learn how to make and activate these virtual environments in [this article](https://blog.deepgram.com/python-virtual-environments/) on our Deepgram blog. +#### Prerecorded Audio Code Samples + +In the `sample-projects/prerecorded-audio` folder, there is a quickstart project to get you quickly started transcribing prerecorded audio (from a local file or a remote file) with Deepgram. + +- Quickstart + #### Streaming Audio Code Samples -In the `sample-projects` folder, there are examples from four different Python web frameworks of how to do live streaming audio transcription with Deepgram. These include: +In the `sample-projects/streaming-audio` folder, there are examples from four different Python web frameworks of how to do live streaming audio transcription with Deepgram. These include: - Flask 2.0 - FastAPI - Django - Quart +There is also a Quickstart that you can use to get up and running without a Python web framework. + +- Quickstart + ## Testing ### Setup @@ -165,6 +175,15 @@ pytest --api-key tests/ pytest --cov=deepgram --api-key tests/ ``` +### Using Example Projects to test features + +Use the example projects to test features if there aren't currently the appropriate tests for your use case. Go to the folder `examples` and look for these two projects, which can be used to test out features in the Deepgram Python SDK. + +- prerecorded +- streaming + +These are standalone projects, so you will need to follow the instructions in the `README.md` files for each project to get it running. + ## Development and Contributing Interested in contributing? We ❤️ pull requests! diff --git a/deepgram/transcription.py b/deepgram/transcription.py index 002173d0..b0225c8e 100644 --- a/deepgram/transcription.py +++ b/deepgram/transcription.py @@ -41,13 +41,13 @@ async def __call__( as a function. This is useful for creating instances of the class, where we can call `PrerecordedTranscription()` and pass in arguments to set up an instance of the class. For example: - + prerecorded_transcription = PrerecordedTranscription(...) - + :param source:TranscriptionSource: Used to Pass in the audio file. :param timeout:float: (optional) The request timeout (if not set, defaults to `aiohttp`'s default timeout) :return: A `PrerecordedTranscriptionResponse` object, which contains the transcription results. - + """ if 'buffer' in source and 'mimetype' not in source: @@ -92,21 +92,20 @@ def __init__(self, options: Options, def __call__( self, source: TranscriptionSource, timeout: float = None ) -> PrerecordedTranscriptionResponse: - """ The __call__ function is a special method that allows the class to be called as a function. This is useful for creating instances of the class, where we can call `SyncPrerecordedTranscription()` and pass in arguments to set up an instance of the class. For example: - + sync_prerecorded_transcription = SyncPrerecordedTranscription(...) - + :param source:TranscriptionSource: Used to Pass in the audio file. :param timeout:float: (optional) The request timeout, excluding the upload time of the audio file. :return: A `prerecordedtranscriptionresponse` object, which contains the transcription results. - + """ - + if 'buffer' in source and 'mimetype' not in source: raise DeepgramApiError( 'Mimetype must be provided if the source is bytes', @@ -142,11 +141,11 @@ def __init__(self, options: Options, It initializes all of the attributes that are part of the object, and can be accessed using "self." notation. In this case, it sets up a list to store any messages received from Transcribe Streaming. - + :param options:Options: Used to Pass the options for the transcription job. :param transcription_options:LiveOptions: Used to Pass in the configuration for the transcription job. :return: None. - + """ self.options = options @@ -167,9 +166,9 @@ async def __call__(self) -> 'LiveTranscription': as a function. In this case, it is used to connect the client and start the transcription process. It returns itself after starting so that operations can be chained. - + :return: The object itself. - + """ self._socket = await _socket_connect( f'{self._root}{_make_query_string(self.transcription_options)}', @@ -182,11 +181,11 @@ async def _start(self) -> None: """ The _start function is the main function of the LiveTranscription class. It is responsible for creating a websocket connection to Deepgram Transcribe, - and then listening for incoming messages from that socket. It also sends any - messages that are in its queue (which is populated by other functions). The - _start function will run until it receives a message with an empty transcription, + and then listening for incoming messages from that socket. It also sends any + messages that are in its queue (which is populated by other functions). The + _start function will run until it receives a message with an empty transcription, at which point it will close the socket and return. - + :return: None. """ @@ -214,7 +213,7 @@ async def _start(self) -> None: parsed ) self.received.append(parsed) - if 'sha256' in parsed: + if 'sha256' in parsed: self.done = True except json.decoder.JSONDecodeError: self._ping_handlers( @@ -233,7 +232,7 @@ async def _receiver(self) -> None: The _receiver function is a coroutine that receives messages from the socket and puts them in a queue. It is started by calling start_receiver() on an instance of AsyncSocket. It runs until the socket is closed, or until an exception occurs. - + :return: None. """ @@ -243,7 +242,7 @@ async def _receiver(self) -> None: body = await self._socket.recv() self._queue.put_nowait((True, body)) except Exception as exc: - self.done = True # socket closed, will terminate on next loop + self.done = True # socket closed, will terminate on next loop def _ping_handlers(self, event_type: LiveTranscriptionEvent, body: Any) -> None: @@ -251,13 +250,13 @@ def _ping_handlers(self, event_type: LiveTranscriptionEvent, The _ping_handlers function is a callback that is called when the transcription service sends a ping event. It calls all of the functions in self.handlers, which are registered by calling add_ping_handler(). - + :param event_type:LiveTranscriptionEvent: Used to Determine if the function should be called. :param body:Any: Used to Pass the event data to the handler function. :return: The list of handlers for the event type. """ - + for handled_type, func in self.handlers: if handled_type is event_type: if inspect.iscoroutinefunction(func): @@ -323,7 +322,8 @@ async def finish(self) -> None: """Closes the connection to the Deepgram endpoint, waiting until ASR is complete on all submitted data.""" - self.send(json.dumps({"type": "CloseStream"})) # Set message for "data is finished sending" + # Set message for "data is finished sending" + self.send(json.dumps({"type": "CloseStream"})) while not self.done: await asyncio.sleep(0.1) @@ -348,7 +348,7 @@ def __init__(self, options: Options) -> None: async def prerecorded( self, source: TranscriptionSource, options: PrerecordedOptions = None, - endpoint = "/listen", + endpoint="/listen", timeout: float = None, **kwargs ) -> PrerecordedTranscriptionResponse: @@ -364,7 +364,7 @@ async def prerecorded( def sync_prerecorded( self, source: TranscriptionSource, options: PrerecordedOptions = None, - endpoint = "/listen", + endpoint="/listen", timeout: float = None, **kwargs ) -> PrerecordedTranscriptionResponse: @@ -378,7 +378,7 @@ def sync_prerecorded( )(source, timeout=timeout) async def live( - self, options: LiveOptions = None, endpoint = "/listen", **kwargs + self, options: LiveOptions = None, endpoint="/listen", **kwargs ) -> LiveTranscription: """Provides a client to send raw audio data to be transcribed.""" if options is None: diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 00000000..9225432b --- /dev/null +++ b/examples/README.md @@ -0,0 +1,54 @@ +# Examples Folder - Test Features Locally + +The example projects are meant to be used to test features locally by contributors working on this SDK. In the `requirements.txt` file, the deepgram package used is the local version: + +``` +-e ../../ +``` + +## Steps to Test Your Code + +If you are contributing changes to this SDK, you can test those changes by using the `prerecorded` or `streaming` projects in the `examples` folder. Here are the steps to follow: + +1. Make your changes to the SDK (be sure you are on a branch you have created to do this work). + +2. Install dependencies + +`pip install -r requirements.txt` + +Replace the API key where it says 'YOUR_DEEPGRAM_API_KEY' + +`DEEPGRAM_API_KEY = 'YOUR_DEEPGRAM_API_KEY'` + +Make sure you're in the directory with the `main.py` file and run the project with the following command. + +`python3 main.py` + +## How to verify that you're testing the local changes + +If you want to be sure that you are testing the local `deepgram` package, you can run this check. + +### Step 1 + +Launch the Python interpreter by typing `python` in the terminal. Make sure you are in the folder with the `main.py` file you will be using to run the test. + +``` +python +``` + +### Step 2 + +Inside the interpreter, run the following code. This will import the `importlib` modules and use `find_spec()` to determine the location of the imported module. + +```py +import importlib.util + +spec = importlib.util.find_spec("deepgram") +if spec is not None: + print("Module 'deepgram' is imported from:", spec.origin) +else: + print("Module 'deepgram' is not found.") + +``` + +This code checks whether the module named "deepgram" is imported and, if so, prints its origin (i.e., the location from where it's imported). diff --git a/examples/prerecorded/main.py b/examples/prerecorded/main.py new file mode 100644 index 00000000..db229b53 --- /dev/null +++ b/examples/prerecorded/main.py @@ -0,0 +1,72 @@ +# Example filename: deepgram_test.py + +import json +import asyncio + +from deepgram import Deepgram + +# Your Deepgram API Key +# DEEPGRAM_API_KEY = 'YOUR_DEEPGRAM_API_KEY' +DEEPGRAM_API_KEY = '' + +# Location of the file you want to transcribe. Should include filename and extension. +# Example of a local file: ../../Audio/life-moves-pretty-fast.wav +# Example of a remote file: https://static.deepgram.com/examples/interview_speech-analytics.wav +FILE = 'https://static.deepgram.com/examples/interview_speech-analytics.wav' + +# Mimetype for the file you want to transcribe +# Include this line only if transcribing a local file +# Example: audio/wav +MIMETYPE = 'audio/mpeg' + + +async def main(): + + # Initialize the Deepgram SDK + deepgram = Deepgram(DEEPGRAM_API_KEY) + + # Check whether requested file is local or remote, and prepare source + if FILE.startswith('http'): + # file is remote + # Set the source + source = { + 'url': FILE + } + else: + # file is local + # Open the audio file + audio = open(FILE, 'rb') + + # Set the source + source = { + 'buffer': audio, + 'mimetype': MIMETYPE + } + + # Send the audio to Deepgram and get the response + response = await asyncio.create_task( + deepgram.transcription.prerecorded( + source, + { + 'detect_language': "true", + 'summarize': "v2", + } + ) + ) + + # Write the response to the console + print(json.dumps(response, indent=4)) + + # Write only the transcript to the console + # print(response["results"]["channels"][0]["alternatives"][0]["transcript"]) + + # print(response["results"]["channels"]) + +try: + # If running in a Jupyter notebook, Jupyter is already running an event loop, so run main with this line instead: + # await main() + asyncio.run(main()) +except Exception as e: + exception_type, exception_object, exception_traceback = sys.exc_info() + line_number = exception_traceback.tb_lineno + print(f'line {line_number}: {exception_type} - {e}') diff --git a/examples/prerecorded/requirements.txt b/examples/prerecorded/requirements.txt new file mode 100644 index 00000000..ab70bdb3 --- /dev/null +++ b/examples/prerecorded/requirements.txt @@ -0,0 +1 @@ +-e ../../ diff --git a/examples/streaming/main.py b/examples/streaming/main.py new file mode 100644 index 00000000..4f1ebbd8 --- /dev/null +++ b/examples/streaming/main.py @@ -0,0 +1,54 @@ +# Example filename: deepgram_test.py + +from deepgram import Deepgram +import asyncio +import aiohttp + +# Your Deepgram API Key +DEEPGRAM_API_KEY = '' + +# URL for the realtime streaming audio you would like to transcribe +URL = 'http://stream.live.vc.bbcmedia.co.uk/bbc_world_service' + + +async def main(): + # Initialize the Deepgram SDK + deepgram = Deepgram(DEEPGRAM_API_KEY) + + # Create a websocket connection to Deepgram + # In this example, punctuation is turned on, interim results are turned off, and language is set to UK English. + try: + deepgramLive = await deepgram.transcription.live({ + 'smart_format': True, + 'interim_results': False, + 'language': 'en-US', + 'model': 'nova', + }) + except Exception as e: + print(f'Could not open socket: {e}') + return + + # Listen for the connection to close + deepgramLive.registerHandler(deepgramLive.event.CLOSE, lambda c: print( + f'Connection closed with code {c}.')) + + # Listen for any transcripts received from Deepgram and write them to the console + deepgramLive.registerHandler(deepgramLive.event.TRANSCRIPT_RECEIVED, print) + + # Listen for the connection to open and send streaming audio from the URL to Deepgram + async with aiohttp.ClientSession() as session: + async with session.get(URL) as audio: + while True: + data = await audio.content.readany() + deepgramLive.send(data) + + # If no data is being sent from the live stream, then break out of the loop. + if not data: + break + + # Indicate that we've finished sending data by sending the customary zero-byte message to the Deepgram streaming endpoint, and wait until we get back the final summary metadata object + await deepgramLive.finish() + +# If running in a Jupyter notebook, Jupyter is already running an event loop, so run main with this line instead: +# await main() +asyncio.run(main()) diff --git a/examples/streaming/requirements.txt b/examples/streaming/requirements.txt new file mode 100644 index 00000000..d2a44749 --- /dev/null +++ b/examples/streaming/requirements.txt @@ -0,0 +1,3 @@ +-e ../../ +asyncio +aiohttp \ No newline at end of file diff --git a/sample-projects/prerecorded-audio/Quickstart/README.md b/sample-projects/prerecorded-audio/Quickstart/README.md new file mode 100644 index 00000000..dc7fc739 --- /dev/null +++ b/sample-projects/prerecorded-audio/Quickstart/README.md @@ -0,0 +1,13 @@ +# Prerecorded Audio Transcription + +To run this example, first install the dependencies in the requirements.txt file. + +`pip install -r requirements.txt` + +Replace the API key where it says 'YOUR_DEEPGRAM_API_KEY' + +`DEEPGRAM_API_KEY = 'YOUR_DEEPGRAM_API_KEY'` + +Make sure you're in the directory with the `main.py` file and run the project with the following command. + +`python3 main.py` diff --git a/sample-projects/prerecorded-audio/Quickstart/main.py b/sample-projects/prerecorded-audio/Quickstart/main.py new file mode 100644 index 00000000..11848d00 --- /dev/null +++ b/sample-projects/prerecorded-audio/Quickstart/main.py @@ -0,0 +1,71 @@ +# Example filename: deepgram_test.py + +from deepgram import Deepgram +import asyncio +import json + +# Your Deepgram API Key +# DEEPGRAM_API_KEY = 'YOUR_DEEPGRAM_API_KEY' +DEEPGRAM_API_KEY = 'YOUR_DEEPGRAM_API_KEY' + +# Location of the file you want to transcribe. Should include filename and extension. +# Example of a local file: ../../Audio/life-moves-pretty-fast.wav +# Example of a remote file: https://static.deepgram.com/examples/interview_speech-analytics.wav +FILE = 'https://static.deepgram.com/examples/interview_speech-analytics.wav' + +# Mimetype for the file you want to transcribe +# Include this line only if transcribing a local file +# Example: audio/wav +MIMETYPE = 'audio/mpeg' + + +async def main(): + + # Initialize the Deepgram SDK + deepgram = Deepgram(DEEPGRAM_API_KEY) + + # Check whether requested file is local or remote, and prepare source + if FILE.startswith('http'): + # file is remote + # Set the source + source = { + 'url': FILE + } + else: + # file is local + # Open the audio file + audio = open(FILE, 'rb') + + # Set the source + source = { + 'buffer': audio, + 'mimetype': MIMETYPE + } + + # Send the audio to Deepgram and get the response + response = await asyncio.create_task( + deepgram.transcription.prerecorded( + source, + { + 'detect_language': "true", + 'summarize': "v2", + } + ) + ) + + # Write the response to the console + print(json.dumps(response, indent=4)) + + # Write only the transcript to the console + # print(response["results"]["channels"][0]["alternatives"][0]["transcript"]) + + # print(response["results"]["channels"]) + +try: + # If running in a Jupyter notebook, Jupyter is already running an event loop, so run main with this line instead: + # await main() + asyncio.run(main()) +except Exception as e: + exception_type, exception_object, exception_traceback = sys.exc_info() + line_number = exception_traceback.tb_lineno + print(f'line {line_number}: {exception_type} - {e}') diff --git a/sample-projects/prerecorded-audio/Quickstart/requirements.txt b/sample-projects/prerecorded-audio/Quickstart/requirements.txt new file mode 100644 index 00000000..efaa158c --- /dev/null +++ b/sample-projects/prerecorded-audio/Quickstart/requirements.txt @@ -0,0 +1,2 @@ +deepgram-sdk +asyncio diff --git a/sample-projects/streaming-audio/Quickstart/README.md b/sample-projects/streaming-audio/Quickstart/README.md new file mode 100644 index 00000000..cce9aef3 --- /dev/null +++ b/sample-projects/streaming-audio/Quickstart/README.md @@ -0,0 +1,13 @@ +# Streaming Audio Transcription + +To run this example, first install the dependencies in the requirements.txt file. + +`pip install -r requirements.txt` + +Replace the API key where it says 'YOUR_DEEPGRAM_API_KEY' + +`DEEPGRAM_API_KEY = 'YOUR_DEEPGRAM_API_KEY'` + +Make sure you're in the directory with the `main.py` file and run the project with the following command. + +`python3 main.py` diff --git a/sample-projects/streaming-audio/Quickstart/main.py b/sample-projects/streaming-audio/Quickstart/main.py new file mode 100644 index 00000000..94ec97e2 --- /dev/null +++ b/sample-projects/streaming-audio/Quickstart/main.py @@ -0,0 +1,54 @@ +# Example filename: deepgram_test.py + +from deepgram import Deepgram +import asyncio +import aiohttp + +# Your Deepgram API Key +DEEPGRAM_API_KEY = 'YOUR_DEEPGRAM_API_KEY' + +# URL for the realtime streaming audio you would like to transcribe +URL = 'http://stream.live.vc.bbcmedia.co.uk/bbc_world_service' + + +async def main(): + # Initialize the Deepgram SDK + deepgram = Deepgram(DEEPGRAM_API_KEY) + + # Create a websocket connection to Deepgram + # In this example, punctuation is turned on, interim results are turned off, and language is set to UK English. + try: + deepgramLive = await deepgram.transcription.live({ + 'smart_format': True, + 'interim_results': False, + 'language': 'en-US', + 'model': 'nova', + }) + except Exception as e: + print(f'Could not open socket: {e}') + return + + # Listen for the connection to close + deepgramLive.registerHandler(deepgramLive.event.CLOSE, lambda c: print( + f'Connection closed with code {c}.')) + + # Listen for any transcripts received from Deepgram and write them to the console + deepgramLive.registerHandler(deepgramLive.event.TRANSCRIPT_RECEIVED, print) + + # Listen for the connection to open and send streaming audio from the URL to Deepgram + async with aiohttp.ClientSession() as session: + async with session.get(URL) as audio: + while True: + data = await audio.content.readany() + deepgramLive.send(data) + + # If no data is being sent from the live stream, then break out of the loop. + if not data: + break + + # Indicate that we've finished sending data by sending the customary zero-byte message to the Deepgram streaming endpoint, and wait until we get back the final summary metadata object + await deepgramLive.finish() + +# If running in a Jupyter notebook, Jupyter is already running an event loop, so run main with this line instead: +# await main() +asyncio.run(main()) diff --git a/sample-projects/streaming-audio/Quickstart/requirements.txt b/sample-projects/streaming-audio/Quickstart/requirements.txt new file mode 100644 index 00000000..17ed4717 --- /dev/null +++ b/sample-projects/streaming-audio/Quickstart/requirements.txt @@ -0,0 +1,3 @@ +deepgram-sdk +asyncio +aiohttp \ No newline at end of file From 67758af5871307d24bbdb6fcfd2e7a80bc21d6ea Mon Sep 17 00:00:00 2001 From: Sandra Rodgers Date: Wed, 23 Aug 2023 17:12:57 -0500 Subject: [PATCH 04/13] updates main readme --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9f845dfe..e1a73a37 100644 --- a/README.md +++ b/README.md @@ -175,9 +175,11 @@ pytest --api-key tests/ pytest --cov=deepgram --api-key tests/ ``` -### Using Example Projects to test features +### Using Example Projects to test new features -Use the example projects to test features if there aren't currently the appropriate tests for your use case. Go to the folder `examples` and look for these two projects, which can be used to test out features in the Deepgram Python SDK. +Contributors to the SDK can test their changes locally by running the projects in the `examples` folder. This can be done when making changes without adding a unit test, but of course it is recommended that you add unit tests for any feature additions made to the SDK. + +Go to the folder `examples` and look for these two projects, which can be used to test out features in the Deepgram Python SDK: - prerecorded - streaming From aa6b19196695e180af30a8a3f0e39f7a75d9b7d6 Mon Sep 17 00:00:00 2001 From: Sandra Rodgers Date: Wed, 23 Aug 2023 17:21:30 -0500 Subject: [PATCH 05/13] remove formatting changes --- deepgram/transcription.py | 52 +++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/deepgram/transcription.py b/deepgram/transcription.py index b0225c8e..ed7890f2 100644 --- a/deepgram/transcription.py +++ b/deepgram/transcription.py @@ -41,13 +41,13 @@ async def __call__( as a function. This is useful for creating instances of the class, where we can call `PrerecordedTranscription()` and pass in arguments to set up an instance of the class. For example: - + prerecorded_transcription = PrerecordedTranscription(...) - + :param source:TranscriptionSource: Used to Pass in the audio file. :param timeout:float: (optional) The request timeout (if not set, defaults to `aiohttp`'s default timeout) :return: A `PrerecordedTranscriptionResponse` object, which contains the transcription results. - + """ if 'buffer' in source and 'mimetype' not in source: @@ -92,20 +92,21 @@ def __init__(self, options: Options, def __call__( self, source: TranscriptionSource, timeout: float = None ) -> PrerecordedTranscriptionResponse: + """ The __call__ function is a special method that allows the class to be called as a function. This is useful for creating instances of the class, where we can call `SyncPrerecordedTranscription()` and pass in arguments to set up an instance of the class. For example: - + sync_prerecorded_transcription = SyncPrerecordedTranscription(...) - + :param source:TranscriptionSource: Used to Pass in the audio file. :param timeout:float: (optional) The request timeout, excluding the upload time of the audio file. :return: A `prerecordedtranscriptionresponse` object, which contains the transcription results. - + """ - + if 'buffer' in source and 'mimetype' not in source: raise DeepgramApiError( 'Mimetype must be provided if the source is bytes', @@ -141,11 +142,11 @@ def __init__(self, options: Options, It initializes all of the attributes that are part of the object, and can be accessed using "self." notation. In this case, it sets up a list to store any messages received from Transcribe Streaming. - + :param options:Options: Used to Pass the options for the transcription job. :param transcription_options:LiveOptions: Used to Pass in the configuration for the transcription job. :return: None. - + """ self.options = options @@ -166,9 +167,9 @@ async def __call__(self) -> 'LiveTranscription': as a function. In this case, it is used to connect the client and start the transcription process. It returns itself after starting so that operations can be chained. - + :return: The object itself. - + """ self._socket = await _socket_connect( f'{self._root}{_make_query_string(self.transcription_options)}', @@ -181,11 +182,11 @@ async def _start(self) -> None: """ The _start function is the main function of the LiveTranscription class. It is responsible for creating a websocket connection to Deepgram Transcribe, - and then listening for incoming messages from that socket. It also sends any - messages that are in its queue (which is populated by other functions). The - _start function will run until it receives a message with an empty transcription, + and then listening for incoming messages from that socket. It also sends any + messages that are in its queue (which is populated by other functions). The + _start function will run until it receives a message with an empty transcription, at which point it will close the socket and return. - + :return: None. """ @@ -213,7 +214,7 @@ async def _start(self) -> None: parsed ) self.received.append(parsed) - if 'sha256' in parsed: + if 'sha256' in parsed: self.done = True except json.decoder.JSONDecodeError: self._ping_handlers( @@ -232,7 +233,7 @@ async def _receiver(self) -> None: The _receiver function is a coroutine that receives messages from the socket and puts them in a queue. It is started by calling start_receiver() on an instance of AsyncSocket. It runs until the socket is closed, or until an exception occurs. - + :return: None. """ @@ -242,7 +243,7 @@ async def _receiver(self) -> None: body = await self._socket.recv() self._queue.put_nowait((True, body)) except Exception as exc: - self.done = True # socket closed, will terminate on next loop + self.done = True # socket closed, will terminate on next loop def _ping_handlers(self, event_type: LiveTranscriptionEvent, body: Any) -> None: @@ -250,13 +251,13 @@ def _ping_handlers(self, event_type: LiveTranscriptionEvent, The _ping_handlers function is a callback that is called when the transcription service sends a ping event. It calls all of the functions in self.handlers, which are registered by calling add_ping_handler(). - + :param event_type:LiveTranscriptionEvent: Used to Determine if the function should be called. :param body:Any: Used to Pass the event data to the handler function. :return: The list of handlers for the event type. """ - + for handled_type, func in self.handlers: if handled_type is event_type: if inspect.iscoroutinefunction(func): @@ -322,8 +323,7 @@ async def finish(self) -> None: """Closes the connection to the Deepgram endpoint, waiting until ASR is complete on all submitted data.""" - # Set message for "data is finished sending" - self.send(json.dumps({"type": "CloseStream"})) + self.send(json.dumps({"type": "CloseStream"})) # Set message for "data is finished sending" while not self.done: await asyncio.sleep(0.1) @@ -348,7 +348,7 @@ def __init__(self, options: Options) -> None: async def prerecorded( self, source: TranscriptionSource, options: PrerecordedOptions = None, - endpoint="/listen", + endpoint = "/listen", timeout: float = None, **kwargs ) -> PrerecordedTranscriptionResponse: @@ -364,7 +364,7 @@ async def prerecorded( def sync_prerecorded( self, source: TranscriptionSource, options: PrerecordedOptions = None, - endpoint="/listen", + endpoint = "/listen", timeout: float = None, **kwargs ) -> PrerecordedTranscriptionResponse: @@ -378,7 +378,7 @@ def sync_prerecorded( )(source, timeout=timeout) async def live( - self, options: LiveOptions = None, endpoint="/listen", **kwargs + self, options: LiveOptions = None, endpoint = "/listen", **kwargs ) -> LiveTranscription: """Provides a client to send raw audio data to be transcribed.""" if options is None: @@ -386,4 +386,4 @@ async def live( full_options = cast(LiveOptions, {**options, **kwargs}) return await LiveTranscription( self.options, full_options, endpoint - )() + )() \ No newline at end of file From 946e3742c2d6a5693a44baef4e5aab617a33fe04 Mon Sep 17 00:00:00 2001 From: Sandra Rodgers Date: Wed, 23 Aug 2023 17:23:39 -0500 Subject: [PATCH 06/13] remove formatting --- deepgram/transcription.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/deepgram/transcription.py b/deepgram/transcription.py index ed7890f2..7a0b4d31 100644 --- a/deepgram/transcription.py +++ b/deepgram/transcription.py @@ -386,4 +386,5 @@ async def live( full_options = cast(LiveOptions, {**options, **kwargs}) return await LiveTranscription( self.options, full_options, endpoint - )() \ No newline at end of file + )() + \ No newline at end of file From 92becaed3f66da2cb0523441a23c2edffd1f2808 Mon Sep 17 00:00:00 2001 From: Sandra Rodgers Date: Wed, 23 Aug 2023 17:24:09 -0500 Subject: [PATCH 07/13] remove formatting --- deepgram/transcription.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/deepgram/transcription.py b/deepgram/transcription.py index 7a0b4d31..ed7890f2 100644 --- a/deepgram/transcription.py +++ b/deepgram/transcription.py @@ -386,5 +386,4 @@ async def live( full_options = cast(LiveOptions, {**options, **kwargs}) return await LiveTranscription( self.options, full_options, endpoint - )() - \ No newline at end of file + )() \ No newline at end of file From ea2a6219e975ce3c74c7562f7049ea219696cb38 Mon Sep 17 00:00:00 2001 From: Sandra Rodgers Date: Wed, 23 Aug 2023 17:26:49 -0500 Subject: [PATCH 08/13] removed a modified file --- deepgram/transcription.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deepgram/transcription.py b/deepgram/transcription.py index ed7890f2..002173d0 100644 --- a/deepgram/transcription.py +++ b/deepgram/transcription.py @@ -386,4 +386,4 @@ async def live( full_options = cast(LiveOptions, {**options, **kwargs}) return await LiveTranscription( self.options, full_options, endpoint - )() \ No newline at end of file + )() From e1d47e2266027aac32d84accc85490f549eb7832 Mon Sep 17 00:00:00 2001 From: Sandra Rodgers Date: Thu, 24 Aug 2023 13:17:46 -0500 Subject: [PATCH 09/13] removes sample-projects folder --- README.md | 23 ---- .../prerecorded-audio/Quickstart/README.md | 13 -- .../prerecorded-audio/Quickstart/main.py | 71 ---------- .../Quickstart/requirements.txt | 2 - .../live-transcription-django/README.md | 22 ---- .../stream/manage.py | 22 ---- .../stream/requirements.txt | 39 ------ .../stream/stream/__init__.py | 0 .../stream/stream/asgi.py | 26 ---- .../stream/stream/settings.py | 124 ------------------ .../stream/stream/urls.py | 23 ---- .../stream/stream/wsgi.py | 16 --- .../stream/transcript/__init__.py | 0 .../stream/transcript/admin.py | 3 - .../stream/transcript/apps.py | 6 - .../stream/transcript/consumers.py | 42 ------ .../stream/transcript/migrations/__init__.py | 0 .../stream/transcript/models.py | 3 - .../stream/transcript/routing.py | 7 - .../templates/transcript/index.html | 53 -------- .../stream/transcript/tests.py | 3 - .../stream/transcript/urls.py | 7 - .../stream/transcript/views.py | 4 - .../live-transcription-fastapi/README.md | 27 ---- .../live-transcription-fastapi/main.py | 56 -------- .../requirements.txt | 38 ------ .../templates/index.html | 52 -------- .../Flask/live-transcription-flask/README.md | 22 ---- .../Flask/live-transcription-flask/main.py | 62 --------- .../live-transcription-flask/requirements.txt | 20 --- .../templates/index.html | 50 ------- .../Quart/live-transcription-quart/README.md | 33 ----- .../Quart/live-transcription-quart/main.py | 57 -------- .../live-transcription-quart/requirements.txt | 28 ---- .../templates/index.html | 51 ------- .../streaming-audio/Quickstart/README.md | 13 -- .../streaming-audio/Quickstart/main.py | 54 -------- .../Quickstart/requirements.txt | 3 - 38 files changed, 1075 deletions(-) delete mode 100644 sample-projects/prerecorded-audio/Quickstart/README.md delete mode 100644 sample-projects/prerecorded-audio/Quickstart/main.py delete mode 100644 sample-projects/prerecorded-audio/Quickstart/requirements.txt delete mode 100644 sample-projects/streaming-audio/Django/live-transcription-django/README.md delete mode 100755 sample-projects/streaming-audio/Django/live-transcription-django/stream/manage.py delete mode 100644 sample-projects/streaming-audio/Django/live-transcription-django/stream/requirements.txt delete mode 100644 sample-projects/streaming-audio/Django/live-transcription-django/stream/stream/__init__.py delete mode 100644 sample-projects/streaming-audio/Django/live-transcription-django/stream/stream/asgi.py delete mode 100644 sample-projects/streaming-audio/Django/live-transcription-django/stream/stream/settings.py delete mode 100644 sample-projects/streaming-audio/Django/live-transcription-django/stream/stream/urls.py delete mode 100644 sample-projects/streaming-audio/Django/live-transcription-django/stream/stream/wsgi.py delete mode 100644 sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/__init__.py delete mode 100644 sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/admin.py delete mode 100644 sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/apps.py delete mode 100644 sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/consumers.py delete mode 100644 sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/migrations/__init__.py delete mode 100644 sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/models.py delete mode 100644 sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/routing.py delete mode 100644 sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/templates/transcript/index.html delete mode 100644 sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/tests.py delete mode 100644 sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/urls.py delete mode 100644 sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/views.py delete mode 100644 sample-projects/streaming-audio/FastAPI/live-transcription-fastapi/README.md delete mode 100644 sample-projects/streaming-audio/FastAPI/live-transcription-fastapi/main.py delete mode 100644 sample-projects/streaming-audio/FastAPI/live-transcription-fastapi/requirements.txt delete mode 100644 sample-projects/streaming-audio/FastAPI/live-transcription-fastapi/templates/index.html delete mode 100644 sample-projects/streaming-audio/Flask/live-transcription-flask/README.md delete mode 100644 sample-projects/streaming-audio/Flask/live-transcription-flask/main.py delete mode 100644 sample-projects/streaming-audio/Flask/live-transcription-flask/requirements.txt delete mode 100644 sample-projects/streaming-audio/Flask/live-transcription-flask/templates/index.html delete mode 100644 sample-projects/streaming-audio/Quart/live-transcription-quart/README.md delete mode 100644 sample-projects/streaming-audio/Quart/live-transcription-quart/main.py delete mode 100644 sample-projects/streaming-audio/Quart/live-transcription-quart/requirements.txt delete mode 100644 sample-projects/streaming-audio/Quart/live-transcription-quart/templates/index.html delete mode 100644 sample-projects/streaming-audio/Quickstart/README.md delete mode 100644 sample-projects/streaming-audio/Quickstart/main.py delete mode 100644 sample-projects/streaming-audio/Quickstart/requirements.txt diff --git a/README.md b/README.md index e1a73a37..49e94e7f 100644 --- a/README.md +++ b/README.md @@ -131,29 +131,6 @@ Depending on your preference, you can also add parameters as named arguments, in response = await dg_client.transcription.prerecorded(source, punctuate=True, keywords=['first:5', 'second']) ``` -## Code Samples - -To run the sample code, you may want to create a virtual environment to isolate your Python projects, but it's not required. You can learn how to make and activate these virtual environments in [this article](https://blog.deepgram.com/python-virtual-environments/) on our Deepgram blog. - -#### Prerecorded Audio Code Samples - -In the `sample-projects/prerecorded-audio` folder, there is a quickstart project to get you quickly started transcribing prerecorded audio (from a local file or a remote file) with Deepgram. - -- Quickstart - -#### Streaming Audio Code Samples - -In the `sample-projects/streaming-audio` folder, there are examples from four different Python web frameworks of how to do live streaming audio transcription with Deepgram. These include: - -- Flask 2.0 -- FastAPI -- Django -- Quart - -There is also a Quickstart that you can use to get up and running without a Python web framework. - -- Quickstart - ## Testing ### Setup diff --git a/sample-projects/prerecorded-audio/Quickstart/README.md b/sample-projects/prerecorded-audio/Quickstart/README.md deleted file mode 100644 index dc7fc739..00000000 --- a/sample-projects/prerecorded-audio/Quickstart/README.md +++ /dev/null @@ -1,13 +0,0 @@ -# Prerecorded Audio Transcription - -To run this example, first install the dependencies in the requirements.txt file. - -`pip install -r requirements.txt` - -Replace the API key where it says 'YOUR_DEEPGRAM_API_KEY' - -`DEEPGRAM_API_KEY = 'YOUR_DEEPGRAM_API_KEY'` - -Make sure you're in the directory with the `main.py` file and run the project with the following command. - -`python3 main.py` diff --git a/sample-projects/prerecorded-audio/Quickstart/main.py b/sample-projects/prerecorded-audio/Quickstart/main.py deleted file mode 100644 index 11848d00..00000000 --- a/sample-projects/prerecorded-audio/Quickstart/main.py +++ /dev/null @@ -1,71 +0,0 @@ -# Example filename: deepgram_test.py - -from deepgram import Deepgram -import asyncio -import json - -# Your Deepgram API Key -# DEEPGRAM_API_KEY = 'YOUR_DEEPGRAM_API_KEY' -DEEPGRAM_API_KEY = 'YOUR_DEEPGRAM_API_KEY' - -# Location of the file you want to transcribe. Should include filename and extension. -# Example of a local file: ../../Audio/life-moves-pretty-fast.wav -# Example of a remote file: https://static.deepgram.com/examples/interview_speech-analytics.wav -FILE = 'https://static.deepgram.com/examples/interview_speech-analytics.wav' - -# Mimetype for the file you want to transcribe -# Include this line only if transcribing a local file -# Example: audio/wav -MIMETYPE = 'audio/mpeg' - - -async def main(): - - # Initialize the Deepgram SDK - deepgram = Deepgram(DEEPGRAM_API_KEY) - - # Check whether requested file is local or remote, and prepare source - if FILE.startswith('http'): - # file is remote - # Set the source - source = { - 'url': FILE - } - else: - # file is local - # Open the audio file - audio = open(FILE, 'rb') - - # Set the source - source = { - 'buffer': audio, - 'mimetype': MIMETYPE - } - - # Send the audio to Deepgram and get the response - response = await asyncio.create_task( - deepgram.transcription.prerecorded( - source, - { - 'detect_language': "true", - 'summarize': "v2", - } - ) - ) - - # Write the response to the console - print(json.dumps(response, indent=4)) - - # Write only the transcript to the console - # print(response["results"]["channels"][0]["alternatives"][0]["transcript"]) - - # print(response["results"]["channels"]) - -try: - # If running in a Jupyter notebook, Jupyter is already running an event loop, so run main with this line instead: - # await main() - asyncio.run(main()) -except Exception as e: - exception_type, exception_object, exception_traceback = sys.exc_info() - line_number = exception_traceback.tb_lineno - print(f'line {line_number}: {exception_type} - {e}') diff --git a/sample-projects/prerecorded-audio/Quickstart/requirements.txt b/sample-projects/prerecorded-audio/Quickstart/requirements.txt deleted file mode 100644 index efaa158c..00000000 --- a/sample-projects/prerecorded-audio/Quickstart/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -deepgram-sdk -asyncio diff --git a/sample-projects/streaming-audio/Django/live-transcription-django/README.md b/sample-projects/streaming-audio/Django/live-transcription-django/README.md deleted file mode 100644 index bed88c30..00000000 --- a/sample-projects/streaming-audio/Django/live-transcription-django/README.md +++ /dev/null @@ -1,22 +0,0 @@ -# Live Transcription With Python and Django - -To run this project create a virtual environment by running the below commands. You can learn more about setting up a virtual environment in this article. - -``` -mkdir [% NAME_OF_YOUR_DIRECTORY %] -cd [% NAME_OF_YOUR_DIRECTORY %] -python3 -m venv venv -source venv/bin/activate -``` - -Make sure your virtual environment is activated and install the dependencies in the requirements.txt file inside. - -`pip install -r requirements.txt` - -Make sure you're in the directory with the manage.py file and run the project in the development server. - -`python3 manage.py runserver` - -Pull up a browser and go to your localhost, http://127.0.0.1:8000/. - -Allow access to your microphone and start speaking. A transcript of your audio will appear in the browser. \ No newline at end of file diff --git a/sample-projects/streaming-audio/Django/live-transcription-django/stream/manage.py b/sample-projects/streaming-audio/Django/live-transcription-django/stream/manage.py deleted file mode 100755 index 3288579a..00000000 --- a/sample-projects/streaming-audio/Django/live-transcription-django/stream/manage.py +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env python -"""Django's command-line utility for administrative tasks.""" -import os -import sys - - -def main(): - """Run administrative tasks.""" - os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'stream.settings') - try: - from django.core.management import execute_from_command_line - except ImportError as exc: - raise ImportError( - "Couldn't import Django. Are you sure it's installed and " - "available on your PYTHONPATH environment variable? Did you " - "forget to activate a virtual environment?" - ) from exc - execute_from_command_line(sys.argv) - - -if __name__ == '__main__': - main() diff --git a/sample-projects/streaming-audio/Django/live-transcription-django/stream/requirements.txt b/sample-projects/streaming-audio/Django/live-transcription-django/stream/requirements.txt deleted file mode 100644 index e4baec2a..00000000 --- a/sample-projects/streaming-audio/Django/live-transcription-django/stream/requirements.txt +++ /dev/null @@ -1,39 +0,0 @@ -aiohttp==3.8.5 -aiosignal==1.2.0 -asgiref==3.5.0 -async-timeout==4.0.2 -attrs==21.4.0 -autobahn==22.2.2 -Automat==20.2.0 -certifi==2023.7.22 -cffi==1.15.0 -channels==3.0.4 -charset-normalizer==2.0.12 -constantly==15.1.0 -cryptography==41.0.3 -daphne==3.0.2 -deepgram-sdk==0.2.4 -Django==4.1.10 -frozenlist==1.3.0 -hyperlink==21.0.0 -idna==3.3 -incremental==21.3.0 -multidict==6.0.2 -oauthlib==3.2.0 -pyasn1==0.4.8 -pyasn1-modules==0.2.8 -pycparser==2.21 -pyOpenSSL==22.0.0 -python-dotenv==0.19.2 -requests==2.31.0 -requests-oauthlib==1.3.1 -service-identity==21.1.0 -six==1.16.0 -sqlparse==0.4.4 -Twisted==22.1.0 -txaio==22.2.1 -typing_extensions==4.1.1 -urllib3==1.26.8 -websockets==10.2 -yarl==1.7.2 -zope.interface==5.4.0 diff --git a/sample-projects/streaming-audio/Django/live-transcription-django/stream/stream/__init__.py b/sample-projects/streaming-audio/Django/live-transcription-django/stream/stream/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/sample-projects/streaming-audio/Django/live-transcription-django/stream/stream/asgi.py b/sample-projects/streaming-audio/Django/live-transcription-django/stream/stream/asgi.py deleted file mode 100644 index 137f2d45..00000000 --- a/sample-projects/streaming-audio/Django/live-transcription-django/stream/stream/asgi.py +++ /dev/null @@ -1,26 +0,0 @@ -""" -ASGI config for stream project. - -It exposes the ASGI callable as a module-level variable named ``application``. - -For more information on this file, see -https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ -""" - -import os - -from channels.auth import AuthMiddlewareStack -from channels.routing import ProtocolTypeRouter, URLRouter -from django.core.asgi import get_asgi_application -import transcript.routing - -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "stream.settings") - -application = ProtocolTypeRouter({ - "http": get_asgi_application(), - "websocket": AuthMiddlewareStack( - URLRouter( - transcript.routing.websocket_urlpatterns - ) - ), -}) diff --git a/sample-projects/streaming-audio/Django/live-transcription-django/stream/stream/settings.py b/sample-projects/streaming-audio/Django/live-transcription-django/stream/stream/settings.py deleted file mode 100644 index df158100..00000000 --- a/sample-projects/streaming-audio/Django/live-transcription-django/stream/stream/settings.py +++ /dev/null @@ -1,124 +0,0 @@ -""" -Django settings for stream project. - -Generated by 'django-admin startproject' using Django 4.0.3. - -For more information on this file, see -https://docs.djangoproject.com/en/4.0/topics/settings/ - -For the full list of settings and their values, see -https://docs.djangoproject.com/en/4.0/ref/settings/ -""" - -from pathlib import Path - -# Build paths inside the project like this: BASE_DIR / 'subdir'. -BASE_DIR = Path(__file__).resolve().parent.parent - - -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ - -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True - -ALLOWED_HOSTS = [] - - -# Application definition - -INSTALLED_APPS = [ - 'channels', - 'transcript', - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', -] - -MIDDLEWARE = [ - 'django.middleware.security.SecurityMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', -] - -ROOT_URLCONF = 'stream.urls' - -TEMPLATES = [ - { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', - ], - }, - }, -] - -WSGI_APPLICATION = 'stream.wsgi.application' - - -# Database -# https://docs.djangoproject.com/en/4.0/ref/settings/#databases - -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': BASE_DIR / 'db.sqlite3', - } -} - - -# Password validation -# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators - -AUTH_PASSWORD_VALIDATORS = [ - { - 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', - }, -] - - -# Internationalization -# https://docs.djangoproject.com/en/4.0/topics/i18n/ - -LANGUAGE_CODE = 'en-us' - -TIME_ZONE = 'UTC' - -USE_I18N = True - -USE_TZ = True - - -# Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/4.0/howto/static-files/ - -STATIC_URL = 'static/' - -# Default primary key field type -# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field - -DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' - -ASGI_APPLICATION = 'stream.asgi.application' \ No newline at end of file diff --git a/sample-projects/streaming-audio/Django/live-transcription-django/stream/stream/urls.py b/sample-projects/streaming-audio/Django/live-transcription-django/stream/stream/urls.py deleted file mode 100644 index 6fdc7811..00000000 --- a/sample-projects/streaming-audio/Django/live-transcription-django/stream/stream/urls.py +++ /dev/null @@ -1,23 +0,0 @@ -"""stream URL Configuration - -The `urlpatterns` list routes URLs to views. For more information please see: - https://docs.djangoproject.com/en/4.0/topics/http/urls/ -Examples: -Function views - 1. Add an import: from my_app import views - 2. Add a URL to urlpatterns: path('', views.home, name='home') -Class-based views - 1. Add an import: from other_app.views import Home - 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') -Including another URLconf - 1. Import the include() function: from django.urls import include, path - 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) -""" -from django.conf.urls import include -from django.contrib import admin -from django.urls import path - -urlpatterns = [ - path('', include('transcript.urls')), - path('admin/', admin.site.urls), -] diff --git a/sample-projects/streaming-audio/Django/live-transcription-django/stream/stream/wsgi.py b/sample-projects/streaming-audio/Django/live-transcription-django/stream/stream/wsgi.py deleted file mode 100644 index 1cd83868..00000000 --- a/sample-projects/streaming-audio/Django/live-transcription-django/stream/stream/wsgi.py +++ /dev/null @@ -1,16 +0,0 @@ -""" -WSGI config for stream project. - -It exposes the WSGI callable as a module-level variable named ``application``. - -For more information on this file, see -https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/ -""" - -import os - -from django.core.wsgi import get_wsgi_application - -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'stream.settings') - -application = get_wsgi_application() diff --git a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/__init__.py b/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/admin.py b/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/admin.py deleted file mode 100644 index 8c38f3f3..00000000 --- a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/admin.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.contrib import admin - -# Register your models here. diff --git a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/apps.py b/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/apps.py deleted file mode 100644 index ec9da48a..00000000 --- a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/apps.py +++ /dev/null @@ -1,6 +0,0 @@ -from django.apps import AppConfig - - -class TranscriptConfig(AppConfig): - default_auto_field = 'django.db.models.BigAutoField' - name = 'transcript' diff --git a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/consumers.py b/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/consumers.py deleted file mode 100644 index 19857fc6..00000000 --- a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/consumers.py +++ /dev/null @@ -1,42 +0,0 @@ -from channels.generic.websocket import AsyncWebsocketConsumer -from dotenv import load_dotenv -from deepgram import Deepgram -from typing import Dict - -import os - -load_dotenv() - -class TranscriptConsumer(AsyncWebsocketConsumer): - dg_client = Deepgram(os.getenv('DEEPGRAM_API_KEY')) - - async def get_transcript(self, data: Dict) -> None: - if 'channel' in data: - transcript = data['channel']['alternatives'][0]['transcript'] - - if transcript: - await self.send(transcript) - - - async def connect_to_deepgram(self): - try: - self.socket = await self.dg_client.transcription.live({'punctuate': True, 'interim_results': False}) - self.socket.registerHandler(self.socket.event.CLOSE, lambda c: print(f'Connection closed with code {c}.')) - self.socket.registerHandler(self.socket.event.TRANSCRIPT_RECEIVED, self.get_transcript) - - except Exception as e: - raise Exception(f'Could not open socket: {e}') - - async def connect(self): - await self.connect_to_deepgram() - await self.accept() - - - async def disconnect(self, close_code): - await self.channel_layer.group_discard( - self.room_group_name, - self.channel_name - ) - - async def receive(self, bytes_data): - self.socket.send(bytes_data) diff --git a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/migrations/__init__.py b/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/migrations/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/models.py b/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/models.py deleted file mode 100644 index 71a83623..00000000 --- a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/models.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.db import models - -# Create your models here. diff --git a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/routing.py b/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/routing.py deleted file mode 100644 index 4833a008..00000000 --- a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/routing.py +++ /dev/null @@ -1,7 +0,0 @@ -from django.urls import re_path - -from . import consumers - -websocket_urlpatterns = [ - re_path(r'listen', consumers.TranscriptConsumer.as_asgi()), -] \ No newline at end of file diff --git a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/templates/transcript/index.html b/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/templates/transcript/index.html deleted file mode 100644 index 8ea65c54..00000000 --- a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/templates/transcript/index.html +++ /dev/null @@ -1,53 +0,0 @@ - - - - Chat - - -

Transcribe Audio With Django

-

Connection status will go here

-

- - - - \ No newline at end of file diff --git a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/tests.py b/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/tests.py deleted file mode 100644 index 7ce503c2..00000000 --- a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/urls.py b/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/urls.py deleted file mode 100644 index 3ef24d97..00000000 --- a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/urls.py +++ /dev/null @@ -1,7 +0,0 @@ -from django.urls import path - -from . import views - -urlpatterns = [ - path('', views.index, name='index'), -] \ No newline at end of file diff --git a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/views.py b/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/views.py deleted file mode 100644 index dd6609a4..00000000 --- a/sample-projects/streaming-audio/Django/live-transcription-django/stream/transcript/views.py +++ /dev/null @@ -1,4 +0,0 @@ -from django.shortcuts import render - -def index(request): - return render(request, 'transcript/index.html') \ No newline at end of file diff --git a/sample-projects/streaming-audio/FastAPI/live-transcription-fastapi/README.md b/sample-projects/streaming-audio/FastAPI/live-transcription-fastapi/README.md deleted file mode 100644 index 0d82c23d..00000000 --- a/sample-projects/streaming-audio/FastAPI/live-transcription-fastapi/README.md +++ /dev/null @@ -1,27 +0,0 @@ -# Live Transcription With Python and FastAPI - -To run this project create a virtual environment by running the below commands. You can learn more about setting up a virtual environment in this [article](https://developers.deepgram.com/blog/2022/02/python-virtual-environments/). - -``` -mkdir [% NAME_OF_YOUR_DIRECTORY %] -cd [% NAME_OF_YOUR_DIRECTORY %] -python3 -m venv venv -source venv/bin/activate -``` - -Make sure your virtual environment is activated and install the dependencies in the requirements.txt file inside. - -``` -pip install -r requirements.txt -``` - -Make sure you're in the directory with the **main.py** file and run the project in the development server. - -``` -uvicorn main:app --reload -``` - -Pull up a browser and go to your localhost, http://127.0.0.1:8000/. - -Allow access to your microphone and start speaking. A transcript of your audio will appear in the browser. - diff --git a/sample-projects/streaming-audio/FastAPI/live-transcription-fastapi/main.py b/sample-projects/streaming-audio/FastAPI/live-transcription-fastapi/main.py deleted file mode 100644 index 56e44727..00000000 --- a/sample-projects/streaming-audio/FastAPI/live-transcription-fastapi/main.py +++ /dev/null @@ -1,56 +0,0 @@ -from fastapi import FastAPI, Request, WebSocket -from fastapi.responses import HTMLResponse -from fastapi.templating import Jinja2Templates -from typing import Dict, Callable -from deepgram import Deepgram -from dotenv import load_dotenv -import os - -load_dotenv() - -app = FastAPI() - -dg_client = Deepgram(os.getenv('DEEPGRAM_API_KEY')) - -templates = Jinja2Templates(directory="templates") - -async def process_audio(fast_socket: WebSocket): - async def get_transcript(data: Dict) -> None: - if 'channel' in data: - transcript = data['channel']['alternatives'][0]['transcript'] - - if transcript: - await fast_socket.send_text(transcript) - - deepgram_socket = await connect_to_deepgram(get_transcript) - - return deepgram_socket - -async def connect_to_deepgram(transcript_received_handler: Callable[[Dict], None]): - try: - socket = await dg_client.transcription.live({'punctuate': True, 'interim_results': False}) - socket.registerHandler(socket.event.CLOSE, lambda c: print(f'Connection closed with code {c}.')) - socket.registerHandler(socket.event.TRANSCRIPT_RECEIVED, transcript_received_handler) - - return socket - except Exception as e: - raise Exception(f'Could not open socket: {e}') - -@app.get("/", response_class=HTMLResponse) -def get(request: Request): - return templates.TemplateResponse("index.html", {"request": request}) - -@app.websocket("/listen") -async def websocket_endpoint(websocket: WebSocket): - await websocket.accept() - - try: - deepgram_socket = await process_audio(websocket) - - while True: - data = await websocket.receive_bytes() - deepgram_socket.send(data) - except Exception as e: - raise Exception(f'Could not process audio: {e}') - finally: - await websocket.close() \ No newline at end of file diff --git a/sample-projects/streaming-audio/FastAPI/live-transcription-fastapi/requirements.txt b/sample-projects/streaming-audio/FastAPI/live-transcription-fastapi/requirements.txt deleted file mode 100644 index 561bc09f..00000000 --- a/sample-projects/streaming-audio/FastAPI/live-transcription-fastapi/requirements.txt +++ /dev/null @@ -1,38 +0,0 @@ -aiohttp==3.8.5 -aiosignal==1.2.0 -anyio==3.5.0 -asgiref==3.5.0 -async-timeout==4.0.2 -attrs==21.4.0 -certifi==2023.7.22 -charset-normalizer==2.0.12 -click==8.0.4 -deepgram-sdk==0.2.4 -dnspython==2.2.0 -email-validator==1.1.3 -fastapi==0.74.1 -frozenlist==1.3.0 -h11==0.13.0 -httptools==0.2.0 -idna==3.3 -itsdangerous==2.1.0 -Jinja2==3.0.3 -MarkupSafe==2.1.0 -multidict==6.0.2 -orjson==3.6.7 -pydantic==1.9.0 -python-dotenv==0.19.2 -python-multipart==0.0.5 -PyYAML==5.4.1 -requests==2.31.0 -six==1.16.0 -sniffio==1.2.0 -starlette==0.17.1 -typing_extensions==4.1.1 -ujson==4.3.0 -urllib3==1.26.8 -uvicorn==0.15.0 -uvloop==0.16.0 -watchgod==0.7 -websockets==10.2 -yarl==1.7.2 diff --git a/sample-projects/streaming-audio/FastAPI/live-transcription-fastapi/templates/index.html b/sample-projects/streaming-audio/FastAPI/live-transcription-fastapi/templates/index.html deleted file mode 100644 index 8f81ef39..00000000 --- a/sample-projects/streaming-audio/FastAPI/live-transcription-fastapi/templates/index.html +++ /dev/null @@ -1,52 +0,0 @@ - - - - Live Transcription - - -

Transcribe Audio With FastAPI

-

Connection status will go here

-

- - - - \ No newline at end of file diff --git a/sample-projects/streaming-audio/Flask/live-transcription-flask/README.md b/sample-projects/streaming-audio/Flask/live-transcription-flask/README.md deleted file mode 100644 index cb949cab..00000000 --- a/sample-projects/streaming-audio/Flask/live-transcription-flask/README.md +++ /dev/null @@ -1,22 +0,0 @@ -# Live Transcription With Python, Flask 2.0 and Deepgram - -To run this project create a virtual environment by running the below commands. You can learn more about setting up a virtual environment in this article. - -``` -mkdir [% NAME_OF_YOUR_DIRECTORY %] -cd [% NAME_OF_YOUR_DIRECTORY %] -python3 -m venv venv -source venv/bin/activate -``` - -Make sure your virtual environment is activated and install the dependencies in the requirements.txt file inside. - -`pip install -r requirements.txt` - -Make sure you're in the directory with the main.py file and run the project in the development server. - -`python main.py` - -Pull up a browser and go to your localhost, `http://127.0.0.1:8000/`. - -Allow access to your microphone and start speaking. A transcript of your audio will appear in the browser. diff --git a/sample-projects/streaming-audio/Flask/live-transcription-flask/main.py b/sample-projects/streaming-audio/Flask/live-transcription-flask/main.py deleted file mode 100644 index 74cd3775..00000000 --- a/sample-projects/streaming-audio/Flask/live-transcription-flask/main.py +++ /dev/null @@ -1,62 +0,0 @@ -from flask import Flask, render_template -from deepgram import Deepgram -from dotenv import load_dotenv -import os -import asyncio -from aiohttp import web -from aiohttp_wsgi import WSGIHandler - -from typing import Dict, Callable - - -load_dotenv() - -app = Flask('aioflask') - -dg_client = Deepgram(os.getenv('DEEPGRAM_API_KEY')) - -async def process_audio(fast_socket: web.WebSocketResponse): - async def get_transcript(data: Dict) -> None: - if 'channel' in data: - transcript = data['channel']['alternatives'][0]['transcript'] - - if transcript: - await fast_socket.send_str(transcript) - - deepgram_socket = await connect_to_deepgram(get_transcript) - - return deepgram_socket - -async def connect_to_deepgram(transcript_received_handler: Callable[[Dict], None]) -> str: - try: - socket = await dg_client.transcription.live({'punctuate': True, 'interim_results': False}) - socket.registerHandler(socket.event.CLOSE, lambda c: print(f'Connection closed with code {c}.')) - socket.registerHandler(socket.event.TRANSCRIPT_RECEIVED, transcript_received_handler) - - return socket - except Exception as e: - raise Exception(f'Could not open socket: {e}') - -@app.route('/') -def index(): - return render_template('index.html') - -async def socket(request): - ws = web.WebSocketResponse() - await ws.prepare(request) - - deepgram_socket = await process_audio(ws) - - while True: - data = await ws.receive_bytes() - deepgram_socket.send(data) - - - -if __name__ == "__main__": - loop = asyncio.get_event_loop() - aio_app = web.Application() - wsgi = WSGIHandler(app) - aio_app.router.add_route('*', '/{path_info: *}', wsgi.handle_request) - aio_app.router.add_route('GET', '/listen', socket) - web.run_app(aio_app, port=5555) \ No newline at end of file diff --git a/sample-projects/streaming-audio/Flask/live-transcription-flask/requirements.txt b/sample-projects/streaming-audio/Flask/live-transcription-flask/requirements.txt deleted file mode 100644 index d5ad131e..00000000 --- a/sample-projects/streaming-audio/Flask/live-transcription-flask/requirements.txt +++ /dev/null @@ -1,20 +0,0 @@ -aiohttp==3.8.5 -aiohttp-wsgi==0.10.0 -aiosignal==1.2.0 -asgiref==3.5.0 -async-timeout==4.0.2 -attrs==21.4.0 -charset-normalizer==2.0.12 -click==8.0.4 -deepgram-sdk==0.2.4 -Flask==2.2.5 -frozenlist==1.3.0 -idna==3.3 -itsdangerous==2.1.0 -Jinja2==3.0.3 -MarkupSafe==2.1.0 -multidict==6.0.2 -python-dotenv==0.19.2 -websockets==10.2 -Werkzeug==2.0.3 -yarl==1.7.2 diff --git a/sample-projects/streaming-audio/Flask/live-transcription-flask/templates/index.html b/sample-projects/streaming-audio/Flask/live-transcription-flask/templates/index.html deleted file mode 100644 index 1b000045..00000000 --- a/sample-projects/streaming-audio/Flask/live-transcription-flask/templates/index.html +++ /dev/null @@ -1,50 +0,0 @@ - - - - Live Transcription - - -

Transcribe Audio With Flask 2.0

-

Connection status will go here

-

- - - - - diff --git a/sample-projects/streaming-audio/Quart/live-transcription-quart/README.md b/sample-projects/streaming-audio/Quart/live-transcription-quart/README.md deleted file mode 100644 index 47475358..00000000 --- a/sample-projects/streaming-audio/Quart/live-transcription-quart/README.md +++ /dev/null @@ -1,33 +0,0 @@ -# Live Transcription With Python and Quart - -To run this project create a virtual environment by running the below commands. You can learn more about setting up a virtual environment in this [article](https://developers.deepgram.com/blog/2022/02/python-virtual-environments/). - -``` -mkdir [% NAME_OF_YOUR_DIRECTORY %] -cd [% NAME_OF_YOUR_DIRECTORY %] -python3 -m venv venv -source venv/bin/activate -``` - -Make sure your virtual environment is activated and install the dependencies in the requirements.txt file inside. - -``` -pip install -r requirements.txt -``` - -Make sure you're in the directory with the `main.py` file and export your application: - -``` -export QUART_APP=main:app -``` - -Now run the project in the development server. - -``` -python main.py -``` - -Pull up a browser and go to your localhost, http://127.0.0.1:3000/. - -Allow access to your microphone and start speaking. A transcript of your audio will appear in the browser. - diff --git a/sample-projects/streaming-audio/Quart/live-transcription-quart/main.py b/sample-projects/streaming-audio/Quart/live-transcription-quart/main.py deleted file mode 100644 index 4038b016..00000000 --- a/sample-projects/streaming-audio/Quart/live-transcription-quart/main.py +++ /dev/null @@ -1,57 +0,0 @@ -from quart import Quart, render_template, websocket -from deepgram import Deepgram -from dotenv import load_dotenv -from typing import Dict, Callable - -import os - -load_dotenv() - -app = Quart(__name__) - -dg_client = Deepgram(os.getenv('DEEPGRAM_API_KEY')) - -async def process_audio(fast_socket): - async def get_transcript(data: Dict) -> None: - if 'channel' in data: - transcript = data['channel']['alternatives'][0]['transcript'] - - if transcript: - await fast_socket.send(transcript) - - deepgram_socket = await connect_to_deepgram(get_transcript) - - return deepgram_socket - -async def connect_to_deepgram(transcript_received_handler: Callable[[Dict], None]) -> str: - try: - socket = await dg_client.transcription.live({'punctuate': True, 'interim_results': False}) - socket.registerHandler(socket.event.CLOSE, lambda c: print(f'Connection closed with code {c}.')) - socket.registerHandler(socket.event.TRANSCRIPT_RECEIVED, transcript_received_handler) - - return socket - except Exception as e: - raise Exception(f'Could not open socket: {e}') - -@app.route('/') -async def index(): - return await render_template('index.html') - -@app.websocket('/listen') -async def websocket_endpoint(): - - try: - deepgram_socket = await process_audio(websocket) - - while True: - data = await websocket.receive() - deepgram_socket.send(data) - except Exception as e: - raise Exception(f'Could not process audio: {e}') - finally: - websocket.close(1000) - - - -if __name__ == "__main__": - app.run('localhost', port=3000, debug=True) \ No newline at end of file diff --git a/sample-projects/streaming-audio/Quart/live-transcription-quart/requirements.txt b/sample-projects/streaming-audio/Quart/live-transcription-quart/requirements.txt deleted file mode 100644 index 1a4b86c6..00000000 --- a/sample-projects/streaming-audio/Quart/live-transcription-quart/requirements.txt +++ /dev/null @@ -1,28 +0,0 @@ -aiofiles==0.8.0 -aiohttp==3.8.5 -aiosignal==1.2.0 -async-timeout==4.0.2 -attrs==21.4.0 -blinker==1.4 -charset-normalizer==2.0.12 -click==8.0.4 -deepgram-sdk==0.2.4 -frozenlist==1.3.0 -h11==0.13.0 -h2==4.1.0 -hpack==4.0.0 -hypercorn==0.13.2 -hyperframe==6.0.1 -idna==3.3 -itsdangerous==2.1.0 -Jinja2==3.0.3 -MarkupSafe==2.1.0 -multidict==6.0.2 -priority==2.0.0 -python-dotenv==0.19.2 -quart==0.16.3 -toml==0.10.2 -websockets==10.2 -Werkzeug==2.0.3 -wsproto==1.1.0 -yarl==1.7.2 diff --git a/sample-projects/streaming-audio/Quart/live-transcription-quart/templates/index.html b/sample-projects/streaming-audio/Quart/live-transcription-quart/templates/index.html deleted file mode 100644 index d2b5d47e..00000000 --- a/sample-projects/streaming-audio/Quart/live-transcription-quart/templates/index.html +++ /dev/null @@ -1,51 +0,0 @@ - - - - Live Transcription - - -

Transcribe Audio With Quart

-

Connection status will go here

-

- - - - - diff --git a/sample-projects/streaming-audio/Quickstart/README.md b/sample-projects/streaming-audio/Quickstart/README.md deleted file mode 100644 index cce9aef3..00000000 --- a/sample-projects/streaming-audio/Quickstart/README.md +++ /dev/null @@ -1,13 +0,0 @@ -# Streaming Audio Transcription - -To run this example, first install the dependencies in the requirements.txt file. - -`pip install -r requirements.txt` - -Replace the API key where it says 'YOUR_DEEPGRAM_API_KEY' - -`DEEPGRAM_API_KEY = 'YOUR_DEEPGRAM_API_KEY'` - -Make sure you're in the directory with the `main.py` file and run the project with the following command. - -`python3 main.py` diff --git a/sample-projects/streaming-audio/Quickstart/main.py b/sample-projects/streaming-audio/Quickstart/main.py deleted file mode 100644 index 94ec97e2..00000000 --- a/sample-projects/streaming-audio/Quickstart/main.py +++ /dev/null @@ -1,54 +0,0 @@ -# Example filename: deepgram_test.py - -from deepgram import Deepgram -import asyncio -import aiohttp - -# Your Deepgram API Key -DEEPGRAM_API_KEY = 'YOUR_DEEPGRAM_API_KEY' - -# URL for the realtime streaming audio you would like to transcribe -URL = 'http://stream.live.vc.bbcmedia.co.uk/bbc_world_service' - - -async def main(): - # Initialize the Deepgram SDK - deepgram = Deepgram(DEEPGRAM_API_KEY) - - # Create a websocket connection to Deepgram - # In this example, punctuation is turned on, interim results are turned off, and language is set to UK English. - try: - deepgramLive = await deepgram.transcription.live({ - 'smart_format': True, - 'interim_results': False, - 'language': 'en-US', - 'model': 'nova', - }) - except Exception as e: - print(f'Could not open socket: {e}') - return - - # Listen for the connection to close - deepgramLive.registerHandler(deepgramLive.event.CLOSE, lambda c: print( - f'Connection closed with code {c}.')) - - # Listen for any transcripts received from Deepgram and write them to the console - deepgramLive.registerHandler(deepgramLive.event.TRANSCRIPT_RECEIVED, print) - - # Listen for the connection to open and send streaming audio from the URL to Deepgram - async with aiohttp.ClientSession() as session: - async with session.get(URL) as audio: - while True: - data = await audio.content.readany() - deepgramLive.send(data) - - # If no data is being sent from the live stream, then break out of the loop. - if not data: - break - - # Indicate that we've finished sending data by sending the customary zero-byte message to the Deepgram streaming endpoint, and wait until we get back the final summary metadata object - await deepgramLive.finish() - -# If running in a Jupyter notebook, Jupyter is already running an event loop, so run main with this line instead: -# await main() -asyncio.run(main()) diff --git a/sample-projects/streaming-audio/Quickstart/requirements.txt b/sample-projects/streaming-audio/Quickstart/requirements.txt deleted file mode 100644 index 17ed4717..00000000 --- a/sample-projects/streaming-audio/Quickstart/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -deepgram-sdk -asyncio -aiohttp \ No newline at end of file From c6d50a2ab45cb6266b9b49db6a3d6fb9b4a8b0b2 Mon Sep 17 00:00:00 2001 From: Sandra Rodgers Date: Thu, 24 Aug 2023 13:39:10 -0500 Subject: [PATCH 10/13] update readme --- examples/README.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/examples/README.md b/examples/README.md index 9225432b..ac2d4cf2 100644 --- a/examples/README.md +++ b/examples/README.md @@ -10,16 +10,23 @@ The example projects are meant to be used to test features locally by contributo If you are contributing changes to this SDK, you can test those changes by using the `prerecorded` or `streaming` projects in the `examples` folder. Here are the steps to follow: -1. Make your changes to the SDK (be sure you are on a branch you have created to do this work). +### Add Your Code +Make your changes to the SDK (be sure you are on a branch you have created to do this work). -2. Install dependencies +### Install dependencies + +Run the following command to install the project dependencies: `pip install -r requirements.txt` +### Edit the API key + Replace the API key where it says 'YOUR_DEEPGRAM_API_KEY' `DEEPGRAM_API_KEY = 'YOUR_DEEPGRAM_API_KEY'` +### Run the project + Make sure you're in the directory with the `main.py` file and run the project with the following command. `python3 main.py` From 4f9c75460a389042e1b337e0ae0f02abde55bace Mon Sep 17 00:00:00 2001 From: Sandra Rodgers Date: Thu, 24 Aug 2023 13:54:22 -0500 Subject: [PATCH 11/13] update readme --- examples/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/README.md b/examples/README.md index ac2d4cf2..41127816 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,4 +1,4 @@ -# Examples Folder - Test Features Locally +# Examples for Testing Features Locally The example projects are meant to be used to test features locally by contributors working on this SDK. In the `requirements.txt` file, the deepgram package used is the local version: From b716fe56574ce3fa248166c1feb796adbe58feb4 Mon Sep 17 00:00:00 2001 From: Sandra Rodgers Date: Mon, 28 Aug 2023 17:20:48 -0500 Subject: [PATCH 12/13] update readme --- examples/README.md | 5 +++-- requirements-dev.txt | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/README.md b/examples/README.md index 41127816..0a8774eb 100644 --- a/examples/README.md +++ b/examples/README.md @@ -11,11 +11,12 @@ The example projects are meant to be used to test features locally by contributo If you are contributing changes to this SDK, you can test those changes by using the `prerecorded` or `streaming` projects in the `examples` folder. Here are the steps to follow: ### Add Your Code + Make your changes to the SDK (be sure you are on a branch you have created to do this work). ### Install dependencies -Run the following command to install the project dependencies: +From inside the `examples/` folder, run the following command to install the project dependencies: `pip install -r requirements.txt` @@ -29,7 +30,7 @@ Replace the API key where it says 'YOUR_DEEPGRAM_API_KEY' Make sure you're in the directory with the `main.py` file and run the project with the following command. -`python3 main.py` +`python main.py` ## How to verify that you're testing the local changes diff --git a/requirements-dev.txt b/requirements-dev.txt index b218315d..7a360ab6 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -8,3 +8,4 @@ aiohttp pytest pytest-asyncio fuzzywuzzy +pytest-cov \ No newline at end of file From 7f981d6d130bd6481d865f92eaafd5d13c048d40 Mon Sep 17 00:00:00 2001 From: Sandra Rodgers Date: Tue, 29 Aug 2023 09:53:54 -0500 Subject: [PATCH 13/13] update example readme --- README.md | 5 +++-- examples/README.md | 36 +++++++++++++++++++++++++----------- examples/prerecorded/main.py | 3 +-- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 49e94e7f..c1f6cb7f 100644 --- a/README.md +++ b/README.md @@ -135,9 +135,10 @@ response = await dg_client.transcription.prerecorded(source, punctuate=True, key ### Setup +Run the following command to install `pytest` and `pytest-cov` as dev dependencies. + ``` -pip install pytest -pip install pytest-cov +pip install -r requirements-dev.txt ``` ### Run All Tests diff --git a/examples/README.md b/examples/README.md index 0a8774eb..4f4467a2 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,28 +1,38 @@ # Examples for Testing Features Locally -The example projects are meant to be used to test features locally by contributors working on this SDK. In the `requirements.txt` file, the deepgram package used is the local version: +The example projects are meant to be used to test features locally by contributors working on this SDK, but they can also be used as quickstarts to get up and running with the Deeegram Python SDK. -``` --e ../../ -``` +Here are the steps to follow to run the examples with the **local version** of the SDK: -## Steps to Test Your Code +## Add Your Code -If you are contributing changes to this SDK, you can test those changes by using the `prerecorded` or `streaming` projects in the `examples` folder. Here are the steps to follow: +Make your changes to the SDK (be sure you are on a branch you have created to do this work). -### Add Your Code +## Install dependencies -Make your changes to the SDK (be sure you are on a branch you have created to do this work). +You can choose between two methods for installing the Deepgram SDK from the local folder: -### Install dependencies +### Install locally with the `examples//requirements.txt` file -From inside the `examples/` folder, run the following command to install the project dependencies: +Move to within the `examples/` folder and run the following command to install the project dependencies. This command will install the dependencies from the local repo due to the package being added as `-e ../../` The`-e` indicates that a package should be installed in "editable" mode, which means in-place from the source code (local folder). `pip install -r requirements.txt` +### Install locally with `pip install -e` + +The other method that can be used to install the Deepgram SDK from the local project is to use `pip install -e`. In this case, you would: + +``` +pip uninstall deepgram-sdk # If it's already installed +cd /path/to/deepgram-python-sdk/ # navigate to inside the deepgram SDK +pip install -e . +``` + +This will install the SDK from the local source code in editable mode, so any changes made inside the project will be instantly usable from the example files. + ### Edit the API key -Replace the API key where it says 'YOUR_DEEPGRAM_API_KEY' +Inside the example file, replace the API key where it says 'YOUR_DEEPGRAM_API_KEY' `DEEPGRAM_API_KEY = 'YOUR_DEEPGRAM_API_KEY'` @@ -32,6 +42,10 @@ Make sure you're in the directory with the `main.py` file and run the project wi `python main.py` +### After testing + +After you have used the example files to test your code, be sure to reset the example file to the way it was when you started (i.e. discard features you may have added to the options dictionary when testing features). + ## How to verify that you're testing the local changes If you want to be sure that you are testing the local `deepgram` package, you can run this check. diff --git a/examples/prerecorded/main.py b/examples/prerecorded/main.py index db229b53..e6c67295 100644 --- a/examples/prerecorded/main.py +++ b/examples/prerecorded/main.py @@ -6,8 +6,7 @@ from deepgram import Deepgram # Your Deepgram API Key -# DEEPGRAM_API_KEY = 'YOUR_DEEPGRAM_API_KEY' -DEEPGRAM_API_KEY = '' +DEEPGRAM_API_KEY = 'YOUR_DEEPGRAM_API_KEY' # Location of the file you want to transcribe. Should include filename and extension. # Example of a local file: ../../Audio/life-moves-pretty-fast.wav