asr_engine.py 3.0 KB
Newer Older
W
WilliamZhang06 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# Copyright (c) 2022 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.
W
WilliamZhang06 已提交
14
import io
L
lym0302 已提交
15
import time
W
WilliamZhang06 已提交
16

17 18
import paddle

W
WilliamZhang06 已提交
19
from paddlespeech.cli.asr.infer import ASRExecutor
20
from paddlespeech.cli.log import logger
L
lym0302 已提交
21
from paddlespeech.server.engine.base_engine import BaseEngine
W
WilliamZhang06 已提交
22 23 24 25

__all__ = ['ASREngine']


W
WilliamZhang06 已提交
26 27 28 29 30 31
class ASRServerExecutor(ASRExecutor):
    def __init__(self):
        super().__init__()
        pass


32
class ASREngine(BaseEngine):
W
WilliamZhang06 已提交
33 34 35 36 37
    """ASR server engine

    Args:
        metaclass: Defaults to Singleton.
    """
38

39
    def __init__(self):
W
WilliamZhang06 已提交
40
        super(ASREngine, self).__init__()
41

L
lym0302 已提交
42
    def init(self, config: dict) -> bool:
43 44 45 46
        """init engine resource

        Args:
            config_file (str): config file
W
WilliamZhang06 已提交
47

48 49 50 51 52
        Returns:
            bool: init failed or success
        """
        self.input = None
        self.output = None
W
WilliamZhang06 已提交
53
        self.executor = ASRServerExecutor()
L
lym0302 已提交
54
        self.config = config
L
lym0302 已提交
55 56 57 58 59 60 61 62 63 64 65
        try:
            if self.config.device:
                self.device = self.config.device
            else:
                self.device = paddle.get_device()
            paddle.set_device(self.device)
        except BaseException:
            logger.error(
                "Set device failed, please check if device is already used and the parameter 'device' in the yaml file"
            )

66 67 68 69
        self.executor._init_from_path(
            self.config.model, self.config.lang, self.config.sample_rate,
            self.config.cfg_path, self.config.decode_method,
            self.config.ckpt_path)
W
WilliamZhang06 已提交
70

L
lym0302 已提交
71 72
        logger.info("Initialize ASR server engine successfully on device: %s." %
                    (self.device))
73
        return True
W
WilliamZhang06 已提交
74

W
WilliamZhang06 已提交
75
    def run(self, audio_data):
76
        """engine run 
W
WilliamZhang06 已提交
77

78 79 80 81 82 83 84
        Args:
            audio_data (bytes): base64.b64decode
        """
        if self.executor._check(
                io.BytesIO(audio_data), self.config.sample_rate,
                self.config.force_yes):
            logger.info("start run asr engine")
W
WilliamZhang06 已提交
85
            self.executor.preprocess(self.config.model, io.BytesIO(audio_data))
L
lym0302 已提交
86
            st = time.time()
W
WilliamZhang06 已提交
87
            self.executor.infer(self.config.model)
L
lym0302 已提交
88
            infer_time = time.time() - st
89
            self.output = self.executor.postprocess()  # Retrieve result of asr.
W
WilliamZhang06 已提交
90 91
        else:
            logger.info("file check failed!")
92
            self.output = None
W
WilliamZhang06 已提交
93

L
lym0302 已提交
94 95 96
        logger.info("inference time: {}".format(infer_time))
        logger.info("asr engine type: python")

W
WilliamZhang06 已提交
97
    def postprocess(self):
98 99
        """postprocess
        """
W
WilliamZhang06 已提交
100
        return self.output