File size: 11,367 Bytes
2267fac
 
 
 
8f39105
2267fac
c9ba89c
ac87612
2267fac
 
 
d03c698
2267fac
8f39105
 
2267fac
 
 
 
 
 
 
 
 
 
168b5c0
03aae6c
2267fac
03aae6c
f392af0
c9ba89c
d03c698
59cce29
 
 
03aae6c
59cce29
03aae6c
59cce29
 
 
 
 
03aae6c
59cce29
03aae6c
59cce29
 
7903607
 
 
03aae6c
7903607
03aae6c
f392af0
c9ba89c
3e3be60
 
 
 
 
 
 
 
 
 
 
 
 
59cce29
 
 
 
 
03aae6c
59cce29
03aae6c
59cce29
 
 
 
d03c698
 
 
03aae6c
d03c698
03aae6c
d03c698
59cce29
 
 
 
 
 
03aae6c
59cce29
03aae6c
59cce29
 
2267fac
 
 
 
c9ba89c
 
2267fac
c9ba89c
2267fac
c9ba89c
8f39105
c9ba89c
 
 
 
 
 
2267fac
c9ba89c
8f39105
c9ba89c
 
 
 
 
 
 
 
8f39105
c9ba89c
 
 
 
 
 
 
 
8f39105
c9ba89c
 
 
 
 
 
 
 
8f39105
c9ba89c
 
 
 
 
 
2267fac
 
8dc832e
4281a4a
2267fac
 
3194abe
2267fac
 
f392af0
2267fac
f392af0
2267fac
 
 
 
8f39105
d8e6dc5
8f39105
2267fac
 
 
 
 
 
 
 
 
 
26dfa9a
2267fac
 
 
d03c698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e3be60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9ba89c
3194abe
168b5c0
c9ba89c
2267fac
8f39105
2267fac
ac87612
c9ba89c
2267fac
 
c9ba89c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac87612
d03c698
168b5c0
 
 
c9ba89c
168b5c0
d03c698
 
 
c9ba89c
d03c698
74df484
 
 
 
 
168b5c0
d03c698
168b5c0
2267fac
 
 
 
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from enum import Enum
from functools import lru_cache
import logging
import os
import platform
from pathlib import Path

import huggingface_hub
import sherpa
import sherpa_onnx

main_logger = logging.getLogger("main")


class EnumDecodingMethod(Enum):
    greedy_search = "greedy_search"
    modified_beam_search = "modified_beam_search"


model_map = {
    "Chinese": [
        {
            "repo_id": "csukuangfj/wenet-chinese-model",
            "nn_model_file": "final.zip",
            "nn_model_file_sub_folder": ".",
            "tokens_file": "units.txt",
            "tokens_file_sub_folder": ".",
            "normalize_samples": False,
            "loader": "load_sherpa_offline_recognizer",
        },
        {
            "repo_id": "csukuangfj/sherpa-onnx-paraformer-zh-2024-03-09",
            "nn_model_file": "model.int8.onnx",
            "nn_model_file_sub_folder": ".",
            "tokens_file": "tokens.txt",
            "tokens_file_sub_folder": ".",
            "loader": "load_sherpa_offline_recognizer_from_paraformer",
        },
        {
            "repo_id": "csukuangfj/sherpa-onnx-paraformer-zh-small-2024-03-09",
            "nn_model_file": "model.int8.onnx",
            "nn_model_file_sub_folder": ".",
            "tokens_file": "tokens.txt",
            "tokens_file_sub_folder": ".",
            "loader": "load_sherpa_offline_recognizer_from_paraformer",
        },
        {
            "repo_id": "luomingshuang/icefall_asr_wenetspeech_pruned_transducer_stateless2",
            "nn_model_file": "cpu_jit_epoch_10_avg_2_torch_1.7.1.pt",
            "nn_model_file_sub_folder": "exp",
            "tokens_file": "tokens.txt",
            "tokens_file_sub_folder": "data/lang_char",
            "normalize_samples": True,
            "loader": "load_sherpa_offline_recognizer",
        },
        {
            "repo_id": "zrjin/sherpa-onnx-zipformer-multi-zh-hans-2023-9-2",
            "encoder_model_file": "encoder-epoch-20-avg-1.onnx",
            "encoder_model_file_sub_folder": ".",
            "decoder_model_file": "decoder-epoch-20-avg-1.onnx",
            "decoder_model_file_sub_folder": ".",
            "joiner_model_file": "joiner-epoch-20-avg-1.onnx",
            "joiner_model_file_sub_folder": ".",
            "tokens_file": "tokens.txt",
            "tokens_file_sub_folder": ".",
            "loader": "load_sherpa_offline_recognizer_from_transducer",
        },
    ],
    "English": [
        {
            "repo_id": "csukuangfj/sherpa-onnx-paraformer-en-2024-03-09",
            "nn_model_file": "model.int8.onnx",
            "nn_model_file_sub_folder": ".",
            "tokens_file": "tokens.txt",
            "tokens_file_sub_folder": ".",
            "loader": "load_sherpa_offline_recognizer_from_paraformer",
        },
    ],
    "Chinese+English": [
        {
            "repo_id": "csukuangfj/sherpa-onnx-paraformer-zh-2023-03-28",
            "nn_model_file": "model.int8.onnx",
            "nn_model_file_sub_folder": ".",
            "tokens_file": "tokens.txt",
            "tokens_file_sub_folder": ".",
            "loader": "load_sherpa_offline_recognizer_from_paraformer",
        },
    ],
    "Chinese+Cantonese+English": [
        {
            "repo_id": "csukuangfj/sherpa-onnx-paraformer-trilingual-zh-cantonese-en",
            "nn_model_file": "model.int8.onnx",
            "nn_model_file_sub_folder": ".",
            "tokens_file": "tokens.txt",
            "tokens_file_sub_folder": ".",
            "loader": "load_sherpa_offline_recognizer_from_paraformer",
        },
    ]
}


def download_model(local_model_dir: str,
                   **kwargs,
                   ):
    repo_id = kwargs["repo_id"]

    if "nn_model_file" in kwargs.keys():
        main_logger.info("download nn_model_file. filename: {}, subfolder: {}".format(kwargs["nn_model_file"], kwargs["nn_model_file_sub_folder"]))
        _ = huggingface_hub.hf_hub_download(
            repo_id=repo_id,
            filename=kwargs["nn_model_file"],
            subfolder=kwargs["nn_model_file_sub_folder"],
            local_dir=local_model_dir,
        )

    if "encoder_model_file" in kwargs.keys():
        main_logger.info("download encoder_model_file. filename: {}, subfolder: {}".format(kwargs["encoder_model_file"], kwargs["encoder_model_file_sub_folder"]))
        _ = huggingface_hub.hf_hub_download(
            repo_id=repo_id,
            filename=kwargs["encoder_model_file"],
            subfolder=kwargs["encoder_model_file_sub_folder"],
            local_dir=local_model_dir,
        )

    if "decoder_model_file" in kwargs.keys():
        main_logger.info("download decoder_model_file. filename: {}, subfolder: {}".format(kwargs["decoder_model_file"], kwargs["decoder_model_file_sub_folder"]))
        _ = huggingface_hub.hf_hub_download(
            repo_id=repo_id,
            filename=kwargs["decoder_model_file"],
            subfolder=kwargs["decoder_model_file_sub_folder"],
            local_dir=local_model_dir,
        )

    if "joiner_model_file" in kwargs.keys():
        main_logger.info("download joiner_model_file. filename: {}, subfolder: {}".format(kwargs["joiner_model_file"], kwargs["joiner_model_file_sub_folder"]))
        _ = huggingface_hub.hf_hub_download(
            repo_id=repo_id,
            filename=kwargs["joiner_model_file"],
            subfolder=kwargs["joiner_model_file_sub_folder"],
            local_dir=local_model_dir,
        )

    if "tokens_file" in kwargs.keys():
        main_logger.info("download tokens_file. filename: {}, subfolder: {}".format(kwargs["tokens_file"], kwargs["tokens_file_sub_folder"]))
        _ = huggingface_hub.hf_hub_download(
            repo_id=repo_id,
            filename=kwargs["tokens_file"],
            subfolder=kwargs["tokens_file_sub_folder"],
            local_dir=local_model_dir,
        )


def load_sherpa_offline_recognizer(nn_model_file: str,
                                   tokens_file: str,
                                   sample_rate: int = 16000,
                                   num_active_paths: int = 2,
                                   decoding_method: str = "greedy_search",
                                   num_mel_bins: int = 80,
                                   frame_dither: int = 0,
                                   normalize_samples: bool = False,
                                   ):
    feat_config = sherpa.FeatureConfig(normalize_samples=normalize_samples)
    feat_config.fbank_opts.frame_opts.samp_freq = sample_rate
    feat_config.fbank_opts.mel_opts.num_bins = num_mel_bins
    feat_config.fbank_opts.frame_opts.dither = frame_dither

    if not os.path.exists(nn_model_file):
        raise AssertionError("nn_model_file not found. nn_model_file: {}".format(nn_model_file))

    config = sherpa.OfflineRecognizerConfig(
        nn_model=nn_model_file,
        tokens=tokens_file,
        use_gpu=False,
        feat_config=feat_config,
        decoding_method=decoding_method,
        num_active_paths=num_active_paths,
    )

    recognizer = sherpa.OfflineRecognizer(config)

    return recognizer


def load_sherpa_offline_recognizer_from_paraformer(nn_model_file: str,
                                                   tokens_file: str,
                                                   sample_rate: int = 16000,
                                                   decoding_method: str = "greedy_search",
                                                   feature_dim: int = 80,
                                                   num_threads: int = 2,
                                                   ):
    recognizer = sherpa_onnx.OfflineRecognizer.from_paraformer(
        paraformer=nn_model_file,
        tokens=tokens_file,
        num_threads=num_threads,
        sample_rate=sample_rate,
        feature_dim=feature_dim,
        decoding_method=decoding_method,
        debug=False,
    )
    return recognizer


def load_sherpa_offline_recognizer_from_transducer(encoder_model_file: str,
                                                   decoder_model_file: str,
                                                   joiner_model_file: str,
                                                   tokens_file: str,
                                                   sample_rate: int = 16000,
                                                   decoding_method: str = "greedy_search",
                                                   feature_dim: int = 80,
                                                   num_threads: int = 2,
                                                   num_active_paths: int = 2,
                                                   ):
    recognizer = sherpa_onnx.OfflineRecognizer.from_transducer(
        encoder=encoder_model_file,
        decoder=decoder_model_file,
        joiner=joiner_model_file,
        tokens=tokens_file,
        num_threads=num_threads,
        sample_rate=sample_rate,
        feature_dim=feature_dim,
        decoding_method=decoding_method,
        max_active_paths=num_active_paths,
    )
    return recognizer


def load_recognizer(local_model_dir: Path,
                    decoding_method: str = "greedy_search",
                    num_active_paths: int = 4,
                    **kwargs
                    ):
    if not local_model_dir.exists():
        download_model(
            local_model_dir=local_model_dir.as_posix(),
            **kwargs,
        )

    loader = kwargs["loader"]

    kwargs_ = dict()
    if "nn_model_file" in kwargs.keys():
        nn_model_file = (local_model_dir / kwargs["nn_model_file"]).as_posix()
        kwargs_["nn_model_file"] = nn_model_file
    if "encoder_model_file" in kwargs.keys():
        encoder_model_file = (local_model_dir / kwargs["encoder_model_file"]).as_posix()
        kwargs_["encoder_model_file"] = encoder_model_file
    if "decoder_model_file" in kwargs.keys():
        decoder_model_file = (local_model_dir / kwargs["decoder_model_file"]).as_posix()
        kwargs_["decoder_model_file"] = decoder_model_file
    if "joiner_model_file" in kwargs.keys():
        joiner_model_file = (local_model_dir / kwargs["joiner_model_file"]).as_posix()
        kwargs_["joiner_model_file"] = joiner_model_file
    if "tokens_file" in kwargs.keys():
        tokens_file = (local_model_dir / kwargs["tokens_file"]).as_posix()
        kwargs_["tokens_file"] = tokens_file
    if "normalize_samples" in kwargs.keys():
        kwargs_["normalize_samples"] = kwargs["normalize_samples"]

    if loader == "load_sherpa_offline_recognizer":
        recognizer = load_sherpa_offline_recognizer(
            decoding_method=decoding_method,
            num_active_paths=num_active_paths,
            **kwargs_
        )
    elif loader == "load_sherpa_offline_recognizer_from_paraformer":
        recognizer = load_sherpa_offline_recognizer_from_paraformer(
            decoding_method=decoding_method,
            **kwargs_
        )
    elif loader == "load_sherpa_offline_recognizer_from_transducer":
        recognizer = load_sherpa_offline_recognizer_from_transducer(
            decoding_method=decoding_method,
            **kwargs_
        )
    else:
        raise NotImplementedError("loader not support: {}".format(loader))
    return recognizer


if __name__ == "__main__":
    pass