Browse Source

[to #42362853] feat: rename config to configuration and remove repeated task fileds

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/9010802
master
wenmeng.zwm 3 years ago
parent
commit
8a030ead72
8 changed files with 36 additions and 31 deletions
  1. +0
    -0
      configs/examples/configuration.json
  2. +0
    -0
      configs/examples/configuration.py
  3. +0
    -0
      configs/examples/configuration.yaml
  4. +21
    -14
      modelscope/pipelines/util.py
  5. +1
    -1
      modelscope/preprocessors/image.py
  6. +6
    -6
      modelscope/utils/config.py
  7. +3
    -5
      modelscope/utils/constant.py
  8. +5
    -5
      tests/utils/test_config.py

configs/examples/config.json → configs/examples/configuration.json View File


configs/examples/config.py → configs/examples/configuration.py View File


configs/examples/config.yaml → configs/examples/configuration.yaml View File


+ 21
- 14
modelscope/pipelines/util.py View File

@@ -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)


+ 1
- 1
modelscope/preprocessors/image.py View File

@@ -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",


+ 6
- 6
modelscope/utils/config.py View File

@@ -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


+ 3
- 5
modelscope/utils/constant.py View File

@@ -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'

+ 5
- 5
tests/utils/test_config.py View File

@@ -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)


Loading…
Cancel
Save