add offline websocket server/client (#98)
This commit is contained in:
158
python-api-examples/offline-websocket-client-decode-files-paralell.py
Executable file
158
python-api-examples/offline-websocket-client-decode-files-paralell.py
Executable file
@@ -0,0 +1,158 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# Copyright (c) 2023 Xiaomi Corporation
|
||||
|
||||
"""
|
||||
A websocket client for sherpa-onnx-offline-websocket-server
|
||||
|
||||
This file shows how to transcribe multiple
|
||||
files in parallel. We create a separate connection for transcribing each file.
|
||||
|
||||
Usage:
|
||||
./offline-websocket-client-decode-files-parallel.py \
|
||||
--server-addr localhost \
|
||||
--server-port 6006 \
|
||||
/path/to/foo.wav \
|
||||
/path/to/bar.wav \
|
||||
/path/to/16kHz.wav \
|
||||
/path/to/8kHz.wav
|
||||
|
||||
(Note: You have to first start the server before starting the client)
|
||||
|
||||
You can find the server at
|
||||
https://github.com/k2-fsa/sherpa-onnx/blob/master/sherpa-onnx/csrc/offline-websocket-server.cc
|
||||
|
||||
Note: The server is implemented in C++.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import logging
|
||||
import wave
|
||||
from typing import Tuple
|
||||
|
||||
try:
|
||||
import websockets
|
||||
except ImportError:
|
||||
print("please run:")
|
||||
print("")
|
||||
print(" pip install websockets")
|
||||
print("")
|
||||
print("before you run this script")
|
||||
print("")
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
def get_args():
|
||||
parser = argparse.ArgumentParser(
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--server-addr",
|
||||
type=str,
|
||||
default="localhost",
|
||||
help="Address of the server",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--server-port",
|
||||
type=int,
|
||||
default=6006,
|
||||
help="Port of the server",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"sound_files",
|
||||
type=str,
|
||||
nargs="+",
|
||||
help="The input sound file(s) to decode. Each file must be of WAVE"
|
||||
"format with a single channel, and each sample has 16-bit, "
|
||||
"i.e., int16_t. "
|
||||
"The sample rate of the file can be arbitrary and does not need to "
|
||||
"be 16 kHz",
|
||||
)
|
||||
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def read_wave(wave_filename: str) -> Tuple[np.ndarray, int]:
|
||||
"""
|
||||
Args:
|
||||
wave_filename:
|
||||
Path to a wave file. It should be single channel and each sample should
|
||||
be 16-bit. Its sample rate does not need to be 16kHz.
|
||||
Returns:
|
||||
Return a tuple containing:
|
||||
- A 1-D array of dtype np.float32 containing the samples, which are
|
||||
normalized to the range [-1, 1].
|
||||
- sample rate of the wave file
|
||||
"""
|
||||
|
||||
with wave.open(wave_filename) as f:
|
||||
assert f.getnchannels() == 1, f.getnchannels()
|
||||
assert f.getsampwidth() == 2, f.getsampwidth() # it is in bytes
|
||||
num_samples = f.getnframes()
|
||||
samples = f.readframes(num_samples)
|
||||
samples_int16 = np.frombuffer(samples, dtype=np.int16)
|
||||
samples_float32 = samples_int16.astype(np.float32)
|
||||
|
||||
samples_float32 = samples_float32 / 32768
|
||||
return samples_float32, f.getframerate()
|
||||
|
||||
|
||||
async def run(
|
||||
server_addr: str,
|
||||
server_port: int,
|
||||
wave_filename: str,
|
||||
):
|
||||
async with websockets.connect(
|
||||
f"ws://{server_addr}:{server_port}"
|
||||
) as websocket: # noqa
|
||||
logging.info(f"Sending {wave_filename}")
|
||||
samples, sample_rate = read_wave(wave_filename)
|
||||
assert isinstance(sample_rate, int)
|
||||
assert samples.dtype == np.float32, samples.dtype
|
||||
assert samples.ndim == 1, samples.dim
|
||||
buf = sample_rate.to_bytes(4, byteorder="little") # 4 bytes
|
||||
buf += (samples.size * 4).to_bytes(4, byteorder="little")
|
||||
buf += samples.tobytes()
|
||||
|
||||
await websocket.send(buf)
|
||||
|
||||
decoding_results = await websocket.recv()
|
||||
logging.info(f"{wave_filename}\n{decoding_results}")
|
||||
|
||||
# to signal that the client has sent all the data
|
||||
await websocket.send("Done")
|
||||
|
||||
|
||||
async def main():
|
||||
args = get_args()
|
||||
logging.info(vars(args))
|
||||
|
||||
server_addr = args.server_addr
|
||||
server_port = args.server_port
|
||||
sound_files = args.sound_files
|
||||
|
||||
all_tasks = []
|
||||
for wave_filename in sound_files:
|
||||
task = asyncio.create_task(
|
||||
run(
|
||||
server_addr=server_addr,
|
||||
server_port=server_port,
|
||||
wave_filename=wave_filename,
|
||||
)
|
||||
)
|
||||
all_tasks.append(task)
|
||||
|
||||
await asyncio.gather(*all_tasks)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
formatter = (
|
||||
"%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s" # noqa
|
||||
)
|
||||
logging.basicConfig(format=formatter, level=logging.INFO)
|
||||
asyncio.run(main())
|
||||
152
python-api-examples/offline-websocket-client-decode-files-sequential.py
Executable file
152
python-api-examples/offline-websocket-client-decode-files-sequential.py
Executable file
@@ -0,0 +1,152 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# Copyright (c) 2023 Xiaomi Corporation
|
||||
|
||||
"""
|
||||
A websocket client for sherpa-onnx-offline-websocket-server
|
||||
|
||||
This file shows how to use a single connection to transcribe multiple
|
||||
files sequentially.
|
||||
|
||||
Usage:
|
||||
./offline-websocket-client-decode-files-sequential.py \
|
||||
--server-addr localhost \
|
||||
--server-port 6006 \
|
||||
/path/to/foo.wav \
|
||||
/path/to/bar.wav \
|
||||
/path/to/16kHz.wav \
|
||||
/path/to/8kHz.wav
|
||||
|
||||
(Note: You have to first start the server before starting the client)
|
||||
|
||||
You can find the server at
|
||||
https://github.com/k2-fsa/sherpa-onnx/blob/master/sherpa-onnx/csrc/offline-websocket-server.cc
|
||||
|
||||
Note: The server is implemented in C++.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import logging
|
||||
import wave
|
||||
from typing import List, Tuple
|
||||
|
||||
try:
|
||||
import websockets
|
||||
except ImportError:
|
||||
print("please run:")
|
||||
print("")
|
||||
print(" pip install websockets")
|
||||
print("")
|
||||
print("before you run this script")
|
||||
print("")
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
def get_args():
|
||||
parser = argparse.ArgumentParser(
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--server-addr",
|
||||
type=str,
|
||||
default="localhost",
|
||||
help="Address of the server",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--server-port",
|
||||
type=int,
|
||||
default=6006,
|
||||
help="Port of the server",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"sound_files",
|
||||
type=str,
|
||||
nargs="+",
|
||||
help="The input sound file(s) to decode. Each file must be of WAVE"
|
||||
"format with a single channel, and each sample has 16-bit, "
|
||||
"i.e., int16_t. "
|
||||
"The sample rate of the file can be arbitrary and does not need to "
|
||||
"be 16 kHz",
|
||||
)
|
||||
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def read_wave(wave_filename: str) -> Tuple[np.ndarray, int]:
|
||||
"""
|
||||
Args:
|
||||
wave_filename:
|
||||
Path to a wave file. It should be single channel and each sample should
|
||||
be 16-bit. Its sample rate does not need to be 16kHz.
|
||||
Returns:
|
||||
Return a tuple containing:
|
||||
- A 1-D array of dtype np.float32 containing the samples, which are
|
||||
normalized to the range [-1, 1].
|
||||
- sample rate of the wave file
|
||||
"""
|
||||
|
||||
with wave.open(wave_filename) as f:
|
||||
assert f.getnchannels() == 1, f.getnchannels()
|
||||
assert f.getsampwidth() == 2, f.getsampwidth() # it is in bytes
|
||||
num_samples = f.getnframes()
|
||||
samples = f.readframes(num_samples)
|
||||
samples_int16 = np.frombuffer(samples, dtype=np.int16)
|
||||
samples_float32 = samples_int16.astype(np.float32)
|
||||
|
||||
samples_float32 = samples_float32 / 32768
|
||||
return samples_float32, f.getframerate()
|
||||
|
||||
|
||||
async def run(
|
||||
server_addr: str,
|
||||
server_port: int,
|
||||
sound_files: List[str],
|
||||
):
|
||||
async with websockets.connect(
|
||||
f"ws://{server_addr}:{server_port}"
|
||||
) as websocket: # noqa
|
||||
for wave_filename in sound_files:
|
||||
logging.info(f"Sending {wave_filename}")
|
||||
samples, sample_rate = read_wave(wave_filename)
|
||||
assert isinstance(sample_rate, int)
|
||||
assert samples.dtype == np.float32, samples.dtype
|
||||
assert samples.ndim == 1, samples.dim
|
||||
buf = sample_rate.to_bytes(4, byteorder="little") # 4 bytes
|
||||
buf += (samples.size * 4).to_bytes(4, byteorder="little")
|
||||
buf += samples.tobytes()
|
||||
|
||||
await websocket.send(buf)
|
||||
|
||||
decoding_results = await websocket.recv()
|
||||
print(decoding_results)
|
||||
|
||||
# to signal that the client has sent all the data
|
||||
await websocket.send("Done")
|
||||
|
||||
|
||||
async def main():
|
||||
args = get_args()
|
||||
logging.info(vars(args))
|
||||
|
||||
server_addr = args.server_addr
|
||||
server_port = args.server_port
|
||||
sound_files = args.sound_files
|
||||
|
||||
await run(
|
||||
server_addr=server_addr,
|
||||
server_port=server_port,
|
||||
sound_files=sound_files,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
formatter = (
|
||||
"%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s" # noqa
|
||||
)
|
||||
logging.basicConfig(format=formatter, level=logging.INFO)
|
||||
asyncio.run(main())
|
||||
@@ -1,8 +1,15 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
This file demonstrates how to use sherpa-onnx Python API to recognize
|
||||
a single file.
|
||||
This file demonstrates how to use sherpa-onnx Python API to transcribe
|
||||
file(s) with a streaming model.
|
||||
|
||||
Usage:
|
||||
./online-decode-files.py \
|
||||
/path/to/foo.wav \
|
||||
/path/to/bar.wav \
|
||||
/path/to/16kHz.wav \
|
||||
/path/to/8kHz.wav
|
||||
|
||||
Please refer to
|
||||
https://k2-fsa.github.io/sherpa/onnx/index.html
|
||||
@@ -13,17 +20,12 @@ import argparse
|
||||
import time
|
||||
import wave
|
||||
from pathlib import Path
|
||||
from typing import Tuple
|
||||
|
||||
import numpy as np
|
||||
import sherpa_onnx
|
||||
|
||||
|
||||
def assert_file_exists(filename: str):
|
||||
assert Path(
|
||||
filename
|
||||
).is_file(), f"{filename} does not exist!\nPlease refer to https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html to download it"
|
||||
|
||||
|
||||
def get_args():
|
||||
parser = argparse.ArgumentParser(
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter
|
||||
@@ -68,26 +70,58 @@ def get_args():
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--wave-filename",
|
||||
"sound_files",
|
||||
type=str,
|
||||
help="""Path to the wave filename.
|
||||
Should have a single channel with 16-bit samples.
|
||||
It does not need to be 16kHz. It can have any sampling rate.
|
||||
""",
|
||||
nargs="+",
|
||||
help="The input sound file(s) to decode. Each file must be of WAVE"
|
||||
"format with a single channel, and each sample has 16-bit, "
|
||||
"i.e., int16_t. "
|
||||
"The sample rate of the file can be arbitrary and does not need to "
|
||||
"be 16 kHz",
|
||||
)
|
||||
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def assert_file_exists(filename: str):
|
||||
assert Path(filename).is_file(), (
|
||||
f"{filename} does not exist!\n"
|
||||
"Please refer to "
|
||||
"https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html to download it"
|
||||
)
|
||||
|
||||
|
||||
def read_wave(wave_filename: str) -> Tuple[np.ndarray, int]:
|
||||
"""
|
||||
Args:
|
||||
wave_filename:
|
||||
Path to a wave file. It should be single channel and each sample should
|
||||
be 16-bit. Its sample rate does not need to be 16kHz.
|
||||
Returns:
|
||||
Return a tuple containing:
|
||||
- A 1-D array of dtype np.float32 containing the samples, which are
|
||||
normalized to the range [-1, 1].
|
||||
- sample rate of the wave file
|
||||
"""
|
||||
|
||||
with wave.open(wave_filename) as f:
|
||||
assert f.getnchannels() == 1, f.getnchannels()
|
||||
assert f.getsampwidth() == 2, f.getsampwidth() # it is in bytes
|
||||
num_samples = f.getnframes()
|
||||
samples = f.readframes(num_samples)
|
||||
samples_int16 = np.frombuffer(samples, dtype=np.int16)
|
||||
samples_float32 = samples_int16.astype(np.float32)
|
||||
|
||||
samples_float32 = samples_float32 / 32768
|
||||
return samples_float32, f.getframerate()
|
||||
|
||||
|
||||
def main():
|
||||
args = get_args()
|
||||
assert_file_exists(args.encoder)
|
||||
assert_file_exists(args.decoder)
|
||||
assert_file_exists(args.joiner)
|
||||
assert_file_exists(args.tokens)
|
||||
if not Path(args.wave_filename).is_file():
|
||||
print(f"{args.wave_filename} does not exist!")
|
||||
return
|
||||
|
||||
recognizer = sherpa_onnx.OnlineRecognizer(
|
||||
tokens=args.tokens,
|
||||
@@ -99,42 +133,44 @@ def main():
|
||||
feature_dim=80,
|
||||
decoding_method=args.decoding_method,
|
||||
)
|
||||
with wave.open(args.wave_filename) as f:
|
||||
# If the wave file has a different sampling rate from the one
|
||||
# expected by the model (16 kHz in our case), we will do
|
||||
# resampling inside sherpa-onnx
|
||||
wave_file_sample_rate = f.getframerate()
|
||||
|
||||
assert f.getnchannels() == 1, f.getnchannels()
|
||||
assert f.getsampwidth() == 2, f.getsampwidth() # it is in bytes
|
||||
num_samples = f.getnframes()
|
||||
samples = f.readframes(num_samples)
|
||||
samples_int16 = np.frombuffer(samples, dtype=np.int16)
|
||||
samples_float32 = samples_int16.astype(np.float32)
|
||||
|
||||
samples_float32 = samples_float32 / 32768
|
||||
|
||||
duration = len(samples_float32) / wave_file_sample_rate
|
||||
|
||||
start_time = time.time()
|
||||
print("Started!")
|
||||
start_time = time.time()
|
||||
|
||||
stream = recognizer.create_stream()
|
||||
streams = []
|
||||
total_duration = 0
|
||||
for wave_filename in args.sound_files:
|
||||
assert_file_exists(wave_filename)
|
||||
samples, sample_rate = read_wave(wave_filename)
|
||||
duration = len(samples) / sample_rate
|
||||
total_duration += duration
|
||||
|
||||
stream.accept_waveform(wave_file_sample_rate, samples_float32)
|
||||
s = recognizer.create_stream()
|
||||
s.accept_waveform(sample_rate, samples)
|
||||
|
||||
tail_paddings = np.zeros(int(0.2 * wave_file_sample_rate), dtype=np.float32)
|
||||
stream.accept_waveform(wave_file_sample_rate, tail_paddings)
|
||||
tail_paddings = np.zeros(int(0.2 * sample_rate), dtype=np.float32)
|
||||
s.accept_waveform(sample_rate, tail_paddings)
|
||||
|
||||
stream.input_finished()
|
||||
s.input_finished()
|
||||
|
||||
while recognizer.is_ready(stream):
|
||||
recognizer.decode_stream(stream)
|
||||
streams.append(s)
|
||||
|
||||
print(recognizer.get_result(stream))
|
||||
|
||||
print("Done!")
|
||||
while True:
|
||||
ready_list = []
|
||||
for s in streams:
|
||||
if recognizer.is_ready(s):
|
||||
ready_list.append(s)
|
||||
if len(ready_list) == 0:
|
||||
break
|
||||
recognizer.decode_streams(ready_list)
|
||||
results = [recognizer.get_result(s) for s in streams]
|
||||
end_time = time.time()
|
||||
print("Done!")
|
||||
|
||||
for wave_filename, result in zip(args.sound_files, results):
|
||||
print(f"{wave_filename}\n{result}")
|
||||
print("-" * 10)
|
||||
|
||||
elapsed_seconds = end_time - start_time
|
||||
rtf = elapsed_seconds / duration
|
||||
print(f"num_threads: {args.num_threads}")
|
||||
@@ -27,7 +27,6 @@ https://github.com/k2-fsa/sherpa-onnx/blob/master/sherpa-onnx/csrc/online-websoc
|
||||
import argparse
|
||||
import asyncio
|
||||
import logging
|
||||
import time
|
||||
import wave
|
||||
|
||||
try:
|
||||
|
||||
5
python-api-examples/online-websocket-client-microphone.py
Normal file → Executable file
5
python-api-examples/online-websocket-client-microphone.py
Normal file → Executable file
@@ -24,13 +24,12 @@ https://github.com/k2-fsa/sherpa-onnx/blob/master/sherpa-onnx/csrc/online-websoc
|
||||
import argparse
|
||||
import asyncio
|
||||
import sys
|
||||
import time
|
||||
|
||||
import numpy as np
|
||||
|
||||
try:
|
||||
import sounddevice as sd
|
||||
except ImportError as e:
|
||||
except ImportError:
|
||||
print("Please install sounddevice first. You can use")
|
||||
print()
|
||||
print(" pip install sounddevice")
|
||||
@@ -134,7 +133,7 @@ async def run(
|
||||
await websocket.send(indata.tobytes())
|
||||
|
||||
decoding_results = await receive_task
|
||||
print("\nFinal result is:\n{decoding_results}")
|
||||
print(f"\nFinal result is:\n{decoding_results}")
|
||||
|
||||
|
||||
async def main():
|
||||
|
||||
@@ -13,7 +13,7 @@ from pathlib import Path
|
||||
|
||||
try:
|
||||
import sounddevice as sd
|
||||
except ImportError as e:
|
||||
except ImportError:
|
||||
print("Please install sounddevice first. You can use")
|
||||
print()
|
||||
print(" pip install sounddevice")
|
||||
@@ -25,9 +25,11 @@ import sherpa_onnx
|
||||
|
||||
|
||||
def assert_file_exists(filename: str):
|
||||
assert Path(
|
||||
filename
|
||||
).is_file(), f"{filename} does not exist!\nPlease refer to https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html to download it"
|
||||
assert Path(filename).is_file(), (
|
||||
f"{filename} does not exist!\n"
|
||||
"Please refer to "
|
||||
"https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html to download it"
|
||||
)
|
||||
|
||||
|
||||
def get_args():
|
||||
|
||||
@@ -12,7 +12,7 @@ from pathlib import Path
|
||||
|
||||
try:
|
||||
import sounddevice as sd
|
||||
except ImportError as e:
|
||||
except ImportError:
|
||||
print("Please install sounddevice first. You can use")
|
||||
print()
|
||||
print(" pip install sounddevice")
|
||||
@@ -24,9 +24,11 @@ import sherpa_onnx
|
||||
|
||||
|
||||
def assert_file_exists(filename: str):
|
||||
assert Path(
|
||||
filename
|
||||
).is_file(), f"{filename} does not exist!\nPlease refer to https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html to download it"
|
||||
assert Path(filename).is_file(), (
|
||||
f"{filename} does not exist!\n"
|
||||
"Please refer to "
|
||||
"https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html to download it"
|
||||
)
|
||||
|
||||
|
||||
def get_args():
|
||||
|
||||
Reference in New Issue
Block a user