File size: 20,192 Bytes
76d6ddf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 | # Copyright 2025 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Contains commands to interact with repositories on the Hugging Face Hub."""
import enum
from typing import Annotated
import click
from huggingface_hub import SpaceHardware, SpaceStorage
from huggingface_hub.cli._cli_utils import SoftChoice
from huggingface_hub.errors import CLIError, HfHubHTTPError, RepositoryNotFoundError, RevisionNotFoundError
from huggingface_hub.hf_api import REPO_REGIONS
from ._city_game import run_city_game
from ._cli_utils import (
REPO_LIST_DEFAULT_LIMIT,
EnvFileOpt,
EnvOpt,
LimitOpt,
PrivateOpt,
RepoIdArg,
RepoType,
RepoTypeOpt,
RevisionOpt,
SearchOpt,
SecretsFileOpt,
SecretsOpt,
TokenOpt,
VolumesOpt,
env_map_to_key_value_list,
get_hf_api,
parse_env_map,
parse_volumes,
typer_factory,
)
from ._cp import make_cp
from ._file_listing import format_size
from ._framework import Argument, Option
from ._output import OutputFormat, out
repos_cli = typer_factory(help="Manage repos on the Hub.")
@repos_cli.group_callback(invoke_without_command=True)
def _repos_callback(ctx: click.Context) -> None:
if ctx.info_name == "repo":
out.warning("`hf repo` is deprecated in favor of `hf repos`.")
class RepoTypeAll(str, enum.Enum):
model = "model"
dataset = "dataset"
space = "space"
bucket = "bucket"
class GatedChoices(str, enum.Enum):
auto = "auto"
manual = "manual"
false = "false"
PublicOpt = Annotated[
bool | None,
Option(
"--public",
help="Whether to make the repo public. Ignored if the repo already exists.",
),
]
ProtectedOpt = Annotated[
bool | None,
Option(
"--protected",
help="Whether to make the Space protected (Spaces only). Ignored if the repo already exists.",
),
]
SpaceHardwareOpt = Annotated[
str | None,
Option(
"--flavor",
help="Space hardware flavor (e.g. 'cpu-basic', 't4-medium', 'l4x4'). Only for Spaces.",
click_type=SoftChoice(SpaceHardware),
),
]
SpaceStorageOpt = Annotated[
SpaceStorage | None,
Option(
"--storage",
help="(Deprecated, use volumes instead) Space persistent storage tier ('small', 'medium', or 'large'). Only for Spaces.",
),
]
SpaceSleepTimeOpt = Annotated[
int | None,
Option(
"--sleep-time",
help="Seconds of inactivity before the Space is put to sleep. Use -1 to disable. Only for Spaces.",
),
]
tag_cli = typer_factory(help="Manage tags for a repo on the Hub.")
branch_cli = typer_factory(help="Manage branches for a repo on the Hub.")
repos_cli.add_group(tag_cli, name="tag")
repos_cli.add_group(branch_cli, name="branch")
@repos_cli.command(
"list | ls",
examples=[
"hf repos ls",
"hf repos ls --explore",
"hf repos ls --namespace my-org --search bert",
],
)
def repo_list(
namespace: Annotated[
str | None,
Option(
help="Organization name. If not provided, lists repos for the authenticated user.",
),
] = None,
repo_type: Annotated[
RepoTypeAll | None,
Option(
"--type",
"--repo-type",
help="Filter by repository type (model, dataset, space, or bucket).",
),
] = None,
search: SearchOpt = None,
limit: LimitOpt = REPO_LIST_DEFAULT_LIMIT,
explore: Annotated[
bool,
Option("--explore", help="Explore your repos as an interactive 3D city."),
] = False,
token: TokenOpt = None,
) -> None:
"""List all repos (models, datasets, spaces, buckets) with storage info."""
api = get_hf_api(token=token)
repos = list(api.list_user_repos(namespace=namespace))
if repo_type is not None:
repos = [r for r in repos if r.type == repo_type.value]
if search is not None:
search_lower = search.lower()
repos = [r for r in repos if search_lower in r.id.lower()]
total = len(repos)
if explore:
if out.mode == OutputFormat.human:
run_city_game(repos)
return
raise CLIError("Repository exploration is only available in terminal.")
if limit > 0:
repos = repos[:limit]
items = [
{
"id": r.id,
"type": r.type,
"updated": r.updated_at.strftime("%Y-%m-%d"),
"visibility": r.visibility,
"storage": format_size(r.storage, human_readable=True),
"%_of_total": f"{r.storage_percent:.1f}%",
}
for r in repos
]
out.table(items, id_key="id", alignments={"storage": "right", "%_of_total": "right"})
if limit > 0 and total > limit:
out.hint(f"Showing {limit} of {total} repos. Use `--limit 0` to list all.")
@repos_cli.command(
"create",
examples=[
"hf repos create my-model",
"hf repos create my-dataset --repo-type dataset --private",
"hf repos create my-space --type space --sdk gradio --flavor t4-medium --secrets HF_TOKEN -e THEME=dark --protected",
"hf repos create my-jupyterlab --type space --template SpacesExamples/jupyterlab",
"hf repos create my-space --type space --sdk gradio -v hf://org/my-model:/models -v hf://buckets/org/b:/data",
"hf repos create my-model --region us",
],
)
def repo_create(
repo_id: RepoIdArg,
repo_type: RepoTypeOpt = RepoType.model,
sdk: Annotated[
str | None,
Option(
"--sdk",
"--space-sdk",
help="Hugging Face Spaces SDK type. Required when --type is set to 'space'.",
),
] = None,
template: Annotated[
str | None,
Option(
"--template",
help=(
"Create a Space from an official template. Pass a template repo id (e.g. "
"'SpacesExamples/jupyterlab') or its short name (e.g. 'JupyterLab'). List available templates with "
"`hf spaces templates`. Spaces only."
),
),
] = None,
private: PrivateOpt = None,
public: PublicOpt = None,
protected: ProtectedOpt = None,
token: TokenOpt = None,
exist_ok: Annotated[
bool,
Option(
help="Do not raise an error if repo already exists.",
),
] = False,
resource_group_id: Annotated[
str | None,
Option(
help="Resource group in which to create the repo. Resource groups is only available for Enterprise Hub organizations.",
),
] = None,
region: Annotated[
REPO_REGIONS | None,
Option(
"--region",
help="Cloud region in which to create the repo. Can be one of 'us' or 'eu'. Requires Team plan or above.",
),
] = None,
hardware: SpaceHardwareOpt = None,
storage: SpaceStorageOpt = None,
sleep_time: SpaceSleepTimeOpt = None,
secrets: SecretsOpt = None,
secrets_file: SecretsFileOpt = None,
env: EnvOpt = None,
env_file: EnvFileOpt = None,
volume: VolumesOpt = None,
) -> None:
"""Create a new repo on the Hub."""
api = get_hf_api(token=token)
repo_url = api.create_repo(
repo_id=repo_id,
repo_type=repo_type.value,
visibility="private" if private else "public" if public else "protected" if protected else None, # type: ignore [arg-type]
token=token,
exist_ok=exist_ok,
resource_group_id=resource_group_id,
region=region,
space_sdk=sdk,
space_hardware=hardware,
space_storage=storage,
space_sleep_time=sleep_time,
space_secrets=env_map_to_key_value_list(parse_env_map(secrets, secrets_file)),
space_variables=env_map_to_key_value_list(parse_env_map(env, env_file)),
space_volumes=parse_volumes(volume),
space_template=template,
)
out.result("Repo created", repo_id=repo_url.repo_id, url=str(repo_url))
@repos_cli.command(
"duplicate",
examples=[
"hf repos duplicate openai/gdpval --type dataset",
"hf repos duplicate multimodalart/dreambooth-training my-dreambooth --type space --flavor l4x4 --secrets HF_TOKEN --private",
"hf repos duplicate org/my-space my-space --type space -v hf://org/my-model:/models -v hf://buckets/org/b:/data",
],
)
def repo_duplicate(
from_id: RepoIdArg,
to_id: Annotated[
str | None,
Argument(
help="Destination repo ID (e.g. `myorg/my-copy`). Defaults to your namespace with the same repo name.",
),
] = None,
repo_type: RepoTypeOpt = RepoType.model,
private: PrivateOpt = None,
public: PublicOpt = None,
protected: ProtectedOpt = None,
token: TokenOpt = None,
exist_ok: Annotated[
bool,
Option(
help="Do not raise an error if repo already exists.",
),
] = False,
hardware: SpaceHardwareOpt = None,
storage: SpaceStorageOpt = None,
sleep_time: SpaceSleepTimeOpt = None,
secrets: SecretsOpt = None,
secrets_file: SecretsFileOpt = None,
env: EnvOpt = None,
env_file: EnvFileOpt = None,
volume: VolumesOpt = None,
) -> None:
"""Duplicate a repo on the Hub (model, dataset, or Space)."""
api = get_hf_api(token=token)
repo_url = api.duplicate_repo(
from_id=from_id,
to_id=to_id,
repo_type=repo_type.value,
visibility="private" if private else "public" if public else "protected" if protected else None, # type: ignore [arg-type]
token=token,
exist_ok=exist_ok,
space_hardware=hardware,
space_storage=storage,
space_sleep_time=sleep_time,
space_secrets=env_map_to_key_value_list(parse_env_map(secrets, secrets_file)),
space_variables=env_map_to_key_value_list(parse_env_map(env, env_file)),
space_volumes=parse_volumes(volume),
)
out.result("Repo duplicated", from_id=from_id, to_id=repo_url.repo_id, url=str(repo_url))
@repos_cli.command("delete", examples=["hf repos delete my-model"])
def repo_delete(
repo_id: RepoIdArg,
repo_type: RepoTypeOpt = RepoType.model,
token: TokenOpt = None,
missing_ok: Annotated[
bool,
Option(
help="If set to True, do not raise an error if repo does not exist.",
),
] = False,
yes: Annotated[
bool,
Option(
"-y",
"--yes",
help="Answer Yes to prompt automatically.",
),
] = False,
) -> None:
"""Delete a repo from the Hub. This is an irreversible operation."""
out.confirm(f"You are about to permanently delete {repo_type.value} '{repo_id}'. Proceed?", yes=yes)
api = get_hf_api(token=token)
api.delete_repo(
repo_id=repo_id,
repo_type=repo_type.value,
missing_ok=missing_ok,
)
out.result("Repo deleted", repo_id=repo_id)
@repos_cli.command("move", examples=["hf repos move old-namespace/my-model new-namespace/my-model"])
def repo_move(
from_id: RepoIdArg,
to_id: RepoIdArg,
token: TokenOpt = None,
repo_type: RepoTypeOpt = RepoType.model,
) -> None:
"""Move a repository from a namespace to another namespace."""
api = get_hf_api(token=token)
api.move_repo(
from_id=from_id,
to_id=to_id,
repo_type=repo_type.value,
)
out.result("Repo moved", from_id=from_id, to_id=to_id)
@repos_cli.command(
"settings",
examples=[
"hf repos settings my-model --private",
"hf repos settings my-model --gated auto",
"hf repos settings my-space --repo-type space --protected",
],
)
def repo_settings(
repo_id: RepoIdArg,
gated: Annotated[
GatedChoices | None,
Option(
help="The gated status for the repository.",
),
] = None,
private: PrivateOpt = None,
public: PublicOpt = None,
protected: ProtectedOpt = None,
token: TokenOpt = None,
repo_type: RepoTypeOpt = RepoType.model,
) -> None:
"""Update the settings of a repository."""
api = get_hf_api(token=token)
api.update_repo_settings(
repo_id=repo_id,
gated=(None if gated is None else False if gated is GatedChoices.false else gated.value),
visibility="private" if private else "public" if public else "protected" if protected else None, # type: ignore [arg-type]
repo_type=repo_type.value,
)
out.result("Repo settings updated", repo_id=repo_id)
@repos_cli.command(
"delete-files",
examples=[
"hf repos delete-files my-model file.txt",
'hf repos delete-files my-model "*.json"',
"hf repos delete-files my-model folder/",
],
)
def repo_delete_files(
repo_id: RepoIdArg,
patterns: Annotated[
list[str],
Argument(
help="Glob patterns to match files to delete. Based on fnmatch, '*' matches files recursively.",
),
],
repo_type: RepoTypeOpt = RepoType.model,
revision: RevisionOpt = None,
commit_message: Annotated[
str | None,
Option(
help="The summary / title / first line of the generated commit.",
),
] = None,
commit_description: Annotated[
str | None,
Option(
help="The description of the generated commit.",
),
] = None,
create_pr: Annotated[
bool,
Option(
help="Whether to create a new Pull Request for these changes.",
),
] = False,
token: TokenOpt = None,
) -> None:
"""Delete files from a repo on the Hub."""
api = get_hf_api(token=token)
url = api.delete_files(
delete_patterns=patterns,
repo_id=repo_id,
repo_type=repo_type.value,
revision=revision,
commit_message=commit_message,
commit_description=commit_description,
create_pr=create_pr,
)
out.result("Files deleted", repo_id=repo_id, commit_url=url)
# `hf repos cp` is an alias for the top-level `hf cp` command (see `cli/_cp.py`).
repos_cli.command(
name="cp",
examples=[
# Download (repo or bucket -> local / stdout)
"hf repos cp hf://username/my-model/config.json config.json",
"hf repos cp hf://datasets/username/my-dataset/data.csv data/",
"hf repos cp hf://username/my-model/config.json -",
# Upload (local / stdin -> repo)
"hf repos cp model.safetensors hf://username/my-model/model.safetensors",
"hf repos cp config.json hf://username/my-model/logs/",
"hf repos cp - hf://username/my-model/config.json",
# Remote to remote (repo -> repo)
"hf repos cp hf://username/source-model/config.json hf://username/dest-model/config.json",
"hf repos cp hf://datasets/username/my-dataset/processed/ hf://datasets/username/dest-dataset/processed/",
"hf repos cp hf://username/my-model/logs/ hf://username/archive-model/logs/",
],
)(make_cp("repos"))
@branch_cli.command(
"create",
examples=[
"hf repos branch create my-model dev",
"hf repos branch create my-model dev --revision abc123",
],
)
def branch_create(
repo_id: RepoIdArg,
branch: Annotated[
str,
Argument(
help="The name of the branch to create.",
),
],
revision: RevisionOpt = None,
token: TokenOpt = None,
repo_type: RepoTypeOpt = RepoType.model,
exist_ok: Annotated[
bool,
Option(
help="If set to True, do not raise an error if branch already exists.",
),
] = False,
) -> None:
"""Create a new branch for a repo on the Hub."""
api = get_hf_api(token=token)
api.create_branch(
repo_id=repo_id,
branch=branch,
revision=revision,
repo_type=repo_type.value,
exist_ok=exist_ok,
)
out.result("Branch created", branch=branch, repo_type=repo_type.value, repo_id=repo_id)
@branch_cli.command("delete", examples=["hf repos branch delete my-model dev"])
def branch_delete(
repo_id: RepoIdArg,
branch: Annotated[
str,
Argument(
help="The name of the branch to delete.",
),
],
token: TokenOpt = None,
repo_type: RepoTypeOpt = RepoType.model,
) -> None:
"""Delete a branch from a repo on the Hub."""
api = get_hf_api(token=token)
api.delete_branch(
repo_id=repo_id,
branch=branch,
repo_type=repo_type.value,
)
out.result("Branch deleted", branch=branch, repo_type=repo_type.value, repo_id=repo_id)
@tag_cli.command(
"create",
examples=[
"hf repos tag create my-model v1.0",
'hf repos tag create my-model v1.0 -m "First release"',
],
)
def tag_create(
repo_id: RepoIdArg,
tag: Annotated[
str,
Argument(
help="The name of the tag to create.",
),
],
message: Annotated[
str | None,
Option(
"-m",
"--message",
help="The description of the tag to create.",
),
] = None,
revision: RevisionOpt = None,
token: TokenOpt = None,
repo_type: RepoTypeOpt = RepoType.model,
) -> None:
"""Create a tag for a repo."""
repo_type_str = repo_type.value
api = get_hf_api(token=token)
try:
api.create_tag(repo_id=repo_id, tag=tag, tag_message=message, revision=revision, repo_type=repo_type_str)
except RepositoryNotFoundError as e:
raise CLIError(f"{repo_type_str.capitalize()} '{repo_id}' not found.") from e
except RevisionNotFoundError as e:
raise CLIError(f"Revision '{revision}' not found.") from e
except HfHubHTTPError as e:
if e.response.status_code == 409:
raise CLIError(f"Tag '{tag}' already exists on '{repo_id}'.") from e
raise
out.result("Tag created", tag=tag, repo_type=repo_type_str, repo_id=repo_id)
@tag_cli.command("list | ls", examples=["hf repos tag list my-model"])
def tag_list(
repo_id: RepoIdArg,
token: TokenOpt = None,
repo_type: RepoTypeOpt = RepoType.model,
) -> None:
"""List tags for a repo."""
repo_type_str = repo_type.value
api = get_hf_api(token=token)
try:
refs = api.list_repo_refs(repo_id=repo_id, repo_type=repo_type_str)
except RepositoryNotFoundError as e:
raise CLIError(f"{repo_type_str.capitalize()} '{repo_id}' not found.") from e
items = [{"name": t.name, "target_commit": t.target_commit, "ref": t.ref} for t in refs.tags]
out.table(items)
@tag_cli.command("delete", examples=["hf repos tag delete my-model v1.0"])
def tag_delete(
repo_id: RepoIdArg,
tag: Annotated[
str,
Argument(
help="The name of the tag to delete.",
),
],
yes: Annotated[
bool,
Option(
"-y",
"--yes",
help="Answer Yes to prompt automatically",
),
] = False,
token: TokenOpt = None,
repo_type: RepoTypeOpt = RepoType.model,
) -> None:
"""Delete a tag for a repo."""
repo_type_str = repo_type.value
out.text(f"You are about to delete tag {tag} on {repo_type_str} {repo_id}")
out.confirm("Proceed?", yes=yes)
api = get_hf_api(token=token)
try:
api.delete_tag(repo_id=repo_id, tag=tag, repo_type=repo_type_str)
except RepositoryNotFoundError as e:
raise CLIError(f"{repo_type_str.capitalize()} '{repo_id}' not found.") from e
except RevisionNotFoundError as e:
raise CLIError(f"Tag '{tag}' not found on '{repo_id}'.") from e
out.result("Tag deleted", tag=tag, repo_type=repo_type_str, repo_id=repo_id)
|