diff --git a/docs/source/en/package_reference/hf_api.md b/docs/source/en/package_reference/hf_api.md index 8dd7e19828..a28b4f22e9 100644 --- a/docs/source/en/package_reference/hf_api.md +++ b/docs/source/en/package_reference/hf_api.md @@ -71,6 +71,10 @@ models = hf_api.list_models() [[autodoc]] huggingface_hub.hf_api.SpaceInfo +### User + +[[autodoc]] huggingface_hub.hf_api.User + ### UserLikes [[autodoc]] huggingface_hub.hf_api.UserLikes diff --git a/src/huggingface_hub/__init__.py b/src/huggingface_hub/__init__.py index 7a476dad9b..43303e0d61 100644 --- a/src/huggingface_hub/__init__.py +++ b/src/huggingface_hub/__init__.py @@ -143,6 +143,7 @@ "HfApi", "ModelSearchArguments", "RepoUrl", + "User", "UserLikes", "add_collection_item", "add_space_secret", @@ -188,6 +189,7 @@ "list_models", "list_repo_commits", "list_repo_files", + "list_repo_likers", "list_repo_refs", "list_spaces", "merge_pull_request", @@ -462,6 +464,7 @@ def __dir__(): HfApi, # noqa: F401 ModelSearchArguments, # noqa: F401 RepoUrl, # noqa: F401 + User, # noqa: F401 UserLikes, # noqa: F401 add_collection_item, # noqa: F401 add_space_secret, # noqa: F401 @@ -507,6 +510,7 @@ def __dir__(): list_models, # noqa: F401 list_repo_commits, # noqa: F401 list_repo_files, # noqa: F401 + list_repo_likers, # noqa: F401 list_repo_refs, # noqa: F401 list_spaces, # noqa: F401 merge_pull_request, # noqa: F401 diff --git a/src/huggingface_hub/hf_api.py b/src/huggingface_hub/hf_api.py index ca9a003d21..33443e6f64 100644 --- a/src/huggingface_hub/hf_api.py +++ b/src/huggingface_hub/hf_api.py @@ -925,23 +925,18 @@ class User: Contains information about a user on the Hub. Args: - name (`str`): - Name of the user. - avatarUrl (`str`): + avatar_url (`str`): URL of the user's avatar. - fullName (`str`): + username (`str`): + Name of the user on the Hub (unique). + fullname (`str`): User's full name. """ # Metadata - name: str - avatarUrl: str - fullName: str - - def __init__(self, data: Dict) -> None: - self.name = data["name"] - self.avatarUrl = data["avatarUrl"] - self.fullName = data["fullName"] + avatar_url: str + username: str + fullname: str def future_compatible(fn: CallableT) -> CallableT: @@ -1079,11 +1074,9 @@ def whoami(self, token: Optional[str] = None) -> Dict: hf_raise_for_status(r) except HTTPError as e: raise HTTPError( - ( - "Invalid user token. If you didn't pass a user token, make sure you " - "are properly logged in by executing `huggingface-cli login`, and " - "if you did pass a user token, double-check it's correct." - ), + "Invalid user token. If you didn't pass a user token, make sure you " + "are properly logged in by executing `huggingface-cli login`, and " + "if you did pass a user token, double-check it's correct.", request=e.request, response=e.response, ) from e @@ -1787,9 +1780,9 @@ def list_repo_likers( """ # Construct the API endpoint - path = f"{self.endpoint}/api/models/{repo_id}/likers" - if repo_type: - path = f"{self.endpoint}/api/{repo_type}/{repo_id}/likers" + if repo_type is None: + repo_type = REPO_TYPE_MODEL + path = f"{self.endpoint}/api/{repo_type}s/{repo_id}/likers" headers = self._build_hf_headers(token=token) # Make the request @@ -1798,7 +1791,14 @@ def list_repo_likers( # Parse the results into User objects likers_data = response.json() - return [User(**user_data) for user_data in likers_data] + return [ + User( + username=user_data["user"], + fullname=user_data["fullname"], + avatar_url=user_data["avatarUrl"], + ) + for user_data in likers_data + ] @validate_hf_hub_args def model_info( @@ -5682,11 +5682,9 @@ def request_space_hardware( """ if sleep_time is not None and hardware == SpaceHardware.CPU_BASIC: warnings.warn( - ( - "If your Space runs on the default 'cpu-basic' hardware, it will go to sleep if inactive for more" - " than 48 hours. This value is not configurable. If you don't want your Space to deactivate or if" - " you want to set a custom sleep time, you need to upgrade to a paid Hardware." - ), + "If your Space runs on the default 'cpu-basic' hardware, it will go to sleep if inactive for more" + " than 48 hours. This value is not configurable. If you don't want your Space to deactivate or if" + " you want to set a custom sleep time, you need to upgrade to a paid Hardware.", UserWarning, ) payload: Dict[str, Any] = {"flavor": hardware} @@ -5739,11 +5737,9 @@ def set_space_sleep_time(self, repo_id: str, sleep_time: int, *, token: Optional hardware = runtime.requested_hardware or runtime.hardware if hardware == SpaceHardware.CPU_BASIC: warnings.warn( - ( - "If your Space runs on the default 'cpu-basic' hardware, it will go to sleep if inactive for more" - " than 48 hours. This value is not configurable. If you don't want your Space to deactivate or if" - " you want to set a custom sleep time, you need to upgrade to a paid Hardware." - ), + "If your Space runs on the default 'cpu-basic' hardware, it will go to sleep if inactive for more" + " than 48 hours. This value is not configurable. If you don't want your Space to deactivate or if" + " you want to set a custom sleep time, you need to upgrade to a paid Hardware.", UserWarning, ) return runtime @@ -5919,11 +5915,9 @@ def duplicate_space( if sleep_time is not None and hardware == SpaceHardware.CPU_BASIC: warnings.warn( - ( - "If your Space runs on the default 'cpu-basic' hardware, it will go to sleep if inactive for more" - " than 48 hours. This value is not configurable. If you don't want your Space to deactivate or if" - " you want to set a custom sleep time, you need to upgrade to a paid Hardware." - ), + "If your Space runs on the default 'cpu-basic' hardware, it will go to sleep if inactive for more" + " than 48 hours. This value is not configurable. If you don't want your Space to deactivate or if" + " you want to set a custom sleep time, you need to upgrade to a paid Hardware.", UserWarning, ) @@ -6551,6 +6545,7 @@ def _parse_revision_from_pr_url(pr_url: str) -> str: # Activity API list_liked_repos = api.list_liked_repos +list_repo_likers = api.list_repo_likers like = api.like unlike = api.unlike diff --git a/tests/test_hf_api.py b/tests/test_hf_api.py index 1b85dc9c43..be427c8cee 100644 --- a/tests/test_hf_api.py +++ b/tests/test_hf_api.py @@ -2476,7 +2476,7 @@ def test_list_repo_likers(self) -> None: likers = self.api.list_repo_likers(repo_id, token=TOKEN) # Check if the test user is in the list of likers - liker_usernames = [user.name for user in likers] + liker_usernames = [user.username for user in likers] self.assertGreater(len(likers), 0) self.assertIn(USER, liker_usernames) @@ -3150,10 +3150,8 @@ def test_repo_url_class(self): # __repr__ is modified for debugging purposes self.assertEqual( repr(url), - ( - "RepoUrl('https://huggingface.co/gpt2'," - " endpoint='https://huggingface.co', repo_type='model', repo_id='gpt2')" - ), + "RepoUrl('https://huggingface.co/gpt2'," + " endpoint='https://huggingface.co', repo_type='model', repo_id='gpt2')", ) def test_repo_url_endpoint(self):