Skip to content

Commit 50c769d

Browse files
[HfApi] remove like endpoint (#2739)
1 parent b2c9a14 commit 50c769d

File tree

3 files changed

+15
-128
lines changed

3 files changed

+15
-128
lines changed

src/huggingface_hub/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@
202202
"get_user_overview",
203203
"get_webhook",
204204
"grant_access",
205-
"like",
206205
"list_accepted_access_requests",
207206
"list_collections",
208207
"list_datasets",
@@ -742,7 +741,6 @@ def __dir__():
742741
get_user_overview, # noqa: F401
743742
get_webhook, # noqa: F401
744743
grant_access, # noqa: F401
745-
like, # noqa: F401
746744
list_accepted_access_requests, # noqa: F401
747745
list_collections, # noqa: F401
748746
list_datasets, # noqa: F401

src/huggingface_hub/hf_api.py

Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,57 +2246,6 @@ def list_spaces(
22462246
item["siblings"] = None
22472247
yield SpaceInfo(**item)
22482248

2249-
@validate_hf_hub_args
2250-
def like(
2251-
self,
2252-
repo_id: str,
2253-
*,
2254-
token: Union[bool, str, None] = None,
2255-
repo_type: Optional[str] = None,
2256-
) -> None:
2257-
"""
2258-
Like a given repo on the Hub (e.g. set as favorite).
2259-
2260-
See also [`unlike`] and [`list_liked_repos`].
2261-
2262-
Args:
2263-
repo_id (`str`):
2264-
The repository to like. Example: `"user/my-cool-model"`.
2265-
2266-
token (Union[bool, str, None], optional):
2267-
A valid user access token (string). Defaults to the locally saved
2268-
token, which is the recommended method for authentication (see
2269-
https://huggingface.co/docs/huggingface_hub/quick-start#authentication).
2270-
To disable authentication, pass `False`.
2271-
2272-
repo_type (`str`, *optional*):
2273-
Set to `"dataset"` or `"space"` if liking a dataset or space, `None` or
2274-
`"model"` if liking a model. Default is `None`.
2275-
2276-
Raises:
2277-
[`~utils.RepositoryNotFoundError`]:
2278-
If repository is not found (error 404): wrong repo_id/repo_type, private
2279-
but not authenticated or repo does not exist.
2280-
2281-
Example:
2282-
```python
2283-
>>> from huggingface_hub import like, list_liked_repos, unlike
2284-
>>> like("gpt2")
2285-
>>> "gpt2" in list_liked_repos().models
2286-
True
2287-
>>> unlike("gpt2")
2288-
>>> "gpt2" in list_liked_repos().models
2289-
False
2290-
```
2291-
"""
2292-
if repo_type is None:
2293-
repo_type = constants.REPO_TYPE_MODEL
2294-
response = get_session().post(
2295-
url=f"{self.endpoint}/api/{repo_type}s/{repo_id}/like",
2296-
headers=self._build_hf_headers(token=token),
2297-
)
2298-
hf_raise_for_status(response)
2299-
23002249
@validate_hf_hub_args
23012250
def unlike(
23022251
self,
@@ -2308,7 +2257,9 @@ def unlike(
23082257
"""
23092258
Unlike a given repo on the Hub (e.g. remove from favorite list).
23102259
2311-
See also [`like`] and [`list_liked_repos`].
2260+
To prevent spam usage, it is not possible to `like` a repository from a script.
2261+
2262+
See also [`list_liked_repos`].
23122263
23132264
Args:
23142265
repo_id (`str`):
@@ -2331,9 +2282,8 @@ def unlike(
23312282
23322283
Example:
23332284
```python
2334-
>>> from huggingface_hub import like, list_liked_repos, unlike
2335-
>>> like("gpt2")
2336-
>>> "gpt2" in list_liked_repos().models
2285+
>>> from huggingface_hub import list_liked_repos, unlike
2286+
>>> "gpt2" in list_liked_repos().models # we assume you have already liked gpt2
23372287
True
23382288
>>> unlike("gpt2")
23392289
>>> "gpt2" in list_liked_repos().models
@@ -2360,7 +2310,7 @@ def list_liked_repos(
23602310
This list is public so token is optional. If `user` is not passed, it defaults to
23612311
the logged in user.
23622312
2363-
See also [`like`] and [`unlike`].
2313+
See also [`unlike`].
23642314
23652315
Args:
23662316
user (`str`, *optional*):
@@ -2434,7 +2384,7 @@ def list_repo_likers(
24342384
"""
24352385
List all users who liked a given repo on the hugging Face Hub.
24362386
2437-
See also [`like`] and [`list_liked_repos`].
2387+
See also [`list_liked_repos`].
24382388
24392389
Args:
24402390
repo_id (`str`):
@@ -9572,7 +9522,6 @@ def _parse_revision_from_pr_url(pr_url: str) -> str:
95729522
# Activity API
95739523
list_liked_repos = api.list_liked_repos
95749524
list_repo_likers = api.list_repo_likers
9575-
like = api.like
95769525
unlike = api.unlike
95779526

95789527
# Community API

tests/test_hf_api.py

Lines changed: 8 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3053,73 +3053,10 @@ class ActivityApiTest(unittest.TestCase):
30533053
def setUpClass(cls) -> None:
30543054
cls.api = HfApi() # no auth!
30553055

3056-
def test_like_and_unlike_repo(self) -> None:
3057-
# Create and like a private and a public repo
3058-
repo_id_private = self.api.create_repo(repo_name(), token=TOKEN, private=True).repo_id
3059-
self.api.like(repo_id_private, token=TOKEN)
3060-
3061-
repo_id_public = self.api.create_repo(repo_name(), token=TOKEN, private=False).repo_id
3062-
self.api.like(repo_id_public, token=TOKEN)
3063-
3064-
# Get likes as public and authenticated
3065-
likes = self.api.list_liked_repos(USER)
3066-
likes_with_auth = self.api.list_liked_repos(USER, token=TOKEN)
3067-
3068-
# Public repo is shown in liked repos
3069-
self.assertIn(repo_id_public, likes.models)
3070-
self.assertIn(repo_id_public, likes_with_auth.models)
3071-
3072-
# Private repo is NOT shown in liked repos, even when authenticated
3073-
# This is by design. See https://github.com/huggingface/moon-landing/pull/4879 (internal link)
3074-
self.assertNotIn(repo_id_private, likes.models)
3075-
self.assertNotIn(repo_id_private, likes_with_auth.models)
3076-
3077-
# Unlike repo and check not in liked list
3078-
self.api.unlike(repo_id_public, token=TOKEN)
3079-
self.api.unlike(repo_id_private, token=TOKEN)
3080-
likes_after_unlike = self.api.list_liked_repos(USER)
3081-
self.assertNotIn(repo_id_public, likes_after_unlike.models) # Unliked
3082-
3083-
# Cleanup
3084-
self.api.delete_repo(repo_id_public, token=TOKEN)
3085-
self.api.delete_repo(repo_id_private, token=TOKEN)
3086-
3087-
def test_like_missing_repo(self) -> None:
3088-
with self.assertRaises(RepositoryNotFoundError):
3089-
self.api.like("missing_repo_id", token=TOKEN)
3090-
3056+
def test_unlike_missing_repo(self) -> None:
30913057
with self.assertRaises(RepositoryNotFoundError):
30923058
self.api.unlike("missing_repo_id", token=TOKEN)
30933059

3094-
def test_like_twice(self) -> None:
3095-
# Create and like repo
3096-
repo_id = self.api.create_repo(repo_name(), token=TOKEN, private=True).repo_id
3097-
3098-
# Can like twice
3099-
self.api.like(repo_id, token=TOKEN)
3100-
self.api.like(repo_id, token=TOKEN)
3101-
3102-
# Can unlike twice
3103-
self.api.unlike(repo_id, token=TOKEN)
3104-
self.api.unlike(repo_id, token=TOKEN)
3105-
3106-
# Cleanup
3107-
self.api.delete_repo(repo_id, token=TOKEN)
3108-
3109-
def test_list_liked_repos_no_auth(self) -> None:
3110-
# Create a repo + like
3111-
repo_id = self.api.create_repo(repo_name(), exist_ok=True, token=TOKEN).repo_id
3112-
self.api.like(repo_id, token=TOKEN)
3113-
3114-
# Fetch liked repos without auth
3115-
likes = self.api.list_liked_repos(USER, token=False)
3116-
self.assertEqual(likes.user, USER)
3117-
self.assertGreater(len(likes.models) + len(likes.datasets) + len(likes.spaces), 0)
3118-
self.assertIn(repo_id, likes.models)
3119-
3120-
# Cleanup
3121-
self.api.delete_repo(repo_id, token=TOKEN)
3122-
31233060
def test_list_likes_repos_auth_and_implicit_user(self) -> None:
31243061
# User is implicit
31253062
likes = self.api.list_liked_repos(token=TOKEN)
@@ -3364,9 +3301,12 @@ def test_commit_to_repo_in_background(self, repo_url: RepoUrl) -> None:
33643301
@use_tmp_repo()
33653302
def test_run_as_future(self, repo_url: RepoUrl) -> None:
33663303
repo_id = repo_url.repo_id
3367-
self._api.run_as_future(self._api.like, repo_id)
3304+
# update repo visibility to private
3305+
self._api.run_as_future(self._api.update_repo_settings, repo_id=repo_id, private=True)
33683306
future_1 = self._api.run_as_future(self._api.model_info, repo_id=repo_id)
3369-
self._api.run_as_future(self._api.unlike, repo_id)
3307+
3308+
# update repo visibility to public
3309+
self._api.run_as_future(self._api.update_repo_settings, repo_id=repo_id, private=False)
33703310
future_2 = self._api.run_as_future(self._api.model_info, repo_id=repo_id)
33713311

33723312
self.assertIsInstance(future_1, Future)
@@ -3381,8 +3321,8 @@ def test_run_as_future(self, repo_url: RepoUrl) -> None:
33813321
assert future_2.done()
33823322

33833323
# Like/unlike is correct
3384-
self.assertEqual(info_1.likes, 1)
3385-
self.assertEqual(info_2.likes, 0)
3324+
self.assertEqual(info_1.private, True)
3325+
self.assertEqual(info_2.private, False)
33863326

33873327

33883328
class TestDownloadHfApiAlias(unittest.TestCase):

0 commit comments

Comments
 (0)