Create a configuration

This commit is contained in:
Dita Aji Pratama 2026-09-15 14:59:25 +07:00
parent 66a2b136f6
commit a7152733b2
8 changed files with 64 additions and 34 deletions

View File

@ -12,11 +12,27 @@ python3 -m venv .venv
## Usage ## Usage
```bash ```bash
python stt_runner.py \ python stt_runner.py [--language=Indonesian] audio1.wav audio2.wav ...
--conv-frontend=path/conv_frontend.onnx \
--encoder=path/encoder.onnx \
--decoder=path/decoder.onnx \
--tokenizer=path/tokenizer \
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
View File

12
config/asr.py Normal file
View 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
View 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
View File

View 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
View 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")

View File

@ -1,23 +1,25 @@
import sys import sys
from pathlib import Path from pathlib import Path
import sherpa_onnx, soundfile as sf 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): def stt_run(args):
print("Recognize...") print("Recognize...")
recognizer = sherpa_onnx.OfflineRecognizer.from_qwen3_asr( # qwen3 asr recognizer = sherpa_onnx.OfflineRecognizer.from_qwen3_asr( # qwen3 asr
conv_frontend = args.conv_frontend, conv_frontend = str(CONV_FRONTEND),
encoder = args.encoder, encoder = str(ENCODER),
decoder = args.decoder, decoder = str(DECODER),
tokenizer = args.tokenizer, tokenizer = str(TOKENIZER),
hotwords = args.hotwords, hotwords = HOTWORDS,
num_threads = args.num_threads, num_threads = NUM_THREADS,
sample_rate = 16000, sample_rate = SAMPLE_RATE,
feature_dim = 128, feature_dim = FEATURE_DIM,
provider = args.provider, provider = PROVIDER,
max_total_len = args.max_total_len, max_total_len = MAX_TOTAL_LEN,
max_new_tokens = args.max_new_tokens, max_new_tokens = MAX_NEW_TOKENS,
) )
print("Recognizer ready!") print("Recognizer ready!")
@ -31,8 +33,9 @@ def stt_run(args):
stream = recognizer.create_stream() stream = recognizer.create_stream()
if args.language: language = args.language if args.language is not None else LANGUAGE
stream.set_option("language", args.language) if language:
stream.set_option("language", language)
stream.accept_waveform(sr, audio) stream.accept_waveform(sr, audio)
recognizer.decode_stream(stream) # Inference execution for `stream.result` recognizer.decode_stream(stream) # Inference execution for `stream.result`