-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev(narugo): add ls_repo into the commands
- Loading branch information
1 parent
d5f0207
commit 131abe4
Showing
5 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from .entry import hfutilscli | ||
|
||
if __name__ == '__main__': | ||
hfutilscli() | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import fnmatch | ||
from typing import Optional | ||
|
||
import click | ||
from huggingface_hub.utils import LocalTokenNotFoundError | ||
|
||
from .base import CONTEXT_SETTINGS, ClickErrorException | ||
from ..operate.base import REPO_TYPES, get_hf_client | ||
|
||
|
||
class NoLocalAuthentication(ClickErrorException): | ||
""" | ||
Exception raised when there is no local authentication token. | ||
""" | ||
exit_code = 0x31 | ||
|
||
|
||
def _add_ls_repo_subcommand(cli: click.Group) -> click.Group: | ||
""" | ||
Add the ls_repo subcommand to the CLI. | ||
:param cli: The click Group object. | ||
:type cli: click.Group | ||
:return: The updated click Group object. | ||
:rtype: click.Group | ||
""" | ||
|
||
@cli.command('ls_repo', help='List repositories from HuggingFace.\n\n' | ||
'Set environment $HF_TOKEN to use your own access token.', | ||
context_settings=CONTEXT_SETTINGS) | ||
@click.option('-a', '--author', 'author', type=str, default=None, | ||
help='Author of the repositories. Search my repositories when not given.') | ||
@click.option('-t', '--type', 'repo_type', type=click.Choice(REPO_TYPES), default='dataset', | ||
help='Type of the HuggingFace repository.', show_default=True) | ||
@click.option('-p', '--pattern', 'pattern', type=str, default='*', | ||
help='Pattern of the repository names.', show_default=True) | ||
def ls(author: Optional[str], repo_type: str, pattern: str): | ||
""" | ||
List repositories from HuggingFace. | ||
:param author: Author of the repositories. | ||
:type author: Optional[str] | ||
:param repo_type: Type of the HuggingFace repository. | ||
:type repo_type: str | ||
:param pattern: Pattern of the repository names. | ||
:type pattern: str | ||
""" | ||
hf_client = get_hf_client() | ||
if not author: | ||
try: | ||
info = hf_client.whoami() | ||
author = author or info['name'] | ||
except LocalTokenNotFoundError: | ||
raise NoLocalAuthentication( | ||
'Authentication failed.\n' | ||
'Make sure you have set the correct Huggingface token.\n' | ||
'Or if need to use this with guest mode, please explicitly set the `-a` option.' | ||
) | ||
|
||
if repo_type == 'model': | ||
r = hf_client.list_models(author=author) | ||
elif repo_type == 'dataset': | ||
r = hf_client.list_datasets(author=author) | ||
elif repo_type == 'space': | ||
r = hf_client.list_spaces(author=author) | ||
else: | ||
raise ValueError(f'Unknown repository type - {repo_type!r}.') # pragma: no cover | ||
|
||
for repo_item in r: | ||
if fnmatch.fnmatch(repo_item.id, pattern): | ||
print(repo_item.id) | ||
|
||
return cli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import os | ||
from unittest.mock import patch | ||
|
||
import click | ||
import pytest | ||
from hbutils.testing import simulate_entry | ||
from huggingface_hub import HfApi | ||
|
||
from hfutils.entry import hfutilscli | ||
|
||
|
||
@pytest.fixture() | ||
def no_hf_token(): | ||
def _get_hf_client(): | ||
return HfApi(token='') | ||
|
||
with patch('hfutils.entry.ls_repo.get_hf_client', _get_hf_client), \ | ||
patch.dict(os.environ, {'HF_TOKEN': ''}): | ||
yield | ||
|
||
|
||
@pytest.mark.unittest | ||
class TestEntryLsRepo: | ||
def test_ls_repo(self): | ||
result = simulate_entry(hfutilscli, [ | ||
'hfutils', 'ls_repo', | ||
]) | ||
assert result.exitcode == 0 | ||
repos = click.unstyle(result.stdout).splitlines(keepends=False) | ||
assert 'narugo/manual_packs' in repos | ||
assert 'narugo/csip_v1_info' in repos | ||
|
||
def test_ls_repo_space(self): | ||
result = simulate_entry(hfutilscli, [ | ||
'hfutils', 'ls_repo', '-t', 'space', | ||
]) | ||
assert result.exitcode == 0 | ||
repos = click.unstyle(result.stdout).splitlines(keepends=False) | ||
assert 'narugo/jupyterlab' in repos | ||
assert 'narugo/CDC_anime_demo' in repos | ||
|
||
def test_ls_repo_model(self): | ||
result = simulate_entry(hfutilscli, [ | ||
'hfutils', 'ls_repo', '-t', 'model', | ||
]) | ||
assert result.exitcode == 0 | ||
repos = click.unstyle(result.stdout).splitlines(keepends=False) | ||
assert 'narugo/gchar_models' in repos | ||
assert 'narugo/test_v1.5_kristen' in repos | ||
assert 'narugo/test_v1.5_nian' in repos | ||
|
||
def test_ls_repo_anonymous(self, no_hf_token): | ||
result = simulate_entry(hfutilscli, [ | ||
'hfutils', 'ls_repo', | ||
]) | ||
assert result.exitcode == 0x31 | ||
stdout_lines = click.unstyle(result.stdout).splitlines(keepends=False) | ||
assert len(stdout_lines) == 0 | ||
stderr_lines = click.unstyle(result.stderr).splitlines(keepends=False) | ||
assert 'Authentication failed.' in stderr_lines |