Create a configuration
This commit is contained in:
parent
66a2b136f6
commit
a7152733b2
28
README.md
28
README.md
@ -12,11 +12,27 @@ python3 -m venv .venv
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
python stt_runner.py \
|
||||
--conv-frontend=path/conv_frontend.onnx \
|
||||
--encoder=path/encoder.onnx \
|
||||
--decoder=path/decoder.onnx \
|
||||
--tokenizer=path/tokenizer \
|
||||
audio1.wav audio2.wav ...
|
||||
python stt_runner.py [--language=Indonesian] audio1.wav audio2.wav ...
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
Model paths and inference parameters are hardcoded in `config/`:
|
||||
|
||||
- `config/model.py` — model paths (conv_frontend, encoder, decoder, tokenizer under `models/`)
|
||||
- `config/asr.py` — inference params: `LANGUAGE`, `HOTWORDS`, `NUM_THREADS`, `PROVIDER`, `SAMPLE_RATE`, `FEATURE_DIM`, `MAX_TOTAL_LEN`, `MAX_NEW_TOKENS`
|
||||
|
||||
`LANGUAGE` defaults to `""` (all languages / auto-detect). Passing `--language` on the CLI overrides it.
|
||||
|
||||
## Download Model (Qwen3-ASR 1.7B int8)
|
||||
|
||||
```bash
|
||||
BASE="https://modelscope.cn/models/zengshuishui/Qwen3-ASR-onnx/resolve/master"
|
||||
mkdir -p models/model_1.7B models/tokenizer
|
||||
wget -O models/model_1.7B/conv_frontend.onnx "$BASE/model_1.7B/conv_frontend.onnx"
|
||||
wget -O models/model_1.7B/encoder.int8.onnx "$BASE/model_1.7B/encoder.int8.onnx"
|
||||
wget -O models/model_1.7B/decoder.int8.onnx "$BASE/model_1.7B/decoder.int8.onnx"
|
||||
for f in vocab.json merges.txt tokenizer_config.json preprocessor_config.json config.json chat_template.json; do
|
||||
wget -O "models/tokenizer/$f" "$BASE/tokenizer/$f"
|
||||
done
|
||||
```
|
||||
0
config/__init__.py
Normal file
0
config/__init__.py
Normal file
12
config/asr.py
Normal file
12
config/asr.py
Normal file
@ -0,0 +1,12 @@
|
||||
NUM_THREADS = 2
|
||||
SAMPLE_RATE = 16000
|
||||
FEATURE_DIM = 128
|
||||
PROVIDER = "cpu"
|
||||
MAX_TOTAL_LEN = 2048
|
||||
MAX_NEW_TOKENS = 256
|
||||
|
||||
LANGUAGE = "" # "" = support all languages / auto-detect
|
||||
HOTWORDS = "" # Comma-separated hotword phrases, e.g. "AcmeCorp, FooBar".
|
||||
# Biases the model to transcribe these phrases correctly,
|
||||
# useful for brand/product/person names the model may
|
||||
# otherwise mishear. Leave empty to disable.
|
||||
8
config/model.py
Normal file
8
config/model.py
Normal file
@ -0,0 +1,8 @@
|
||||
from pathlib import Path
|
||||
|
||||
MODEL_DIR = Path(__file__).resolve().parent.parent / "models"
|
||||
|
||||
CONV_FRONTEND = MODEL_DIR / "model_1.7B" / "conv_frontend.onnx"
|
||||
ENCODER = MODEL_DIR / "model_1.7B" / "encoder.int8.onnx"
|
||||
DECODER = MODEL_DIR / "model_1.7B" / "decoder.int8.onnx"
|
||||
TOKENIZER = MODEL_DIR / "tokenizer"
|
||||
0
core/__init__.py
Normal file
0
core/__init__.py
Normal file
@ -1,14 +0,0 @@
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--conv-frontend", type=str, required=True)
|
||||
parser.add_argument("--encoder", type=str, required=True)
|
||||
parser.add_argument("--decoder", type=str, required=True)
|
||||
parser.add_argument("--tokenizer", type=str, required=True)
|
||||
parser.add_argument("--language", type=str, default="", help="Force language, e.g. Indonesian, English, Chinese")
|
||||
parser.add_argument("--hotwords", type=str, default="", help="Comma-separated hotword phrases, e.g. 'foo,bar'")
|
||||
parser.add_argument("--num-threads", type=int, default=2)
|
||||
parser.add_argument("--provider", type=str, default="cpu", choices=["cpu", "cuda"])
|
||||
parser.add_argument("--max-total-len", type=int, default=2048)
|
||||
parser.add_argument("--max-new-tokens", type=int, default=256)
|
||||
parser.add_argument("sounds", nargs="+", help="Audio files to transcribe")
|
||||
5
core/argsroom.py
Normal file
5
core/argsroom.py
Normal file
@ -0,0 +1,5 @@
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--language", type=str, default=None, help="Force language, overrides config LANGUAGE (e.g. Indonesian, English, Chinese)")
|
||||
parser.add_argument("sounds", nargs="+", help="Audio files to transcribe")
|
||||
@ -1,23 +1,25 @@
|
||||
import sys
|
||||
from pathlib import Path
|
||||
import sherpa_onnx, soundfile as sf
|
||||
from core import args_parser as ap
|
||||
from core import argsroom as ap
|
||||
from config.model import CONV_FRONTEND, ENCODER, DECODER, TOKENIZER
|
||||
from config.asr import LANGUAGE, HOTWORDS, NUM_THREADS, SAMPLE_RATE, FEATURE_DIM, PROVIDER, MAX_TOTAL_LEN, MAX_NEW_TOKENS
|
||||
|
||||
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,
|
||||
conv_frontend = str(CONV_FRONTEND),
|
||||
encoder = str(ENCODER),
|
||||
decoder = str(DECODER),
|
||||
tokenizer = str(TOKENIZER),
|
||||
hotwords = HOTWORDS,
|
||||
num_threads = NUM_THREADS,
|
||||
sample_rate = SAMPLE_RATE,
|
||||
feature_dim = FEATURE_DIM,
|
||||
provider = PROVIDER,
|
||||
max_total_len = MAX_TOTAL_LEN,
|
||||
max_new_tokens = MAX_NEW_TOKENS,
|
||||
)
|
||||
print("Recognizer ready!")
|
||||
|
||||
@ -31,8 +33,9 @@ def stt_run(args):
|
||||
|
||||
stream = recognizer.create_stream()
|
||||
|
||||
if args.language:
|
||||
stream.set_option("language", args.language)
|
||||
language = args.language if args.language is not None else LANGUAGE
|
||||
if language:
|
||||
stream.set_option("language", language)
|
||||
stream.accept_waveform(sr, audio)
|
||||
|
||||
recognizer.decode_stream(stream) # Inference execution for `stream.result`
|
||||
|
||||
Loading…
Reference in New Issue
Block a user