hf.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Copyright 2020 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. import sys
  15. import traceback
  16. from typing import Annotated
  17. import typer
  18. from huggingface_hub import __version__, constants
  19. from huggingface_hub.cli._cli_utils import check_cli_update, fallback_typer_group_factory, typer_factory
  20. from huggingface_hub.cli._errors import format_known_exception
  21. from huggingface_hub.cli.auth import auth_cli
  22. from huggingface_hub.cli.buckets import buckets_cli, sync
  23. from huggingface_hub.cli.cache import cache_cli
  24. from huggingface_hub.cli.collections import collections_cli
  25. from huggingface_hub.cli.datasets import datasets_cli
  26. from huggingface_hub.cli.discussions import discussions_cli
  27. from huggingface_hub.cli.download import DOWNLOAD_EXAMPLES, download
  28. from huggingface_hub.cli.extensions import (
  29. dispatch_unknown_top_level_extension,
  30. extensions_cli,
  31. list_installed_extensions_for_help,
  32. )
  33. from huggingface_hub.cli.inference_endpoints import ie_cli
  34. from huggingface_hub.cli.jobs import jobs_cli
  35. from huggingface_hub.cli.lfs import lfs_enable_largefiles, lfs_multipart_upload
  36. from huggingface_hub.cli.models import models_cli
  37. from huggingface_hub.cli.papers import papers_cli
  38. from huggingface_hub.cli.repo_files import repo_files_cli
  39. from huggingface_hub.cli.repos import repos_cli
  40. from huggingface_hub.cli.skills import skills_cli
  41. from huggingface_hub.cli.spaces import spaces_cli
  42. from huggingface_hub.cli.system import env, version
  43. from huggingface_hub.cli.upload import UPLOAD_EXAMPLES, upload
  44. from huggingface_hub.cli.upload_large_folder import UPLOAD_LARGE_FOLDER_EXAMPLES, upload_large_folder
  45. from huggingface_hub.cli.webhooks import webhooks_cli
  46. from huggingface_hub.utils import ANSI, logging
  47. app = typer_factory(
  48. help="Hugging Face Hub CLI",
  49. cls=fallback_typer_group_factory(
  50. dispatch_unknown_top_level_extension,
  51. extra_commands_provider=list_installed_extensions_for_help,
  52. ),
  53. )
  54. def _version_callback(value: bool) -> None:
  55. if value:
  56. print(__version__)
  57. raise typer.Exit()
  58. @app.callback(invoke_without_command=True)
  59. def app_callback(
  60. version: Annotated[
  61. bool | None, typer.Option("--version", callback=_version_callback, is_eager=True, hidden=True)
  62. ] = None,
  63. ) -> None:
  64. pass
  65. # top level single commands (defined in their respective files)
  66. app.command()(sync)
  67. app.command(examples=DOWNLOAD_EXAMPLES)(download)
  68. app.command(examples=UPLOAD_EXAMPLES)(upload)
  69. app.command(examples=UPLOAD_LARGE_FOLDER_EXAMPLES)(upload_large_folder)
  70. app.command(topic="help")(env)
  71. app.command(topic="help")(version)
  72. app.command(hidden=True)(lfs_enable_largefiles)
  73. app.command(hidden=True)(lfs_multipart_upload)
  74. # command groups
  75. app.add_typer(auth_cli, name="auth")
  76. app.add_typer(buckets_cli, name="buckets")
  77. app.add_typer(cache_cli, name="cache")
  78. app.add_typer(collections_cli, name="collections")
  79. app.add_typer(datasets_cli, name="datasets")
  80. app.add_typer(discussions_cli, name="discussions")
  81. app.add_typer(jobs_cli, name="jobs")
  82. app.add_typer(models_cli, name="models")
  83. app.add_typer(papers_cli, name="papers")
  84. app.add_typer(repos_cli, name="repos | repo")
  85. app.add_typer(repo_files_cli, name="repo-files", hidden=True)
  86. app.add_typer(skills_cli, name="skills")
  87. app.add_typer(spaces_cli, name="spaces")
  88. app.add_typer(webhooks_cli, name="webhooks")
  89. app.add_typer(ie_cli, name="endpoints")
  90. app.add_typer(extensions_cli, name="extensions | ext")
  91. def main():
  92. if not constants.HF_DEBUG:
  93. logging.set_verbosity_info()
  94. check_cli_update("huggingface_hub")
  95. try:
  96. app()
  97. except Exception as e:
  98. message = format_known_exception(e)
  99. if message:
  100. print(f"Error: {message}", file=sys.stderr)
  101. if constants.HF_DEBUG:
  102. traceback.print_exc()
  103. else:
  104. print(ANSI.gray("Set HF_DEBUG=1 as environment variable for full traceback."))
  105. sys.exit(1)
  106. raise
  107. if __name__ == "__main__":
  108. main()