-
Notifications
You must be signed in to change notification settings - Fork 135
/
train.py
307 lines (253 loc) · 10.9 KB
/
train.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import tarfile
import os
import subprocess
import requests
import urllib.parse
import shutil
import json
import time
from cog import BaseModel, Input, Path, Secret
from huggingface_hub import hf_hub_download
os.environ["DOWNLOAD_LATEST_WEIGHTS_MANIFEST"] = "true"
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
HF_TEMP_DIR = "TEMP_HF"
USER_MODELS_DIR = "user_models"
def is_civitai_url(url: str):
return url.startswith("https://civitai.com")
def civitai_url_with_token(url: str, civitai_api_token: Secret):
if not is_civitai_url(url):
return url
if not civitai_api_token:
return url
parsed_url = urllib.parse.urlparse(url)
query_params = urllib.parse.parse_qs(parsed_url.query)
query_params['token'] = [civitai_api_token.get_secret_value()]
new_query = urllib.parse.urlencode(query_params, doseq=True)
return urllib.parse.urlunparse(parsed_url._replace(query=new_query))
def is_huggingface_url(url: str):
return url.startswith("https://huggingface.co")
def extract_parts_from_huggingface_url(url: str):
# HUGGINGFACE_CO_URL_TEMPLATE
# https://huggingface.co/{repo_id}/resolve/{revision}/{filename}
parsed_url = urllib.parse.urlparse(url)
path_parts = parsed_url.path.split("/")
if len(path_parts) < 5:
raise ValueError(
f"HuggingFace URL does not contain enough parts to extract all required parts: {url}"
)
repo_id = f"{path_parts[1]}/{path_parts[2]}"
revision = path_parts[4]
filename_and_path = path_parts[5:]
filename = filename_and_path[-1]
return repo_id, revision, filename_and_path, filename
def get_filename_from_content_disposition(content_disposition):
filename = None
if "filename*" in content_disposition:
# Extract and decode the filename* parameter
filename_star = content_disposition.split("filename*=")[1].split(";")[0].strip()
filename = urllib.parse.unquote(filename_star.split("''")[1])
elif "filename" in content_disposition:
# Extract the filename parameter
filename = content_disposition.split("filename=")[1].split(";")[0].strip('"')
return filename
def get_filename_from_url(url, civitai_api_token: Secret = None):
if is_civitai_url(url):
url = civitai_url_with_token(url, civitai_api_token)
try:
# First try with HEAD request
response = requests.head(url, allow_redirects=True)
# Check if the response contains the Content-Disposition header
if "Content-Disposition" in response.headers:
content_disposition = response.headers["Content-Disposition"]
filename = get_filename_from_content_disposition(content_disposition)
else:
# If HEAD request fails to get filename, fall back to partial GET request
response = requests.get(
url,
headers={"Range": "bytes=0-1024"},
stream=True,
allow_redirects=True,
)
if "Content-Disposition" in response.headers:
content_disposition = response.headers["Content-Disposition"]
filename = get_filename_from_content_disposition(content_disposition)
else:
# Fallback to the last part of the URL if no Content-Disposition header is present
filename = url.split("/")[-1]
if "." not in filename:
print(f"No extension found for {filename}, assuming safetensors")
filename += ".safetensors"
print("civitai filename:", filename)
return filename
except Exception as e:
return str(e)
def download_from_civitai(
url: str,
filename: str = "checkpoint.safetensors",
civitai_api_token: Secret = None,
):
print(f"Downloading {url} to {filename}")
url = civitai_url_with_token(url, civitai_api_token)
start_time = time.time()
try:
result = subprocess.run(["pget", "-f", url, filename], timeout=600)
if result.returncode != 0:
raise RuntimeError(
"Download failed. You need to pass in a valid CivitAI API token if the download showed a 401 Unauthorized error. You can create an API key from the bottom of https://civitai.com/user/account"
)
except subprocess.TimeoutExpired:
raise RuntimeError("Download failed due to timeout")
print(f"Successfully downloaded {filename}")
end_time = time.time()
print(f"Downloaded in: {end_time - start_time:.2f} seconds")
return filename
def download_from_huggingface(
url: str,
file_type: str = "CHECKPOINTS",
huggingface_read_token: Secret = None,
):
repo_id, revision, filename_and_path, filename = extract_parts_from_huggingface_url(
url
)
start_time = time.time()
print("Downloading from HuggingFace:")
print("url:", url)
print("repo_id:", repo_id)
print("revision:", revision)
print("filename_and_path:", "/".join(filename_and_path))
print("filename:", filename)
token = (
huggingface_read_token.get_secret_value() if huggingface_read_token else False
)
hf_hub_download(
repo_id=repo_id,
revision=revision,
filename="/".join(filename_and_path),
local_dir=HF_TEMP_DIR,
token=token,
)
# Move the downloaded file from HF_TEMP_DIR to the appropriate directory
src_path = os.path.join(HF_TEMP_DIR, "/".join(filename_and_path))
dest_dir = os.path.join(USER_MODELS_DIR, file_type.lower())
os.makedirs(dest_dir, exist_ok=True)
dest_path = os.path.join(dest_dir, filename)
shutil.move(src_path, dest_path)
print(f"Successfully downloaded {filename}")
end_time = time.time()
print(f"Downloaded in: {end_time - start_time:.2f} seconds")
return filename
def clean_directories():
for dir in [HF_TEMP_DIR, USER_MODELS_DIR]:
dir = Path(dir)
if dir.exists() and dir.is_dir():
shutil.rmtree(dir)
class TrainingOutput(BaseModel):
weights: Path
def train(
checkpoints: str = Input(
description="A list of HuggingFace or CivitAI download URLs (use line breaks to upload multiples)",
default=None,
),
loras: str = Input(
description="A list of HuggingFace or CivitAI download URLs (use line breaks to upload multiples)",
default=None,
),
upscale_models: str = Input(
description="A list of HuggingFace or CivitAI download URLs (use line breaks to upload multiples)",
default=None,
),
embedding_models: str = Input(
description="A list of HuggingFace or CivitAI download URLs (use line breaks to upload multiples)",
default=None,
),
controlnets: str = Input(
description="A list of HuggingFace or CivitAI download URLs (use line breaks to upload multiples)",
default=None,
),
animatediff_models: str = Input(
description="A list of HuggingFace or CivitAI download URLs (use line breaks to upload multiples)",
default=None,
),
animatediff_loras: str = Input(
description="A list of HuggingFace or CivitAI download URLs (use line breaks to upload multiples)",
default=None,
),
huggingface_read_token: Secret = Input(
description="Optional: Your HuggingFace read token. Only needed if you are trying to download HuggingFace weights that require authentication. You can create or get a read token from https://huggingface.co/settings/tokens",
default=None,
),
civitai_api_token: Secret = Input(
description="Optional: Your CivitAI API token. Only needed if you are trying to download CivitAI weights that require authentication. You can create an API key from the bottom of https://civitai.com/user/account",
default=None,
),
) -> TrainingOutput:
clean_directories()
lists_of_urls = {
"CHECKPOINTS": checkpoints.splitlines() if checkpoints else [],
"LORAS": loras.splitlines() if loras else [],
"UPSCALE_MODELS": upscale_models.splitlines() if upscale_models else [],
"EMBEDDINGS": embedding_models.splitlines() if embedding_models else [],
"CONTROLNET": controlnets.splitlines() if controlnets else [],
"ANIMATEDIFF_MODELS": animatediff_models.splitlines()
if animatediff_models
else [],
"ANIMATEDIFF_MOTION_LORA": animatediff_loras.splitlines()
if animatediff_loras
else [],
}
lists_of_urls = {
k: [url.strip() for url in v] for k, v in lists_of_urls.items() if v
}
filenames = {}
for file_type, urls in lists_of_urls.items():
filenames[file_type] = []
for url in urls:
if not (is_huggingface_url(url) or is_civitai_url(url)):
raise ValueError("URL must be from 'huggingface.co' or 'civitai.com'")
if is_civitai_url(url):
filename = get_filename_from_url(url, civitai_api_token)
download_from_civitai(
url,
filename=f"{USER_MODELS_DIR}/{file_type.lower()}/{filename}",
civitai_api_token=civitai_api_token,
)
filenames[file_type].append(filename)
elif is_huggingface_url(url):
filename = download_from_huggingface(
url,
file_type=file_type,
huggingface_read_token=huggingface_read_token,
)
filenames[file_type].append(filename)
try:
weights_json_path = os.path.join(USER_MODELS_DIR, "weights.json")
with open(weights_json_path, "w") as json_file:
json.dump(filenames, json_file, indent=2)
except Exception as e:
raise RuntimeError(
f"No files were downloaded. Could not write weights.json: {e}"
)
# Create a tar file of the weights
tar_file_path = "weights.tar"
with tarfile.open(tar_file_path, "w") as tar:
# Add the user_models directory to the tar file
user_models_dir = Path(USER_MODELS_DIR)
if user_models_dir.exists() and user_models_dir.is_dir():
for root, _, files in os.walk(user_models_dir):
for file in files:
file_path = os.path.join(root, file)
tar.add(
file_path, arcname=os.path.relpath(file_path, user_models_dir)
)
print(f"Added {file_path} to tar file.")
tar_file_size = os.path.getsize(tar_file_path) / (1024 * 1024) # size in MB
print(f"Size of the tar file: {tar_file_size:.2f} MB")
if tar_file_size > 9000:
print("If weights are larger than ~10GB, you may find that uploads will fail.")
clean_directories()
print("====================================")
print("When using your new model, use these filenames in your JSON workflow:")
for file_type, files in filenames.items():
for filename in files:
print(filename)
return TrainingOutput(weights=Path("weights.tar"))