From c94ebdc52cdcf52b9e400fe2090efc953f895b4e Mon Sep 17 00:00:00 2001 From: KP <109694228@qq.com> Date: Tue, 30 Nov 2021 14:22:32 +0800 Subject: [PATCH] Add python api for executor. --- paddlespeech/cli/executor.py | 15 +++++++++++++++ paddlespeech/cli/s2t/infer.py | 19 +++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/paddlespeech/cli/executor.py b/paddlespeech/cli/executor.py index 2261e011..2314bd6d 100644 --- a/paddlespeech/cli/executor.py +++ b/paddlespeech/cli/executor.py @@ -14,6 +14,7 @@ import os from abc import ABC from abc import abstractmethod +from typing import List from typing import Union import paddle @@ -64,3 +65,17 @@ class BaseExecutor(ABC): Output postprocess and return human-readable results such as texts and audio files. """ pass + + @abstractmethod + def execute(self, argv: List[str]) -> bool: + """ + Command line entry. + """ + pass + + @abstractmethod + def __call__(self, *arg, **kwargs): + """ + Python API to call an executor. + """ + pass diff --git a/paddlespeech/cli/s2t/infer.py b/paddlespeech/cli/s2t/infer.py index 6aa29add..9509e311 100644 --- a/paddlespeech/cli/s2t/infer.py +++ b/paddlespeech/cli/s2t/infer.py @@ -126,6 +126,9 @@ class S2TExecutor(BaseExecutor): pass def execute(self, argv: List[str]) -> bool: + """ + Command line entry. + """ parser_args = self.parser.parse_args(argv) print(parser_args) @@ -137,12 +140,20 @@ class S2TExecutor(BaseExecutor): device = parser_args.device try: - self._init_from_path(model, lang, config, ckpt_path) - self.preprocess(audio_file) - self.infer() - res = self.postprocess() # Retrieve result of s2t. + res = self(model, lang, config, ckpt_path, audio_file, device) print(res) return True except Exception as e: print(e) return False + + def __call__(self, model, lang, config, ckpt_path, audio_file, device): + """ + Python API to call an executor. + """ + self._init_from_path(model, lang, config, ckpt_path) + self.preprocess(audio_file) + self.infer() + res = self.postprocess() # Retrieve result of s2t. + + return res