huggingface-hub 0.33.5__py3-none-any.whl → 0.35.0rc0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of huggingface-hub might be problematic. Click here for more details.

Files changed (68) hide show
  1. huggingface_hub/__init__.py +487 -525
  2. huggingface_hub/_commit_api.py +21 -28
  3. huggingface_hub/_jobs_api.py +145 -0
  4. huggingface_hub/_local_folder.py +7 -1
  5. huggingface_hub/_login.py +5 -5
  6. huggingface_hub/_oauth.py +1 -1
  7. huggingface_hub/_snapshot_download.py +11 -6
  8. huggingface_hub/_upload_large_folder.py +46 -23
  9. huggingface_hub/cli/__init__.py +27 -0
  10. huggingface_hub/cli/_cli_utils.py +69 -0
  11. huggingface_hub/cli/auth.py +210 -0
  12. huggingface_hub/cli/cache.py +405 -0
  13. huggingface_hub/cli/download.py +181 -0
  14. huggingface_hub/cli/hf.py +66 -0
  15. huggingface_hub/cli/jobs.py +522 -0
  16. huggingface_hub/cli/lfs.py +198 -0
  17. huggingface_hub/cli/repo.py +243 -0
  18. huggingface_hub/cli/repo_files.py +128 -0
  19. huggingface_hub/cli/system.py +52 -0
  20. huggingface_hub/cli/upload.py +316 -0
  21. huggingface_hub/cli/upload_large_folder.py +132 -0
  22. huggingface_hub/commands/_cli_utils.py +5 -0
  23. huggingface_hub/commands/delete_cache.py +3 -1
  24. huggingface_hub/commands/download.py +4 -0
  25. huggingface_hub/commands/env.py +3 -0
  26. huggingface_hub/commands/huggingface_cli.py +2 -0
  27. huggingface_hub/commands/repo.py +4 -0
  28. huggingface_hub/commands/repo_files.py +4 -0
  29. huggingface_hub/commands/scan_cache.py +3 -1
  30. huggingface_hub/commands/tag.py +3 -1
  31. huggingface_hub/commands/upload.py +4 -0
  32. huggingface_hub/commands/upload_large_folder.py +3 -1
  33. huggingface_hub/commands/user.py +11 -1
  34. huggingface_hub/commands/version.py +3 -0
  35. huggingface_hub/constants.py +1 -0
  36. huggingface_hub/file_download.py +16 -5
  37. huggingface_hub/hf_api.py +519 -7
  38. huggingface_hub/hf_file_system.py +8 -16
  39. huggingface_hub/hub_mixin.py +3 -3
  40. huggingface_hub/inference/_client.py +38 -39
  41. huggingface_hub/inference/_common.py +38 -11
  42. huggingface_hub/inference/_generated/_async_client.py +50 -51
  43. huggingface_hub/inference/_generated/types/__init__.py +1 -0
  44. huggingface_hub/inference/_generated/types/image_to_video.py +60 -0
  45. huggingface_hub/inference/_mcp/cli.py +36 -18
  46. huggingface_hub/inference/_mcp/constants.py +8 -0
  47. huggingface_hub/inference/_mcp/types.py +3 -0
  48. huggingface_hub/inference/_providers/__init__.py +4 -1
  49. huggingface_hub/inference/_providers/_common.py +3 -6
  50. huggingface_hub/inference/_providers/fal_ai.py +85 -42
  51. huggingface_hub/inference/_providers/hf_inference.py +17 -9
  52. huggingface_hub/inference/_providers/replicate.py +19 -1
  53. huggingface_hub/keras_mixin.py +2 -2
  54. huggingface_hub/repocard.py +1 -1
  55. huggingface_hub/repository.py +2 -2
  56. huggingface_hub/utils/_auth.py +1 -1
  57. huggingface_hub/utils/_cache_manager.py +2 -2
  58. huggingface_hub/utils/_dotenv.py +51 -0
  59. huggingface_hub/utils/_headers.py +1 -1
  60. huggingface_hub/utils/_runtime.py +1 -1
  61. huggingface_hub/utils/_xet.py +6 -2
  62. huggingface_hub/utils/_xet_progress_reporting.py +141 -0
  63. {huggingface_hub-0.33.5.dist-info → huggingface_hub-0.35.0rc0.dist-info}/METADATA +7 -8
  64. {huggingface_hub-0.33.5.dist-info → huggingface_hub-0.35.0rc0.dist-info}/RECORD +68 -51
  65. {huggingface_hub-0.33.5.dist-info → huggingface_hub-0.35.0rc0.dist-info}/entry_points.txt +1 -0
  66. {huggingface_hub-0.33.5.dist-info → huggingface_hub-0.35.0rc0.dist-info}/LICENSE +0 -0
  67. {huggingface_hub-0.33.5.dist-info → huggingface_hub-0.35.0rc0.dist-info}/WHEEL +0 -0
  68. {huggingface_hub-0.33.5.dist-info → huggingface_hub-0.35.0rc0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,210 @@
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
+ """Contains commands to authenticate to the Hugging Face Hub and interact with your repositories.
15
+
16
+ Usage:
17
+ # login and save token locally.
18
+ hf auth login --token=hf_*** --add-to-git-credential
19
+
20
+ # switch between tokens
21
+ hf auth switch
22
+
23
+ # list all tokens
24
+ hf auth list
25
+
26
+ # logout from all tokens
27
+ hf auth logout
28
+
29
+ # check which account you are logged in as
30
+ hf auth whoami
31
+ """
32
+
33
+ from argparse import _SubParsersAction
34
+ from typing import List, Optional
35
+
36
+ from requests.exceptions import HTTPError
37
+
38
+ from huggingface_hub.commands import BaseHuggingfaceCLICommand
39
+ from huggingface_hub.constants import ENDPOINT
40
+ from huggingface_hub.hf_api import HfApi
41
+
42
+ from .._login import auth_list, auth_switch, login, logout
43
+ from ..utils import get_stored_tokens, get_token, logging
44
+ from ._cli_utils import ANSI
45
+
46
+
47
+ logger = logging.get_logger(__name__)
48
+
49
+ try:
50
+ from InquirerPy import inquirer
51
+ from InquirerPy.base.control import Choice
52
+
53
+ _inquirer_py_available = True
54
+ except ImportError:
55
+ _inquirer_py_available = False
56
+
57
+
58
+ class AuthCommands(BaseHuggingfaceCLICommand):
59
+ @staticmethod
60
+ def register_subcommand(parser: _SubParsersAction):
61
+ # Create the main 'auth' command
62
+ auth_parser = parser.add_parser("auth", help="Manage authentication (login, logout, etc.).")
63
+ auth_subparsers = auth_parser.add_subparsers(help="Authentication subcommands")
64
+
65
+ # Add 'login' as a subcommand of 'auth'
66
+ login_parser = auth_subparsers.add_parser(
67
+ "login", help="Log in using a token from huggingface.co/settings/tokens"
68
+ )
69
+ login_parser.add_argument(
70
+ "--token",
71
+ type=str,
72
+ help="Token generated from https://huggingface.co/settings/tokens",
73
+ )
74
+ login_parser.add_argument(
75
+ "--add-to-git-credential",
76
+ action="store_true",
77
+ help="Optional: Save token to git credential helper.",
78
+ )
79
+ login_parser.set_defaults(func=lambda args: AuthLogin(args))
80
+
81
+ # Add 'logout' as a subcommand of 'auth'
82
+ logout_parser = auth_subparsers.add_parser("logout", help="Log out")
83
+ logout_parser.add_argument(
84
+ "--token-name",
85
+ type=str,
86
+ help="Optional: Name of the access token to log out from.",
87
+ )
88
+ logout_parser.set_defaults(func=lambda args: AuthLogout(args))
89
+
90
+ # Add 'whoami' as a subcommand of 'auth'
91
+ whoami_parser = auth_subparsers.add_parser(
92
+ "whoami", help="Find out which huggingface.co account you are logged in as."
93
+ )
94
+ whoami_parser.set_defaults(func=lambda args: AuthWhoami(args))
95
+
96
+ # Existing subcommands
97
+ auth_switch_parser = auth_subparsers.add_parser("switch", help="Switch between access tokens")
98
+ auth_switch_parser.add_argument(
99
+ "--token-name",
100
+ type=str,
101
+ help="Optional: Name of the access token to switch to.",
102
+ )
103
+ auth_switch_parser.add_argument(
104
+ "--add-to-git-credential",
105
+ action="store_true",
106
+ help="Optional: Save token to git credential helper.",
107
+ )
108
+ auth_switch_parser.set_defaults(func=lambda args: AuthSwitch(args))
109
+
110
+ auth_list_parser = auth_subparsers.add_parser("list", help="List all stored access tokens")
111
+ auth_list_parser.set_defaults(func=lambda args: AuthList(args))
112
+
113
+
114
+ class BaseAuthCommand:
115
+ def __init__(self, args):
116
+ self.args = args
117
+ self._api = HfApi()
118
+
119
+
120
+ class AuthLogin(BaseAuthCommand):
121
+ def run(self):
122
+ logging.set_verbosity_info()
123
+ login(
124
+ token=self.args.token,
125
+ add_to_git_credential=self.args.add_to_git_credential,
126
+ )
127
+
128
+
129
+ class AuthLogout(BaseAuthCommand):
130
+ def run(self):
131
+ logging.set_verbosity_info()
132
+ logout(token_name=self.args.token_name)
133
+
134
+
135
+ class AuthSwitch(BaseAuthCommand):
136
+ def run(self):
137
+ logging.set_verbosity_info()
138
+ token_name = self.args.token_name
139
+ if token_name is None:
140
+ token_name = self._select_token_name()
141
+
142
+ if token_name is None:
143
+ print("No token name provided. Aborting.")
144
+ exit()
145
+ auth_switch(token_name, add_to_git_credential=self.args.add_to_git_credential)
146
+
147
+ def _select_token_name(self) -> Optional[str]:
148
+ token_names = list(get_stored_tokens().keys())
149
+
150
+ if not token_names:
151
+ logger.error("No stored tokens found. Please login first.")
152
+ return None
153
+
154
+ if _inquirer_py_available:
155
+ return self._select_token_name_tui(token_names)
156
+ # if inquirer is not available, use a simpler terminal UI
157
+ print("Available stored tokens:")
158
+ for i, token_name in enumerate(token_names, 1):
159
+ print(f"{i}. {token_name}")
160
+ while True:
161
+ try:
162
+ choice = input("Enter the number of the token to switch to (or 'q' to quit): ")
163
+ if choice.lower() == "q":
164
+ return None
165
+ index = int(choice) - 1
166
+ if 0 <= index < len(token_names):
167
+ return token_names[index]
168
+ else:
169
+ print("Invalid selection. Please try again.")
170
+ except ValueError:
171
+ print("Invalid input. Please enter a number or 'q' to quit.")
172
+
173
+ def _select_token_name_tui(self, token_names: List[str]) -> Optional[str]:
174
+ choices = [Choice(token_name, name=token_name) for token_name in token_names]
175
+ try:
176
+ return inquirer.select(
177
+ message="Select a token to switch to:",
178
+ choices=choices,
179
+ default=None,
180
+ ).execute()
181
+ except KeyboardInterrupt:
182
+ logger.info("Token selection cancelled.")
183
+ return None
184
+
185
+
186
+ class AuthList(BaseAuthCommand):
187
+ def run(self):
188
+ logging.set_verbosity_info()
189
+ auth_list()
190
+
191
+
192
+ class AuthWhoami(BaseAuthCommand):
193
+ def run(self):
194
+ token = get_token()
195
+ if token is None:
196
+ print("Not logged in")
197
+ exit()
198
+ try:
199
+ info = self._api.whoami(token)
200
+ print(info["name"])
201
+ orgs = [org["name"] for org in info["orgs"]]
202
+ if orgs:
203
+ print(ANSI.bold("orgs: "), ",".join(orgs))
204
+
205
+ if ENDPOINT != "https://huggingface.co":
206
+ print(f"Authenticated through private endpoint: {ENDPOINT}")
207
+ except HTTPError as e:
208
+ print(e)
209
+ print(ANSI.red(e.response.text))
210
+ exit(1)
@@ -0,0 +1,405 @@
1
+ # coding=utf-8
2
+ # Copyright 2025-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 the 'hf cache' command group with 'scan' and 'delete' subcommands."""
16
+
17
+ import os
18
+ import time
19
+ from argparse import Namespace, _SubParsersAction
20
+ from functools import wraps
21
+ from tempfile import mkstemp
22
+ from typing import Any, Callable, Iterable, List, Literal, Optional, Union
23
+
24
+ from ..utils import (
25
+ CachedRepoInfo,
26
+ CachedRevisionInfo,
27
+ CacheNotFound,
28
+ HFCacheInfo,
29
+ scan_cache_dir,
30
+ )
31
+ from . import BaseHuggingfaceCLICommand
32
+ from ._cli_utils import ANSI, tabulate
33
+
34
+
35
+ # --- DELETE helpers (from delete_cache.py) ---
36
+ try:
37
+ from InquirerPy import inquirer
38
+ from InquirerPy.base.control import Choice
39
+ from InquirerPy.separator import Separator
40
+
41
+ _inquirer_py_available = True
42
+ except ImportError:
43
+ _inquirer_py_available = False
44
+
45
+ SortingOption_T = Literal["alphabetical", "lastUpdated", "lastUsed", "size"]
46
+ _CANCEL_DELETION_STR = "CANCEL_DELETION"
47
+
48
+
49
+ def require_inquirer_py(fn: Callable) -> Callable:
50
+ @wraps(fn)
51
+ def _inner(*args, **kwargs):
52
+ if not _inquirer_py_available:
53
+ raise ImportError(
54
+ "The 'cache delete' command requires extra dependencies for the TUI.\n"
55
+ "Please run 'pip install huggingface_hub[cli]' to install them.\n"
56
+ "Otherwise, disable TUI using the '--disable-tui' flag."
57
+ )
58
+ return fn(*args, **kwargs)
59
+
60
+ return _inner
61
+
62
+
63
+ class CacheCommand(BaseHuggingfaceCLICommand):
64
+ @staticmethod
65
+ def register_subcommand(parser: _SubParsersAction):
66
+ cache_parser = parser.add_parser("cache", help="Manage local cache directory.")
67
+ cache_subparsers = cache_parser.add_subparsers(dest="cache_command", help="Cache subcommands")
68
+ # Scan subcommand
69
+ scan_parser = cache_subparsers.add_parser("scan", help="Scan cache directory.")
70
+ scan_parser.add_argument(
71
+ "--dir",
72
+ type=str,
73
+ default=None,
74
+ help="cache directory to scan (optional). Default to the default HuggingFace cache.",
75
+ )
76
+ scan_parser.add_argument(
77
+ "-v",
78
+ "--verbose",
79
+ action="count",
80
+ default=0,
81
+ help="show a more verbose output",
82
+ )
83
+ scan_parser.set_defaults(func=CacheCommand, cache_command="scan")
84
+ # Delete subcommand
85
+ delete_parser = cache_subparsers.add_parser("delete", help="Delete revisions from the cache directory.")
86
+ delete_parser.add_argument(
87
+ "--dir",
88
+ type=str,
89
+ default=None,
90
+ help="cache directory (optional). Default to the default HuggingFace cache.",
91
+ )
92
+ delete_parser.add_argument(
93
+ "--disable-tui",
94
+ action="store_true",
95
+ help=(
96
+ "Disable Terminal User Interface (TUI) mode. Useful if your platform/terminal doesn't support the multiselect menu."
97
+ ),
98
+ )
99
+ delete_parser.add_argument(
100
+ "--sort",
101
+ nargs="?",
102
+ choices=["alphabetical", "lastUpdated", "lastUsed", "size"],
103
+ help=(
104
+ "Sort repositories by the specified criteria. Options: "
105
+ "'alphabetical' (A-Z), "
106
+ "'lastUpdated' (newest first), "
107
+ "'lastUsed' (most recent first), "
108
+ "'size' (largest first)."
109
+ ),
110
+ )
111
+ delete_parser.set_defaults(func=CacheCommand, cache_command="delete")
112
+
113
+ def __init__(self, args: Namespace) -> None:
114
+ self.args = args
115
+ self.verbosity: int = getattr(args, "verbose", 0)
116
+ self.cache_dir: Optional[str] = getattr(args, "dir", None)
117
+ self.disable_tui: bool = getattr(args, "disable_tui", False)
118
+ self.sort_by: Optional[SortingOption_T] = getattr(args, "sort", None)
119
+ self.cache_command: Optional[str] = getattr(args, "cache_command", None)
120
+
121
+ def run(self):
122
+ if self.cache_command == "scan":
123
+ self._run_scan()
124
+ elif self.cache_command == "delete":
125
+ self._run_delete()
126
+ else:
127
+ print("Please specify a cache subcommand (scan or delete). Use -h for help.")
128
+
129
+ def _run_scan(self):
130
+ try:
131
+ t0 = time.time()
132
+ hf_cache_info = scan_cache_dir(self.cache_dir)
133
+ t1 = time.time()
134
+ except CacheNotFound as exc:
135
+ cache_dir = exc.cache_dir
136
+ print(f"Cache directory not found: {cache_dir}")
137
+ return
138
+ print(get_table(hf_cache_info, verbosity=self.verbosity))
139
+ print(
140
+ f"\nDone in {round(t1 - t0, 1)}s. Scanned {len(hf_cache_info.repos)} repo(s)"
141
+ f" for a total of {ANSI.red(hf_cache_info.size_on_disk_str)}."
142
+ )
143
+ if len(hf_cache_info.warnings) > 0:
144
+ message = f"Got {len(hf_cache_info.warnings)} warning(s) while scanning."
145
+ if self.verbosity >= 3:
146
+ print(ANSI.gray(message))
147
+ for warning in hf_cache_info.warnings:
148
+ print(ANSI.gray(warning))
149
+ else:
150
+ print(ANSI.gray(message + " Use -vvv to print details."))
151
+
152
+ def _run_delete(self):
153
+ hf_cache_info = scan_cache_dir(self.cache_dir)
154
+ if self.disable_tui:
155
+ selected_hashes = _manual_review_no_tui(hf_cache_info, preselected=[], sort_by=self.sort_by)
156
+ else:
157
+ selected_hashes = _manual_review_tui(hf_cache_info, preselected=[], sort_by=self.sort_by)
158
+ if len(selected_hashes) > 0 and _CANCEL_DELETION_STR not in selected_hashes:
159
+ confirm_message = _get_expectations_str(hf_cache_info, selected_hashes) + " Confirm deletion ?"
160
+ if self.disable_tui:
161
+ confirmed = _ask_for_confirmation_no_tui(confirm_message)
162
+ else:
163
+ confirmed = _ask_for_confirmation_tui(confirm_message)
164
+ if confirmed:
165
+ strategy = hf_cache_info.delete_revisions(*selected_hashes)
166
+ print("Start deletion.")
167
+ strategy.execute()
168
+ print(
169
+ f"Done. Deleted {len(strategy.repos)} repo(s) and"
170
+ f" {len(strategy.snapshots)} revision(s) for a total of"
171
+ f" {strategy.expected_freed_size_str}."
172
+ )
173
+ return
174
+ print("Deletion is cancelled. Do nothing.")
175
+
176
+
177
+ def get_table(hf_cache_info: HFCacheInfo, *, verbosity: int = 0) -> str:
178
+ if verbosity == 0:
179
+ return tabulate(
180
+ rows=[
181
+ [
182
+ repo.repo_id,
183
+ repo.repo_type,
184
+ "{:>12}".format(repo.size_on_disk_str),
185
+ repo.nb_files,
186
+ repo.last_accessed_str,
187
+ repo.last_modified_str,
188
+ ", ".join(sorted(repo.refs)),
189
+ str(repo.repo_path),
190
+ ]
191
+ for repo in sorted(hf_cache_info.repos, key=lambda repo: repo.repo_path)
192
+ ],
193
+ headers=[
194
+ "REPO ID",
195
+ "REPO TYPE",
196
+ "SIZE ON DISK",
197
+ "NB FILES",
198
+ "LAST_ACCESSED",
199
+ "LAST_MODIFIED",
200
+ "REFS",
201
+ "LOCAL PATH",
202
+ ],
203
+ )
204
+ else:
205
+ return tabulate(
206
+ rows=[
207
+ [
208
+ repo.repo_id,
209
+ repo.repo_type,
210
+ revision.commit_hash,
211
+ "{:>12}".format(revision.size_on_disk_str),
212
+ revision.nb_files,
213
+ revision.last_modified_str,
214
+ ", ".join(sorted(revision.refs)),
215
+ str(revision.snapshot_path),
216
+ ]
217
+ for repo in sorted(hf_cache_info.repos, key=lambda repo: repo.repo_path)
218
+ for revision in sorted(repo.revisions, key=lambda revision: revision.commit_hash)
219
+ ],
220
+ headers=[
221
+ "REPO ID",
222
+ "REPO TYPE",
223
+ "REVISION",
224
+ "SIZE ON DISK",
225
+ "NB FILES",
226
+ "LAST_MODIFIED",
227
+ "REFS",
228
+ "LOCAL PATH",
229
+ ],
230
+ )
231
+
232
+
233
+ def _get_repo_sorting_key(repo: CachedRepoInfo, sort_by: Optional[SortingOption_T] = None):
234
+ if sort_by == "alphabetical":
235
+ return (repo.repo_type, repo.repo_id.lower())
236
+ elif sort_by == "lastUpdated":
237
+ return -max(rev.last_modified for rev in repo.revisions)
238
+ elif sort_by == "lastUsed":
239
+ return -repo.last_accessed
240
+ elif sort_by == "size":
241
+ return -repo.size_on_disk
242
+ else:
243
+ return (repo.repo_type, repo.repo_id)
244
+
245
+
246
+ @require_inquirer_py
247
+ def _manual_review_tui(
248
+ hf_cache_info: HFCacheInfo, preselected: List[str], sort_by: Optional[SortingOption_T] = None
249
+ ) -> List[str]:
250
+ choices = _get_tui_choices_from_scan(repos=hf_cache_info.repos, preselected=preselected, sort_by=sort_by)
251
+ checkbox = inquirer.checkbox(
252
+ message="Select revisions to delete:",
253
+ choices=choices,
254
+ cycle=False,
255
+ height=100,
256
+ instruction=_get_expectations_str(
257
+ hf_cache_info, selected_hashes=[c.value for c in choices if isinstance(c, Choice) and c.enabled]
258
+ ),
259
+ long_instruction="Press <space> to select, <enter> to validate and <ctrl+c> to quit without modification.",
260
+ transformer=lambda result: f"{len(result)} revision(s) selected.",
261
+ )
262
+
263
+ def _update_expectations(_):
264
+ checkbox._instruction = _get_expectations_str(
265
+ hf_cache_info,
266
+ selected_hashes=[choice["value"] for choice in checkbox.content_control.choices if choice["enabled"]],
267
+ )
268
+
269
+ checkbox.kb_func_lookup["toggle"].append({"func": _update_expectations})
270
+ try:
271
+ return checkbox.execute()
272
+ except KeyboardInterrupt:
273
+ return []
274
+
275
+
276
+ @require_inquirer_py
277
+ def _ask_for_confirmation_tui(message: str, default: bool = True) -> bool:
278
+ return inquirer.confirm(message, default=default).execute()
279
+
280
+
281
+ def _get_tui_choices_from_scan(
282
+ repos: Iterable[CachedRepoInfo], preselected: List[str], sort_by: Optional[SortingOption_T] = None
283
+ ) -> List:
284
+ choices: List[Union["Choice", "Separator"]] = []
285
+ choices.append(
286
+ Choice(
287
+ _CANCEL_DELETION_STR, name="None of the following (if selected, nothing will be deleted).", enabled=False
288
+ )
289
+ )
290
+ sorted_repos = sorted(repos, key=lambda repo: _get_repo_sorting_key(repo, sort_by))
291
+ for repo in sorted_repos:
292
+ choices.append(
293
+ Separator(
294
+ f"\n{repo.repo_type.capitalize()} {repo.repo_id} ({repo.size_on_disk_str}, used {repo.last_accessed_str})"
295
+ )
296
+ )
297
+ for revision in sorted(repo.revisions, key=_revision_sorting_order):
298
+ choices.append(
299
+ Choice(
300
+ revision.commit_hash,
301
+ name=(
302
+ f"{revision.commit_hash[:8]}: {', '.join(sorted(revision.refs)) or '(detached)'} # modified {revision.last_modified_str}"
303
+ ),
304
+ enabled=revision.commit_hash in preselected,
305
+ )
306
+ )
307
+ return choices
308
+
309
+
310
+ def _manual_review_no_tui(
311
+ hf_cache_info: HFCacheInfo, preselected: List[str], sort_by: Optional[SortingOption_T] = None
312
+ ) -> List[str]:
313
+ fd, tmp_path = mkstemp(suffix=".txt")
314
+ os.close(fd)
315
+ lines = []
316
+ sorted_repos = sorted(hf_cache_info.repos, key=lambda repo: _get_repo_sorting_key(repo, sort_by))
317
+ for repo in sorted_repos:
318
+ lines.append(
319
+ f"\n# {repo.repo_type.capitalize()} {repo.repo_id} ({repo.size_on_disk_str}, used {repo.last_accessed_str})"
320
+ )
321
+ for revision in sorted(repo.revisions, key=_revision_sorting_order):
322
+ lines.append(
323
+ f"{'' if revision.commit_hash in preselected else '#'} {revision.commit_hash} # Refs: {', '.join(sorted(revision.refs)) or '(detached)'} # modified {revision.last_modified_str}"
324
+ )
325
+ with open(tmp_path, "w") as f:
326
+ f.write(_MANUAL_REVIEW_NO_TUI_INSTRUCTIONS)
327
+ f.write("\n".join(lines))
328
+ instructions = f"""
329
+ TUI is disabled. In order to select which revisions you want to delete, please edit
330
+ the following file using the text editor of your choice. Instructions for manual
331
+ editing are located at the beginning of the file. Edit the file, save it and confirm
332
+ to continue.
333
+ File to edit: {ANSI.bold(tmp_path)}
334
+ """
335
+ print("\n".join(line.strip() for line in instructions.strip().split("\n")))
336
+ while True:
337
+ selected_hashes = _read_manual_review_tmp_file(tmp_path)
338
+ if _ask_for_confirmation_no_tui(
339
+ _get_expectations_str(hf_cache_info, selected_hashes) + " Continue ?", default=False
340
+ ):
341
+ break
342
+ os.remove(tmp_path)
343
+ return sorted(selected_hashes)
344
+
345
+
346
+ def _ask_for_confirmation_no_tui(message: str, default: bool = True) -> bool:
347
+ YES = ("y", "yes", "1")
348
+ NO = ("n", "no", "0")
349
+ DEFAULT = ""
350
+ ALL = YES + NO + (DEFAULT,)
351
+ full_message = message + (" (Y/n) " if default else " (y/N) ")
352
+ while True:
353
+ answer = input(full_message).lower()
354
+ if answer == DEFAULT:
355
+ return default
356
+ if answer in YES:
357
+ return True
358
+ if answer in NO:
359
+ return False
360
+ print(f"Invalid input. Must be one of {ALL}")
361
+
362
+
363
+ def _get_expectations_str(hf_cache_info: HFCacheInfo, selected_hashes: List[str]) -> str:
364
+ if _CANCEL_DELETION_STR in selected_hashes:
365
+ return "Nothing will be deleted."
366
+ strategy = hf_cache_info.delete_revisions(*selected_hashes)
367
+ return f"{len(selected_hashes)} revisions selected counting for {strategy.expected_freed_size_str}."
368
+
369
+
370
+ def _read_manual_review_tmp_file(tmp_path: str) -> List[str]:
371
+ with open(tmp_path) as f:
372
+ content = f.read()
373
+ lines = [line.strip() for line in content.split("\n")]
374
+ selected_lines = [line for line in lines if not line.startswith("#")]
375
+ selected_hashes = [line.split("#")[0].strip() for line in selected_lines]
376
+ return [hash for hash in selected_hashes if len(hash) > 0]
377
+
378
+
379
+ _MANUAL_REVIEW_NO_TUI_INSTRUCTIONS = f"""
380
+ # INSTRUCTIONS
381
+ # ------------
382
+ # This is a temporary file created by running `hf cache delete --disable-tui`. It contains a set of revisions that can be deleted from your local cache directory.
383
+ #
384
+ # Please manually review the revisions you want to delete:
385
+ # - Revision hashes can be commented out with '#'.
386
+ # - Only non-commented revisions in this file will be deleted.
387
+ # - Revision hashes that are removed from this file are ignored as well.
388
+ # - If `{_CANCEL_DELETION_STR}` line is uncommented, the all cache deletion is cancelled and no changes will be applied.
389
+ #
390
+ # Once you've manually reviewed this file, please confirm deletion in the terminal. This file will be automatically removed once done.
391
+ # ------------
392
+
393
+ # KILL SWITCH
394
+ # ------------
395
+ # Un-comment following line to completely cancel the deletion process
396
+ # {_CANCEL_DELETION_STR}
397
+ # ------------
398
+
399
+ # REVISIONS
400
+ # ------------
401
+ """.strip()
402
+
403
+
404
+ def _revision_sorting_order(revision: CachedRevisionInfo) -> Any:
405
+ return revision.last_modified