diff --git a/modelscope/hub/api.py b/modelscope/hub/api.py index 1955976c..7b5f8886 100644 --- a/modelscope/hub/api.py +++ b/modelscope/hub/api.py @@ -427,7 +427,9 @@ class HubApi: return tag_url def delete_model(self, model_id: str, endpoint: Optional[str] = None, token: Optional[str] = None): - """Delete model_id from ModelScope. + """ + @deprecated + Delete model_id from ModelScope. Args: model_id (str): The model id. @@ -440,6 +442,12 @@ class HubApi: Note: model_id = {owner}/{name} """ + warnings.warn( + 'This function is deprecated due to security reasons, ' + 'and will be recovered in future versions with proper token authentication. ', + DeprecationWarning, + stacklevel=2 + ) cookies = self.get_cookies(access_token=token, cookies_required=True) if not endpoint: endpoint = self.endpoint @@ -709,6 +717,7 @@ class HubApi: token: Optional[str] = None ): """ + @deprecated Delete a repository from ModelScope. Args: @@ -722,6 +731,12 @@ class HubApi: Could be set to `https://ai.modelscope.ai` for international version. token (str): Access token of the ModelScope. """ + warnings.warn( + 'This function is deprecated due to security reasons, ' + 'and will be recovered in future versions with proper token authentication. ', + DeprecationWarning, + stacklevel=2 + ) if not endpoint: endpoint = self.endpoint @@ -1356,6 +1371,7 @@ class HubApi: endpoint: Optional[str] = None, token: Optional[str] = None): """ + @deprecated Delete a dataset from ModelScope. Args: @@ -1366,6 +1382,12 @@ class HubApi: Returns: None """ + warnings.warn( + 'This function is deprecated due to security reasons, ' + 'and will be recovered in future versions with proper token authentication. ', + DeprecationWarning, + stacklevel=2 + ) cookies = self.get_cookies(access_token=token, cookies_required=True) if not endpoint: endpoint = self.endpoint diff --git a/tests/cli/test_download_cmd.py b/tests/cli/test_download_cmd.py index aa58c568..519314a5 100644 --- a/tests/cli/test_download_cmd.py +++ b/tests/cli/test_download_cmd.py @@ -9,10 +9,13 @@ import uuid from modelscope.hub.api import HubApi from modelscope.hub.constants import Licenses, ModelVisibility from modelscope.hub.repository import Repository +from modelscope.utils.logger import get_logger from modelscope.utils.test_utils import (TEST_ACCESS_TOKEN1, TEST_MODEL_CHINESE_NAME, TEST_MODEL_ORG) +logger = get_logger() + DEFAULT_GIT_PATH = 'git' download_model_file_name = 'test.bin' @@ -48,8 +51,11 @@ class DownloadCMDTest(unittest.TestCase): repo.tag_and_push(self.revision, 'Test revision') def tearDown(self): - self.api.delete_model(model_id=self.model_id) shutil.rmtree(self.tmp_dir) + try: + self.api.delete_model(model_id=self.model_id) + except Exception as e: + logger.warning(f'Error deleting model {self.model_id}: {e}') super().tearDown() def test_download(self): diff --git a/tests/cli/test_modelcard_cmd.py b/tests/cli/test_modelcard_cmd.py index 6dff2fe3..d27dfd66 100644 --- a/tests/cli/test_modelcard_cmd.py +++ b/tests/cli/test_modelcard_cmd.py @@ -1,5 +1,4 @@ import os -import os.path as osp import shutil import subprocess import tempfile @@ -7,8 +6,11 @@ import unittest import uuid from modelscope.hub.api import HubApi +from modelscope.utils.logger import get_logger from modelscope.utils.test_utils import TEST_ACCESS_TOKEN1, TEST_MODEL_ORG +logger = get_logger() + os.environ['MKL_THREADING_LAYER'] = 'GNU' @@ -28,7 +30,11 @@ class ModelUploadCMDTest(unittest.TestCase): print(self.tmp_dir, self.task_name, self.model_name) def tearDown(self): - self.api.delete_model(model_id=self.model_id) + try: + self.api.delete_model(model_id=self.model_id) + except Exception as e: + logger.warning(f'Failed to delete model {self.model_id}: {e}') + shutil.rmtree(self.tmp_dir) super().tearDown() diff --git a/tests/hub/test_commit_scheduler.py b/tests/hub/test_commit_scheduler.py index d746c330..53063ee9 100644 --- a/tests/hub/test_commit_scheduler.py +++ b/tests/hub/test_commit_scheduler.py @@ -14,10 +14,13 @@ from modelscope.hub.constants import Visibility from modelscope.hub.errors import NotExistError from modelscope.hub.file_download import _repo_file_download from modelscope.utils.constant import DEFAULT_REPOSITORY_REVISION +from modelscope.utils.logger import get_logger from modelscope.utils.repo_utils import CommitInfo, CommitOperationAdd from modelscope.utils.test_utils import (TEST_ACCESS_TOKEN1, TEST_MODEL_ORG, delete_credential, test_level) +logger = get_logger() + class TestCommitScheduler(unittest.TestCase): """Test suite for ModelScope CommitScheduler functionality.""" @@ -68,8 +71,8 @@ class TestCommitScheduler(unittest.TestCase): if hasattr(self, 'api') and TEST_ACCESS_TOKEN1: self.api.login(TEST_ACCESS_TOKEN1) self.api.delete_repo(repo_id=self.repo_id, repo_type='dataset') - except Exception: - pass + except Exception as e: + logger.warning(f'Failed to delete test repo {self.repo_id}: {e}') # Clean up temporary directories if self.cache_dir.exists(): diff --git a/tests/hub/test_create_aigc_model.py b/tests/hub/test_create_aigc_model.py index a17f9b8a..0529ebd6 100644 --- a/tests/hub/test_create_aigc_model.py +++ b/tests/hub/test_create_aigc_model.py @@ -33,8 +33,8 @@ class TestCreateAigcModel(unittest.TestCase): try: self.api.login(TEST_ACCESS_TOKEN1) self.api.delete_model(model_id=self.repo_id) - except HTTPError: - pass # It's ok if the repo doesn't exist (e.g., creation failed) + except Exception as e: + logger.warning(f'Error deleting model {self.repo_id}: {e}') os.remove(self.tmp_file_path) delete_credential() diff --git a/tests/hub/test_create_repo.py b/tests/hub/test_create_repo.py index b5658075..5b712507 100644 --- a/tests/hub/test_create_repo.py +++ b/tests/hub/test_create_repo.py @@ -22,10 +22,13 @@ class TestCreateRepo(unittest.TestCase): self.repo_id_dataset: str = f'{TEST_ORG}/test_create_repo_dataset_{uuid.uuid4().hex[-6:]}' def tearDown(self): - self.api.delete_repo( - repo_id=self.repo_id_model, repo_type=REPO_TYPE_MODEL) - self.api.delete_repo( - repo_id=self.repo_id_dataset, repo_type=REPO_TYPE_DATASET) + try: + self.api.delete_repo( + repo_id=self.repo_id_model, repo_type=REPO_TYPE_MODEL) + self.api.delete_repo( + repo_id=self.repo_id_dataset, repo_type=REPO_TYPE_DATASET) + except Exception as e: + logger.warning(f'Failed to delete repo during tearDown: {e} !') delete_credential() @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') diff --git a/tests/hub/test_hub_operation.py b/tests/hub/test_hub_operation.py index 41589aed..dd7e5891 100644 --- a/tests/hub/test_hub_operation.py +++ b/tests/hub/test_hub_operation.py @@ -4,7 +4,6 @@ import shutil import tempfile import unittest import uuid -from pathlib import Path from shutil import rmtree import requests @@ -15,11 +14,13 @@ 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.utils.constant import ModelFile -from modelscope.utils.file_utils import get_model_cache_dir +from modelscope.utils.logger import get_logger from modelscope.utils.test_utils import (TEST_ACCESS_TOKEN1, TEST_MODEL_CHINESE_NAME, TEST_MODEL_ORG) +logger = get_logger() + DEFAULT_GIT_PATH = 'git' download_model_file_name = 'test.bin' @@ -41,7 +42,10 @@ class HubOperationTest(unittest.TestCase): ) def tearDown(self): - self.api.delete_model(model_id=self.model_id) + try: + self.api.delete_model(model_id=self.model_id) + except Exception as e: + logger.warning(f'delete model {self.model_id} failed, {e}') def prepare_case(self): temporary_dir = tempfile.mkdtemp() diff --git a/tests/hub/test_hub_private_files.py b/tests/hub/test_hub_private_files.py index a343808f..6ece46d6 100644 --- a/tests/hub/test_hub_private_files.py +++ b/tests/hub/test_hub_private_files.py @@ -51,7 +51,10 @@ class HubPrivateFileDownloadTest(unittest.TestCase): # to ensure the temporary model is deleted. self.api.login(TEST_ACCESS_TOKEN1) os.chdir(self.old_cwd) - self.api.delete_model(model_id=self.model_id) + try: + self.api.delete_model(model_id=self.model_id) + except Exception as e: + print(f'delete model {self.model_id} failed, {e}') def test_snapshot_download_private_model(self): self.prepare_case() diff --git a/tests/hub/test_hub_private_repository.py b/tests/hub/test_hub_private_repository.py index 1b7c41cd..4306be45 100644 --- a/tests/hub/test_hub_private_repository.py +++ b/tests/hub/test_hub_private_repository.py @@ -35,7 +35,10 @@ class HubPrivateRepositoryTest(unittest.TestCase): def tearDown(self): self.api.login(TEST_ACCESS_TOKEN1) os.chdir(self.old_cwd) - self.api.delete_model(model_id=self.model_id) + try: + self.api.delete_model(model_id=self.model_id) + except Exception as e: + print(f'delete model {self.model_id} failed, {e}') def test_clone_private_repo_no_permission(self): token, _ = self.api.login(TEST_ACCESS_TOKEN2) diff --git a/tests/hub/test_hub_repository.py b/tests/hub/test_hub_repository.py index 92d89e74..a7f399b7 100644 --- a/tests/hub/test_hub_repository.py +++ b/tests/hub/test_hub_repository.py @@ -45,7 +45,10 @@ class HubRepositoryTest(unittest.TestCase): def tearDown(self): os.chdir(self.old_cwd) - self.api.delete_model(model_id=self.model_id) + try: + self.api.delete_model(model_id=self.model_id) + except Exception as e: + logger.warning(f'Failed to delete model {self.model_id}: {e}') def test_clone_repo(self): Repository(self.model_dir, clone_from=self.model_id) diff --git a/tests/hub/test_hub_revision.py b/tests/hub/test_hub_revision.py index 9a1e9f8a..98bea95e 100644 --- a/tests/hub/test_hub_revision.py +++ b/tests/hub/test_hub_revision.py @@ -39,7 +39,10 @@ class HubRevisionTest(unittest.TestCase): ) def tearDown(self): - self.api.delete_model(model_id=self.model_id) + try: + self.api.delete_model(model_id=self.model_id) + except Exception as e: + logger.warning(f'delete model {self.model_id} failed, {e}') def prepare_repo_data(self): temporary_dir = tempfile.mkdtemp() diff --git a/tests/hub/test_hub_revision_release_mode.py b/tests/hub/test_hub_revision_release_mode.py index a3cf8cdf..77cb1724 100644 --- a/tests/hub/test_hub_revision_release_mode.py +++ b/tests/hub/test_hub_revision_release_mode.py @@ -47,7 +47,10 @@ class HubRevisionTest(unittest.TestCase): } def tearDown(self): - self.api.delete_model(model_id=self.model_id) + try: + self.api.delete_model(model_id=self.model_id) + except Exception as e: + logger.warning(f'delete model {self.model_id} failed, {e}') def prepare_repo_data(self): temporary_dir = tempfile.mkdtemp() diff --git a/tests/hub/test_hub_upload.py b/tests/hub/test_hub_upload.py index 640ddf69..b1b582bc 100644 --- a/tests/hub/test_hub_upload.py +++ b/tests/hub/test_hub_upload.py @@ -44,8 +44,9 @@ class HubUploadTest(unittest.TestCase): shutil.rmtree(self.model_dir, ignore_errors=True) try: self.api.delete_model(model_id=self.create_model_name) - except Exception: - pass + except Exception as e: + logger.warning( + f'Failed to delete model {self.create_model_name}: {e}') def test_repo_exist(self): res = self.api.repo_exists('Qwen/Qwen2.5-7B-Instruct') @@ -59,6 +60,7 @@ class HubUploadTest(unittest.TestCase): 'Qwen/not-a-repo', repo_type=REPO_TYPE_DATASET) self.assertFalse(res) + @unittest.skipUnless(test_level() >= 1, 'skip test in current test level') def test_upload_exits_repo_master(self): logger.info('basic test for upload!') self.api.login(TEST_ACCESS_TOKEN1) @@ -124,7 +126,7 @@ class HubUploadTest(unittest.TestCase): revision='new_revision/version1') assert not os.path.exists(os.path.join(self.repo_path, 'add3.py')) - @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') + @unittest.skipUnless(test_level() >= 1, 'skip test in current test level') def test_upload_non_exists_repo(self): logger.info('test upload non exists repo!') self.api.login(TEST_ACCESS_TOKEN1) @@ -143,7 +145,7 @@ class HubUploadTest(unittest.TestCase): assert os.path.exists(os.path.join(self.repo_path, 'add1.py')) shutil.rmtree(self.repo_path, ignore_errors=True) - @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') + @unittest.skipUnless(test_level() >= 1, 'skip test in current test level') def test_upload_without_token(self): logger.info('test upload without login!') self.api.login(TEST_ACCESS_TOKEN1) @@ -155,7 +157,7 @@ class HubUploadTest(unittest.TestCase): visibility=ModelVisibility.PUBLIC, license=Licenses.APACHE_V2) - @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') + @unittest.skipUnless(test_level() >= 1, 'skip test in current test level') def test_upload_invalid_repo(self): logger.info('test upload to invalid repo!') self.api.login(TEST_ACCESS_TOKEN1) @@ -166,7 +168,7 @@ class HubUploadTest(unittest.TestCase): visibility=ModelVisibility.PUBLIC, license=Licenses.APACHE_V2) - @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') + @unittest.skipUnless(test_level() >= 1, 'skip test in current test level') def test_push_to_hub(self): ret = push_to_hub( repo_name=self.create_model_name, @@ -174,7 +176,7 @@ class HubUploadTest(unittest.TestCase): token=TEST_ACCESS_TOKEN1) self.assertTrue(ret is True) - @unittest.skipUnless(test_level() >= 0, 'skip test in current test level') + @unittest.skipUnless(test_level() >= 1, 'skip test in current test level') def test_push_to_hub_async(self): future = push_to_hub_async( repo_name=self.create_model_name, diff --git a/tests/hub/test_upload_file_folder.py b/tests/hub/test_upload_file_folder.py index aab0a1fd..ddb87b4e 100644 --- a/tests/hub/test_upload_file_folder.py +++ b/tests/hub/test_upload_file_folder.py @@ -47,10 +47,13 @@ class TestUploadFileFolder(unittest.TestCase): def tearDown(self): # Remove repositories - self.api.delete_repo( - repo_id=self.repo_id_model, repo_type=REPO_TYPE_MODEL) - self.api.delete_repo( - repo_id=self.repo_id_dataset, repo_type=REPO_TYPE_DATASET) + try: + self.api.delete_repo( + repo_id=self.repo_id_model, repo_type=REPO_TYPE_MODEL) + self.api.delete_repo( + repo_id=self.repo_id_dataset, repo_type=REPO_TYPE_DATASET) + except Exception as e: + logger.warning(f'Failed to delete repo: {e}') # Clean up the temporary credentials delete_credential() diff --git a/tests/preprocessors/test_image.py b/tests/preprocessors/test_image.py index a912b4b1..613fd2ae 100644 --- a/tests/preprocessors/test_image.py +++ b/tests/preprocessors/test_image.py @@ -5,10 +5,12 @@ import unittest from PIL import Image from modelscope.preprocessors import load_image +from modelscope.utils.test_utils import test_level class ImagePreprocessorTest(unittest.TestCase): + @unittest.skipUnless(test_level() >= 1, 'skip test in current test level') def test_load(self): img = load_image('data/test/images/image_matting.png') self.assertTrue(isinstance(img, Image.Image)) diff --git a/tests/utils/test_hf_util.py b/tests/utils/test_hf_util.py index c162e067..82f3583f 100644 --- a/tests/utils/test_hf_util.py +++ b/tests/utils/test_hf_util.py @@ -56,8 +56,9 @@ class HFUtilTest(unittest.TestCase): shutil.rmtree(self.model_dir, ignore_errors=True) try: self.api.delete_model(model_id=self.create_model_name) - except Exception: - pass + except Exception as e: + logger.warning( + f'Failed to delete model {self.create_model_name}: {e}') def test_auto_tokenizer(self): from modelscope import AutoTokenizer @@ -296,8 +297,10 @@ class HFUtilTest(unittest.TestCase): save_dir = './tmp_test_hf_pipeline' try: os.system(f'rm -rf {save_dir}') - self.api.delete_model(repo_id) - # wait for delete repo + try: + self.api.delete_model(repo_id) + except Exception as e: + logger.warning(f'Failed to delete model {repo_id}: {e}') import time time.sleep(5) except Exception: diff --git a/tests/utils/test_input_output.py b/tests/utils/test_input_output.py index 53b75a39..1051596e 100644 --- a/tests/utils/test_input_output.py +++ b/tests/utils/test_input_output.py @@ -1,11 +1,10 @@ import base64 import unittest -import json - from modelscope.utils.constant import Tasks from modelscope.utils.input_output import ( PipelineInfomation, service_base64_input_to_pipeline_input) +from modelscope.utils.test_utils import test_level def encode_image_to_base64(image): @@ -116,6 +115,7 @@ class PipelineInputOutputTest(unittest.TestCase): } assert expect_schema == schema + @unittest.skipUnless(test_level() >= 1, 'skip test in current test level') def test_input_output_encode_decode(self): with open('data/test/images/image_captioning.png', 'rb') as f: image = f.read()