Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility to get repo name #364

Merged
merged 1 commit into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,17 @@ def login(self, username: str, password: str) -> str:
write_to_credential_store(username, password)
return d["token"]

def whoami(self, token: str) -> Dict:
def whoami(self, token: Optional[str] = None) -> Dict:
"""
Call HF API to know "whoami".

Args:
token (``str``): Hugging Face token.
token (``str``, `optional`):
Hugging Face token. Will default to the locally saved token if not provided.
"""
if token is None:
token = HfFolder.get_token()

Comment on lines +321 to +331
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT @osanseviero? I think this is a great improvement in terms of user experience and should also be used to reopen conversations about:

  • having the HfApi.login saving the token
  • having the token default to the locally saved token when not passed through the HfApi

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree it's a great improvement! Thanks @sgugger! (related issue: #154)

path = "{}/api/whoami-v2".format(self.endpoint)
r = requests.get(path, headers={"authorization": "Bearer {}".format(token)})
try:
Expand Down Expand Up @@ -797,6 +801,35 @@ def upload_file(
d = r.json()
return d["url"]

def get_full_repo_name(
self,
model_id: str,
organization: Optional[str] = None,
token: Optional[str] = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a similar fashion to what is done here

):
"""
Returns the repository name for a given model ID and optional organization.

Args:
model_id (``str``):
The name of the model.
organization (``str``, `optional`):
If passed, the repository name will be in the organization namespace instead of the
user namespace.
token (``str``, `optional`):
The Hugging Face authentication token

Returns:
``str``: The repository name in the user's namespace ({username}/{model_id}) if no
organization is passed, and under the organization namespace ({organization}/{model_id})
otherwise.
"""
if organization is None:
username = self.whoami(token=token)["name"]
return f"{username}/{model_id}"
else:
return f"{organization}/{model_id}"


class HfFolder:
path_token = expanduser("~/.huggingface/token")
Expand Down
9 changes: 9 additions & 0 deletions tests/test_hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@ def test_upload_file_conflict(self):
finally:
self._api.delete_repo(token=self._token, name=REPO_NAME)

def test_get_full_repo_name(self):
repo_name_with_no_org = self._api.get_full_repo_name("model", token=self._token)
self.assertEqual(repo_name_with_no_org, f"{USER}/model")

repo_name_with_no_org = self._api.get_full_repo_name(
"model", organization="org", token=self._token
)
self.assertEqual(repo_name_with_no_org, "org/model")


class HfApiPublicTest(unittest.TestCase):
def test_staging_list_models(self):
Expand Down