generated from deepgram-starters/project-template
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from deepgram-starters/sr/adding-test
Sr/adding test
- Loading branch information
Showing
4 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
name: Update Deepgram SDK | ||
|
||
on: | ||
schedule: | ||
- cron: '0 0 * * 1' # Runs every Monday at midnight | ||
workflow_dispatch: | ||
|
||
jobs: | ||
check-update: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.10' | ||
|
||
- name: Install required tools | ||
run: sudo apt-get install -y jq curl | ||
|
||
- name: Get current Deepgram Python SDK version | ||
id: get-deepgram-version | ||
run: | | ||
DEEPGRAM_VERSION=$(curl -s https://api.github.com/repos/deepgram/deepgram-python-sdk/releases/latest | jq -r '.tag_name') | ||
echo "version=$DEEPGRAM_VERSION" >> $GITHUB_ENV | ||
- name: Check installed Deepgram SDK version | ||
id: check-installed-version | ||
run: | | ||
INSTALLED_VERSION=$(pip show deepgram-sdk | grep Version | cut -d' ' -f2) | ||
echo "installed_version=$INSTALLED_VERSION" >> $GITHUB_ENV | ||
- name: Compare versions and update if necessary | ||
run: | | ||
LATEST_VERSION=${{ env.version }} | ||
INSTALLED_VERSION=${{ env.installed_version }} | ||
if [ "$LATEST_VERSION" != "$INSTALLED_VERSION" ]; then | ||
echo "Updating Deepgram SDK from $INSTALLED_VERSION to $LATEST_VERSION" | ||
pip install deepgram-sdk==$LATEST_VERSION | ||
git config --global user.name 'github-actions[bot]' | ||
git config --global user.email 'github-actions[bot]@users.noreply.github.com' | ||
git add requirements.txt | ||
# Check if there are changes to commit | ||
if git diff-index --quiet HEAD --; then | ||
echo "No changes to commit" | ||
else | ||
git commit -m "chore: update Deepgram SDK to $LATEST_VERSION" | ||
git push | ||
fi | ||
else | ||
echo "Deepgram SDK is up to date" | ||
fi | ||
- name: Install dependencies | ||
run: pip install -r requirements.txt | ||
|
||
- name: Install dev dependencies | ||
run: pip install -r requirements-dev.txt | ||
|
||
- name: Run tests | ||
id: run-tests | ||
env: | ||
DEEPGRAM_API_KEY: ${{ secrets.DEEPGRAM_API_KEY }} | ||
run: pytest tests/ | ||
|
||
- name: Notify on failure | ||
if: failure() | ||
uses: slackapi/[email protected] | ||
with: | ||
payload: | | ||
{ | ||
"text": "The tests have FAILED for ${{ github.repository }}." | ||
} | ||
env: | ||
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} | ||
|
||
- name: Notify on success | ||
if: success() | ||
uses: slackapi/[email protected] | ||
with: | ||
payload: | | ||
{ | ||
"text": "The tests have passed for ${{ github.repository }}." | ||
} | ||
env: | ||
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# pip install -r requirements.txt | ||
|
||
# Testing | ||
pytest | ||
httpx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import os | ||
import httpx | ||
from dotenv import load_dotenv | ||
import pytest | ||
from deepgram import DeepgramClient, LiveTranscriptionEvents, LiveOptions | ||
import threading | ||
import time | ||
|
||
load_dotenv() | ||
|
||
DEEPGRAM_API_KEY = os.getenv("DEEPGRAM_API_KEY") | ||
|
||
URL = "http://stream.live.vc.bbcmedia.co.uk/bbc_world_service" | ||
|
||
transcript_received_event = threading.Event() | ||
|
||
def test_deepgram_live_transcription(): | ||
assert DEEPGRAM_API_KEY is not None, "DEEPGRAM_API_KEY is not set" | ||
|
||
deepgram: DeepgramClient = DeepgramClient(DEEPGRAM_API_KEY) | ||
|
||
print("Deepgram client created", deepgram) | ||
|
||
dg_connection = deepgram.listen.live.v("1") | ||
|
||
def on_open(self, open, **kwargs): | ||
print(f"\n\n{open}\n\n") | ||
|
||
def on_message(self, result, **kwargs): | ||
print("Received message") | ||
sentence = result.channel.alternatives[0].transcript | ||
assert sentence is not None, "No transcript received" | ||
print(sentence) | ||
if sentence: | ||
transcript_received_event.set() | ||
|
||
def on_close(self, close, **kwargs): | ||
print(f"\n\n{close}\n\n") | ||
|
||
def on_error(self, error, **kwargs): | ||
print(f"\n\n{error}\n\n") | ||
|
||
dg_connection.on(LiveTranscriptionEvents.Open, on_open) | ||
dg_connection.on(LiveTranscriptionEvents.Transcript, on_message) | ||
dg_connection.on(LiveTranscriptionEvents.Close, on_close) | ||
dg_connection.on(LiveTranscriptionEvents.Error, on_error) | ||
|
||
options = LiveOptions(model="nova-2", language="en-US") | ||
|
||
if not dg_connection.start(options): | ||
print("Failed to start connection") | ||
return | ||
|
||
print("Connection started") | ||
|
||
try: | ||
with httpx.stream("GET", URL) as r: | ||
start_time = time.time() | ||
for data in r.iter_bytes(): | ||
dg_connection.send(data) | ||
# Wait for the transcript or timeout after 10 seconds | ||
if transcript_received_event.wait(timeout=3): | ||
break | ||
# Break the loop after a certain period to avoid an infinite loop | ||
if time.time() - start_time > 20: | ||
break | ||
except Exception as e: | ||
pytest.fail(f"Failed to stream data: {e}") | ||
|
||
dg_connection.finish() | ||
|
||
print("Finished") |