Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add download support for modelscople. #2032

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion keras_hub/src/utils/preset_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@
KAGGLE_PREFIX = "kaggle://"
GS_PREFIX = "gs://"
HF_PREFIX = "hf://"
MODELSCOPE_PREFIX = "modelscope://"

KAGGLE_SCHEME = "kaggle"
GS_SCHEME = "gs"
HF_SCHEME = "hf"

MODELSCOPE_SCHEME = "modelscope"
ASSET_DIR = "assets"
TOKENIZER_ASSET_DIR = f"{ASSET_DIR}/tokenizer"

Expand Down Expand Up @@ -165,6 +166,37 @@ def get_file(preset, path):
raise ValueError(message)
elif scheme in tf_registered_schemes():
return tf_copy_gfile_to_cache(preset, path)
elif scheme == MODELSCOPE_SCHEME:
try:
from modelscope.hub.snapshot_download import snapshot_download
except ImportError:
raise ImportError(
"To load a preset from ModelScope {preset} using from_preset,"
"install the modelscope package with: pip install modelscope."
)
modelscope_handle = preset.removeprefix(MODELSCOPE_SCHEME + "://")
try:
return_path = snapshot_download(modelscope_handle) + "/" + path
if os.path.exists(return_path):
return return_path
raise FileNotFoundError(
f"`{return_path}` doesn't exist in preset directory `{preset}`."
)
except ValueError as e:
raise ValueError(
"ModelScope handles should follow the format "
f"'modelscope://{{org}}/{{model}}' "
"(e.g., 'modelscope://username/bert_base_en')."
f"Received: preset='{preset}.'"
) from e
except EntryNotFoundError as e:
message = str(e)
if message.find("403 Client Error"):
raise FileNotFoundError(
f"`{path}` not exist in preset directory `{preset}`."
)
else:
raise ValueError(message)
elif scheme == HF_SCHEME:
if huggingface_hub is None:
raise ImportError(
Expand Down
Loading