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
54 changes: 54 additions & 0 deletions src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9580,6 +9580,59 @@ def list_organization_members(self, organization: str, token: Union[bool, str, N
):
yield User(**member)

@validate_hf_hub_args
def list_organization_models(
self,
organization: str,
*,
p: Optional[int] = None,
sort: Optional[str] = None,
search: Optional[str] = None,
token: Union[bool, str, None] = None,
) -> Iterable[ModelInfo]:
"""
List models for an organization using the Hub

Args:
organization (`str`):
Organization name to list models for.
p (`int`, *optional*):
Page index
sort (`str`, *optional*):
Sort order, for example `downloads`, `likes`, `created`,
`alphabetical`, or `modified`.
search (`str`, *optional*):
Free-text filter to apply to model names and descriptions.
token (Union[bool, str, None], *optional*):
A valid user access token (string). Defaults to the locally saved
token. To disable authentication, pass `False`.

Returns:
`Iterable[ModelInfo]`: an iterable of [`ModelInfo`] objects for the page returned by the endpoint.

Raises:
[`HfHubHTTPError`]: HTTP errors from the Hub (404 if the organization does not exist).
"""
params: dict[str, Union[int, str]] = {}
if sort is not None:
params["sort"] = sort
if search is not None:
params["search"] = search
if p is not None:
params["p"] = p

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

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 +10898,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_models = api.list_organization_models
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_models(self) -> None:
models = list(self.api.list_organization_models("openai", p=1, sort="downloads"))
assert isinstance(models, list)
if models:
assert hasattr(models[0], "id")

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