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 1 commit 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
36 changes: 35 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,39 @@ 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:
raise ImportError(
"`from_preset()` requires the `modelscope` package to "
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the error message can modified as follows
"To load a preset from ModelScope {preset} using from_preset, install the "
"modelscope package with: pip install modelscope."

"load from '{preset}'. "
"Please install 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 HFValidationError as e:
raise ValueError(
"Unexpected ModelScope preset. Hugging Face model handles "
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Typo - Hugging Face model handles -> ModelScope

"should have the form 'modelscope://{org}/{model}'. For example, "
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: improve error message as - ModelScope handles should follow the format "
f"'modelscope://{{org}}/{{model}}' (e.g., 'modelscope://username/bert_base_en'). "
f"Received: preset='{preset}'.

f"'modelscope://username/bert_base_en'. Received: preset={preset}."
) from e
except EntryNotFoundError as e:
message = str(e)
if message.find("403 Client Error"):
raise FileNotFoundError(
f"`{path}` doesn't exist in preset directory `{preset}`."
)
else:
raise ValueError(message)
elif scheme == HF_SCHEME:
if huggingface_hub is None:
raise ImportError(
Expand Down
Loading