File size: 2,184 Bytes
5ce1fe8
 
 
941850e
5ce1fe8
941850e
 
 
 
5ce1fe8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159e07d
5ce1fe8
 
 
 
 
159e07d
5ce1fe8
 
159e07d
5ce1fe8
 
 
 
159e07d
 
5ce1fe8
 
159e07d
 
5ce1fe8
 
 
159e07d
5ce1fe8
 
 
 
 
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
import os
from pathlib import Path
import sys

pwd = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(pwd, "../../"))

import huggingface_hub
import sherpa

from project_settings import project_path


def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--repo_id",
        default="csukuangfj/wenet-chinese-model",
        # default="csukuangfj/wenet-english-model",
        type=str
    )

    parser.add_argument("--model_filename", default="final.zip", type=str)
    parser.add_argument("--tokens_filename", default="units.txt", type=str)

    parser.add_argument(
        "--pretrained_model_dir",
        default=(project_path / "pretrained_models").as_posix(),
        type=str
    )
    args = parser.parse_args()
    return args


def main():
    args = get_args()

    pretrained_model_dir = Path(args.pretrained_model_dir)
    pretrained_model_dir.mkdir(exist_ok=True)

    model_dir = pretrained_model_dir / "huggingface" / args.repo_id
    model_dir.mkdir(exist_ok=True)

    print("download model")
    model_filename = huggingface_hub.hf_hub_download(
        repo_id=args.repo_id,
        filename=args.model_filename,
        subfolder=".",
        local_dir=model_dir.as_posix(),
    )
    print(model_filename)

    print("download tokens")
    tokens_filename = huggingface_hub.hf_hub_download(
        repo_id=args.repo_id,
        filename=args.tokens_filename,
        subfolder=".",
        local_dir=model_dir.as_posix(),
    )
    print(tokens_filename)

    feat_config = sherpa.FeatureConfig(normalize_samples=False)
    feat_config.fbank_opts.frame_opts.samp_freq = 16000
    feat_config.fbank_opts.mel_opts.num_bins = 80
    feat_config.fbank_opts.frame_opts.dither = 0

    config = sherpa.OfflineRecognizerConfig(
        nn_model=model_filename,
        tokens=tokens_filename,
        use_gpu=False,
        feat_config=feat_config,
        decoding_method="greedy_search",
        num_active_paths=2,
    )

    recognizer = sherpa.OfflineRecognizer(config)
    print(recognizer)
    return


if __name__ == "__main__":
    main()