asr_engine.py 3.1 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 22
from paddlespeech.server.engine.base_engine import BaseEngine
from paddlespeech.server.utils.config import get_config
W
WilliamZhang06 已提交
23 24 25 26

__all__ = ['ASREngine']


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


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

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

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

43 44 45 46 47
    def init(self, config_file: str) -> bool:
        """init engine resource

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

49 50 51 52 53
        Returns:
            bool: init failed or success
        """
        self.input = None
        self.output = None
W
WilliamZhang06 已提交
54 55
        self.executor = ASRServerExecutor()

56
        self.config = get_config(config_file)
L
lym0302 已提交
57 58 59 60 61 62 63 64 65 66 67
        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"
            )

68 69 70 71
        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 已提交
72

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

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

80 81 82 83 84 85 86
        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 已提交
87
            self.executor.preprocess(self.config.model, io.BytesIO(audio_data))
L
lym0302 已提交
88
            st = time.time()
W
WilliamZhang06 已提交
89
            self.executor.infer(self.config.model)
L
lym0302 已提交
90
            infer_time = time.time() - st
91
            self.output = self.executor.postprocess()  # Retrieve result of asr.
W
WilliamZhang06 已提交
92 93
        else:
            logger.info("file check failed!")
94
            self.output = None
W
WilliamZhang06 已提交
95

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

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