File size: 33,284 Bytes
6b31279 0c9d7b7 d60566f 9c3d4ca b0eec9a d60566f 6b31279 0c9d7b7 f59be55 0c9d7b7 d633661 6b31279 d633661 b0eec9a d633661 b0eec9a d633661 b0eec9a d633661 e6d227e b0eec9a e6d227e b0eec9a d633661 e6d227e b0eec9a d633661 b0eec9a 39b3b3e 0c9d7b7 d633661 39b3b3e 0c9d7b7 39b3b3e 0c9d7b7 39b3b3e 0c9d7b7 fa00390 0c9d7b7 e6d227e 756d91b 62e20af 39b3b3e 6b31279 39b3b3e 6b31279 39b3b3e 6b31279 39b3b3e 6b31279 39b3b3e 0c9d7b7 39b3b3e ee6ba22 39b3b3e 0c9d7b7 39b3b3e 0c9d7b7 39b3b3e 0c9d7b7 c342af5 62e20af c342af5 62e20af 16be569 62e20af 27c18ec 6fb68e3 27c18ec 5cb3bb4 4a82406 27c18ec 39b3b3e 0c9d7b7 39b3b3e ee6ba22 39b3b3e 0c9d7b7 ee6ba22 6b31279 0c9d7b7 6b31279 09d9587 0c9d7b7 09d9587 39b3b3e 4abdf19 0c9d7b7 39b3b3e 451aa75 0ae65b0 fc7e843 39b3b3e 0c2bc86 18d7e37 f59be55 4abdf19 9fa23bb ccb04a3 ee6ba22 39b3b3e 0c2bc86 18d7e37 0c2bc86 451aa75 0ae65b0 fc7e843 f59be55 0c9d7b7 39b3b3e 0c2bc86 39b3b3e 4abdf19 9fa23bb 4abdf19 0c9d7b7 39b3b3e 0c9d7b7 39b3b3e 0c9d7b7 39b3b3e ee6ba22 39b3b3e 0c9d7b7 39b3b3e 09d9587 0c9d7b7 39b3b3e 0c9d7b7 39b3b3e 9b54310 0c9d7b7 39b3b3e 9b54310 ee6ba22 39b3b3e 9b54310 0c9d7b7 39b3b3e 9b54310 09d9587 9b54310 0c9d7b7 09d9587 39b3b3e 0c9d7b7 39b3b3e f80684a 0c9d7b7 f80684a e491b4f f80684a ee6ba22 f80684a e491b4f 0c9d7b7 f80684a e491b4f f80684a 0c9d7b7 f80684a 0c9d7b7 f80684a b1459d7 0c9d7b7 b1459d7 0c9d7b7 b1459d7 0c9d7b7 b1459d7 0c9d7b7 b1459d7 6f26bbb 0c9d7b7 6f26bbb 5681f66 6f26bbb 5681f66 0c9d7b7 5681f66 0c9d7b7 6f26bbb 0c9d7b7 6f26bbb 0c9d7b7 6f26bbb e6d227e c342af5 e6d227e 62e20af e6d227e b0eec9a d633661 b0eec9a 9c3d4ca b0eec9a 4e478c6 d633661 e618c84 a461307 e618c84 39b3b3e d633661 f80684a e618c84 e491b4f 39b3b3e b1459d7 376cd19 0c9d7b7 2f9ed4b 39b3b3e 27c18ec 7fc713f 4e478c6 39b3b3e 0ae65b0 fc7e843 4abdf19 f59be55 4abdf19 9fa23bb ccb04a3 0c9d7b7 39b3b3e 9b54310 ce37a1c 39b3b3e 6f26bbb 5681f66 1f16e2f 6f26bbb 0c9d7b7 e6d227e b0eec9a 62e20af 39b3b3e 3425b9c 6f26bbb 0c9d7b7 e6d227e 62e20af 39b3b3e f80684a 3425b9c 6f26bbb 0c9d7b7 e6d227e 62e20af 39b3b3e |
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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 |
# Copyright 2022 Xiaomi Corp. (authors: Fangjun Kuang)
#
# See LICENSE for clarification regarding multiple authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from functools import lru_cache
from typing import Union
import torch
import torchaudio
from huggingface_hub import hf_hub_download
os.system(
"cp -v /home/user/.local/lib/python3.8/site-packages/k2/lib/*.so /home/user/.local/lib/python3.8/site-packages/sherpa/lib/"
)
import k2 # noqa
import sherpa
import sherpa_onnx
import numpy as np
from typing import Tuple
import wave
sample_rate = 16000
def read_wave(wave_filename: str) -> Tuple[np.ndarray, int]:
"""
Args:
wave_filename:
Path to a wave file. It should be single channel and each sample should
be 16-bit. Its sample rate does not need to be 16kHz.
Returns:
Return a tuple containing:
- A 1-D array of dtype np.float32 containing the samples, which are
normalized to the range [-1, 1].
- sample rate of the wave file
"""
with wave.open(wave_filename) as f:
assert f.getnchannels() == 1, f.getnchannels()
assert f.getsampwidth() == 2, f.getsampwidth() # it is in bytes
num_samples = f.getnframes()
samples = f.readframes(num_samples)
samples_int16 = np.frombuffer(samples, dtype=np.int16)
samples_float32 = samples_int16.astype(np.float32)
samples_float32 = samples_float32 / 32768
return samples_float32, f.getframerate()
def decode_offline_recognizer(
recognizer: sherpa.OfflineRecognizer,
filename: str,
) -> str:
s = recognizer.create_stream()
s.accept_wave_file(filename)
recognizer.decode_stream(s)
text = s.result.text.strip()
return text.lower()
def decode_online_recognizer(
recognizer: sherpa.OnlineRecognizer,
filename: str,
) -> str:
samples, actual_sample_rate = torchaudio.load(filename)
assert sample_rate == actual_sample_rate, (
sample_rate,
actual_sample_rate,
)
samples = samples[0].contiguous()
s = recognizer.create_stream()
tail_padding = torch.zeros(int(sample_rate * 0.3), dtype=torch.float32)
s.accept_waveform(sample_rate, samples)
s.accept_waveform(sample_rate, tail_padding)
s.input_finished()
while recognizer.is_ready(s):
recognizer.decode_stream(s)
text = recognizer.get_result(s).text
return text.strip().lower()
def decode_offline_recognizer_sherpa_onnx(
recognizer: sherpa_onnx.OfflineRecognizer,
filename: str,
) -> str:
s = recognizer.create_stream()
samples, sample_rate = read_wave(filename)
s.accept_waveform(sample_rate, samples)
recognizer.decode_stream(s)
return s.result.text.lower()
def decode_online_recognizer_sherpa_onnx(
recognizer: sherpa_onnx.OnlineRecognizer,
filename: str,
) -> str:
s = recognizer.create_stream()
samples, sample_rate = read_wave(filename)
s.accept_waveform(sample_rate, samples)
tail_paddings = np.zeros(int(0.3 * sample_rate), dtype=np.float32)
s.accept_waveform(sample_rate, tail_paddings)
s.input_finished()
while recognizer.is_ready(s):
recognizer.decode_stream(s)
return recognizer.get_result(s).lower()
def decode(
recognizer: Union[
sherpa.OfflineRecognizer,
sherpa.OnlineRecognizer,
sherpa_onnx.OfflineRecognizer,
sherpa_onnx.OnlineRecognizer,
],
filename: str,
) -> str:
if isinstance(recognizer, sherpa.OfflineRecognizer):
return decode_offline_recognizer(recognizer, filename)
elif isinstance(recognizer, sherpa.OnlineRecognizer):
return decode_online_recognizer(recognizer, filename)
elif isinstance(recognizer, sherpa_onnx.OfflineRecognizer):
return decode_offline_recognizer_sherpa_onnx(recognizer, filename)
elif isinstance(recognizer, sherpa_onnx.OnlineRecognizer):
return decode_online_recognizer_sherpa_onnx(recognizer, filename)
else:
raise ValueError(f"Unknown recognizer type {type(recognizer)}")
@lru_cache(maxsize=30)
def get_pretrained_model(
repo_id: str,
decoding_method: str,
num_active_paths: int,
) -> Union[sherpa.OfflineRecognizer, sherpa.OnlineRecognizer]:
if repo_id in chinese_models:
return chinese_models[repo_id](
repo_id, decoding_method=decoding_method, num_active_paths=num_active_paths
)
elif repo_id in english_models:
return english_models[repo_id](
repo_id, decoding_method=decoding_method, num_active_paths=num_active_paths
)
elif repo_id in chinese_english_mixed_models:
return chinese_english_mixed_models[repo_id](
repo_id, decoding_method=decoding_method, num_active_paths=num_active_paths
)
elif repo_id in tibetan_models:
return tibetan_models[repo_id](
repo_id, decoding_method=decoding_method, num_active_paths=num_active_paths
)
elif repo_id in arabic_models:
return arabic_models[repo_id](
repo_id, decoding_method=decoding_method, num_active_paths=num_active_paths
)
elif repo_id in german_models:
return german_models[repo_id](
repo_id, decoding_method=decoding_method, num_active_paths=num_active_paths
)
elif repo_id in french_models:
return french_models[repo_id](
repo_id, decoding_method=decoding_method, num_active_paths=num_active_paths
)
elif repo_id in japanese_models:
return japanese_models[repo_id](
repo_id, decoding_method=decoding_method, num_active_paths=num_active_paths
)
elif repo_id in russian_models:
return russian_models[repo_id](
repo_id, decoding_method=decoding_method, num_active_paths=num_active_paths
)
else:
raise ValueError(f"Unsupported repo_id: {repo_id}")
def _get_nn_model_filename(
repo_id: str,
filename: str,
subfolder: str = "exp",
) -> str:
nn_model_filename = hf_hub_download(
repo_id=repo_id,
filename=filename,
subfolder=subfolder,
)
return nn_model_filename
def _get_bpe_model_filename(
repo_id: str,
filename: str = "bpe.model",
subfolder: str = "data/lang_bpe_500",
) -> str:
bpe_model_filename = hf_hub_download(
repo_id=repo_id,
filename=filename,
subfolder=subfolder,
)
return bpe_model_filename
def _get_token_filename(
repo_id: str,
filename: str = "tokens.txt",
subfolder: str = "data/lang_char",
) -> str:
token_filename = hf_hub_download(
repo_id=repo_id,
filename=filename,
subfolder=subfolder,
)
return token_filename
@lru_cache(maxsize=10)
def _get_aishell2_pretrained_model(
repo_id: str,
decoding_method: str,
num_active_paths: int,
) -> sherpa.OfflineRecognizer:
assert repo_id in [
# context-size 1
"yuekai/icefall-asr-aishell2-pruned-transducer-stateless5-A-2022-07-12", # noqa
# context-size 2
"yuekai/icefall-asr-aishell2-pruned-transducer-stateless5-B-2022-07-12", # noqa
], repo_id
nn_model = _get_nn_model_filename(
repo_id=repo_id,
filename="cpu_jit.pt",
)
tokens = _get_token_filename(repo_id=repo_id)
feat_config = sherpa.FeatureConfig()
feat_config.fbank_opts.frame_opts.samp_freq = sample_rate
feat_config.fbank_opts.mel_opts.num_bins = 80
feat_config.fbank_opts.frame_opts.dither = 0
config = sherpa.OfflineRecognizerConfig(
nn_model=nn_model,
tokens=tokens,
use_gpu=False,
feat_config=feat_config,
decoding_method=decoding_method,
num_active_paths=num_active_paths,
)
recognizer = sherpa.OfflineRecognizer(config)
return recognizer
@lru_cache(maxsize=10)
def _get_russian_pre_trained_model(
repo_id: str, decoding_method: str, num_active_paths: int
) -> sherpa_onnx.OfflineRecognizer:
assert repo_id in (
"alphacep/vosk-model-ru",
"alphacep/vosk-model-small-ru",
), repo_id
if repo_id == "alphacep/vosk-model-ru":
model_dir = "am-onnx"
elif repo_id == "alphacep/vosk-model-small-ru":
model_dir = "am"
encoder_model = _get_nn_model_filename(
repo_id=repo_id,
filename="encoder.onnx",
subfolder=model_dir,
)
decoder_model = _get_nn_model_filename(
repo_id=repo_id,
filename="decoder.onnx",
subfolder=model_dir,
)
joiner_model = _get_nn_model_filename(
repo_id=repo_id,
filename="joiner.onnx",
subfolder=model_dir,
)
tokens = _get_token_filename(repo_id=repo_id, subfolder="lang")
recognizer = sherpa_onnx.OfflineRecognizer.from_transducer(
tokens=tokens,
encoder=encoder_model,
decoder=decoder_model,
joiner=joiner_model,
num_threads=2,
sample_rate=16000,
feature_dim=80,
decoding_method=decoding_method,
)
return recognizer
@lru_cache(maxsize=10)
def _get_whisper_model(
repo_id: str, decoding_method: str, num_active_paths: int
) -> sherpa_onnx.OfflineRecognizer:
name = repo_id.split("-")[1]
assert name in ("tiny.en", "base.en", "small.en", "medium.en"), repo_id
full_repo_id = "csukuangfj/sherpa-onnx-whisper-" + name
encoder = _get_nn_model_filename(
repo_id=full_repo_id,
filename=f"{name}-encoder.int8.ort",
subfolder=".",
)
decoder = _get_nn_model_filename(
repo_id=full_repo_id,
filename=f"{name}-decoder.int8.ort",
subfolder=".",
)
tokens = _get_token_filename(
repo_id=full_repo_id, subfolder=".", filename=f"{name}-tokens.txt"
)
recognizer = sherpa_onnx.OfflineRecognizer.from_whisper(
encoder=encoder,
decoder=decoder,
tokens=tokens,
num_threads=2,
)
return recognizer
@lru_cache(maxsize=10)
def _get_gigaspeech_pre_trained_model(
repo_id: str,
decoding_method: str,
num_active_paths: int,
) -> sherpa.OfflineRecognizer:
assert repo_id in [
"wgb14/icefall-asr-gigaspeech-pruned-transducer-stateless2",
], repo_id
nn_model = _get_nn_model_filename(
repo_id=repo_id,
filename="cpu_jit-iter-3488000-avg-20.pt",
)
tokens = "./giga-tokens.txt"
feat_config = sherpa.FeatureConfig()
feat_config.fbank_opts.frame_opts.samp_freq = sample_rate
feat_config.fbank_opts.mel_opts.num_bins = 80
feat_config.fbank_opts.frame_opts.dither = 0
config = sherpa.OfflineRecognizerConfig(
nn_model=nn_model,
tokens=tokens,
use_gpu=False,
feat_config=feat_config,
decoding_method=decoding_method,
num_active_paths=num_active_paths,
)
recognizer = sherpa.OfflineRecognizer(config)
return recognizer
@lru_cache(maxsize=10)
def _get_english_model(
repo_id: str,
decoding_method: str,
num_active_paths: int,
) -> sherpa.OfflineRecognizer:
assert repo_id in [
"WeijiZhuang/icefall-asr-librispeech-pruned-transducer-stateless8-2022-12-02", # noqa
"yfyeung/icefall-asr-multidataset-pruned_transducer_stateless7-2023-05-04", # noqa
"yfyeung/icefall-asr-finetune-mux-pruned_transducer_stateless7-2023-05-19", # noqa
"csukuangfj/icefall-asr-librispeech-pruned-transducer-stateless3-2022-05-13", # noqa
"csukuangfj/icefall-asr-librispeech-pruned-transducer-stateless7-2022-11-11", # noqa
"csukuangfj/icefall-asr-librispeech-pruned-transducer-stateless8-2022-11-14", # noqa
"Zengwei/icefall-asr-librispeech-zipformer-large-2023-05-16", # noqa
"Zengwei/icefall-asr-librispeech-zipformer-2023-05-15", # noqa
"Zengwei/icefall-asr-librispeech-zipformer-small-2023-05-16", # noqa
"videodanchik/icefall-asr-tedlium3-conformer-ctc2",
"pkufool/icefall_asr_librispeech_conformer_ctc",
"WayneWiser/icefall-asr-librispeech-conformer-ctc2-jit-bpe-500-2022-07-21",
], repo_id
filename = "cpu_jit.pt"
if (
repo_id
== "csukuangfj/icefall-asr-librispeech-pruned-transducer-stateless7-2022-11-11"
):
filename = "cpu_jit-torch-1.10.0.pt"
if (
repo_id
== "WeijiZhuang/icefall-asr-librispeech-pruned-transducer-stateless8-2022-12-02"
):
filename = "cpu_jit-torch-1.10.pt"
if (
repo_id
== "yfyeung/icefall-asr-multidataset-pruned_transducer_stateless7-2023-05-04"
):
filename = "cpu_jit-epoch-30-avg-4.pt"
if (
repo_id
== "yfyeung/icefall-asr-finetune-mux-pruned_transducer_stateless7-2023-05-19"
):
filename = "cpu_jit-epoch-20-avg-5.pt"
if repo_id in (
"Zengwei/icefall-asr-librispeech-zipformer-large-2023-05-16",
"Zengwei/icefall-asr-librispeech-zipformer-2023-05-15",
"Zengwei/icefall-asr-librispeech-zipformer-small-2023-05-16",
):
filename = "jit_script.pt"
nn_model = _get_nn_model_filename(
repo_id=repo_id,
filename=filename,
)
subfolder = "data/lang_bpe_500"
if repo_id in (
"videodanchik/icefall-asr-tedlium3-conformer-ctc2",
"pkufool/icefall_asr_librispeech_conformer_ctc",
):
subfolder = "data/lang_bpe"
tokens = _get_token_filename(repo_id=repo_id, subfolder=subfolder)
feat_config = sherpa.FeatureConfig()
feat_config.fbank_opts.frame_opts.samp_freq = sample_rate
feat_config.fbank_opts.mel_opts.num_bins = 80
feat_config.fbank_opts.frame_opts.dither = 0
config = sherpa.OfflineRecognizerConfig(
nn_model=nn_model,
tokens=tokens,
use_gpu=False,
feat_config=feat_config,
decoding_method=decoding_method,
num_active_paths=num_active_paths,
)
recognizer = sherpa.OfflineRecognizer(config)
return recognizer
@lru_cache(maxsize=10)
def _get_wenetspeech_pre_trained_model(
repo_id: str,
decoding_method: str,
num_active_paths: int,
):
assert repo_id in [
"luomingshuang/icefall_asr_wenetspeech_pruned_transducer_stateless2",
], repo_id
nn_model = _get_nn_model_filename(
repo_id=repo_id,
filename="cpu_jit_epoch_10_avg_2_torch_1.7.1.pt",
)
tokens = _get_token_filename(repo_id=repo_id)
feat_config = sherpa.FeatureConfig()
feat_config.fbank_opts.frame_opts.samp_freq = sample_rate
feat_config.fbank_opts.mel_opts.num_bins = 80
feat_config.fbank_opts.frame_opts.dither = 0
config = sherpa.OfflineRecognizerConfig(
nn_model=nn_model,
tokens=tokens,
use_gpu=False,
feat_config=feat_config,
decoding_method=decoding_method,
num_active_paths=num_active_paths,
)
recognizer = sherpa.OfflineRecognizer(config)
return recognizer
@lru_cache(maxsize=10)
def _get_chinese_english_mixed_model(
repo_id: str,
decoding_method: str,
num_active_paths: int,
):
assert repo_id in [
"luomingshuang/icefall_asr_tal-csasr_pruned_transducer_stateless5",
"ptrnull/icefall-asr-conv-emformer-transducer-stateless2-zh",
], repo_id
if repo_id == "luomingshuang/icefall_asr_tal-csasr_pruned_transducer_stateless5":
filename = "cpu_jit.pt"
subfolder = "data/lang_char"
elif repo_id == "ptrnull/icefall-asr-conv-emformer-transducer-stateless2-zh":
filename = "cpu_jit-epoch-11-avg-1.pt"
subfolder = "data/lang_char_bpe"
nn_model = _get_nn_model_filename(
repo_id=repo_id,
filename=filename,
)
tokens = _get_token_filename(repo_id=repo_id, subfolder=subfolder)
feat_config = sherpa.FeatureConfig()
feat_config.fbank_opts.frame_opts.samp_freq = sample_rate
feat_config.fbank_opts.mel_opts.num_bins = 80
feat_config.fbank_opts.frame_opts.dither = 0
config = sherpa.OfflineRecognizerConfig(
nn_model=nn_model,
tokens=tokens,
use_gpu=False,
feat_config=feat_config,
decoding_method=decoding_method,
num_active_paths=num_active_paths,
)
recognizer = sherpa.OfflineRecognizer(config)
return recognizer
@lru_cache(maxsize=10)
def _get_alimeeting_pre_trained_model(
repo_id: str,
decoding_method: str,
num_active_paths: int,
):
assert repo_id in [
"desh2608/icefall-asr-alimeeting-pruned-transducer-stateless7",
"luomingshuang/icefall_asr_alimeeting_pruned_transducer_stateless2",
], repo_id
if repo_id == "desh2608/icefall-asr-alimeeting-pruned-transducer-stateless7":
filename = "cpu_jit.pt"
elif repo_id == "luomingshuang/icefall_asr_alimeeting_pruned_transducer_stateless2":
filename = "cpu_jit_torch_1.7.1.pt"
nn_model = _get_nn_model_filename(
repo_id=repo_id,
filename=filename,
)
tokens = _get_token_filename(repo_id=repo_id)
feat_config = sherpa.FeatureConfig()
feat_config.fbank_opts.frame_opts.samp_freq = sample_rate
feat_config.fbank_opts.mel_opts.num_bins = 80
feat_config.fbank_opts.frame_opts.dither = 0
config = sherpa.OfflineRecognizerConfig(
nn_model=nn_model,
tokens=tokens,
use_gpu=False,
feat_config=feat_config,
decoding_method=decoding_method,
num_active_paths=num_active_paths,
)
recognizer = sherpa.OfflineRecognizer(config)
return recognizer
@lru_cache(maxsize=10)
def _get_wenet_model(
repo_id: str,
decoding_method: str,
num_active_paths: int,
):
assert repo_id in [
"csukuangfj/wenet-chinese-model",
"csukuangfj/wenet-english-model",
], repo_id
nn_model = _get_nn_model_filename(
repo_id=repo_id,
filename="final.zip",
subfolder=".",
)
tokens = _get_token_filename(
repo_id=repo_id,
filename="units.txt",
subfolder=".",
)
feat_config = sherpa.FeatureConfig(normalize_samples=False)
feat_config.fbank_opts.frame_opts.samp_freq = sample_rate
feat_config.fbank_opts.mel_opts.num_bins = 80
feat_config.fbank_opts.frame_opts.dither = 0
config = sherpa.OfflineRecognizerConfig(
nn_model=nn_model,
tokens=tokens,
use_gpu=False,
feat_config=feat_config,
decoding_method=decoding_method,
num_active_paths=num_active_paths,
)
recognizer = sherpa.OfflineRecognizer(config)
return recognizer
@lru_cache(maxsize=10)
def _get_aidatatang_200zh_pretrained_mode(
repo_id: str,
decoding_method: str,
num_active_paths: int,
):
assert repo_id in [
"luomingshuang/icefall_asr_aidatatang-200zh_pruned_transducer_stateless2",
], repo_id
nn_model = _get_nn_model_filename(
repo_id=repo_id,
filename="cpu_jit_torch.1.7.1.pt",
)
tokens = _get_token_filename(repo_id=repo_id)
feat_config = sherpa.FeatureConfig()
feat_config.fbank_opts.frame_opts.samp_freq = sample_rate
feat_config.fbank_opts.mel_opts.num_bins = 80
feat_config.fbank_opts.frame_opts.dither = 0
config = sherpa.OfflineRecognizerConfig(
nn_model=nn_model,
tokens=tokens,
use_gpu=False,
feat_config=feat_config,
decoding_method=decoding_method,
num_active_paths=num_active_paths,
)
recognizer = sherpa.OfflineRecognizer(config)
return recognizer
@lru_cache(maxsize=10)
def _get_tibetan_pre_trained_model(
repo_id: str,
decoding_method: str,
num_active_paths: int,
):
assert repo_id in [
"syzym/icefall-asr-xbmu-amdo31-pruned-transducer-stateless7-2022-12-02",
"syzym/icefall-asr-xbmu-amdo31-pruned-transducer-stateless5-2022-11-29",
], repo_id
filename = "cpu_jit.pt"
if (
repo_id
== "syzym/icefall-asr-xbmu-amdo31-pruned-transducer-stateless5-2022-11-29"
):
filename = "cpu_jit-epoch-28-avg-23-torch-1.10.0.pt"
nn_model = _get_nn_model_filename(
repo_id=repo_id,
filename=filename,
)
tokens = _get_token_filename(repo_id=repo_id, subfolder="data/lang_bpe_500")
feat_config = sherpa.FeatureConfig()
feat_config.fbank_opts.frame_opts.samp_freq = sample_rate
feat_config.fbank_opts.mel_opts.num_bins = 80
feat_config.fbank_opts.frame_opts.dither = 0
config = sherpa.OfflineRecognizerConfig(
nn_model=nn_model,
tokens=tokens,
use_gpu=False,
feat_config=feat_config,
decoding_method=decoding_method,
num_active_paths=num_active_paths,
)
recognizer = sherpa.OfflineRecognizer(config)
return recognizer
@lru_cache(maxsize=10)
def _get_arabic_pre_trained_model(
repo_id: str,
decoding_method: str,
num_active_paths: int,
):
assert repo_id in [
"AmirHussein/icefall-asr-mgb2-conformer_ctc-2022-27-06",
], repo_id
nn_model = _get_nn_model_filename(
repo_id=repo_id,
filename="cpu_jit.pt",
)
tokens = _get_token_filename(repo_id=repo_id, subfolder="data/lang_bpe_5000")
feat_config = sherpa.FeatureConfig()
feat_config.fbank_opts.frame_opts.samp_freq = sample_rate
feat_config.fbank_opts.mel_opts.num_bins = 80
feat_config.fbank_opts.frame_opts.dither = 0
config = sherpa.OfflineRecognizerConfig(
nn_model=nn_model,
tokens=tokens,
use_gpu=False,
feat_config=feat_config,
decoding_method=decoding_method,
num_active_paths=num_active_paths,
)
recognizer = sherpa.OfflineRecognizer(config)
return recognizer
@lru_cache(maxsize=10)
def _get_german_pre_trained_model(
repo_id: str,
decoding_method: str,
num_active_paths: int,
):
assert repo_id in [
"csukuangfj/wav2vec2.0-torchaudio",
], repo_id
nn_model = _get_nn_model_filename(
repo_id=repo_id,
filename="voxpopuli_asr_base_10k_de.pt",
subfolder=".",
)
tokens = _get_token_filename(
repo_id=repo_id,
filename="tokens-de.txt",
subfolder=".",
)
config = sherpa.OfflineRecognizerConfig(
nn_model=nn_model,
tokens=tokens,
use_gpu=False,
decoding_method=decoding_method,
num_active_paths=num_active_paths,
)
recognizer = sherpa.OfflineRecognizer(config)
return recognizer
@lru_cache(maxsize=10)
def _get_french_pre_trained_model(
repo_id: str,
decoding_method: str,
num_active_paths: int,
):
assert repo_id in [
"shaojieli/sherpa-onnx-streaming-zipformer-fr-2023-04-14",
], repo_id
encoder_model = _get_nn_model_filename(
repo_id=repo_id,
filename="encoder-epoch-29-avg-9-with-averaged-model.onnx",
subfolder=".",
)
decoder_model = _get_nn_model_filename(
repo_id=repo_id,
filename="decoder-epoch-29-avg-9-with-averaged-model.onnx",
subfolder=".",
)
joiner_model = _get_nn_model_filename(
repo_id=repo_id,
filename="joiner-epoch-29-avg-9-with-averaged-model.onnx",
subfolder=".",
)
tokens = _get_token_filename(repo_id=repo_id, subfolder=".")
recognizer = sherpa_onnx.OnlineRecognizer.from_transducer(
tokens=tokens,
encoder=encoder_model,
decoder=decoder_model,
joiner=joiner_model,
num_threads=2,
sample_rate=16000,
feature_dim=80,
decoding_method=decoding_method,
max_active_paths=num_active_paths,
)
return recognizer
@lru_cache(maxsize=10)
def _get_japanese_pre_trained_model(
repo_id: str,
decoding_method: str,
num_active_paths: int,
) -> sherpa.OnlineRecognizer:
repo_id, kind = repo_id.rsplit("-", maxsplit=1)
assert repo_id in [
"TeoWenShen/icefall-asr-csj-pruned-transducer-stateless7-streaming-230208"
], repo_id
assert kind in ("fluent", "disfluent"), kind
encoder_model = _get_nn_model_filename(
repo_id=repo_id, filename="encoder_jit_trace.pt", subfolder=f"exp_{kind}"
)
decoder_model = _get_nn_model_filename(
repo_id=repo_id, filename="decoder_jit_trace.pt", subfolder=f"exp_{kind}"
)
joiner_model = _get_nn_model_filename(
repo_id=repo_id, filename="joiner_jit_trace.pt", subfolder=f"exp_{kind}"
)
tokens = _get_token_filename(repo_id=repo_id)
feat_config = sherpa.FeatureConfig()
feat_config.fbank_opts.frame_opts.samp_freq = sample_rate
feat_config.fbank_opts.mel_opts.num_bins = 80
feat_config.fbank_opts.frame_opts.dither = 0
config = sherpa.OnlineRecognizerConfig(
nn_model="",
encoder_model=encoder_model,
decoder_model=decoder_model,
joiner_model=joiner_model,
tokens=tokens,
use_gpu=False,
feat_config=feat_config,
decoding_method=decoding_method,
num_active_paths=num_active_paths,
chunk_size=32,
)
recognizer = sherpa.OnlineRecognizer(config)
return recognizer
@lru_cache(maxsize=10)
def _get_gigaspeech_pre_trained_model_onnx(
repo_id: str,
decoding_method: str,
num_active_paths: int,
) -> sherpa_onnx.OfflineRecognizer:
assert repo_id in [
"yfyeung/icefall-asr-gigaspeech-zipformer-2023-10-17",
], repo_id
encoder_model = _get_nn_model_filename(
repo_id=repo_id,
filename="encoder-epoch-30-avg-9.onnx",
subfolder="exp",
)
decoder_model = _get_nn_model_filename(
repo_id=repo_id,
filename="decoder-epoch-30-avg-9.onnx",
subfolder="exp",
)
joiner_model = _get_nn_model_filename(
repo_id=repo_id,
filename="joiner-epoch-30-avg-9.onnx",
subfolder="exp",
)
tokens = _get_token_filename(repo_id=repo_id, subfolder="data/lang_bpe_500")
recognizer = sherpa_onnx.OfflineRecognizer.from_transducer(
tokens=tokens,
encoder=encoder_model,
decoder=decoder_model,
joiner=joiner_model,
num_threads=2,
sample_rate=16000,
feature_dim=80,
decoding_method=decoding_method,
max_active_paths=num_active_paths,
)
return recognizer
@lru_cache(maxsize=10)
def _get_paraformer_zh_pre_trained_model(
repo_id: str,
decoding_method: str,
num_active_paths: int,
) -> sherpa_onnx.OfflineRecognizer:
assert repo_id in [
"csukuangfj/sherpa-onnx-paraformer-zh-2023-03-28",
], repo_id
nn_model = _get_nn_model_filename(
repo_id=repo_id,
filename="model.onnx",
subfolder=".",
)
tokens = _get_token_filename(repo_id=repo_id, subfolder=".")
recognizer = sherpa_onnx.OfflineRecognizer.from_paraformer(
paraformer=nn_model,
tokens=tokens,
num_threads=2,
sample_rate=sample_rate,
feature_dim=80,
decoding_method="greedy_search",
debug=False,
)
return recognizer
def _get_multi_zh_hans_pre_trained_model(
repo_id: str,
decoding_method: str,
num_active_paths: int,
) -> sherpa_onnx.OfflineRecognizer:
assert repo_id in ("zrjin/sherpa-onnx-zipformer-multi-zh-hans-2023-9-2",), repo_id
encoder_model = _get_nn_model_filename(
repo_id=repo_id,
filename="encoder-epoch-20-avg-1.onnx",
subfolder=".",
)
decoder_model = _get_nn_model_filename(
repo_id=repo_id,
filename="decoder-epoch-20-avg-1.onnx",
subfolder=".",
)
joiner_model = _get_nn_model_filename(
repo_id=repo_id,
filename="joiner-epoch-20-avg-1.onnx",
subfolder=".",
)
tokens = _get_token_filename(repo_id=repo_id, subfolder=".")
recognizer = sherpa_onnx.OfflineRecognizer.from_transducer(
tokens=tokens,
encoder=encoder_model,
decoder=decoder_model,
joiner=joiner_model,
num_threads=2,
sample_rate=16000,
feature_dim=80,
decoding_method=decoding_method,
max_active_paths=num_active_paths,
)
return recognizer
chinese_models = {
"csukuangfj/sherpa-onnx-paraformer-zh-2023-03-28": _get_paraformer_zh_pre_trained_model,
"luomingshuang/icefall_asr_wenetspeech_pruned_transducer_stateless2": _get_wenetspeech_pre_trained_model, # noqa
"zrjin/sherpa-onnx-zipformer-multi-zh-hans-2023-9-2": _get_multi_zh_hans_pre_trained_model, # noqa
"desh2608/icefall-asr-alimeeting-pruned-transducer-stateless7": _get_alimeeting_pre_trained_model,
"yuekai/icefall-asr-aishell2-pruned-transducer-stateless5-A-2022-07-12": _get_aishell2_pretrained_model, # noqa
"yuekai/icefall-asr-aishell2-pruned-transducer-stateless5-B-2022-07-12": _get_aishell2_pretrained_model, # noqa
"luomingshuang/icefall_asr_aidatatang-200zh_pruned_transducer_stateless2": _get_aidatatang_200zh_pretrained_mode, # noqa
"luomingshuang/icefall_asr_alimeeting_pruned_transducer_stateless2": _get_alimeeting_pre_trained_model, # noqa
"csukuangfj/wenet-chinese-model": _get_wenet_model,
# "csukuangfj/icefall-asr-wenetspeech-lstm-transducer-stateless-2022-10-14": _get_lstm_transducer_model,
}
english_models = {
"whisper-tiny.en": _get_whisper_model,
"whisper-base.en": _get_whisper_model,
"whisper-small.en": _get_whisper_model,
# "whisper-medium.en": _get_whisper_model,
"yfyeung/icefall-asr-gigaspeech-zipformer-2023-10-17": _get_gigaspeech_pre_trained_model_onnx, # noqa
"wgb14/icefall-asr-gigaspeech-pruned-transducer-stateless2": _get_gigaspeech_pre_trained_model, # noqa
"yfyeung/icefall-asr-multidataset-pruned_transducer_stateless7-2023-05-04": _get_english_model, # noqa
"yfyeung/icefall-asr-finetune-mux-pruned_transducer_stateless7-2023-05-19": _get_english_model, # noqa
"WeijiZhuang/icefall-asr-librispeech-pruned-transducer-stateless8-2022-12-02": _get_english_model, # noqa
"csukuangfj/icefall-asr-librispeech-pruned-transducer-stateless8-2022-11-14": _get_english_model, # noqa
"csukuangfj/icefall-asr-librispeech-pruned-transducer-stateless7-2022-11-11": _get_english_model, # noqa
"csukuangfj/icefall-asr-librispeech-pruned-transducer-stateless3-2022-05-13": _get_english_model, # noqa
"Zengwei/icefall-asr-librispeech-zipformer-large-2023-05-16": _get_english_model, # noqa
"Zengwei/icefall-asr-librispeech-zipformer-2023-05-15": _get_english_model, # noqa
"Zengwei/icefall-asr-librispeech-zipformer-small-2023-05-16": _get_english_model, # noqa
"videodanchik/icefall-asr-tedlium3-conformer-ctc2": _get_english_model,
"pkufool/icefall_asr_librispeech_conformer_ctc": _get_english_model,
"WayneWiser/icefall-asr-librispeech-conformer-ctc2-jit-bpe-500-2022-07-21": _get_english_model,
"csukuangfj/wenet-english-model": _get_wenet_model,
}
chinese_english_mixed_models = {
"ptrnull/icefall-asr-conv-emformer-transducer-stateless2-zh": _get_chinese_english_mixed_model,
"luomingshuang/icefall_asr_tal-csasr_pruned_transducer_stateless5": _get_chinese_english_mixed_model, # noqa
}
tibetan_models = {
"syzym/icefall-asr-xbmu-amdo31-pruned-transducer-stateless7-2022-12-02": _get_tibetan_pre_trained_model, # noqa
"syzym/icefall-asr-xbmu-amdo31-pruned-transducer-stateless5-2022-11-29": _get_tibetan_pre_trained_model, # noqa
}
arabic_models = {
"AmirHussein/icefall-asr-mgb2-conformer_ctc-2022-27-06": _get_arabic_pre_trained_model, # noqa
}
german_models = {
"csukuangfj/wav2vec2.0-torchaudio": _get_german_pre_trained_model,
}
french_models = {
"shaojieli/sherpa-onnx-streaming-zipformer-fr-2023-04-14": _get_french_pre_trained_model,
}
japanese_models = {
"TeoWenShen/icefall-asr-csj-pruned-transducer-stateless7-streaming-230208-fluent": _get_japanese_pre_trained_model,
"TeoWenShen/icefall-asr-csj-pruned-transducer-stateless7-streaming-230208-disfluent": _get_japanese_pre_trained_model,
}
russian_models = {
"alphacep/vosk-model-ru": _get_russian_pre_trained_model,
"alphacep/vosk-model-small-ru": _get_russian_pre_trained_model,
}
all_models = {
**chinese_models,
**english_models,
**chinese_english_mixed_models,
# **japanese_models,
**tibetan_models,
**arabic_models,
**german_models,
**french_models,
**russian_models,
}
language_to_models = {
"Chinese": list(chinese_models.keys()),
"English": list(english_models.keys()),
"Chinese+English": list(chinese_english_mixed_models.keys()),
# "Japanese": list(japanese_models.keys()),
"Tibetan": list(tibetan_models.keys()),
"Arabic": list(arabic_models.keys()),
"German": list(german_models.keys()),
"French": list(french_models.keys()),
"Russian": list(russian_models.keys()),
}
|