Skip to content

Commit

Permalink
make style + import at root
Browse files Browse the repository at this point in the history
  • Loading branch information
Wauplin committed Oct 6, 2023
1 parent 21177fb commit 9246cf8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 41 deletions.
4 changes: 4 additions & 0 deletions docs/source/en/package_reference/hf_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/huggingface_hub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
"HfApi",
"ModelSearchArguments",
"RepoUrl",
"User",
"UserLikes",
"add_collection_item",
"add_space_secret",
Expand Down Expand Up @@ -188,6 +189,7 @@
"list_models",
"list_repo_commits",
"list_repo_files",
"list_repo_likers",
"list_repo_refs",
"list_spaces",
"merge_pull_request",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
67 changes: 31 additions & 36 deletions src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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

Expand Down
8 changes: 3 additions & 5 deletions tests/test_hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 9246cf8

Please sign in to comment.