asr_engine.py 2.6 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 15
import io

16 17
import paddle

W
WilliamZhang06 已提交
18
from paddlespeech.cli.asr.infer import ASRExecutor
19
from paddlespeech.cli.log import logger
L
lym0302 已提交
20 21
from paddlespeech.server.engine.base_engine import BaseEngine
from paddlespeech.server.utils.config import get_config
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

42 43 44 45 46
    def init(self, config_file: str) -> bool:
        """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 54
        self.executor = ASRServerExecutor()

55
        self.config = get_config(config_file)
L
lym0302 已提交
56 57 58 59
        if self.config.device is None:
            paddle.set_device(paddle.get_device())
        else:
            paddle.set_device(self.config.device)
60 61 62 63
        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 已提交
64 65

        logger.info("Initialize ASR server engine successfully.")
66
        return True
W
WilliamZhang06 已提交
67

W
WilliamZhang06 已提交
68
    def run(self, audio_data):
69
        """engine run 
W
WilliamZhang06 已提交
70

71 72 73 74 75 76 77
        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 已提交
78 79
            self.executor.preprocess(self.config.model, io.BytesIO(audio_data))
            self.executor.infer(self.config.model)
80
            self.output = self.executor.postprocess()  # Retrieve result of asr.
W
WilliamZhang06 已提交
81 82
        else:
            logger.info("file check failed!")
83
            self.output = None
W
WilliamZhang06 已提交
84 85

    def postprocess(self):
86 87
        """postprocess
        """
W
WilliamZhang06 已提交
88
        return self.output