Fix: deprecate delete_repo, delete_model and delete_dataset due to token a… (#1588)

This commit is contained in:
Xingjun.Wang
2026-01-12 11:51:46 +08:00
committed by GitHub
parent fe6744fbd9
commit 4f867bfe80
17 changed files with 106 additions and 37 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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