1. rename maas_config to configuration 2. remove task field image and video, using cv instead Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/9010802master
| @@ -5,8 +5,22 @@ from typing import List, Union | |||||
| import json | import json | ||||
| from maas_hub.file_download import model_file_download | 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.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]): | 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): | def is_model_name_impl(model): | ||||
| if osp.exists(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: | else: | ||||
| return False | return False | ||||
| else: | 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 | return False | ||||
| else: | |||||
| return True | |||||
| if isinstance(model, str): | if isinstance(model, str): | ||||
| return is_model_name_impl(model) | return is_model_name_impl(model) | ||||
| @@ -9,7 +9,7 @@ from modelscope.utils.constant import Fields | |||||
| from .builder import PREPROCESSORS | from .builder import PREPROCESSORS | ||||
| @PREPROCESSORS.register_module(Fields.image) | |||||
| @PREPROCESSORS.register_module(Fields.cv) | |||||
| class LoadImage: | class LoadImage: | ||||
| """Load an image from file or url. | """Load an image from file or url. | ||||
| Added or updated keys are "filename", "img", "img_shape", | Added or updated keys are "filename", "img", "img_shape", | ||||
| @@ -74,17 +74,17 @@ class Config: | |||||
| {'c': [1, 2, 3], 'd': 'dd'} | {'c': [1, 2, 3], 'd': 'dd'} | ||||
| >>> cfg.b.d | >>> cfg.b.d | ||||
| 'dd' | 'dd' | ||||
| >>> cfg = Config.from_file('configs/examples/config.json') | |||||
| >>> cfg = Config.from_file('configs/examples/configuration.json') | |||||
| >>> cfg.filename | >>> cfg.filename | ||||
| 'configs/examples/config.json' | |||||
| 'configs/examples/configuration.json' | |||||
| >>> cfg.b | >>> cfg.b | ||||
| {'c': [1, 2, 3], 'd': 'dd'} | {'c': [1, 2, 3], 'd': 'dd'} | ||||
| >>> cfg = Config.from_file('configs/examples/config.py') | |||||
| >>> cfg = Config.from_file('configs/examples/configuration.py') | |||||
| >>> cfg.filename | >>> 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 | >>> cfg.filename | ||||
| "configs/examples/config.yaml" | |||||
| "configs/examples/configuration.yaml" | |||||
| """ | """ | ||||
| @staticmethod | @staticmethod | ||||
| @@ -4,8 +4,8 @@ | |||||
| class Fields(object): | class Fields(object): | ||||
| """ Names for different application fields | """ Names for different application fields | ||||
| """ | """ | ||||
| image = 'image' | |||||
| video = 'video' | |||||
| # image = 'image' | |||||
| # video = 'video' | |||||
| cv = 'cv' | cv = 'cv' | ||||
| nlp = 'nlp' | nlp = 'nlp' | ||||
| audio = 'audio' | audio = 'audio' | ||||
| @@ -72,6 +72,4 @@ class Hubs(object): | |||||
| # configuration filename | # configuration filename | ||||
| # in order to avoid conflict with huggingface | |||||
| # config file we use maas_config instead | |||||
| CONFIGFILE = 'maas_config.json' | |||||
| CONFIGFILE = 'configuration.json' | |||||
| @@ -14,25 +14,25 @@ obj = {'a': 1, 'b': {'c': [1, 2, 3], 'd': 'dd'}} | |||||
| class ConfigTest(unittest.TestCase): | class ConfigTest(unittest.TestCase): | ||||
| def test_json(self): | def test_json(self): | ||||
| config_file = 'configs/examples/config.json' | |||||
| config_file = 'configs/examples/configuration.json' | |||||
| cfg = Config.from_file(config_file) | cfg = Config.from_file(config_file) | ||||
| self.assertEqual(cfg.a, 1) | self.assertEqual(cfg.a, 1) | ||||
| self.assertEqual(cfg.b, obj['b']) | self.assertEqual(cfg.b, obj['b']) | ||||
| def test_yaml(self): | def test_yaml(self): | ||||
| config_file = 'configs/examples/config.yaml' | |||||
| config_file = 'configs/examples/configuration.yaml' | |||||
| cfg = Config.from_file(config_file) | cfg = Config.from_file(config_file) | ||||
| self.assertEqual(cfg.a, 1) | self.assertEqual(cfg.a, 1) | ||||
| self.assertEqual(cfg.b, obj['b']) | self.assertEqual(cfg.b, obj['b']) | ||||
| def test_py(self): | def test_py(self): | ||||
| config_file = 'configs/examples/config.py' | |||||
| config_file = 'configs/examples/configuration.py' | |||||
| cfg = Config.from_file(config_file) | cfg = Config.from_file(config_file) | ||||
| self.assertEqual(cfg.a, 1) | self.assertEqual(cfg.a, 1) | ||||
| self.assertEqual(cfg.b, obj['b']) | self.assertEqual(cfg.b, obj['b']) | ||||
| def test_dump(self): | def test_dump(self): | ||||
| config_file = 'configs/examples/config.py' | |||||
| config_file = 'configs/examples/configuration.py' | |||||
| cfg = Config.from_file(config_file) | cfg = Config.from_file(config_file) | ||||
| self.assertEqual(cfg.a, 1) | self.assertEqual(cfg.a, 1) | ||||
| self.assertEqual(cfg.b, obj['b']) | self.assertEqual(cfg.b, obj['b']) | ||||
| @@ -53,7 +53,7 @@ class ConfigTest(unittest.TestCase): | |||||
| self.assertEqual(yaml_str, infile.read()) | self.assertEqual(yaml_str, infile.read()) | ||||
| def test_to_dict(self): | def test_to_dict(self): | ||||
| config_file = 'configs/examples/config.json' | |||||
| config_file = 'configs/examples/configuration.json' | |||||
| cfg = Config.from_file(config_file) | cfg = Config.from_file(config_file) | ||||
| d = cfg.to_dict() | d = cfg.to_dict() | ||||
| print(d) | print(d) | ||||