53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
|
|
import sys
|
||
|
|
from pathlib import Path
|
||
|
|
import sherpa_onnx, soundfile as sf
|
||
|
|
from core import args_parser as ap
|
||
|
|
|
||
|
|
def stt_run(args):
|
||
|
|
|
||
|
|
print("Recognize...")
|
||
|
|
recognizer = sherpa_onnx.OfflineRecognizer.from_qwen3_asr( # qwen3 asr
|
||
|
|
conv_frontend = args.conv_frontend,
|
||
|
|
encoder = args.encoder,
|
||
|
|
decoder = args.decoder,
|
||
|
|
tokenizer = args.tokenizer,
|
||
|
|
hotwords = args.hotwords,
|
||
|
|
num_threads = args.num_threads,
|
||
|
|
sample_rate = 16000,
|
||
|
|
feature_dim = 128,
|
||
|
|
provider = args.provider,
|
||
|
|
max_total_len = args.max_total_len,
|
||
|
|
max_new_tokens = args.max_new_tokens,
|
||
|
|
)
|
||
|
|
print("Recognizer ready!")
|
||
|
|
|
||
|
|
for f in args.sounds: # Multi-file
|
||
|
|
if not Path(f).is_file():
|
||
|
|
print(f"Skip. file not found: {f}", file=sys.stderr)
|
||
|
|
continue
|
||
|
|
|
||
|
|
audio, sr = sf.read(f, dtype="float32", always_2d=True)
|
||
|
|
audio = audio[:, 0]
|
||
|
|
|
||
|
|
stream = recognizer.create_stream()
|
||
|
|
|
||
|
|
if args.language:
|
||
|
|
stream.set_option("language", args.language)
|
||
|
|
stream.accept_waveform(sr, audio)
|
||
|
|
|
||
|
|
recognizer.decode_stream(stream) # Inference execution for `stream.result`
|
||
|
|
|
||
|
|
text = stream.result.text
|
||
|
|
|
||
|
|
if "<asr_text>" in text: # qwen3 asr format
|
||
|
|
text = text.split("<asr_text>", 1)[1]
|
||
|
|
|
||
|
|
print()
|
||
|
|
print(f"File : {f}")
|
||
|
|
print(f"Duration : {len(audio) / sr:.2f} s")
|
||
|
|
print(f"Result : {text}")
|
||
|
|
print()
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
stt_run( ap.parser.parse_args() )
|