-
Notifications
You must be signed in to change notification settings - Fork 849
Enhance repo_type_and_id_from_hf_id of hf_api #3507
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
Open
pulltheflower
wants to merge
7
commits into
huggingface:main
Choose a base branch
from
pulltheflower:enhance-repo-type-and-id-parser-of-hf-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f9f091d
Enhance repo_type_and_id_from_hf_id of hf_api
pulltheflower d67b270
Add missing line
pulltheflower 3048973
Fix failed tests
pulltheflower c3656ef
Format code
pulltheflower 136c1fc
Fix tests and format code
pulltheflower b5420fc
Merge branch 'main' into enhance-repo-type-and-id-parser-of-hf-api
Wauplin 800a091
Apply suggestions from code review
Wauplin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -238,28 +238,62 @@ def repo_type_and_id_from_hf_id(hf_id: str, hub_url: Optional[str] = None) -> tu | |
| """ | ||
| input_hf_id = hf_id | ||
|
|
||
| hub_url = re.sub(r"https?://", "", hub_url if hub_url is not None else constants.ENDPOINT) | ||
| is_hf_url = hub_url in hf_id and "@" not in hf_id | ||
| # Get the hub_url (with or without protocol) | ||
| full_hub_url = hub_url if hub_url is not None else constants.ENDPOINT | ||
| hub_url_without_protocol = re.sub(r"https?://", "", full_hub_url) | ||
|
|
||
| # Check if hf_id is a URL containing the hub_url (check both with and without protocol) | ||
| hf_id_without_protocol = re.sub(r"https?://", "", hf_id) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reused here |
||
| is_hf_url = hub_url_without_protocol in hf_id_without_protocol and "@" not in hf_id | ||
|
|
||
| HFFS_PREFIX = "hf://" | ||
| if hf_id.startswith(HFFS_PREFIX): # Remove "hf://" prefix if exists | ||
| hf_id = hf_id[len(HFFS_PREFIX) :] | ||
|
|
||
| # If it's a URL, strip the endpoint prefix to get the path | ||
| if is_hf_url: | ||
| # Remove protocol if present | ||
| hf_id_normalized = re.sub(r"https?://", "", hf_id) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here |
||
|
|
||
| # Remove the hub_url prefix to get the relative path | ||
| if hf_id_normalized.startswith(hub_url_without_protocol): | ||
| # Strip the hub URL and any leading slashes | ||
| hf_id = hf_id_normalized[len(hub_url_without_protocol) :].lstrip("/") | ||
|
|
||
| url_segments = hf_id.split("/") | ||
| is_hf_id = len(url_segments) <= 3 | ||
|
|
||
| namespace: Optional[str] | ||
| if is_hf_url: | ||
| namespace, repo_id = url_segments[-2:] | ||
| if namespace == hub_url: | ||
| namespace = None | ||
| if len(url_segments) > 2 and hub_url not in url_segments[-3]: | ||
| repo_type = url_segments[-3] | ||
| elif namespace in constants.REPO_TYPES_MAPPING: | ||
| # Mean canonical dataset or model | ||
| repo_type = constants.REPO_TYPES_MAPPING[namespace] | ||
| namespace = None | ||
| # For URLs, we need to extract repo_type, namespace, repo_id | ||
| # Expected format after stripping endpoint: [repo_type]/namespace/repo_id or namespace/repo_id | ||
|
|
||
| if len(url_segments) >= 3: | ||
| # Check if first segment is a repo type | ||
| if url_segments[0] in constants.REPO_TYPES_MAPPING: | ||
| repo_type = constants.REPO_TYPES_MAPPING[url_segments[0]] | ||
| namespace = url_segments[1] | ||
| repo_id = url_segments[2] | ||
| else: | ||
| # First segment is namespace | ||
| namespace = url_segments[0] | ||
| repo_id = url_segments[1] | ||
| repo_type = None | ||
| elif len(url_segments) == 2: | ||
| namespace = url_segments[0] | ||
| repo_id = url_segments[1] | ||
|
|
||
| # Check if namespace is actually a repo type mapping | ||
| if namespace in constants.REPO_TYPES_MAPPING: | ||
| # Mean canonical dataset or model | ||
| repo_type = constants.REPO_TYPES_MAPPING[namespace] | ||
| namespace = None | ||
| else: | ||
| repo_type = None | ||
| else: | ||
| # Single segment | ||
| repo_id = url_segments[0] | ||
| namespace = None | ||
| repo_type = None | ||
| elif is_hf_id: | ||
| if len(url_segments) == 3: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slight optimization but could you define a root-level
_REGEX_HTTP_PROTOCOL = re.compile(r"https?://")variable (next to_REGEX_DISCUSSION_URLfor instance) and then useIt prevents Python from having to compile the regex on each call