-
Notifications
You must be signed in to change notification settings - Fork 0
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 #1 from speechmatics/v0.0.1
Add speechmatics flow client
- Loading branch information
Showing
25 changed files
with
1,572 additions
and
1 deletion.
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,3 @@ | ||
[report] | ||
exclude_lines = | ||
if __name__ == .__main__.: |
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,2 @@ | ||
*.mp3 filter=lfs diff=lfs merge=lfs -text | ||
*.wav filter=lfs diff=lfs merge=lfs -text |
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,27 @@ | ||
--- | ||
name: Bug report | ||
about: Create a report to help us improve | ||
title: '' | ||
labels: bug | ||
assignees: '' | ||
|
||
--- | ||
|
||
**Describe the bug** | ||
A clear and concise description of what the bug is. | ||
|
||
**To Reproduce** | ||
Steps to reproduce the behavior: | ||
1. Go to '...' | ||
2. Click on '....' | ||
3. Scroll down to '....' | ||
4. See error | ||
|
||
**Expected behavior** | ||
A clear and concise description of what you expected to happen. | ||
|
||
**Screenshots/Logs** | ||
If applicable, add screenshots or logs to help explain your problem. | ||
|
||
**Additional context** | ||
Add any other context about the problem here. |
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,20 @@ | ||
--- | ||
name: Feature request | ||
about: Suggest an idea for this project | ||
title: '' | ||
labels: 'request' | ||
assignees: '' | ||
|
||
--- | ||
|
||
**Is your feature request related to a problem? Please describe.** | ||
A clear and concise description of what the problem is, e.g. I'm always frustrated when [...] | ||
|
||
**Describe the solution you'd like** | ||
A clear and concise description of what you want to happen. | ||
|
||
**Describe alternatives you've considered** | ||
A clear and concise description of any alternative solutions or features you've considered. | ||
|
||
**Additional context** | ||
Add any other context or screenshots about the feature request here. |
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,33 @@ | ||
# This workflow will upload a Python Package using Twine when a release is created | ||
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries | ||
|
||
name: Publish | ||
|
||
on: | ||
release: | ||
types: [released] | ||
|
||
jobs: | ||
deploy: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: '3.x' | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install setuptools wheel twine | ||
- name: Build and publish to PyPI | ||
env: | ||
TWINE_USERNAME: '__token__' | ||
TWINE_PASSWORD: ${{ secrets.PYPI_ORG_TOKEN }} | ||
run: | | ||
make build | ||
twine upload dist/* |
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,39 @@ | ||
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions | ||
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions | ||
|
||
name: Tests | ||
|
||
on: [ push, pull_request ] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: [ "3.10", "3.11" ] | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Install portaudio | ||
run: | | ||
sudo apt-get update | ||
sudo apt-get install portaudio19-dev | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Install dependencies | ||
run: pip3 install -r requirements.txt -r requirements-dev.txt | ||
|
||
- name: Install package | ||
run: python3 setup.py install | ||
|
||
- name: Lint | ||
run: make lint | ||
|
||
- name: Unittest | ||
run: make unittest |
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,11 @@ | ||
# Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). | ||
|
||
## [0.0.1] - 2024-10-14 | ||
|
||
### Added | ||
|
||
- Add speechmatics-flow client |
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,25 @@ | ||
SOURCES := speechmatics_flow/ tests/ setup.py | ||
VERSION ?= $(shell cat VERSION) | ||
|
||
.PHONY: all | ||
all: lint test | ||
|
||
.PHONY: lint | ||
lint: | ||
black --check --diff $(SOURCES) | ||
ruff $(SOURCES) | ||
|
||
.PHONY: format | ||
format: | ||
black $(SOURCES) | ||
|
||
.PHONY: test | ||
test: unittest | ||
|
||
.PHONY: unittest | ||
unittest: | ||
pytest -v tests | ||
|
||
.PHONY: build | ||
build: | ||
VERSION=$(VERSION) python setup.py sdist bdist_wheel |
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 |
---|---|---|
@@ -1 +1,39 @@ | ||
# speechmatics-flow | ||
# speechmatics-flow | ||
|
||
Python client library and CLI for Speechmatics' Flow Service API. | ||
|
||
## Getting started | ||
|
||
To install from PyPI: | ||
|
||
```bash | ||
pip install speechmatics-flow | ||
``` | ||
|
||
To install from source: | ||
|
||
```bash | ||
git clone https://github.com/speechmatics/speechmatics-flow | ||
cd speechmatics-flow && python setup.py install | ||
``` | ||
|
||
Windows users may need to run the install command with an extra flag: | ||
|
||
```bash | ||
python setup.py install --user | ||
``` | ||
|
||
## Example command-line usage | ||
|
||
- Setting URLs for connecting to flow service. These values can be used in places of the --url flag: | ||
|
||
*Note: Requires access to microphone | ||
|
||
```bash | ||
speechmatics-flow --url $URL --auth-token $TOKEN - | ||
``` | ||
|
||
## Support | ||
|
||
If you have any issues with this library or encounter any bugs then please get in touch with us at | ||
[email protected]. |
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 @@ | ||
0.0.1 |
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,85 @@ | ||
import asyncio | ||
import io | ||
import ssl | ||
import sys | ||
|
||
import pyaudio | ||
|
||
from speechmatics_flow.client import WebsocketClient | ||
from speechmatics_flow.models import ( | ||
ConnectionSettings, | ||
Interaction, | ||
AudioSettings, | ||
ConversationConfig, | ||
ServerMessageType, | ||
) | ||
|
||
AUTH_TOKEN = "YOUR TOKEN HERE" | ||
|
||
|
||
# Create a websocket client | ||
ssl_context = ssl.create_default_context() | ||
ssl_context.check_hostname = False | ||
ssl_context.verify_mode = ssl.CERT_NONE | ||
client = WebsocketClient( | ||
ConnectionSettings( | ||
url="wss://flow.api.speechmatics.com/v1/flow", | ||
auth_token=AUTH_TOKEN, | ||
ssl_context=None, | ||
) | ||
) | ||
|
||
# Create a buffer to store binary messages sent from the server | ||
audio_buffer = io.BytesIO() | ||
|
||
|
||
# Create callback function which adds binary messages to audio buffer | ||
def binary_msg_handler(msg: bytes): | ||
if isinstance(msg, (bytes, bytearray)): | ||
audio_buffer.write(msg) | ||
|
||
|
||
# Register the callback to be called when the client receives an audio message from the server | ||
client.add_event_handler(ServerMessageType.audio, binary_msg_handler) | ||
|
||
|
||
async def audio_playback(): | ||
"""Read from buffer and play audio back to the user""" | ||
p = pyaudio.PyAudio() | ||
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, output=True) | ||
try: | ||
while True: | ||
# Get the current value from the buffer | ||
audio_to_play = audio_buffer.getvalue() | ||
# Only proceed if there is audio data to play | ||
if audio_to_play: | ||
# Write the audio to the stream | ||
stream.write(audio_to_play) | ||
audio_buffer.seek(0) | ||
audio_buffer.truncate(0) | ||
# Pause briefly before checking the buffer again | ||
await asyncio.sleep(0.05) | ||
finally: | ||
stream.close() | ||
stream.stop_stream() | ||
p.terminate() | ||
|
||
|
||
async def main(): | ||
tasks = [ | ||
# Use the websocket to connect to Flow Service and start a conversation | ||
asyncio.create_task( | ||
client.run( | ||
interactions=[Interaction(sys.stdin.buffer)], | ||
audio_settings=AudioSettings(), | ||
conversation_config=ConversationConfig(), | ||
) | ||
), | ||
# Run audio playback handler which streams audio from audio buffer | ||
asyncio.create_task(audio_playback()), | ||
] | ||
|
||
await asyncio.gather(*tasks) | ||
|
||
|
||
asyncio.run(main()) |
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,3 @@ | ||
[tool.ruff] | ||
# Allow lines to be as long as 120 characters. | ||
line-length = 120 |
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,2 @@ | ||
[pytest] | ||
addopts = -ra --full-trace --cov=speechmatics_flow --cov-branch -o asyncio_mode=auto |
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,6 @@ | ||
pytest==7.1.1 | ||
pytest-mock==3.7.0 | ||
black==22.3.0 | ||
ruff==0.0.280 | ||
pre-commit==2.21.0 | ||
pytest-cov==3.0.0 |
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,3 @@ | ||
httpx==0.27.1 | ||
pyaudio==0.2.14 | ||
websockets>=10 |
Oops, something went wrong.