diff --git a/modelscope/utils/hub.py b/modelscope/utils/hub.py index 01a1b1b0..868e751b 100644 --- a/modelscope/utils/hub.py +++ b/modelscope/utils/hub.py @@ -2,21 +2,39 @@ import os import os.path as osp -from typing import List, Union +from typing import List, Optional, Union -from numpy import deprecate +from requests import HTTPError from modelscope.hub.file_download import model_file_download from modelscope.hub.snapshot_download import snapshot_download -from modelscope.hub.utils.utils import get_cache_dir from modelscope.utils.config import Config from modelscope.utils.constant import ModelFile -# temp solution before the hub-cache is in place -@deprecate -def get_model_cache_dir(model_id: str): - return os.path.join(get_cache_dir(), model_id) +def create_model_if_not_exist( + api, + model_id: str, + chinese_name: str, + visibility: Optional[int] = 5, # 1-private, 5-public + license: Optional[str] = 'apache-2.0', + revision: Optional[str] = 'master'): + exists = True + try: + api.get_model(model_id=model_id, revision=revision) + except HTTPError: + exists = False + if exists: + print(f'model {model_id} already exists, skip creation.') + return False + else: + api.create_model( + model_id=model_id, + chinese_name=chinese_name, + visibility=visibility, + license=license) + print(f'model {model_id} successfully created.') + return True def read_config(model_id_or_path: str): diff --git a/tests/hub/test_hub_examples.py b/tests/hub/test_hub_examples.py new file mode 100644 index 00000000..b63445af --- /dev/null +++ b/tests/hub/test_hub_examples.py @@ -0,0 +1,33 @@ +import unittest + +from maas_hub.maas_api import MaasApi + +from modelscope.utils.hub import create_model_if_not_exist + +USER_NAME = 'maasadmin' +PASSWORD = '12345678' + + +class HubExampleTest(unittest.TestCase): + + def setUp(self): + self.api = MaasApi() + # note this is temporary before official account management is ready + self.api.login(USER_NAME, PASSWORD) + + @unittest.skip('to be used for local test only') + def test_example_model_creation(self): + # ATTENTION:change to proper model names before use + model_name = 'cv_unet_person-image-cartoon_compound-models' + model_chinese_name = '达摩卡通化模型' + model_org = 'damo' + model_id = '%s/%s' % (model_org, model_name) + + created = create_model_if_not_exist(self.api, model_id, + model_chinese_name) + if not created: + print('!! NOT created since model already exists !!') + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/hub/test_hub_operation.py b/tests/hub/test_hub_operation.py index 2277860b..d44cd7c1 100644 --- a/tests/hub/test_hub_operation.py +++ b/tests/hub/test_hub_operation.py @@ -1,6 +1,5 @@ # Copyright (c) Alibaba, Inc. and its affiliates. import os -import os.path as osp import subprocess import tempfile import unittest @@ -8,7 +7,6 @@ import uuid from modelscope.hub.api import HubApi, ModelScopeConfig from modelscope.hub.file_download import model_file_download -from modelscope.hub.repository import Repository from modelscope.hub.snapshot_download import snapshot_download from modelscope.hub.utils.utils import get_gitlab_domain