Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9580,6 +9580,52 @@ def list_organization_members(self, organization: str, token: Union[bool, str, N
):
yield User(**member)

@validate_hf_hub_args
def list_organization_datasets(self, organization: str, *, sort: Optional[str] = None, search: Optional[str] = None, token: Union[bool, str, None] = None) -> Iterable[DatasetInfo]:
"""
List datasets of an organization on the Hub.

Args:
organization (`str`):
Name of the organization to list the datasets of.
sort (`str`, *optional*):
Sorting criteria for the datasets. Supported values include:
`modified`, `created`, `alphabetical`, `likes`, `downloads`,
`most_rows`, `least_rows`.
search (`str`, *optional*):
Search query to filter datasets by name or description.
token (`bool` or `str`, *optional*):
A valid user access token (string). Defaults to the locally saved
token, which is the recommended method for authentication (see
https://huggingface.co/docs/huggingface_hub/quick-start#authentication).
To disable authentication, pass `False`.

Returns:
`Iterable[DatasetInfo]`: A generator yielding [`DatasetInfo`] objects
for each dataset in the organization.

Raises:
[`HfHubHTTPError`]:
HTTP 404 If the organization does not exist on the Hub.
"""
params: dict[str, Any] = {}
if sort is not None:
params["sort"] = sort
if search is not None:
params["search"] = search

r = get_session().get(
f"{constants.ENDPOINT}/api/organizations/{organization}/datasets-json",
params=params,
headers=self._build_hf_headers(token=token),
)
hf_raise_for_status(r)
data = r.json()
for ds in data.get("datasets", []):
if "siblings" not in ds:
ds["siblings"] = None
yield DatasetInfo(**ds)

def list_user_followers(self, username: str, token: Union[bool, str, None] = None) -> Iterable[User]:
"""
Get the list of followers of a user on the Hub.
Expand Down Expand Up @@ -10845,6 +10891,7 @@ def _parse_revision_from_pr_url(pr_url: str) -> str:
get_organization_overview = api.get_organization_overview
list_organization_followers = api.list_organization_followers
list_organization_members = api.list_organization_members
list_organization_datasets = api.list_organization_datasets
list_user_followers = api.list_user_followers
list_user_following = api.list_user_following

Expand Down
6 changes: 6 additions & 0 deletions tests/test_hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4198,6 +4198,12 @@ def test_organization_followers(self) -> None:
assert first_follower.fullname
assert first_follower.avatar_url

def test_list_organization_datasets(self) -> None:
datasets = list(self.api.list_organization_datasets("openai", sort="downloads", search="gsm"))
assert isinstance(datasets, list)
if datasets:
assert hasattr(datasets[0], "id")

def test_user_followers(self) -> None:
followers = self.api.list_user_followers("clem")
assert len(list(followers)) > 500
Expand Down