diff --git a/configs/examples/config.json b/configs/examples/configuration.json similarity index 100% rename from configs/examples/config.json rename to configs/examples/configuration.json diff --git a/configs/examples/config.py b/configs/examples/configuration.py similarity index 100% rename from configs/examples/config.py rename to configs/examples/configuration.py diff --git a/configs/examples/config.yaml b/configs/examples/configuration.yaml similarity index 100% rename from configs/examples/config.yaml rename to configs/examples/configuration.yaml diff --git a/modelscope/pipelines/util.py b/modelscope/pipelines/util.py index caef6b22..43a7ac5a 100644 --- a/modelscope/pipelines/util.py +++ b/modelscope/pipelines/util.py @@ -5,8 +5,22 @@ from typing import List, Union import json from maas_hub.file_download import model_file_download +from matplotlib.pyplot import get +from modelscope.utils.config import Config from modelscope.utils.constant import CONFIGFILE +from modelscope.utils.logger import get_logger + +logger = get_logger() + + +def is_config_has_model(cfg_file): + try: + cfg = Config.from_file(cfg_file) + return hasattr(cfg, 'model') + except Exception as e: + logger.error(f'parse config file {cfg_file} failed: {e}') + return False def is_model_name(model: Union[str, List]): @@ -15,24 +29,17 @@ def is_model_name(model: Union[str, List]): def is_model_name_impl(model): if osp.exists(model): - if osp.exists(osp.join(model, CONFIGFILE)): - return True + cfg_file = osp.join(model, CONFIGFILE) + if osp.exists(cfg_file): + return is_config_has_model(cfg_file) else: return False else: - # try: - # cfg_file = model_file_download(model, CONFIGFILE) - # except Exception: - # cfg_file = None - # TODO @wenmeng.zwm use exception instead of - # following tricky logic - cfg_file = model_file_download(model, CONFIGFILE) - with open(cfg_file, 'r') as infile: - cfg = json.load(infile) - if 'Code' in cfg: + try: + cfg_file = model_file_download(model, CONFIGFILE) + return is_config_has_model(cfg_file) + except Exception: return False - else: - return True if isinstance(model, str): return is_model_name_impl(model) diff --git a/modelscope/preprocessors/image.py b/modelscope/preprocessors/image.py index 142f9484..6bd8aed5 100644 --- a/modelscope/preprocessors/image.py +++ b/modelscope/preprocessors/image.py @@ -9,7 +9,7 @@ from modelscope.utils.constant import Fields from .builder import PREPROCESSORS -@PREPROCESSORS.register_module(Fields.image) +@PREPROCESSORS.register_module(Fields.cv) class LoadImage: """Load an image from file or url. Added or updated keys are "filename", "img", "img_shape", diff --git a/modelscope/utils/config.py b/modelscope/utils/config.py index d0f3f657..df9e38fd 100644 --- a/modelscope/utils/config.py +++ b/modelscope/utils/config.py @@ -74,17 +74,17 @@ class Config: {'c': [1, 2, 3], 'd': 'dd'} >>> cfg.b.d 'dd' - >>> cfg = Config.from_file('configs/examples/config.json') + >>> cfg = Config.from_file('configs/examples/configuration.json') >>> cfg.filename - 'configs/examples/config.json' + 'configs/examples/configuration.json' >>> cfg.b {'c': [1, 2, 3], 'd': 'dd'} - >>> cfg = Config.from_file('configs/examples/config.py') + >>> cfg = Config.from_file('configs/examples/configuration.py') >>> cfg.filename - "configs/examples/config.py" - >>> cfg = Config.from_file('configs/examples/config.yaml') + "configs/examples/configuration.py" + >>> cfg = Config.from_file('configs/examples/configuration.yaml') >>> cfg.filename - "configs/examples/config.yaml" + "configs/examples/configuration.yaml" """ @staticmethod diff --git a/modelscope/utils/constant.py b/modelscope/utils/constant.py index 0d0f2492..fa30dd2a 100644 --- a/modelscope/utils/constant.py +++ b/modelscope/utils/constant.py @@ -4,8 +4,8 @@ class Fields(object): """ Names for different application fields """ - image = 'image' - video = 'video' + # image = 'image' + # video = 'video' cv = 'cv' nlp = 'nlp' audio = 'audio' @@ -72,6 +72,4 @@ class Hubs(object): # configuration filename -# in order to avoid conflict with huggingface -# config file we use maas_config instead -CONFIGFILE = 'maas_config.json' +CONFIGFILE = 'configuration.json' diff --git a/tests/utils/test_config.py b/tests/utils/test_config.py index 48f1d4a8..fb7044e8 100644 --- a/tests/utils/test_config.py +++ b/tests/utils/test_config.py @@ -14,25 +14,25 @@ obj = {'a': 1, 'b': {'c': [1, 2, 3], 'd': 'dd'}} class ConfigTest(unittest.TestCase): def test_json(self): - config_file = 'configs/examples/config.json' + config_file = 'configs/examples/configuration.json' cfg = Config.from_file(config_file) self.assertEqual(cfg.a, 1) self.assertEqual(cfg.b, obj['b']) def test_yaml(self): - config_file = 'configs/examples/config.yaml' + config_file = 'configs/examples/configuration.yaml' cfg = Config.from_file(config_file) self.assertEqual(cfg.a, 1) self.assertEqual(cfg.b, obj['b']) def test_py(self): - config_file = 'configs/examples/config.py' + config_file = 'configs/examples/configuration.py' cfg = Config.from_file(config_file) self.assertEqual(cfg.a, 1) self.assertEqual(cfg.b, obj['b']) def test_dump(self): - config_file = 'configs/examples/config.py' + config_file = 'configs/examples/configuration.py' cfg = Config.from_file(config_file) self.assertEqual(cfg.a, 1) self.assertEqual(cfg.b, obj['b']) @@ -53,7 +53,7 @@ class ConfigTest(unittest.TestCase): self.assertEqual(yaml_str, infile.read()) def test_to_dict(self): - config_file = 'configs/examples/config.json' + config_file = 'configs/examples/configuration.json' cfg = Config.from_file(config_file) d = cfg.to_dict() print(d)