Skip to content

Commit 599d60a

Browse files
authored
[Fix] HubAPI token usage (#1593)
* fix hub push model token * fix token
1 parent bb07c16 commit 599d60a

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

modelscope/hub/api.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,8 @@ def get_model(
514514
def get_endpoint_for_read(self,
515515
repo_id: str,
516516
*,
517-
repo_type: Optional[str] = None) -> str:
517+
repo_type: Optional[str] = None,
518+
token: Optional[str] = None) -> str:
518519
"""Get proper endpoint for read operation (such as download, list etc.)
519520
1. If user has set MODELSCOPE_DOMAIN, construct endpoint with user-specified domain.
520521
If the repo does not exist on that endpoint, throw 404 error, otherwise return the endpoint.
@@ -529,7 +530,7 @@ def get_endpoint_for_read(self,
529530
if s is not None and s.strip() != '':
530531
endpoint = MODELSCOPE_URL_SCHEME + s
531532
try:
532-
self.repo_exists(repo_id=repo_id, repo_type=repo_type, endpoint=endpoint, re_raise=True)
533+
self.repo_exists(repo_id=repo_id, repo_type=repo_type, endpoint=endpoint, re_raise=True, token=token)
533534
except Exception:
534535
logger.error(f'Repo {repo_id} does not exist on {endpoint}.')
535536
raise
@@ -538,13 +539,13 @@ def get_endpoint_for_read(self,
538539
check_cn_first = not is_env_true(MODELSCOPE_PREFER_AI_SITE)
539540
prefer_endpoint = get_endpoint(cn_site=check_cn_first)
540541
if not self.repo_exists(
541-
repo_id, repo_type=repo_type, endpoint=prefer_endpoint):
542+
repo_id, repo_type=repo_type, endpoint=prefer_endpoint, token=token):
542543
alternative_endpoint = get_endpoint(cn_site=(not check_cn_first))
543544
logger.warning(f'Repo {repo_id} not exists on {prefer_endpoint}, '
544545
f'will try on alternative endpoint {alternative_endpoint}.')
545546
try:
546547
self.repo_exists(
547-
repo_id, repo_type=repo_type, endpoint=alternative_endpoint, re_raise=True)
548+
repo_id, repo_type=repo_type, endpoint=alternative_endpoint, re_raise=True, token=token)
548549
except Exception:
549550
logger.error(f'Repo {repo_id} not exists on either {prefer_endpoint} or {alternative_endpoint}')
550551
raise
@@ -2917,6 +2918,7 @@ def delete_files(self,
29172918
page_number=page_number,
29182919
page_size=page_size,
29192920
endpoint=endpoint,
2921+
token=token,
29202922
)
29212923
except Exception as e:
29222924
logger.error(f'Get dataset: {repo_id} file list failed, message: {str(e)}')
@@ -3006,7 +3008,7 @@ def set_repo_visibility(self,
30063008
cookies = self.get_cookies(access_token=token, cookies_required=True)
30073009

30083010
if repo_type == REPO_TYPE_MODEL:
3009-
model_info = self.get_model(model_id=repo_id)
3011+
model_info = self.get_model(model_id=repo_id, token=token)
30103012
path = f'{self.endpoint}/api/v1/models/{repo_id}'
30113013
tasks = model_info.get('Tasks')
30123014
model_tasks = ''
@@ -3039,6 +3041,7 @@ def set_repo_visibility(self,
30393041
dataset_idx, _ = self.get_dataset_id_and_type(
30403042
dataset_name=repo_id_parts[1],
30413043
namespace=repo_id_parts[0],
3044+
token=token
30423045
)
30433046

30443047
path = f'{self.endpoint}/api/v1/datasets/{dataset_idx}'
@@ -3089,6 +3092,8 @@ def get_cookies():
30893092
if os.path.exists(cookies_path):
30903093
with open(cookies_path, 'rb') as f:
30913094
cookies = pickle.load(f)
3095+
if not cookies:
3096+
return None
30923097
for cookie in cookies:
30933098
if cookie.name == 'm_session_id' and cookie.is_expired() and \
30943099
not ModelScopeConfig.cookie_expired_warning:

modelscope/hub/file_download.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ def _repo_file_download(
221221
if cookies is None:
222222
cookies = _api.get_cookies()
223223
repo_files = []
224-
endpoint = _api.get_endpoint_for_read(repo_id=repo_id, repo_type=repo_type)
224+
endpoint = _api.get_endpoint_for_read(
225+
repo_id=repo_id, repo_type=repo_type, token=token)
225226
file_to_download_meta = None
226227
if repo_type == REPO_TYPE_MODEL:
227228
revision = _api.get_valid_revision(
@@ -263,7 +264,8 @@ def _repo_file_download(
263264
recursive=True,
264265
page_number=page_number,
265266
page_size=page_size,
266-
endpoint=endpoint)
267+
endpoint=endpoint,
268+
token=token)
267269
except Exception as e:
268270
logger.error(
269271
f'Get dataset: {repo_id} file list failed, error: {e}')

modelscope/msdatasets/utils/oss_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def get_credentials(self):
5656
oss_config = self.api.get_dataset_access_config_session(
5757
dataset_name=self.dataset_name,
5858
namespace=self.namespace,
59-
check_cookie=True,
59+
check_cookie=False,
6060
revision=self.revision)
6161

6262
return Credentials(
@@ -83,7 +83,7 @@ def __init__(self, dataset_name, namespace, revision):
8383
oss_config = self.api.get_dataset_access_config_session(
8484
dataset_name=self.dataset_name,
8585
namespace=self.namespace,
86-
check_cookie=True,
86+
check_cookie=False,
8787
revision=self.revision)
8888

8989
if os.getenv('ENABLE_DATASET_ACCELERATION') == 'True':

0 commit comments

Comments
 (0)