repo_files.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # coding=utf-8
  2. # Copyright 2023-present, the HuggingFace Inc. team.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Contains command to update or delete files in a repository using the CLI.
  16. Usage:
  17. # delete all
  18. hf repo-files delete <repo_id> "*"
  19. # delete single file
  20. hf repo-files delete <repo_id> file.txt
  21. # delete single folder
  22. hf repo-files delete <repo_id> folder/
  23. # delete multiple
  24. hf repo-files delete <repo_id> file.txt folder/ file2.txt
  25. # delete multiple patterns
  26. hf repo-files delete <repo_id> file.txt "*.json" "folder/*.parquet"
  27. # delete from different revision / repo-type
  28. hf repo-files delete <repo_id> file.txt --revision=refs/pr/1 --repo-type=dataset
  29. """
  30. from argparse import _SubParsersAction
  31. from typing import List, Optional
  32. from huggingface_hub import logging
  33. from huggingface_hub.commands import BaseHuggingfaceCLICommand
  34. from huggingface_hub.hf_api import HfApi
  35. logger = logging.get_logger(__name__)
  36. class DeleteFilesSubCommand:
  37. def __init__(self, args) -> None:
  38. self.args = args
  39. self.repo_id: str = args.repo_id
  40. self.repo_type: Optional[str] = args.repo_type
  41. self.revision: Optional[str] = args.revision
  42. self.api: HfApi = HfApi(token=args.token, library_name="huggingface-cli")
  43. self.patterns: List[str] = args.patterns
  44. self.commit_message: Optional[str] = args.commit_message
  45. self.commit_description: Optional[str] = args.commit_description
  46. self.create_pr: bool = args.create_pr
  47. self.token: Optional[str] = args.token
  48. def run(self) -> None:
  49. logging.set_verbosity_info()
  50. url = self.api.delete_files(
  51. delete_patterns=self.patterns,
  52. repo_id=self.repo_id,
  53. repo_type=self.repo_type,
  54. revision=self.revision,
  55. commit_message=self.commit_message,
  56. commit_description=self.commit_description,
  57. create_pr=self.create_pr,
  58. )
  59. print(f"Files correctly deleted from repo. Commit: {url}.")
  60. logging.set_verbosity_warning()
  61. class RepoFilesCommand(BaseHuggingfaceCLICommand):
  62. @staticmethod
  63. def register_subcommand(parser: _SubParsersAction):
  64. repo_files_parser = parser.add_parser("repo-files", help="Manage files in a repo on the Hub.")
  65. repo_files_subparsers = repo_files_parser.add_subparsers(
  66. help="Action to execute against the files.",
  67. required=True,
  68. )
  69. delete_subparser = repo_files_subparsers.add_parser(
  70. "delete",
  71. help="Delete files from a repo on the Hub",
  72. )
  73. delete_subparser.set_defaults(func=lambda args: DeleteFilesSubCommand(args))
  74. delete_subparser.add_argument(
  75. "repo_id", type=str, help="The ID of the repo to manage (e.g. `username/repo-name`)."
  76. )
  77. delete_subparser.add_argument(
  78. "patterns",
  79. nargs="+",
  80. type=str,
  81. help="Glob patterns to match files to delete.",
  82. )
  83. delete_subparser.add_argument(
  84. "--repo-type",
  85. choices=["model", "dataset", "space"],
  86. default="model",
  87. help="Type of the repo to upload to (e.g. `dataset`).",
  88. )
  89. delete_subparser.add_argument(
  90. "--revision",
  91. type=str,
  92. help=(
  93. "An optional Git revision to push to. It can be a branch name "
  94. "or a PR reference. If revision does not"
  95. " exist and `--create-pr` is not set, a branch will be automatically created."
  96. ),
  97. )
  98. delete_subparser.add_argument(
  99. "--commit-message", type=str, help="The summary / title / first line of the generated commit."
  100. )
  101. delete_subparser.add_argument(
  102. "--commit-description", type=str, help="The description of the generated commit."
  103. )
  104. delete_subparser.add_argument(
  105. "--create-pr", action="store_true", help="Whether to create a new Pull Request for these changes."
  106. )
  107. delete_subparser.add_argument(
  108. "--token",
  109. type=str,
  110. help="A User Access Token generated from https://huggingface.co/settings/tokens",
  111. )
  112. repo_files_parser.set_defaults(func=RepoFilesCommand)