generate-streaming-asr.py
5.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python3
import argparse
from dataclasses import dataclass
import jinja2
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--total",
type=int,
default=1,
help="Number of runners",
)
parser.add_argument(
"--index",
type=int,
default=0,
help="Index of the current runner",
)
return parser.parse_args()
@dataclass
class Model:
# We will download
# https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/{model_name}.tar.bz2
model_name: str
# The type of the model, e..g, 0, 1, 2. It is hardcoded in the flutter code
# See flutter-example/streaming_asr/lib/online_model.dart
idx: int
# e.g., zh, en, zh_en
lang: str
# e.g., whisper, paraformer, zipformer
short_name: str = ""
# cmd is used to remove extra files from the model directory
cmd: str = ""
rule_fsts: str = ""
def get_models():
models = [
Model(
model_name="sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20",
idx=0,
lang="bilingual_zh_en",
short_name="zipformer",
rule_fsts="itn_zh_number.fst",
cmd="""
if [ ! -f itn_zh_number.fst ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/itn_zh_number.fst
fi
pushd $model_name
rm -fv encoder-epoch-99-avg-1.onnx
rm -fv decoder-epoch-99-avg-1.int8.onnx
rm -fv joiner-epoch-99-avg-1.int8.onnx
rm -fv *.sh
rm -fv bpe.model
rm -fv README.md
rm -fv .gitattributes
rm -fv *state*
rm -rfv test_wavs
ls -lh
popd
""",
),
Model(
model_name="sherpa-onnx-streaming-zipformer-en-2023-06-26",
idx=1,
lang="en",
short_name="zipformer2",
cmd="""
pushd $model_name
rm -fv encoder-epoch-99-avg-1-chunk-16-left-128.onnx
rm -fv decoder-epoch-99-avg-1-chunk-16-left-128.int8.onnx
rm -fv joiner-epoch-99-avg-1-chunk-16-left-128.int8.onnx
rm -fv README.md
rm -fv bpe.model
rm -rfv test_wavs
ls -lh
popd
""",
),
Model(
model_name="icefall-asr-zipformer-streaming-wenetspeech-20230615",
idx=2,
lang="zh",
short_name="zipformer2",
rule_fsts="itn_zh_number.fst",
cmd="""
if [ ! -f itn_zh_number.fst ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/itn_zh_number.fst
fi
pushd $model_name
rm -fv exp/encoder-epoch-12-avg-4-chunk-16-left-128.onnx
rm -fv exp/decoder-epoch-12-avg-4-chunk-16-left-128.int8.onnx
rm -fv exp/joiner-epoch-12-avg-4-chunk-16-left-128.int8.onnx
rm -fv data/lang_char/lexicon.txt
rm -fv data/lang_char/words.txt
rm -rfv test_wavs
rm -fv README.md
ls -lh exp/
ls -lh data/lang_char
popd
""",
),
Model(
model_name="sherpa-onnx-streaming-zipformer-fr-2023-04-14",
idx=3,
lang="fr",
short_name="zipformer",
cmd="""
pushd $model_name
rm -fv encoder-epoch-29-avg-9-with-averaged-model.onnx
rm -fv decoder-epoch-29-avg-9-with-averaged-model.int8.onnx
rm -fv joiner-epoch-29-avg-9-with-averaged-model.int8.onnx
rm -fv *.sh
rm -rfv test_wavs
rm README.md
ls -lh
popd
""",
),
]
return models
def main():
args = get_args()
index = args.index
total = args.total
assert 0 <= index < total, (index, total)
all_model_list = get_models()
num_models = len(all_model_list)
num_per_runner = num_models // total
if num_per_runner <= 0:
raise ValueError(f"num_models: {num_models}, num_runners: {total}")
start = index * num_per_runner
end = start + num_per_runner
remaining = num_models - args.total * num_per_runner
print(f"{index}/{total}: {start}-{end}/{num_models}")
d = dict()
d["model_list"] = all_model_list[start:end]
if index < remaining:
s = args.total * num_per_runner + index
d["model_list"].append(all_model_list[s])
print(f"{s}/{num_models}")
filename_list = [
"./build-macos-streaming-asr.sh",
"./build-windows-streaming-asr.sh",
]
for filename in filename_list:
environment = jinja2.Environment()
with open(f"{filename}.in") as f:
s = f.read()
template = environment.from_string(s)
s = template.render(**d)
with open(filename, "w") as f:
print(s, file=f)
if __name__ == "__main__":
main()