diff --git a/paddlecv/ppcv/ops/models/nlp/inference.py b/paddlecv/ppcv/ops/models/nlp/inference.py index 7ba1f132a9d613288f0c0389eace1a07821cebe9..2e5da8025328b1c54ce6d7cea3829091de75739d 100644 --- a/paddlecv/ppcv/ops/models/nlp/inference.py +++ b/paddlecv/ppcv/ops/models/nlp/inference.py @@ -18,11 +18,11 @@ import os import numpy as np import math import paddle -from paddlenlp import Taskflow from ..base import ModelBaseOp from ppcv.ops.base import create_operators, BaseOp from ppcv.core.workspace import register +from ppcv.utils.utility import check_install from .postprocess import * @@ -43,6 +43,8 @@ class SentimentAnalysisOp(BaseOp): self._init_task(model_cfg) def _init_task(self, model_cfg): + check_install('paddlenlp', 'paddlenlp') + from paddlenlp import Taskflow task = model_cfg.get('task', 'sentiment_analysis') self.nlp = Taskflow(task) @@ -123,6 +125,8 @@ class InformationExtractionOp(SentimentAnalysisOp): self._init_task(model_cfg) def _init_task(self, model_cfg): + check_install('paddlenlp', 'paddlenlp') + from paddlenlp import Taskflow task = model_cfg.get('task', 'information_extraction') schema = model_cfg.get('schema', ['时间', '地点', '人物']) self.nlp = Taskflow(task, schema=schema) diff --git a/paddlecv/ppcv/ops/models/speech/inference.py b/paddlecv/ppcv/ops/models/speech/inference.py index fc180822092db7df83b031a99d9a2b4bd318107e..200de825bc572ffcbfcf8db18119616598edcfe2 100644 --- a/paddlecv/ppcv/ops/models/speech/inference.py +++ b/paddlecv/ppcv/ops/models/speech/inference.py @@ -15,16 +15,19 @@ import importlib import pathlib import os -from paddlespeech.cli.tts import TTSExecutor from ppcv.ops.base import create_operators, BaseOp from ppcv.core.workspace import register +from ppcv.utils.utility import check_install @register class TTSOp(BaseOp): def __init__(self, model_cfg, env_cfg): super(TTSOp, self).__init__(model_cfg, env_cfg) + check_install('paddlespeech', 'paddlespeech') + from paddlespeech.cli.tts import TTSExecutor + mod = importlib.import_module(__name__) env_cfg["batch_size"] = model_cfg.get("batch_size", 1) self.batch_size = env_cfg["batch_size"] diff --git a/paddlecv/ppcv/utils/utility.py b/paddlecv/ppcv/utils/utility.py new file mode 100644 index 0000000000000000000000000000000000000000..bee12c85f64cbddb6cd48f2833d679fdba5f1975 --- /dev/null +++ b/paddlecv/ppcv/utils/utility.py @@ -0,0 +1,37 @@ +# 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. + +import importlib.util +import sys +import subprocess + + +def check_install(module_name, install_name): + spec = importlib.util.find_spec(module_name) + if spec is None: + print(f'Warnning! The {module_name} module is NOT installed') + print( + f'Try install {module_name} module automatically. You can also try to install manually by pip install {install_name}.' + ) + python = sys.executable + try: + subprocess.check_call( + [python, '-m', 'pip', 'install', install_name], + stdout=subprocess.DEVNULL) + print(f'The {module_name} module is now installed') + except subprocess.CalledProcessError as exc: + raise Exception( + f"Install {module_name} failed, please install manually") + else: + print(f"{module_name} has been installed.") diff --git a/paddlecv/requirements.txt b/paddlecv/requirements.txt index c52b7418af719e8b8021be9b8b9f226ca9d4f15b..04d98ed582297f13d956708ab26476d5904aab37 100644 --- a/paddlecv/requirements.txt +++ b/paddlecv/requirements.txt @@ -4,8 +4,6 @@ opencv-contrib-python PyYAML>=5.1 Pillow faiss-cpu==1.7.1.post2 -paddlenlp -paddlespeech pycocotools filterpy shapely