forked from PaddlePaddle/PaddleHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.py
92 lines (77 loc) · 3.83 KB
/
module.py
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
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# 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 pathlib import Path
import sys
import numpy as np
from paddlehub.env import MODULE_HOME
from paddlehub.module.module import moduleinfo, serving
from paddlehub.utils.log import logger
from paddle.utils.download import get_path_from_url
try:
import swig_decoders
except ModuleNotFoundError as e:
logger.error(e)
logger.info('The module requires additional dependencies: swig_decoders. '
'please install via:\n\'git clone https://github.com/PaddlePaddle/DeepSpeech.git '
'&& cd DeepSpeech && git reset --hard b53171694e7b87abe7ea96870b2f4d8e0e2b1485 '
'&& cd deepspeech/decoders/ctcdecoder/swig && sh setup.sh\'')
sys.exit(1)
import paddle
import soundfile as sf
# TODO: Remove system path when deepspeech can be installed via pip.
sys.path.append(os.path.join(MODULE_HOME, 'deepspeech2_aishell'))
from deepspeech.exps.deepspeech2.config import get_cfg_defaults
from deepspeech.utils.utility import UpdateConfig
from .deepspeech_tester import DeepSpeech2Tester
LM_URL = 'https://deepspeech.bj.bcebos.com/zh_lm/zh_giga.no_cna_cmn.prune01244.klm'
LM_MD5 = '29e02312deb2e59b3c8686c7966d4fe3'
@moduleinfo(name="deepspeech2_aishell", version="1.0.0", summary="", author="Baidu", author_email="", type="audio/asr")
class DeepSpeech2(paddle.nn.Layer):
def __init__(self):
super(DeepSpeech2, self).__init__()
# resource
res_dir = os.path.join(MODULE_HOME, 'deepspeech2_aishell', 'assets')
conf_file = os.path.join(res_dir, 'conf/deepspeech2.yaml')
checkpoint = os.path.join(res_dir, 'checkpoints/avg_1.pdparams')
# Download LM manually cause its large size.
lm_path = os.path.join(res_dir, 'data', 'lm')
lm_file = os.path.join(lm_path, LM_URL.split('/')[-1])
if not os.path.isfile(lm_file):
logger.info(f'Downloading lm from {LM_URL}.')
get_path_from_url(url=LM_URL, root_dir=lm_path, md5sum=LM_MD5)
# config
self.model_type = 'offline'
self.config = get_cfg_defaults(self.model_type)
self.config.merge_from_file(conf_file)
# TODO: Remove path updating snippet.
with UpdateConfig(self.config):
self.config.collator.mean_std_filepath = os.path.join(res_dir, self.config.collator.mean_std_filepath)
self.config.collator.vocab_filepath = os.path.join(res_dir, self.config.collator.vocab_filepath)
self.config.collator.augmentation_config = os.path.join(res_dir, self.config.collator.augmentation_config)
self.config.decoding.lang_model_path = os.path.join(res_dir, self.config.decoding.lang_model_path)
# model
self.tester = DeepSpeech2Tester(self.config)
self.tester.setup_model()
self.tester.resume(checkpoint)
@staticmethod
def check_audio(audio_file):
sig, sample_rate = sf.read(audio_file)
assert sample_rate == 16000, 'Excepting sample rate of input audio is 16000, but got {}'.format(sample_rate)
@serving
def speech_recognize(self, audio_file, device='cpu'):
assert os.path.isfile(audio_file), 'File not exists: {}'.format(audio_file)
self.check_audio(audio_file)
paddle.set_device(device)
return self.tester.test(audio_file)[0]