|
15 | 15 |
|
16 | 16 |
|
17 | 17 | import os |
18 | | -import shutil |
19 | 18 | import sys |
20 | 19 | from pathlib import Path |
21 | 20 | from typing import Dict, Optional, Union |
22 | 21 | from uuid import uuid4 |
23 | 22 |
|
24 | | -from huggingface_hub import HfFolder, Repository, whoami |
| 23 | +from huggingface_hub import HfFolder, whoami |
25 | 24 |
|
26 | 25 | from . import __version__ |
27 | | -from .utils import ENV_VARS_TRUE_VALUES, deprecate, logging |
| 26 | +from .utils import ENV_VARS_TRUE_VALUES, logging |
28 | 27 | from .utils.import_utils import ( |
29 | 28 | _flax_version, |
30 | 29 | _jax_version, |
@@ -83,121 +82,6 @@ def get_full_repo_name(model_id: str, organization: Optional[str] = None, token: |
83 | 82 | return f"{organization}/{model_id}" |
84 | 83 |
|
85 | 84 |
|
86 | | -def init_git_repo(args, at_init: bool = False): |
87 | | - """ |
88 | | - Args: |
89 | | - Initializes a git repo in `args.hub_model_id`. |
90 | | - at_init (`bool`, *optional*, defaults to `False`): |
91 | | - Whether this function is called before any training or not. If `self.args.overwrite_output_dir` is `True` |
92 | | - and `at_init` is `True`, the path to the repo (which is `self.args.output_dir`) might be wiped out. |
93 | | - """ |
94 | | - deprecation_message = ( |
95 | | - "Please use `huggingface_hub.Repository`. " |
96 | | - "See `examples/unconditional_image_generation/train_unconditional.py` for an example." |
97 | | - ) |
98 | | - deprecate("init_git_repo()", "0.10.0", deprecation_message) |
99 | | - |
100 | | - if hasattr(args, "local_rank") and args.local_rank not in [-1, 0]: |
101 | | - return |
102 | | - hub_token = args.hub_token if hasattr(args, "hub_token") else None |
103 | | - use_auth_token = True if hub_token is None else hub_token |
104 | | - if not hasattr(args, "hub_model_id") or args.hub_model_id is None: |
105 | | - repo_name = Path(args.output_dir).absolute().name |
106 | | - else: |
107 | | - repo_name = args.hub_model_id |
108 | | - if "/" not in repo_name: |
109 | | - repo_name = get_full_repo_name(repo_name, token=hub_token) |
110 | | - |
111 | | - try: |
112 | | - repo = Repository( |
113 | | - args.output_dir, |
114 | | - clone_from=repo_name, |
115 | | - use_auth_token=use_auth_token, |
116 | | - private=args.hub_private_repo, |
117 | | - ) |
118 | | - except EnvironmentError: |
119 | | - if args.overwrite_output_dir and at_init: |
120 | | - # Try again after wiping output_dir |
121 | | - shutil.rmtree(args.output_dir) |
122 | | - repo = Repository( |
123 | | - args.output_dir, |
124 | | - clone_from=repo_name, |
125 | | - use_auth_token=use_auth_token, |
126 | | - ) |
127 | | - else: |
128 | | - raise |
129 | | - |
130 | | - repo.git_pull() |
131 | | - |
132 | | - # By default, ignore the checkpoint folders |
133 | | - if not os.path.exists(os.path.join(args.output_dir, ".gitignore")): |
134 | | - with open(os.path.join(args.output_dir, ".gitignore"), "w", encoding="utf-8") as writer: |
135 | | - writer.writelines(["checkpoint-*/"]) |
136 | | - |
137 | | - return repo |
138 | | - |
139 | | - |
140 | | -def push_to_hub( |
141 | | - args, |
142 | | - pipeline, |
143 | | - repo: Repository, |
144 | | - commit_message: Optional[str] = "End of training", |
145 | | - blocking: bool = True, |
146 | | - **kwargs, |
147 | | -) -> str: |
148 | | - """ |
149 | | - Parameters: |
150 | | - Upload *self.model* and *self.tokenizer* to the 🤗 model hub on the repo *self.args.hub_model_id*. |
151 | | - commit_message (`str`, *optional*, defaults to `"End of training"`): |
152 | | - Message to commit while pushing. |
153 | | - blocking (`bool`, *optional*, defaults to `True`): |
154 | | - Whether the function should return only when the `git push` has finished. |
155 | | - kwargs: |
156 | | - Additional keyword arguments passed along to [`create_model_card`]. |
157 | | - Returns: |
158 | | - The url of the commit of your model in the given repository if `blocking=False`, a tuple with the url of the |
159 | | - commit and an object to track the progress of the commit if `blocking=True` |
160 | | - """ |
161 | | - deprecation_message = ( |
162 | | - "Please use `huggingface_hub.Repository` and `Repository.push_to_hub()`. " |
163 | | - "See `examples/unconditional_image_generation/train_unconditional.py` for an example." |
164 | | - ) |
165 | | - deprecate("push_to_hub()", "0.10.0", deprecation_message) |
166 | | - |
167 | | - if not hasattr(args, "hub_model_id") or args.hub_model_id is None: |
168 | | - model_name = Path(args.output_dir).name |
169 | | - else: |
170 | | - model_name = args.hub_model_id.split("/")[-1] |
171 | | - |
172 | | - output_dir = args.output_dir |
173 | | - os.makedirs(output_dir, exist_ok=True) |
174 | | - logger.info(f"Saving pipeline checkpoint to {output_dir}") |
175 | | - pipeline.save_pretrained(output_dir) |
176 | | - |
177 | | - # Only push from one node. |
178 | | - if hasattr(args, "local_rank") and args.local_rank not in [-1, 0]: |
179 | | - return |
180 | | - |
181 | | - # Cancel any async push in progress if blocking=True. The commits will all be pushed together. |
182 | | - if ( |
183 | | - blocking |
184 | | - and len(repo.command_queue) > 0 |
185 | | - and repo.command_queue[-1] is not None |
186 | | - and not repo.command_queue[-1].is_done |
187 | | - ): |
188 | | - repo.command_queue[-1]._process.kill() |
189 | | - |
190 | | - git_head_commit_url = repo.push_to_hub(commit_message=commit_message, blocking=blocking, auto_lfs_prune=True) |
191 | | - # push separately the model card to be independent from the rest of the model |
192 | | - create_model_card(args, model_name=model_name) |
193 | | - try: |
194 | | - repo.push_to_hub(commit_message="update model card README.md", blocking=blocking, auto_lfs_prune=True) |
195 | | - except EnvironmentError as exc: |
196 | | - logger.error(f"Error pushing update to the model card. Please read logs and retry.\n${exc}") |
197 | | - |
198 | | - return git_head_commit_url |
199 | | - |
200 | | - |
201 | 85 | def create_model_card(args, model_name): |
202 | 86 | if not is_modelcards_available: |
203 | 87 | raise ValueError( |
|
0 commit comments