_cli_utils.py 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. # Copyright 2022 The HuggingFace Team. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Contains CLI utilities (styling, helpers)."""
  15. import dataclasses
  16. import datetime
  17. import difflib
  18. import importlib.metadata
  19. import json
  20. import os
  21. import re
  22. import subprocess
  23. import sys
  24. import time
  25. from collections.abc import Callable, Sequence
  26. from enum import Enum
  27. from pathlib import Path
  28. from typing import TYPE_CHECKING, Annotated, Any, Literal, TypeVar, Union, cast
  29. import click
  30. import typer
  31. from typer.core import TyperCommand, TyperGroup
  32. from huggingface_hub import Volume, __version__, constants
  33. from huggingface_hub.errors import CLIError
  34. from huggingface_hub.utils import (
  35. ANSI,
  36. disable_progress_bars,
  37. get_session,
  38. hf_raise_for_status,
  39. installation_method,
  40. logging,
  41. tabulate,
  42. )
  43. from huggingface_hub.utils._dotenv import load_dotenv
  44. from ._output import OutputFormatWithAuto, out
  45. logger = logging.get_logger()
  46. # Arbitrary maximum length of a cell in a table output
  47. _MAX_CELL_LENGTH = 35
  48. if TYPE_CHECKING:
  49. from huggingface_hub.hf_api import HfApi
  50. def get_hf_api(token: str | None = None) -> "HfApi":
  51. # Import here to avoid circular import
  52. from huggingface_hub.hf_api import HfApi
  53. return HfApi(token=token, library_name="huggingface-cli", library_version=__version__)
  54. #### TYPER UTILS
  55. CLI_REFERENCE_URL = "https://huggingface.co/docs/huggingface_hub/en/guides/cli"
  56. def generate_epilog(examples: list[str], docs_anchor: str | None = None) -> str:
  57. """Generate an epilog with examples and a Learn More section.
  58. Args:
  59. examples: List of example commands (without the `$ ` prefix).
  60. docs_anchor: Optional anchor for the docs URL (e.g., "#hf-download").
  61. Returns:
  62. Formatted epilog string.
  63. """
  64. docs_url = f"{CLI_REFERENCE_URL}{docs_anchor}" if docs_anchor else CLI_REFERENCE_URL
  65. examples_str = "\n".join(f" $ {ex}" for ex in examples)
  66. return f"""\
  67. Examples
  68. {examples_str}
  69. Learn more
  70. Use `hf <command> --help` for more information about a command.
  71. Read the documentation at {docs_url}
  72. """
  73. TOPIC_T = Literal["main", "help"] | str
  74. FallbackHandlerT = Callable[[list[str], set[str]], int | None]
  75. ExpandPropertyT = TypeVar("ExpandPropertyT", bound=str)
  76. def _format_epilog_no_indent(epilog: str | None, ctx: click.Context, formatter: click.HelpFormatter) -> None:
  77. """Write the epilog without indentation."""
  78. if epilog:
  79. formatter.write_paragraph()
  80. for line in epilog.split("\n"):
  81. formatter.write_text(line)
  82. _ALIAS_SPLIT = re.compile(r"\s*\|\s*")
  83. class HFCliTyperGroup(TyperGroup):
  84. """
  85. Typer Group that:
  86. - lists commands alphabetically within sections.
  87. - separates commands by topic (main, help, etc.).
  88. - formats epilog without extra indentation.
  89. - supports aliases via pipe-separated names (e.g. ``name="list | ls"``).
  90. - rewrites ``--json`` to ``--format json`` for commands that accept ``--format``.
  91. - rewrites ``spaces/user/repo`` to ``user/repo --type space`` for commands that accept ``--type``.
  92. - enriches "No such option" / "No such command" errors with available options or commands.
  93. """
  94. def invoke(self, ctx: click.Context) -> None:
  95. """Enrich unknown-option errors with available options or subcommands.
  96. Catches `NoSuchOption` raised during subcommand `make_context()`
  97. (option parsing). For leaf commands (e.g. `hf repos create --test`)
  98. we list the command's options; for groups (e.g. `hf cache --test`)
  99. we list subcommands since groups have no user-facing options.
  100. """
  101. try:
  102. return super().invoke(ctx)
  103. except click.NoSuchOption as e:
  104. if e.ctx is not None and e.ctx.command is not None:
  105. cmd = e.ctx.command
  106. if isinstance(cmd, click.Group):
  107. # Group has no user-facing options -> show subcommands instead
  108. items = [
  109. (name, sub.get_short_help_str(limit=80))
  110. for name in cmd.list_commands(e.ctx)
  111. if (sub := cmd.get_command(e.ctx, name)) is not None and not sub.hidden
  112. ]
  113. _enrich_usage_error(e, "commands", items)
  114. else:
  115. # Leaf command -> show its options using Click's rich formatting
  116. items = [
  117. record
  118. for p in cmd.get_params(e.ctx)
  119. if isinstance(p, click.Option) and not p.hidden and (record := p.get_help_record(e.ctx))
  120. ]
  121. _enrich_usage_error(e, "options", items)
  122. raise
  123. def resolve_command(self, ctx: click.Context, args: list[str]) -> tuple:
  124. cmd_name = args[0] if args and not args[0].startswith("-") else None
  125. cmd = self.get_command(ctx, cmd_name) if cmd_name else None
  126. if cmd is not None:
  127. self._rewrite_json_shorthand(cmd, args)
  128. self._rewrite_quiet_shorthand(cmd, args)
  129. self._rewrite_repo_type_prefix(cmd, args)
  130. try:
  131. return super().resolve_command(ctx, args)
  132. except click.UsageError as e:
  133. # Unknown subcommand -> add fuzzy suggestions and list available commands.
  134. if cmd is None and cmd_name is not None:
  135. # Expand aliases ("list | ls" → ["list", "ls"]) for accurate fuzzy matching.
  136. visible_names = [
  137. alias
  138. for key, registered in self.commands.items()
  139. if not registered.hidden
  140. for alias in _ALIAS_SPLIT.split(key)
  141. ]
  142. matches = difflib.get_close_matches(cmd_name, visible_names)
  143. if matches:
  144. suggestions = ", ".join(f"'{m}'" for m in matches)
  145. e.message = f"{e.message.rstrip('.')}. Did you mean {suggestions}?"
  146. items = [
  147. (name, sub.get_short_help_str(limit=80))
  148. for name in self.list_commands(ctx)
  149. if (sub := self.get_command(ctx, name)) is not None and not sub.hidden
  150. ]
  151. _enrich_usage_error(e, "commands", items)
  152. raise
  153. @staticmethod
  154. def _rewrite_json_shorthand(cmd: click.Command, args: list[str]) -> None:
  155. """Rewrite hidden ``--json`` shorthand to ``--format json``.
  156. Only applies to commands that accept ``--format``. This avoids rewriting
  157. ``--json`` for commands that pass args through to external binaries
  158. (e.g. ``hf extensions exec``) or that simply don't support ``--format``.
  159. """
  160. if "--json" not in args:
  161. return
  162. has_format_option = any(isinstance(param, click.Option) and "--format" in param.opts for param in cmd.params)
  163. if has_format_option:
  164. if any(arg == "--format" or arg.startswith("--format=") for arg in args):
  165. raise click.UsageError("'--json' and '--format' are mutually exclusive.")
  166. idx = args.index("--json")
  167. args[idx : idx + 1] = ["--format", "json"]
  168. @staticmethod
  169. def _rewrite_quiet_shorthand(cmd: click.Command, args: list[str]) -> None:
  170. """Rewrite ``-q`` / ``--quiet`` shorthand to ``--format quiet``.
  171. Only applies to commands that accept ``--format`` but do NOT already
  172. have their own ``--quiet`` / ``-q`` option.
  173. """
  174. has_quiet = "-q" in args or "--quiet" in args
  175. if not has_quiet:
  176. return
  177. has_format_option = any(isinstance(param, click.Option) and "--format" in param.opts for param in cmd.params)
  178. has_quiet_option = any(
  179. isinstance(param, click.Option) and ("--quiet" in param.opts or "-q" in param.opts) for param in cmd.params
  180. )
  181. if has_format_option and not has_quiet_option:
  182. if any(arg == "--format" or arg.startswith("--format=") for arg in args):
  183. raise click.UsageError("'--quiet' and '--format' are mutually exclusive.")
  184. flag = "-q" if "-q" in args else "--quiet"
  185. idx = args.index(flag)
  186. args[idx : idx + 1] = ["--format", "quiet"]
  187. @staticmethod
  188. def _rewrite_repo_type_prefix(cmd: click.Command, args: list[str]) -> None:
  189. """Rewrite prefixed repo IDs (e.g. ``spaces/user/repo``) to ``user/repo --type space``.
  190. Only applies to commands that have a ``--type`` / ``--repo-type`` option and
  191. at least one repo-ID positional argument (any ``click.Argument`` whose name
  192. ends with ``_id``, e.g. ``repo_id``, ``from_id``, ``to_id``). When the
  193. token that maps to such an argument matches ``{prefix}/org/repo`` (where
  194. *prefix* is one of ``spaces``, ``datasets``, or ``models``), the prefix is
  195. stripped and an implicit ``--type {type}`` is appended. An error is raised
  196. if ``--type`` is also provided explicitly or if multiple prefixed arguments
  197. disagree on the repo type.
  198. Only repo-ID positional slots are inspected so that other positional
  199. arguments (filenames, local paths, patterns …) are never misinterpreted as
  200. prefixed repo IDs.
  201. """
  202. has_type_option = any(isinstance(param, click.Option) and "--type" in param.opts for param in cmd.params)
  203. if not has_type_option:
  204. return
  205. # Locate all repo-ID positional arguments and their indices among Arguments.
  206. repo_id_positions: set[int] = set()
  207. arg_idx = 0
  208. for param in cmd.params:
  209. if isinstance(param, click.Argument):
  210. if param.name in ("repo_id", "from_id", "to_id"):
  211. repo_id_positions.add(arg_idx)
  212. arg_idx += 1
  213. if not repo_id_positions:
  214. return
  215. # Build a set of option names that consume a following value token.
  216. value_options: set[str] = set()
  217. for param in cmd.params:
  218. if isinstance(param, click.Option) and not param.is_flag:
  219. for opt in (*param.opts, *param.secondary_opts):
  220. value_options.add(opt)
  221. # Walk through args (skipping args[0] = command name) to map positional
  222. # slots to their indices in `args`.
  223. positional_count = 0
  224. repo_id_arg_indices: list[int] = []
  225. i = 1
  226. while i < len(args):
  227. arg = args[i]
  228. if arg == "--":
  229. break # everything after -- is positional literal; stop rewriting
  230. if arg.startswith("-"):
  231. if "=" in arg or arg not in value_options:
  232. i += 1 # flag or --opt=val — single token
  233. else:
  234. i += 2 # value-taking option — skip the value too
  235. else:
  236. if positional_count in repo_id_positions:
  237. repo_id_arg_indices.append(i)
  238. positional_count += 1
  239. i += 1
  240. if not repo_id_arg_indices:
  241. return
  242. # Check each repo-ID arg for a type prefix and collect rewrites.
  243. inferred_type: str | None = None
  244. first_prefix: str | None = None
  245. rewrites: list[tuple[int, str]] = [] # (args index, new value without prefix)
  246. for arg_index in repo_id_arg_indices:
  247. parts = args[arg_index].split("/", 2)
  248. if len(parts) != 3 or parts[0] not in constants.REPO_TYPES_MAPPING:
  249. continue
  250. prefix = parts[0]
  251. mapped_type = constants.REPO_TYPES_MAPPING[prefix]
  252. if inferred_type is not None and mapped_type != inferred_type:
  253. raise click.UsageError(f"Conflicting repo type prefixes: '{first_prefix}/' and '{prefix}/'.")
  254. inferred_type = mapped_type
  255. first_prefix = prefix
  256. rewrites.append((arg_index, f"{parts[1]}/{parts[2]}"))
  257. if not rewrites:
  258. return
  259. # Error if --type / --repo-type was also provided explicitly.
  260. if any(
  261. arg == "--type" or arg.startswith("--type=") or arg == "--repo-type" or arg.startswith("--repo-type=")
  262. for arg in args
  263. ):
  264. raise click.UsageError(
  265. f"Ambiguous repo type: got prefix '{first_prefix}/' in repo ID and explicit --type. Use one or the other."
  266. )
  267. # Apply all rewrites and append --type once.
  268. for arg_index, new_value in rewrites:
  269. args[arg_index] = new_value
  270. args.extend(["--type", inferred_type]) # type: ignore
  271. def get_command(self, ctx: click.Context, cmd_name: str) -> click.Command | None:
  272. # Try exact match first
  273. cmd = super().get_command(ctx, cmd_name)
  274. if cmd is not None:
  275. return cmd
  276. # Fall back to alias lookup: check if cmd_name matches any alias
  277. # taken from https://github.com/fastapi/typer/issues/132#issuecomment-2417492805
  278. for registered_name, registered_cmd in self.commands.items():
  279. aliases = _ALIAS_SPLIT.split(registered_name)
  280. if cmd_name in aliases:
  281. return registered_cmd
  282. return None
  283. def _alias_map(self) -> dict[str, list[str]]:
  284. """Build a mapping from primary command name to its aliases (if any)."""
  285. result: dict[str, list[str]] = {}
  286. for registered_name in self.commands:
  287. parts = _ALIAS_SPLIT.split(registered_name)
  288. primary = parts[0]
  289. result[primary] = parts[1:]
  290. return result
  291. def format_commands(self, ctx: click.Context, formatter: click.HelpFormatter) -> None:
  292. topics: dict[str, list] = {}
  293. alias_map = self._alias_map()
  294. for name in self.list_commands(ctx):
  295. cmd = self.get_command(ctx, name)
  296. if cmd is None or cmd.hidden:
  297. continue
  298. help_text = cmd.get_short_help_str(limit=formatter.width)
  299. aliases = alias_map.get(name, [])
  300. if aliases:
  301. help_text = f"{help_text} [alias: {', '.join(aliases)}]"
  302. topic = getattr(cmd, "topic", "main")
  303. topics.setdefault(topic, []).append((name, help_text))
  304. with formatter.section("Main commands"):
  305. formatter.write_dl(topics["main"])
  306. for topic in sorted(topics.keys()):
  307. if topic == "main":
  308. continue
  309. with formatter.section(f"{topic.capitalize()} commands"):
  310. formatter.write_dl(topics[topic])
  311. def format_epilog(self, ctx: click.Context, formatter: click.HelpFormatter) -> None:
  312. # Collect only the first example from each command (to keep group help concise)
  313. # Full examples are shown in individual subcommand help (e.g. `hf buckets sync --help`)
  314. all_examples: list[str] = []
  315. for name in self.list_commands(ctx):
  316. cmd = self.get_command(ctx, name)
  317. if cmd is None or cmd.hidden:
  318. continue
  319. cmd_examples = getattr(cmd, "examples", [])
  320. if cmd_examples:
  321. all_examples.append(cmd_examples[0])
  322. if all_examples:
  323. epilog = generate_epilog(all_examples)
  324. _format_epilog_no_indent(epilog, ctx, formatter)
  325. elif self.epilog:
  326. _format_epilog_no_indent(self.epilog, ctx, formatter)
  327. def list_commands(self, ctx: click.Context) -> list[str]: # type: ignore[name-defined]
  328. # For aliased commands ("list | ls"), use the primary name (first entry).
  329. primary_names: list[str] = []
  330. for name in self.commands:
  331. primary = _ALIAS_SPLIT.split(name)[0]
  332. primary_names.append(primary)
  333. return sorted(primary_names)
  334. def _enrich_usage_error(error: click.UsageError, label: str, items: list[tuple[str, str]]) -> None:
  335. """Append a list of available options or commands to a usage error message."""
  336. if not items or error.ctx is None or f"Available {label} for" in error.message:
  337. return
  338. cmd_path = error.ctx.command_path
  339. lines = [f"\n\nAvailable {label} for '{cmd_path}':"]
  340. for name, help_text in items:
  341. lines.append(f" {name:30s} {help_text}")
  342. lines.append(f"\nRun '{cmd_path} --help' for full details.")
  343. if isinstance(error, click.NoSuchOption) and error.possibilities:
  344. lines.append(f"\nDid you mean: {', '.join(sorted(error.possibilities))}?")
  345. error.possibilities = []
  346. error.message += "\n".join(lines)
  347. def fallback_typer_group_factory(
  348. fallback_handler: FallbackHandlerT,
  349. extra_commands_provider: Callable[[], list[tuple[str, str]]] | None = None,
  350. ) -> type[HFCliTyperGroup]:
  351. """Return a Typer group class that runs a fallback handler before command resolution."""
  352. class FallbackTyperGroup(HFCliTyperGroup):
  353. def resolve_command(self, ctx: click.Context, args: list[str]) -> tuple:
  354. fallback_exit_code = fallback_handler(args, set(self.commands.keys()))
  355. if fallback_exit_code is not None:
  356. raise SystemExit(fallback_exit_code)
  357. return super().resolve_command(ctx, args)
  358. def format_commands(self, ctx: click.Context, formatter: click.HelpFormatter) -> None:
  359. super().format_commands(ctx, formatter)
  360. if extra_commands_provider is not None:
  361. entries = extra_commands_provider()
  362. if entries:
  363. with formatter.section("Extension commands"):
  364. formatter.write_dl(entries)
  365. return FallbackTyperGroup
  366. def HFCliCommand(topic: TOPIC_T, examples: list[str] | None = None) -> type[TyperCommand]:
  367. def format_epilog(self: click.Command, ctx: click.Context, formatter: click.HelpFormatter) -> None:
  368. _format_epilog_no_indent(self.epilog, ctx, formatter)
  369. return type(
  370. f"TyperCommand{topic.capitalize()}",
  371. (TyperCommand,),
  372. {"topic": topic, "examples": examples or [], "format_epilog": format_epilog},
  373. )
  374. class HFCliApp(typer.Typer):
  375. """Custom Typer app for Hugging Face CLI."""
  376. def command( # type: ignore
  377. self,
  378. name: str | None = None,
  379. *,
  380. topic: TOPIC_T = "main",
  381. examples: list[str] | None = None,
  382. context_settings: dict[str, Any] | None = None,
  383. help: str | None = None,
  384. epilog: str | None = None,
  385. short_help: str | None = None,
  386. options_metavar: str = "[OPTIONS]",
  387. add_help_option: bool = True,
  388. no_args_is_help: bool = False,
  389. hidden: bool = False,
  390. deprecated: bool = False,
  391. rich_help_panel: str | None = None,
  392. ) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
  393. # Generate epilog from examples if not explicitly provided
  394. if epilog is None and examples:
  395. epilog = generate_epilog(examples)
  396. def _inner(func: Callable[..., Any]) -> Callable[..., Any]:
  397. return super(HFCliApp, self).command(
  398. name,
  399. cls=HFCliCommand(topic, examples),
  400. context_settings=context_settings,
  401. help=help,
  402. epilog=epilog,
  403. short_help=short_help,
  404. options_metavar=options_metavar,
  405. add_help_option=add_help_option,
  406. no_args_is_help=no_args_is_help,
  407. hidden=hidden,
  408. deprecated=deprecated,
  409. rich_help_panel=rich_help_panel,
  410. )(func)
  411. return _inner
  412. def typer_factory(help: str, epilog: str | None = None, cls: type[TyperGroup] | None = None) -> "HFCliApp":
  413. """Create a Typer app with consistent settings.
  414. Args:
  415. help: Help text for the app.
  416. epilog: Optional epilog text (use `generate_epilog` to create one).
  417. cls: Optional Click group class to use (defaults to `HFCliTyperGroup`).
  418. Returns:
  419. A configured Typer app.
  420. """
  421. if cls is None:
  422. cls = HFCliTyperGroup
  423. return HFCliApp(
  424. help=help,
  425. epilog=epilog,
  426. add_completion=True,
  427. no_args_is_help=True,
  428. cls=cls,
  429. # Disable rich completely for consistent experience
  430. rich_markup_mode=None,
  431. rich_help_panel=None,
  432. pretty_exceptions_enable=False,
  433. # Disable TyperGroup's suggest_commands, it matches against raw aliased
  434. # keys ("list | ls") leaking pipe syntax into user-facing messages.
  435. # HFCliTyperGroup.resolve_command() handles suggestions with expanded names.
  436. suggest_commands=False,
  437. # Increase max content width for better readability
  438. context_settings={
  439. "max_content_width": 120,
  440. "help_option_names": ["-h", "--help"],
  441. },
  442. )
  443. class RepoType(str, Enum):
  444. model = "model"
  445. dataset = "dataset"
  446. space = "space"
  447. RepoIdArg = Annotated[
  448. str,
  449. typer.Argument(
  450. help="The ID of the repo (e.g. `username/repo-name` or `spaces/username/repo-name`).",
  451. ),
  452. ]
  453. RepoTypeOpt = Annotated[
  454. RepoType,
  455. typer.Option(
  456. "--type",
  457. "--repo-type",
  458. help="The type of repository (model, dataset, or space).",
  459. ),
  460. ]
  461. TokenOpt = Annotated[
  462. str | None,
  463. typer.Option(
  464. help="A User Access Token generated from https://huggingface.co/settings/tokens.",
  465. ),
  466. ]
  467. PrivateOpt = Annotated[
  468. bool | None,
  469. typer.Option(
  470. help="Whether to create a private repo if repo doesn't exist on the Hub. Ignored if the repo already exists.",
  471. ),
  472. ]
  473. RevisionOpt = Annotated[
  474. str | None,
  475. typer.Option(
  476. help="Git revision id which can be a branch name, a tag, or a commit hash.",
  477. ),
  478. ]
  479. LimitOpt = Annotated[
  480. int,
  481. typer.Option(help="Limit the number of results."),
  482. ]
  483. AuthorOpt = Annotated[
  484. str | None,
  485. typer.Option(help="Filter by author or organization."),
  486. ]
  487. FilterOpt = Annotated[
  488. list[str] | None,
  489. typer.Option(help="Filter by tags (e.g. 'text-classification'). Can be used multiple times."),
  490. ]
  491. SearchOpt = Annotated[
  492. str | None,
  493. typer.Option(help="Search query."),
  494. ]
  495. # --- Env / Secrets shared options and parsing helpers (used by jobs, repos, etc.) ---
  496. EnvOpt = Annotated[
  497. list[str] | None,
  498. typer.Option(
  499. "-e",
  500. "--env",
  501. help="Set environment variables. E.g. --env ENV=value",
  502. ),
  503. ]
  504. SecretsOpt = Annotated[
  505. list[str] | None,
  506. typer.Option(
  507. "-s",
  508. "--secrets",
  509. help=(
  510. "Set secret environment variables. E.g. --secrets SECRET=value"
  511. " or `--secrets HF_TOKEN` to pass your Hugging Face token."
  512. ),
  513. ),
  514. ]
  515. EnvFileOpt = Annotated[
  516. str | None,
  517. typer.Option(
  518. "--env-file",
  519. help="Read in a file of environment variables.",
  520. ),
  521. ]
  522. SecretsFileOpt = Annotated[
  523. str | None,
  524. typer.Option(
  525. help="Read in a file of secret environment variables.",
  526. ),
  527. ]
  528. def _get_extended_environ() -> dict[str, str]:
  529. """Return a copy of ``os.environ`` with the user's HF token injected (if available)."""
  530. from huggingface_hub import get_token
  531. extended_environ = os.environ.copy()
  532. if (token := get_token()) is not None:
  533. extended_environ["HF_TOKEN"] = token
  534. return extended_environ
  535. def parse_env_map(
  536. env: list[str] | None = None,
  537. env_file: str | None = None,
  538. ) -> dict[str, str | None]:
  539. """Parse ``-e``/``--env``/``-s``/``--secrets`` and ``--env-file``/``--secrets-file`` CLI args into a dict.
  540. Uses an extended environment that includes the user's HF token so that
  541. bare ``--secrets HF_TOKEN`` resolves correctly.
  542. """
  543. extended_environ = _get_extended_environ()
  544. env_map: dict[str, str | None] = {}
  545. if env_file:
  546. env_map.update(load_dotenv(Path(env_file).read_text(), environ=extended_environ))
  547. for env_value in env or []:
  548. env_map.update(load_dotenv(env_value, environ=extended_environ))
  549. return env_map
  550. def env_map_to_key_value_list(env_map: dict[str, str | None]) -> list[dict[str, str]] | None:
  551. """Convert an env/secrets dict to the ``[{"key": ..., "value": ...}]`` format used by the Hub API."""
  552. if not env_map:
  553. return None
  554. return [{"key": k, "value": v or ""} for k, v in env_map.items()]
  555. VolumesOpt = Annotated[
  556. list[str] | None,
  557. typer.Option(
  558. "-v",
  559. "--volume",
  560. help="Mount one or more volumes. Format: hf://[TYPE/]SOURCE:/MOUNT_PATH[:ro]. "
  561. "TYPE is one of: models, datasets, spaces, buckets. "
  562. "TYPE defaults to models if omitted. "
  563. "models, datasets and spaces are always mounted read-only. buckets are read+write by default. "
  564. "E.g. -v hf://gpt2:/data or -v hf://datasets/org/ds:/data or -v hf://buckets/org/b:/mnt:ro",
  565. ),
  566. ]
  567. _HF_PREFIX = "hf://"
  568. _HF_VOLUME_TYPES = {
  569. "models": constants.REPO_TYPE_MODEL,
  570. "datasets": constants.REPO_TYPE_DATASET,
  571. "spaces": constants.REPO_TYPE_SPACE,
  572. "buckets": "bucket",
  573. }
  574. def parse_volumes(volumes: list[str] | None) -> "list[Volume] | None":
  575. """Parse volume specs from CLI arguments.
  576. Format: hf://[TYPE/]SOURCE[/PATH]:/MOUNT_PATH[:ro|:rw]
  577. Where TYPE is one of: models, datasets, spaces, buckets (defaults to models if omitted).
  578. SOURCE is the repo/bucket identifier (e.g. 'username/my-model').
  579. PATH is an optional subfolder inside the repo/bucket.
  580. MOUNT_PATH starts with '/'.
  581. Optional ':ro' or ':rw' suffix for read-only or read-write.
  582. Examples:
  583. hf://gpt2:/data (model, implicit type)
  584. hf://my-org/my-model:/data (model, implicit type)
  585. hf://models/my-org/my-model:/data (model, explicit type)
  586. hf://datasets/my-org/my-dataset:/data:ro
  587. hf://buckets/my-org/my-bucket:/mnt
  588. hf://spaces/my-org/my-space:/app
  589. hf://datasets/org/ds/train:/data (with path inside repo)
  590. hf://buckets/org/b/sub/dir:/mnt (with path inside bucket)
  591. """
  592. if not volumes:
  593. return None
  594. result: list[Volume] = []
  595. for raw_spec in volumes:
  596. # Strip :ro/:rw suffix
  597. spec = raw_spec
  598. read_only = None
  599. if spec.endswith(":ro"):
  600. read_only = True
  601. spec = spec[:-3]
  602. elif spec.endswith(":rw"):
  603. read_only = False
  604. spec = spec[:-3]
  605. # Validate hf:// prefix
  606. if not spec.startswith(_HF_PREFIX):
  607. raise CLIError(
  608. f"Invalid volume format: '{raw_spec}'. Source must start with 'hf://'. "
  609. f"Expected hf://[TYPE/]SOURCE:/MOUNT_PATH[:ro]. E.g. hf://gpt2:/data"
  610. )
  611. spec = spec[len(_HF_PREFIX) :]
  612. # Find the mount path: look for :/ pattern
  613. colon_slash_idx = spec.find(":/")
  614. if colon_slash_idx == -1:
  615. raise CLIError(
  616. f"Invalid volume format: '{raw_spec}'. Expected hf://[TYPE/]SOURCE:/MOUNT_PATH[:ro]. E.g. hf://gpt2:/data"
  617. )
  618. source_part = spec[:colon_slash_idx]
  619. mount_path = spec[colon_slash_idx + 1 :]
  620. # Parse type from source_part (first segment before /)
  621. # Then split remaining into source (namespace/name or name) and optional path.
  622. slash_idx = source_part.find("/")
  623. if slash_idx == -1:
  624. # No slash: bare source like "gpt2" -> model type
  625. vol_type_str = constants.REPO_TYPE_MODEL
  626. source = source_part
  627. path = None
  628. else:
  629. first_segment = source_part[:slash_idx]
  630. if first_segment in _HF_VOLUME_TYPES:
  631. vol_type_str = _HF_VOLUME_TYPES[first_segment]
  632. remaining = source_part[slash_idx + 1 :]
  633. else:
  634. # First segment isn't a known type -> model type
  635. vol_type_str = constants.REPO_TYPE_MODEL
  636. remaining = source_part
  637. # Split remaining into source (namespace/name) and optional path.
  638. # Repo/bucket IDs are "namespace/name" (2 segments) or "name" (1 segment).
  639. # Any extra segments are the path inside the repo/bucket.
  640. parts = remaining.split("/", 2)
  641. if len(parts) >= 3:
  642. source = parts[0] + "/" + parts[1]
  643. path = parts[2]
  644. else:
  645. source = remaining
  646. path = None
  647. result.append(
  648. Volume(
  649. type=vol_type_str,
  650. source=source,
  651. mount_path=mount_path,
  652. read_only=read_only,
  653. path=path,
  654. )
  655. )
  656. return result
  657. class OutputFormat(str, Enum):
  658. """Output format for CLI list commands."""
  659. table = "table"
  660. json = "json"
  661. FormatOpt = Annotated[
  662. OutputFormat,
  663. typer.Option(
  664. help="Output format (table or json).",
  665. ),
  666. ]
  667. def _set_output_mode(value: OutputFormatWithAuto) -> OutputFormatWithAuto:
  668. out.set_mode(value)
  669. if out.mode != OutputFormatWithAuto.human:
  670. disable_progress_bars()
  671. return value
  672. FormatWithAutoOpt = Annotated[
  673. OutputFormatWithAuto,
  674. typer.Option(
  675. help="Output format.",
  676. callback=_set_output_mode,
  677. ),
  678. ]
  679. QuietOpt = Annotated[
  680. bool,
  681. typer.Option(
  682. "-q",
  683. "--quiet",
  684. help="Print only IDs (one per line).",
  685. ),
  686. ]
  687. def _to_header(name: str) -> str:
  688. """Convert a camelCase or PascalCase string to SCREAMING_SNAKE_CASE to be used as table header."""
  689. s = re.sub(r"([a-z])([A-Z])", r"\1_\2", name)
  690. return s.upper()
  691. def _format_value(value: Any) -> str:
  692. """Convert a value to string for terminal display."""
  693. if not value:
  694. return ""
  695. if isinstance(value, bool):
  696. return "✔" if value else ""
  697. if isinstance(value, datetime.datetime):
  698. return value.strftime("%Y-%m-%d")
  699. if isinstance(value, str) and re.match(r"^\d{4}-\d{2}-\d{2}T", value):
  700. return value[:10]
  701. if isinstance(value, list):
  702. return ", ".join(_format_value(v) for v in value)
  703. elif isinstance(value, dict):
  704. if "name" in value: # Likely to be a user or org => print name
  705. return str(value["name"])
  706. # TODO: extend if needed
  707. return json.dumps(value)
  708. return str(value)
  709. def _format_cell(value: Any, max_len: int = _MAX_CELL_LENGTH) -> str:
  710. """Format a value + truncate it for table display."""
  711. cell = _format_value(value)
  712. if len(cell) > max_len:
  713. cell = cell[: max_len - 3] + "..."
  714. return cell
  715. def print_as_table(
  716. items: Sequence[dict[str, Any]],
  717. headers: list[str],
  718. row_fn: Callable[[dict[str, Any]], list[str]],
  719. alignments: dict[str, str] | None = None,
  720. ) -> None:
  721. """Print items as a formatted table.
  722. Args:
  723. items: Sequence of dictionaries representing the items to display.
  724. headers: List of column headers.
  725. row_fn: Function that takes an item dict and returns a list of string values for each column.
  726. alignments: Optional mapping of header name to "left" or "right". Defaults to "left".
  727. """
  728. if not items:
  729. print("No results found.")
  730. return
  731. rows = cast(list[list[Union[str, int]]], [row_fn(item) for item in items])
  732. screaming_headers = [_to_header(h) for h in headers]
  733. # Remap alignments keys to screaming case to match tabulate headers
  734. screaming_alignments = {_to_header(k): v for k, v in (alignments or {}).items()}
  735. print(tabulate(rows, headers=screaming_headers, alignments=screaming_alignments))
  736. def print_list_output(
  737. items: Sequence[dict[str, Any]],
  738. format: OutputFormat,
  739. quiet: bool,
  740. id_key: str = "id",
  741. headers: list[str] | None = None,
  742. row_fn: Callable[[dict[str, Any]], list[str]] | None = None,
  743. alignments: dict[str, str] | None = None,
  744. ) -> None:
  745. """Print list command output in the specified format.
  746. Args:
  747. items: Sequence of dictionaries representing the items to display.
  748. format: Output format.
  749. quiet: If True, print only IDs (one per line).
  750. id_key: Key to use for extracting IDs in quiet mode.
  751. headers: Optional list of column names for headers. If not provided, auto-detected from keys.
  752. row_fn: Optional function to extract row values. If not provided, uses _format_cell on each column.
  753. alignments: Optional mapping of header name to "left" or "right". Defaults to "left".
  754. """
  755. if quiet:
  756. for item in items:
  757. print(item[id_key])
  758. return
  759. if format == OutputFormat.json:
  760. print(json.dumps(list(items), indent=2, default=str))
  761. return
  762. if headers is None:
  763. all_columns = list(items[0].keys()) if items else [id_key]
  764. headers = [col for col in all_columns if any(_format_cell(item.get(col)) for item in items)]
  765. if row_fn is None:
  766. def row_fn(item: dict[str, Any]) -> list[str]:
  767. return [_format_cell(item.get(col)) for col in headers] # type: ignore[union-attr]
  768. print_as_table(items, headers=headers, row_fn=row_fn, alignments=alignments)
  769. def _serialize_value(v: object) -> object:
  770. """Recursively serialize a value to be JSON-compatible."""
  771. if isinstance(v, datetime.datetime):
  772. return v.isoformat()
  773. elif isinstance(v, dict):
  774. return {key: _serialize_value(val) for key, val in v.items() if val is not None}
  775. elif isinstance(v, list):
  776. return [_serialize_value(item) for item in v]
  777. return v
  778. def api_object_to_dict(info: Any) -> dict[str, Any]:
  779. """Convert repo info dataclasses to json-serializable dicts."""
  780. return {k: _serialize_value(v) for k, v in dataclasses.asdict(info).items() if v is not None}
  781. def make_expand_properties_parser(valid_properties: Sequence[ExpandPropertyT]):
  782. """Create a callback to parse and validate comma-separated expand properties."""
  783. def _parse_expand_properties(value: str | None) -> list[ExpandPropertyT] | None:
  784. if value is None:
  785. return None
  786. properties = [p.strip() for p in value.split(",")]
  787. for prop in properties:
  788. if prop not in valid_properties:
  789. raise typer.BadParameter(
  790. f"Invalid expand property: '{prop}'. Valid values are: {', '.join(valid_properties)}"
  791. )
  792. return [cast(ExpandPropertyT, prop) for prop in properties]
  793. return _parse_expand_properties
  794. ### PyPI VERSION CHECKER
  795. def check_cli_update(library: Literal["huggingface_hub", "transformers"]) -> None:
  796. """
  797. Check whether a newer version of a library is available on PyPI.
  798. If a newer version is found and stdin/stderr are attached to a TTY, prompt the user to update interactively.
  799. Otherwise (non-TTY or update command cannot be determined), print a warning to stderr.
  800. If current version is a pre-release (e.g. `1.0.0.rc1`), or a dev version (e.g. `1.0.0.dev1`), no check is performed.
  801. This function is called at the entry point of the CLI. It only performs the check once every 24 hours, and any error
  802. during the check is caught and logged, to avoid breaking the CLI.
  803. Args:
  804. library: The library to check for updates. Currently supports "huggingface_hub" and "transformers".
  805. """
  806. try:
  807. _check_cli_update(library)
  808. except Exception:
  809. # We don't want the CLI to fail on version checks, no matter the reason.
  810. logger.debug("Error while checking for CLI update.", exc_info=True)
  811. def _check_cli_update(library: Literal["huggingface_hub", "transformers"]) -> None:
  812. current_version = importlib.metadata.version(library)
  813. # Skip if current version is a pre-release or dev version
  814. if any(tag in current_version for tag in ["rc", "dev"]):
  815. return
  816. # Skip if already checked in the last 24 hours
  817. if os.path.exists(constants.CHECK_FOR_UPDATE_DONE_PATH):
  818. mtime = os.path.getmtime(constants.CHECK_FOR_UPDATE_DONE_PATH)
  819. if (time.time() - mtime) < 24 * 3600:
  820. return
  821. # Touch the file to mark that we did the check now
  822. Path(constants.CHECK_FOR_UPDATE_DONE_PATH).parent.mkdir(parents=True, exist_ok=True)
  823. Path(constants.CHECK_FOR_UPDATE_DONE_PATH).touch()
  824. # Check latest version from PyPI
  825. response = get_session().get(f"https://pypi.org/pypi/{library}/json", timeout=2)
  826. hf_raise_for_status(response)
  827. data = response.json()
  828. latest_version = data["info"]["version"]
  829. if current_version == latest_version:
  830. return
  831. if library == "huggingface_hub":
  832. update_command = _get_huggingface_hub_update_command()
  833. else:
  834. update_command = _get_transformers_update_command()
  835. if sys.stdin.isatty() and sys.stderr.isatty() and update_command is not None:
  836. _prompt_autoupdate(library, current_version, latest_version, update_command)
  837. else:
  838. display_cmd = " ".join(update_command) if update_command else None
  839. update_hint = f"To update, run: {ANSI.bold(display_cmd)}" if display_cmd else ""
  840. click.echo(
  841. ANSI.yellow(
  842. f"A new version of {library} ({latest_version}) is available! "
  843. f"You are using version {current_version}." + (f"\n{update_hint}" if update_hint else "") + "\n"
  844. ),
  845. file=sys.stderr,
  846. )
  847. def _prompt_autoupdate(
  848. library: str,
  849. current_version: str,
  850. latest_version: str,
  851. update_command: list[str],
  852. ) -> None:
  853. """Interactively ask the user if they want to update, and run the update command if accepted.
  854. After a successful update the CLI exits so the user can re-run their command with the new version.
  855. All output goes to stderr to keep stdout clean for command output.
  856. """
  857. display_cmd = " ".join(update_command)
  858. click.echo("", file=sys.stderr)
  859. click.echo(
  860. ANSI.yellow(f" A new version of {library} is available: {current_version} → {latest_version}"),
  861. file=sys.stderr,
  862. )
  863. click.echo("", file=sys.stderr)
  864. click.echo(
  865. ANSI.yellow(" Do you want to update now? [Y/n] ") + ANSI.gray(f"({display_cmd})") + " ",
  866. file=sys.stderr,
  867. nl=False,
  868. )
  869. try:
  870. raw_answer = sys.stdin.readline()
  871. except (EOFError, KeyboardInterrupt):
  872. click.echo("", file=sys.stderr)
  873. return
  874. if raw_answer == "":
  875. # EOF (e.g. Ctrl+D) — treat as cancellation, not acceptance
  876. click.echo("", file=sys.stderr)
  877. return
  878. answer = raw_answer.strip().lower() # Note: if user press 'Enter', raw_answer is `\n`
  879. if answer in ("", "y", "yes"):
  880. click.echo("", file=sys.stderr)
  881. click.echo(ANSI.gray(f" Running: {display_cmd}"), file=sys.stderr)
  882. click.echo("", file=sys.stderr)
  883. returncode = subprocess.call(update_command)
  884. if returncode == 0:
  885. click.echo("", file=sys.stderr)
  886. click.echo(
  887. ANSI.green(f" ✓ Successfully updated {library} to {latest_version}. Please re-run your command."),
  888. file=sys.stderr,
  889. )
  890. raise SystemExit(0)
  891. else:
  892. click.echo("", file=sys.stderr)
  893. click.echo(
  894. ANSI.red(f" ✗ Update failed (exit code {returncode}). Please update manually."),
  895. file=sys.stderr,
  896. )
  897. else:
  898. click.echo(
  899. ANSI.gray(f" Skipped. You can update later with: {display_cmd}"),
  900. file=sys.stderr,
  901. )
  902. click.echo("", file=sys.stderr)
  903. def _get_huggingface_hub_update_command() -> list[str] | None:
  904. """Return the command to update huggingface_hub as an argv list, or None if the installation method is unknown."""
  905. match installation_method():
  906. case "brew":
  907. return ["brew", "upgrade", "hf"]
  908. case "hf_installer" if os.name == "nt":
  909. return ["powershell", "-NoProfile", "-Command", "iwr -useb https://hf.co/cli/install.ps1 | iex"]
  910. case "hf_installer":
  911. return ["bash", "-c", "curl -LsSf https://hf.co/cli/install.sh | bash -"]
  912. case "pip":
  913. return [sys.executable, "-m", "pip", "install", "-U", "huggingface_hub"]
  914. case _:
  915. return None
  916. def _get_transformers_update_command() -> list[str] | None:
  917. """Return the command to update transformers as an argv list, or None if the installation method is unknown."""
  918. match installation_method():
  919. case "hf_installer" if os.name == "nt":
  920. return [
  921. "powershell",
  922. "-NoProfile",
  923. "-Command",
  924. "iwr -useb https://hf.co/cli/install.ps1 | iex -WithTransformers",
  925. ]
  926. case "hf_installer":
  927. return ["bash", "-c", "curl -LsSf https://hf.co/cli/install.sh | bash -s -- --with-transformers"]
  928. case "pip":
  929. return [sys.executable, "-m", "pip", "install", "-U", "transformers"]
  930. case _:
  931. return None