File size: 5,054 Bytes
783a8bf |
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 |
# coding=utf-8
# Copyright 2023-present, the HuggingFace Inc. team.
#
# 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 command to update or delete files in a repository using the CLI.
Usage:
# delete all
huggingface-cli repo-files <repo_id> delete "*"
# delete single file
huggingface-cli repo-files <repo_id> delete file.txt
# delete single folder
huggingface-cli repo-files <repo_id> delete folder/
# delete multiple
huggingface-cli repo-files <repo_id> delete file.txt folder/ file2.txt
# delete multiple patterns
huggingface-cli repo-files <repo_id> delete file.txt "*.json" "folder/*.parquet"
# delete from different revision / repo-type
huggingface-cli repo-files <repo_id> delete file.txt --revision=refs/pr/1 --repo-type=dataset
"""
from argparse import _SubParsersAction
from typing import List, Optional
from huggingface_hub import logging
from huggingface_hub.commands import BaseHuggingfaceCLICommand
from huggingface_hub.hf_api import HfApi
from ._cli_utils import show_deprecation_warning
logger = logging.get_logger(__name__)
class DeleteFilesSubCommand:
def __init__(self, args) -> None:
self.args = args
self.repo_id: str = args.repo_id
self.repo_type: Optional[str] = args.repo_type
self.revision: Optional[str] = args.revision
self.api: HfApi = HfApi(token=args.token, library_name="huggingface-cli")
self.patterns: List[str] = args.patterns
self.commit_message: Optional[str] = args.commit_message
self.commit_description: Optional[str] = args.commit_description
self.create_pr: bool = args.create_pr
self.token: Optional[str] = args.token
def run(self) -> None:
show_deprecation_warning("huggingface-cli repo-files", "hf repo-files")
logging.set_verbosity_info()
url = self.api.delete_files(
delete_patterns=self.patterns,
repo_id=self.repo_id,
repo_type=self.repo_type,
revision=self.revision,
commit_message=self.commit_message,
commit_description=self.commit_description,
create_pr=self.create_pr,
)
print(f"Files correctly deleted from repo. Commit: {url}.")
logging.set_verbosity_warning()
class RepoFilesCommand(BaseHuggingfaceCLICommand):
@staticmethod
def register_subcommand(parser: _SubParsersAction):
repo_files_parser = parser.add_parser("repo-files", help="Manage files in a repo on the Hub")
repo_files_parser.add_argument(
"repo_id", type=str, help="The ID of the repo to manage (e.g. `username/repo-name`)."
)
repo_files_subparsers = repo_files_parser.add_subparsers(
help="Action to execute against the files.",
required=True,
)
delete_subparser = repo_files_subparsers.add_parser(
"delete",
help="Delete files from a repo on the Hub",
)
delete_subparser.set_defaults(func=lambda args: DeleteFilesSubCommand(args))
delete_subparser.add_argument(
"patterns",
nargs="+",
type=str,
help="Glob patterns to match files to delete.",
)
delete_subparser.add_argument(
"--repo-type",
choices=["model", "dataset", "space"],
default="model",
help="Type of the repo to upload to (e.g. `dataset`).",
)
delete_subparser.add_argument(
"--revision",
type=str,
help=(
"An optional Git revision to push to. It can be a branch name "
"or a PR reference. If revision does not"
" exist and `--create-pr` is not set, a branch will be automatically created."
),
)
delete_subparser.add_argument(
"--commit-message", type=str, help="The summary / title / first line of the generated commit."
)
delete_subparser.add_argument(
"--commit-description", type=str, help="The description of the generated commit."
)
delete_subparser.add_argument(
"--create-pr", action="store_true", help="Whether to create a new Pull Request for these changes."
)
repo_files_parser.add_argument(
"--token",
type=str,
help="A User Access Token generated from https://huggingface.co/settings/tokens",
)
repo_files_parser.set_defaults(func=RepoFilesCommand)
|