Skip to content

Commit

Permalink
Fix list_liked_repos (only public likes are returned) (#1273)
Browse files Browse the repository at this point in the history
* Fix list_liked_repos (only public likes are returned)

* prepare for next likes nedpoint (paginate)

* mypy
  • Loading branch information
Wauplin authored Dec 21, 2022
1 parent 3c422cf commit fe89cd4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
20 changes: 11 additions & 9 deletions src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,8 @@ class UserLikes:
Args:
user (`str`):
Name of the user for which we fetched the likes.
total (`int`):
Total number of likes.
datasets (`List[str]`):
List of datasets liked by the user (as repo_ids).
models (`List[str]`):
Expand All @@ -686,6 +688,7 @@ class UserLikes:

# Metadata
user: str
total: int

# User likes
datasets: List[str]
Expand Down Expand Up @@ -1423,7 +1426,7 @@ def list_liked_repos(
token: Optional[str] = None,
) -> UserLikes:
"""
List all repos liked by a user on huggingface.co.
List all public repos liked by a user on huggingface.co.
This list is public so token is optional. If `user` is not passed, it defaults to
the logged in user.
Expand All @@ -1439,8 +1442,8 @@ def list_liked_repos(
user name.
Returns:
[`UserLikes`]: object containing the user name, the total count of likes and
3 lists of repo ids (1 for models, 1 for datasets and 1 for Spaces).
[`UserLikes`]: object containing the user name and 3 lists of repo ids (1 for
models, 1 for datasets and 1 for Spaces).
Raises:
[`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
Expand Down Expand Up @@ -1473,9 +1476,7 @@ def list_liked_repos(
path = f"{self.endpoint}/api/users/{user}/likes"
headers = self._build_hf_headers(token=token)

r = requests.get(path, headers=headers)
hf_raise_for_status(r)
data = r.json()
likes = list(paginate(path, params={}, headers=headers))
# Looping over a list of items similar to:
# {
# 'createdAt': '2021-09-09T21:53:27.000Z',
Expand All @@ -1487,19 +1488,20 @@ def list_liked_repos(
# Let's loop 3 times over the received list. Less efficient but more straightforward to read.
return UserLikes(
user=user,
total=len(likes),
models=[
like["repo"]["name"]
for like in data["visibleLikes"]
for like in likes
if like["repo"]["type"] == "model"
],
datasets=[
like["repo"]["name"]
for like in data["visibleLikes"]
for like in likes
if like["repo"]["type"] == "dataset"
],
spaces=[
like["repo"]["name"]
for like in data["visibleLikes"]
for like in likes
if like["repo"]["type"] == "space"
],
)
Expand Down
4 changes: 2 additions & 2 deletions src/huggingface_hub/lfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def post_lfs_batch_info(
},
auth=HTTPBasicAuth(
"access_token",
get_token_to_send(token or True), # Token must be provided or retrieved
get_token_to_send(token or True), # type: ignore # Token must be provided or retrieved
),
)
hf_raise_for_status(resp)
Expand Down Expand Up @@ -280,7 +280,7 @@ def lfs_upload(
auth=HTTPBasicAuth(
username="USER",
# Token must be provided or retrieved
password=get_token_to_send(token or True),
password=get_token_to_send(token or True), # type: ignore
),
json={"oid": upload_info.sha256.hex(), "size": upload_info.size},
)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2361,6 +2361,9 @@ def test_list_likes_repos_auth_and_explicit_user(self) -> None:
def test_list_likes_on_production(self) -> None:
# Test julien-c likes a lot of repos !
likes = HfApi().list_liked_repos("julien-c")
self.assertEqual(
len(likes.models) + len(likes.datasets) + len(likes.spaces), likes.total
)
self.assertGreater(len(likes.models), 0)
self.assertGreater(len(likes.datasets), 0)
self.assertGreater(len(likes.spaces), 0)
Expand Down

0 comments on commit fe89cd4

Please sign in to comment.