fix httperror

This commit is contained in:
Yingda Chen
2025-03-04 18:57:04 +08:00
parent f8dc0045e2
commit cb3c6e2902

View File

@@ -17,13 +17,13 @@ from http.cookiejar import CookieJar
from os.path import expanduser from os.path import expanduser
from pathlib import Path from pathlib import Path
from typing import Any, BinaryIO, Dict, Iterable, List, Optional, Tuple, Union from typing import Any, BinaryIO, Dict, Iterable, List, Optional, Tuple, Union
from urllib.error import HTTPError
from urllib.parse import urlencode from urllib.parse import urlencode
import json import json
import requests import requests
from requests import Session from requests import Session
from requests.adapters import HTTPAdapter, Retry from requests.adapters import HTTPAdapter, Retry
from requests.exceptions import HTTPError
from tqdm.auto import tqdm from tqdm.auto import tqdm
from modelscope.hub.constants import (API_HTTP_CLIENT_MAX_RETRIES, from modelscope.hub.constants import (API_HTTP_CLIENT_MAX_RETRIES,
@@ -318,14 +318,12 @@ class HubApi:
""" """
if MODELSCOPE_DOMAIN in os.environ: if MODELSCOPE_DOMAIN in os.environ:
endpoint = MODELSCOPE_URL_SCHEME + os.getenv(MODELSCOPE_DOMAIN) endpoint = MODELSCOPE_URL_SCHEME + os.getenv(MODELSCOPE_DOMAIN)
if not self.repo_exists(repo_id=repo_id, repo_type=repo_type, endpoint=endpoint): try:
raise HTTPError(url=endpoint + '/' + repo_id, self.repo_exists(repo_id=repo_id, repo_type=repo_type, endpoint=endpoint, re_raise=True)
code=404, except Exception:
msg=f'Repo {repo_id} not exists on {endpoint}', logger.error(f'Repo {repo_id} not exists on {endpoint}.')
hdrs={'Content-Type': 'text/html'}, raise
fp=None) return endpoint
else:
return endpoint
check_cn_first = not is_env_true(MODELSCOPE_PREFER_INTL) check_cn_first = not is_env_true(MODELSCOPE_PREFER_INTL)
prefer_endpoint = get_endpoint(cn_site=check_cn_first) prefer_endpoint = get_endpoint(cn_site=check_cn_first)
@@ -333,13 +331,12 @@ class HubApi:
repo_id, repo_type=repo_type, endpoint=prefer_endpoint): repo_id, repo_type=repo_type, endpoint=prefer_endpoint):
logger.warning(f'Repo {repo_id} not exists on {prefer_endpoint}, will try on alternative endpoint.') logger.warning(f'Repo {repo_id} not exists on {prefer_endpoint}, will try on alternative endpoint.')
alternative_endpoint = get_endpoint(cn_site=(not check_cn_first)) alternative_endpoint = get_endpoint(cn_site=(not check_cn_first))
if not self.repo_exists( try:
repo_id, repo_type=repo_type, endpoint=alternative_endpoint): self.self.repo_exists(
raise HTTPError(url=alternative_endpoint + '/' + repo_id, repo_id, repo_type=repo_type, endpoint=alternative_endpoint, re_raise=True)
code=404, except Exception:
msg=f'Repo {repo_id} not exists on either {prefer_endpoint} or {alternative_endpoint}', logger.error(f'Repo {repo_id} not exists on either {prefer_endpoint} or {alternative_endpoint}')
hdrs={'Content-Type': 'text/html'}, raise
fp=None)
else: else:
return alternative_endpoint return alternative_endpoint
else: else:
@@ -351,6 +348,7 @@ class HubApi:
*, *,
repo_type: Optional[str] = None, repo_type: Optional[str] = None,
endpoint: Optional[str] = None, endpoint: Optional[str] = None,
re_raise: Optional[bool] = False
) -> bool: ) -> bool:
""" """
Checks if a repository exists on ModelScope Checks if a repository exists on ModelScope
@@ -365,6 +363,8 @@ class HubApi:
endpoint(`str`): endpoint(`str`):
None or specific endpoint to use, when None, use the default endpoint None or specific endpoint to use, when None, use the default endpoint
set in HubApi class (self.endpoint) set in HubApi class (self.endpoint)
re_raise(`bool`):
raise exception when error
Returns: Returns:
True if the repository exists, False otherwise. True if the repository exists, False otherwise.
""" """
@@ -388,7 +388,10 @@ class HubApi:
if code == 200: if code == 200:
return True return True
elif code == 404: elif code == 404:
return False if re_raise:
raise HTTPError(r)
else:
return False
else: else:
logger.warn(f'Check repo_exists return status code {code}.') logger.warn(f'Check repo_exists return status code {code}.')
raise Exception( raise Exception(