asr_engine.py 2.9 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 16 17 18 19
import io
import os
from typing import List
from typing import Optional
from typing import Union

20 21 22 23
import librosa
import paddle
import soundfile

W
WilliamZhang06 已提交
24
from paddlespeech.cli.asr.infer import ASRExecutor
25
from paddlespeech.cli.log import logger
W
WilliamZhang06 已提交
26 27 28 29
from paddlespeech.s2t.frontend.featurizer.text_featurizer import TextFeaturizer
from paddlespeech.s2t.transform.transformation import Transformation
from paddlespeech.s2t.utils.dynamic_import import dynamic_import
from paddlespeech.s2t.utils.utility import UpdateConfig
L
lym0302 已提交
30 31
from paddlespeech.server.engine.base_engine import BaseEngine
from paddlespeech.server.utils.config import get_config
W
WilliamZhang06 已提交
32 33 34 35

__all__ = ['ASREngine']


W
WilliamZhang06 已提交
36 37 38 39 40 41
class ASRServerExecutor(ASRExecutor):
    def __init__(self):
        super().__init__()
        pass


42
class ASREngine(BaseEngine):
W
WilliamZhang06 已提交
43 44 45 46 47
    """ASR server engine

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

49
    def __init__(self):
W
WilliamZhang06 已提交
50
        super(ASREngine, self).__init__()
51

52 53 54 55 56
    def init(self, config_file: str) -> bool:
        """init engine resource

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

58 59 60 61 62
        Returns:
            bool: init failed or success
        """
        self.input = None
        self.output = None
W
WilliamZhang06 已提交
63 64
        self.executor = ASRServerExecutor()

65 66 67 68 69 70
        self.config = get_config(config_file)
        paddle.set_device(paddle.get_device())
        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 已提交
71 72

        logger.info("Initialize ASR server engine successfully.")
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 86
            self.executor.preprocess(self.config.model, io.BytesIO(audio_data))
            self.executor.infer(self.config.model)
87
            self.output = self.executor.postprocess()  # Retrieve result of asr.
W
WilliamZhang06 已提交
88 89
        else:
            logger.info("file check failed!")
90
            self.output = None
W
WilliamZhang06 已提交
91 92

    def postprocess(self):
93 94
        """postprocess
        """
W
WilliamZhang06 已提交
95
        return self.output