Voice et bot modif
This commit is contained in:
parent
189d56026b
commit
7333a22bcd
10774 changed files with 634644 additions and 933308 deletions
|
|
@ -25,16 +25,16 @@ Usage:
|
|||
"""
|
||||
|
||||
import enum
|
||||
import json
|
||||
from typing import Annotated, Optional, get_args
|
||||
from typing import Annotated, get_args
|
||||
|
||||
import typer
|
||||
|
||||
from huggingface_hub.errors import RepositoryNotFoundError, RevisionNotFoundError
|
||||
from huggingface_hub.errors import CLIError, RepositoryNotFoundError, RevisionNotFoundError
|
||||
from huggingface_hub.hf_api import ExpandModelProperty_T, ModelSort_T
|
||||
from huggingface_hub.utils import ANSI
|
||||
from huggingface_hub.repocard import ModelCard
|
||||
|
||||
from ._cli_utils import (
|
||||
REPO_LIST_DEFAULT_LIMIT,
|
||||
AuthorOpt,
|
||||
FilterOpt,
|
||||
LimitOpt,
|
||||
|
|
@ -43,9 +43,10 @@ from ._cli_utils import (
|
|||
TokenOpt,
|
||||
get_hf_api,
|
||||
make_expand_properties_parser,
|
||||
repo_info_to_dict,
|
||||
typer_factory,
|
||||
)
|
||||
from ._file_listing import list_repo_files_cmd
|
||||
from ._output import _dataclass_to_dict, out
|
||||
|
||||
|
||||
_EXPAND_PROPERTIES = sorted(get_args(ExpandModelProperty_T))
|
||||
|
|
@ -54,9 +55,9 @@ ModelSortEnum = enum.Enum("ModelSortEnum", {s: s for s in _SORT_OPTIONS}, type=s
|
|||
|
||||
|
||||
ExpandOpt = Annotated[
|
||||
Optional[str],
|
||||
str | None,
|
||||
typer.Option(
|
||||
help=f"Comma-separated properties to expand. Example: '--expand=downloads,likes,tags'. Valid: {', '.join(_EXPAND_PROPERTIES)}.",
|
||||
help=f"Comma-separated properties to return. When used, only the listed properties (and id) are returned. Example: '--expand=downloads,likes,tags'. Valid: {', '.join(_EXPAND_PROPERTIES)}.",
|
||||
callback=make_expand_properties_parser(_EXPAND_PROPERTIES),
|
||||
),
|
||||
]
|
||||
|
|
@ -65,32 +66,112 @@ ExpandOpt = Annotated[
|
|||
models_cli = typer_factory(help="Interact with models on the Hub.")
|
||||
|
||||
|
||||
@models_cli.command("ls")
|
||||
@models_cli.command(
|
||||
"list | ls",
|
||||
examples=[
|
||||
"hf models ls --sort downloads --limit 10",
|
||||
'hf models ls --search "llama" --author meta-llama',
|
||||
"hf models ls --num-parameters min:6B,max:128B --sort likes",
|
||||
"hf models ls meta-llama/Llama-3.2-1B-Instruct",
|
||||
"hf models ls meta-llama/Llama-3.2-1B-Instruct -R",
|
||||
"hf models ls meta-llama/Llama-3.2-1B-Instruct --tree -h",
|
||||
],
|
||||
)
|
||||
def models_ls(
|
||||
repo_id: Annotated[
|
||||
str | None,
|
||||
typer.Argument(help="Model ID (e.g. `username/repo-name`) to list files from. If omitted, lists models."),
|
||||
] = None,
|
||||
search: SearchOpt = None,
|
||||
author: AuthorOpt = None,
|
||||
filter: FilterOpt = None,
|
||||
num_parameters: Annotated[
|
||||
str | None,
|
||||
typer.Option(help="Filter by parameter count, e.g. 'min:6B,max:128B'."),
|
||||
] = None,
|
||||
sort: Annotated[
|
||||
Optional[ModelSortEnum],
|
||||
ModelSortEnum | None,
|
||||
typer.Option(help="Sort results."),
|
||||
] = None,
|
||||
limit: LimitOpt = 10,
|
||||
limit: LimitOpt = REPO_LIST_DEFAULT_LIMIT,
|
||||
expand: ExpandOpt = None,
|
||||
human_readable: Annotated[
|
||||
bool,
|
||||
typer.Option("--human-readable", "-h", help="Show sizes in human readable format (only for listing files)."),
|
||||
] = False,
|
||||
as_tree: Annotated[
|
||||
bool,
|
||||
typer.Option("--tree", help="List files in tree format (only for listing files)."),
|
||||
] = False,
|
||||
recursive: Annotated[
|
||||
bool,
|
||||
typer.Option("--recursive", "-R", help="List files recursively (only for listing files)."),
|
||||
] = False,
|
||||
revision: RevisionOpt = None,
|
||||
token: TokenOpt = None,
|
||||
) -> None:
|
||||
"""List models on the Hub."""
|
||||
"""List models on the Hub, or files in a model repo.
|
||||
|
||||
When called with no argument, lists models on the Hub.
|
||||
When called with a model ID, lists files in that model repo.
|
||||
"""
|
||||
if repo_id is not None:
|
||||
if search is not None:
|
||||
raise typer.BadParameter("Cannot use --search when listing files.")
|
||||
if author is not None:
|
||||
raise typer.BadParameter("Cannot use --author when listing files.")
|
||||
if filter is not None:
|
||||
raise typer.BadParameter("Cannot use --filter when listing files.")
|
||||
if num_parameters is not None:
|
||||
raise typer.BadParameter("Cannot use --num-parameters when listing files.")
|
||||
if sort is not None:
|
||||
raise typer.BadParameter("Cannot use --sort when listing files.")
|
||||
if limit != REPO_LIST_DEFAULT_LIMIT:
|
||||
raise typer.BadParameter("Cannot use --limit when listing files.")
|
||||
if expand is not None:
|
||||
raise typer.BadParameter("Cannot use --expand when listing files.")
|
||||
return list_repo_files_cmd(
|
||||
repo_id=repo_id,
|
||||
repo_type="model",
|
||||
human_readable=human_readable,
|
||||
as_tree=as_tree,
|
||||
recursive=recursive,
|
||||
revision=revision,
|
||||
token=token,
|
||||
)
|
||||
|
||||
if as_tree:
|
||||
raise typer.BadParameter("Cannot use --tree when listing models.")
|
||||
if recursive:
|
||||
raise typer.BadParameter("Cannot use --recursive when listing models.")
|
||||
if human_readable:
|
||||
raise typer.BadParameter("Cannot use --human-readable when listing models.")
|
||||
if revision is not None:
|
||||
raise typer.BadParameter("Cannot use --revision when listing models.")
|
||||
api = get_hf_api(token=token)
|
||||
sort_key = sort.value if sort else None
|
||||
results = [
|
||||
repo_info_to_dict(model_info)
|
||||
_dataclass_to_dict(model_info)
|
||||
for model_info in api.list_models(
|
||||
filter=filter, author=author, search=search, sort=sort_key, limit=limit, expand=expand
|
||||
filter=filter,
|
||||
author=author,
|
||||
search=search,
|
||||
num_parameters=num_parameters,
|
||||
sort=sort_key,
|
||||
limit=limit,
|
||||
expand=expand, # type: ignore
|
||||
)
|
||||
]
|
||||
print(json.dumps(results, indent=2))
|
||||
out.table(results)
|
||||
|
||||
|
||||
@models_cli.command("info")
|
||||
@models_cli.command(
|
||||
"info",
|
||||
examples=[
|
||||
"hf models info meta-llama/Llama-3.2-1B-Instruct",
|
||||
"hf models info Qwen/Qwen3.5-9B --expand downloads,likes,tags",
|
||||
],
|
||||
)
|
||||
def models_info(
|
||||
model_id: Annotated[str, typer.Argument(help="The model ID (e.g. `username/repo-name`).")],
|
||||
revision: RevisionOpt = None,
|
||||
|
|
@ -100,11 +181,37 @@ def models_info(
|
|||
"""Get info about a model on the Hub."""
|
||||
api = get_hf_api(token=token)
|
||||
try:
|
||||
info = api.model_info(repo_id=model_id, revision=revision, expand=expand) # type: ignore[arg-type]
|
||||
except RepositoryNotFoundError:
|
||||
print(f"Model {ANSI.bold(model_id)} not found.")
|
||||
raise typer.Exit(code=1)
|
||||
except RevisionNotFoundError:
|
||||
print(f"Revision {ANSI.bold(str(revision))} not found on {ANSI.bold(model_id)}.")
|
||||
raise typer.Exit(code=1)
|
||||
print(json.dumps(repo_info_to_dict(info), indent=2))
|
||||
info = api.model_info(repo_id=model_id, revision=revision, expand=expand) # type: ignore
|
||||
except RepositoryNotFoundError as e:
|
||||
raise CLIError(f"Model '{model_id}' not found.") from e
|
||||
except RevisionNotFoundError as e:
|
||||
raise CLIError(f"Revision '{revision}' not found on '{model_id}'.") from e
|
||||
out.dict(info)
|
||||
|
||||
|
||||
@models_cli.command(
|
||||
"card",
|
||||
examples=[
|
||||
"hf models card google/gemma-4-31B-it",
|
||||
"hf models card google/gemma-4-31B-it --metadata",
|
||||
"hf models card google/gemma-4-31B-it --metadata --format json",
|
||||
"hf models card google/gemma-4-31B-it --text",
|
||||
],
|
||||
)
|
||||
def models_card(
|
||||
model_id: Annotated[str, typer.Argument(help="The model ID (e.g. `username/repo-name`).")],
|
||||
metadata: Annotated[bool, typer.Option("--metadata", help="Output only the metadata from the card.")] = False,
|
||||
text: Annotated[bool, typer.Option("--text", help="Output only the text body (no metadata).")] = False,
|
||||
token: TokenOpt = None,
|
||||
) -> None:
|
||||
"""Get the model card (README) for a model on the Hub."""
|
||||
if metadata and text:
|
||||
raise CLIError("--metadata and --text are mutually exclusive.")
|
||||
card = ModelCard.load(model_id, token=token)
|
||||
if metadata:
|
||||
out.dict(card.data.to_dict())
|
||||
elif text:
|
||||
out.text(card.text)
|
||||
else:
|
||||
out.text(card.content)
|
||||
out.hint(f"Use `hf models card {model_id} --metadata` to extract only the card metadata.")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue