Spaces:
Running
Running
Upload 4 files
Browse files
convert_url_to_diffusers_sdxl_gr.py
CHANGED
@@ -3,7 +3,13 @@ from pathlib import Path
|
|
3 |
import os
|
4 |
import torch
|
5 |
from diffusers import StableDiffusionXLPipeline, AutoencoderKL
|
|
|
6 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
7 |
# also requires aria, gdown, peft, huggingface_hub, safetensors, transformers, accelerate, pytorch_lightning
|
8 |
|
9 |
|
@@ -12,15 +18,12 @@ def list_sub(a, b):
|
|
12 |
|
13 |
|
14 |
def is_repo_name(s):
|
15 |
-
import re
|
16 |
return re.fullmatch(r'^[^/,\s\"\']+/[^/,\s\"\']+$', s)
|
17 |
|
18 |
|
19 |
def split_hf_url(url: str):
|
20 |
-
import re
|
21 |
-
import urllib.parse
|
22 |
try:
|
23 |
-
s = list(re.findall(r'^(?:https?://huggingface.co/)(?:(datasets)/)?(.+?/.+?)/\w+?/.+?/(?:(.+)/)?(
|
24 |
if len(s) < 4: return "", "", "", ""
|
25 |
repo_id = s[1]
|
26 |
repo_type = "dataset" if s[0] == "datasets" else "model"
|
@@ -32,7 +35,6 @@ def split_hf_url(url: str):
|
|
32 |
|
33 |
|
34 |
def download_hf_file(directory, url, hf_token="", progress=gr.Progress(track_tqdm=True)):
|
35 |
-
from huggingface_hub import hf_hub_download
|
36 |
repo_id, filename, subfolder, repo_type = split_hf_url(url)
|
37 |
try:
|
38 |
if subfolder is not None: hf_hub_download(repo_id=repo_id, filename=filename, subfolder=subfolder, repo_type=repo_type, local_dir=directory, token=hf_token)
|
@@ -244,8 +246,8 @@ def fuse_loras(pipe, lora_dict={}, temp_dir=".", civitai_key="", hf_token=""):
|
|
244 |
return pipe
|
245 |
|
246 |
|
247 |
-
def convert_url_to_diffusers_sdxl(url, civitai_key="", hf_token="", is_upload_sf=False,
|
248 |
-
scheduler="Euler a", lora_dict={}, is_local=True, progress=gr.Progress(track_tqdm=True)):
|
249 |
progress(0, desc="Start converting...")
|
250 |
temp_dir = "."
|
251 |
new_file = get_download_file(temp_dir, url, civitai_key, hf_token)
|
@@ -254,72 +256,66 @@ def convert_url_to_diffusers_sdxl(url, civitai_key="", hf_token="", is_upload_sf
|
|
254 |
return ""
|
255 |
new_repo_name = Path(new_file).stem.replace(" ", "_").replace(",", "_").replace(".", "_") #
|
256 |
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
else:
|
264 |
-
if half:
|
265 |
-
pipe = StableDiffusionXLPipeline.from_single_file(new_file, use_safetensors=True, torch_dtype=torch.float16)
|
266 |
-
else:
|
267 |
-
pipe = StableDiffusionXLPipeline.from_single_file(new_file, use_safetensors=True)
|
268 |
|
269 |
new_vae_file = ""
|
270 |
if vae:
|
271 |
-
if is_repo_name(vae):
|
272 |
-
if half:
|
273 |
-
pipe.vae = AutoencoderKL.from_pretrained(vae, torch_dtype=torch.float16)
|
274 |
-
else:
|
275 |
-
pipe.vae = AutoencoderKL.from_pretrained(vae)
|
276 |
else:
|
277 |
new_vae_file = get_download_file(temp_dir, vae, civitai_key, hf_token)
|
278 |
-
if new_vae_file
|
279 |
-
|
280 |
-
|
281 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
282 |
|
283 |
pipe = fuse_loras(pipe, lora_dict, temp_dir, civitai_key, hf_token)
|
284 |
|
285 |
sconf = get_scheduler_config(scheduler)
|
286 |
pipe.scheduler = sconf[0].from_config(pipe.scheduler.config, **sconf[1])
|
287 |
|
288 |
-
|
289 |
-
pipe.save_pretrained(new_repo_name, safe_serialization=True, use_safetensors=True)
|
290 |
-
else:
|
291 |
-
pipe.save_pretrained(new_repo_name, safe_serialization=True, use_safetensors=True)
|
292 |
|
293 |
-
if Path(new_repo_name).exists():
|
294 |
-
save_readme_md(new_repo_name, url)
|
295 |
|
296 |
-
if not
|
297 |
-
|
298 |
-
|
299 |
-
|
|
|
|
|
300 |
|
301 |
progress(1, desc="Converted.")
|
302 |
return new_repo_name
|
303 |
|
304 |
|
305 |
def is_repo_exists(repo_id, hf_token):
|
306 |
-
from huggingface_hub import HfApi
|
307 |
api = HfApi(token=hf_token)
|
308 |
try:
|
309 |
if api.repo_exists(repo_id=repo_id): return True
|
310 |
else: return False
|
311 |
except Exception as e:
|
312 |
-
print(e)
|
313 |
-
print(f"Error: Failed to connect {repo_id}.")
|
314 |
return True # for safe
|
315 |
|
316 |
|
317 |
def create_diffusers_repo(new_repo_id, diffusers_folder, is_private, hf_token, progress=gr.Progress(track_tqdm=True)):
|
318 |
-
from huggingface_hub import HfApi
|
319 |
api = HfApi(token=hf_token)
|
320 |
try:
|
321 |
progress(0, desc="Start uploading...")
|
322 |
-
api.create_repo(repo_id=new_repo_id, token=hf_token, private=is_private)
|
323 |
for path in Path(diffusers_folder).glob("*"):
|
324 |
if path.is_dir():
|
325 |
api.upload_folder(repo_id=new_repo_id, folder_path=str(path), path_in_repo=path.name, token=hf_token)
|
@@ -328,39 +324,40 @@ def create_diffusers_repo(new_repo_id, diffusers_folder, is_private, hf_token, p
|
|
328 |
progress(1, desc="Uploaded.")
|
329 |
url = f"https://huggingface.co/{new_repo_id}"
|
330 |
except Exception as e:
|
331 |
-
print(f"Error: Failed to upload to {new_repo_id}.")
|
332 |
-
print(e)
|
333 |
return ""
|
334 |
return url
|
335 |
|
336 |
|
337 |
-
def convert_url_to_diffusers_repo(dl_url, hf_user, hf_repo, hf_token, civitai_key="", is_private=True,
|
338 |
-
|
|
|
339 |
lora4=None, lora4s=1.0, lora5=None, lora5s=1.0, progress=gr.Progress(track_tqdm=True)):
|
340 |
-
|
|
|
|
|
341 |
if not hf_user:
|
342 |
print(f"Invalid user name: {hf_user}")
|
343 |
progress(1, desc=f"Invalid user name: {hf_user}")
|
344 |
-
return gr.update(value=repo_urls, choices=repo_urls), gr.update(
|
345 |
-
if not civitai_key and os.environ.get("CIVITAI_API_KEY"): civitai_key = os.environ.get("CIVITAI_API_KEY")
|
346 |
lora_dict = {lora1: lora1s, lora2: lora2s, lora3: lora3s, lora4: lora4s, lora5: lora5s}
|
347 |
-
new_path = convert_url_to_diffusers_sdxl(dl_url, civitai_key, hf_token, is_upload_sf,
|
348 |
if not new_path: return ""
|
349 |
new_repo_id = f"{hf_user}/{Path(new_path).stem}"
|
350 |
if hf_repo != "": new_repo_id = f"{hf_user}/{hf_repo}"
|
351 |
if not is_repo_name(new_repo_id):
|
352 |
print(f"Invalid repo name: {new_repo_id}")
|
353 |
progress(1, desc=f"Invalid repo name: {new_repo_id}")
|
354 |
-
return gr.update(value=repo_urls, choices=repo_urls), gr.update(
|
355 |
-
if is_repo_exists(new_repo_id, hf_token):
|
356 |
print(f"Repo already exists: {new_repo_id}")
|
357 |
progress(1, desc=f"Repo already exists: {new_repo_id}")
|
358 |
-
return gr.update(value=repo_urls, choices=repo_urls), gr.update(
|
359 |
repo_url = create_diffusers_repo(new_repo_id, new_path, is_private, hf_token)
|
360 |
shutil.rmtree(new_path)
|
361 |
if not repo_urls: repo_urls = []
|
362 |
repo_urls.append(repo_url)
|
363 |
-
md = "Your new repo
|
364 |
for u in repo_urls:
|
365 |
md += f"[{str(u).split('/')[-2]}/{str(u).split('/')[-1]}]({str(u)})<br>"
|
366 |
return gr.update(value=repo_urls, choices=repo_urls), gr.update(value=md)
|
@@ -370,7 +367,7 @@ if __name__ == "__main__":
|
|
370 |
parser = argparse.ArgumentParser()
|
371 |
|
372 |
parser.add_argument("--url", default=None, type=str, required=True, help="URL of the model to convert.")
|
373 |
-
parser.add_argument("--
|
374 |
parser.add_argument("--scheduler", default="Euler a", type=str, choices=list(SCHEDULER_CONFIG_MAP.keys()), required=False, help="Scheduler name to use.")
|
375 |
parser.add_argument("--vae", default=None, type=str, required=False, help="URL of the VAE to use.")
|
376 |
parser.add_argument("--civitai_key", default=None, type=str, required=False, help="Civitai API Key (If you want to download file from Civitai).")
|
@@ -395,4 +392,4 @@ if __name__ == "__main__":
|
|
395 |
for p in Path(args.loras).glob('**/*.safetensors'):
|
396 |
lora_dict[str(p)] = 1.0
|
397 |
|
398 |
-
convert_url_to_diffusers_sdxl(args.url, args.civitai_key, args.
|
|
|
3 |
import os
|
4 |
import torch
|
5 |
from diffusers import StableDiffusionXLPipeline, AutoencoderKL
|
6 |
+
from transformers import CLIPTokenizer, CLIPTextModel
|
7 |
import gradio as gr
|
8 |
+
from huggingface_hub import hf_hub_download, HfApi
|
9 |
+
import urllib.parse
|
10 |
+
import re
|
11 |
+
import shutil
|
12 |
+
import gc
|
13 |
# also requires aria, gdown, peft, huggingface_hub, safetensors, transformers, accelerate, pytorch_lightning
|
14 |
|
15 |
|
|
|
18 |
|
19 |
|
20 |
def is_repo_name(s):
|
|
|
21 |
return re.fullmatch(r'^[^/,\s\"\']+/[^/,\s\"\']+$', s)
|
22 |
|
23 |
|
24 |
def split_hf_url(url: str):
|
|
|
|
|
25 |
try:
|
26 |
+
s = list(re.findall(r'^(?:https?://huggingface.co/)(?:(datasets)/)?(.+?/.+?)/\w+?/.+?/(?:(.+)/)?(.+?.\w+)(?:\?download=true)?$', url)[0])
|
27 |
if len(s) < 4: return "", "", "", ""
|
28 |
repo_id = s[1]
|
29 |
repo_type = "dataset" if s[0] == "datasets" else "model"
|
|
|
35 |
|
36 |
|
37 |
def download_hf_file(directory, url, hf_token="", progress=gr.Progress(track_tqdm=True)):
|
|
|
38 |
repo_id, filename, subfolder, repo_type = split_hf_url(url)
|
39 |
try:
|
40 |
if subfolder is not None: hf_hub_download(repo_id=repo_id, filename=filename, subfolder=subfolder, repo_type=repo_type, local_dir=directory, token=hf_token)
|
|
|
246 |
return pipe
|
247 |
|
248 |
|
249 |
+
def convert_url_to_diffusers_sdxl(url, civitai_key="", hf_token="", is_upload_sf=False, dtype="fp16", vae="",
|
250 |
+
scheduler="Euler a", lora_dict={}, is_local=True, clip="", progress=gr.Progress(track_tqdm=True)):
|
251 |
progress(0, desc="Start converting...")
|
252 |
temp_dir = "."
|
253 |
new_file = get_download_file(temp_dir, url, civitai_key, hf_token)
|
|
|
256 |
return ""
|
257 |
new_repo_name = Path(new_file).stem.replace(" ", "_").replace(",", "_").replace(".", "_") #
|
258 |
|
259 |
+
type_kwargs = {}
|
260 |
+
kwargs = {}
|
261 |
+
if dtype == "fp16": type_kwargs["torch_dtype"] = torch.float16
|
262 |
+
elif dtype == "fp32": type_kwargs["torch_dtype"] = torch.float32
|
263 |
+
elif dtype == "bf16": type_kwargs["torch_dtype"] = torch.bfloat16
|
264 |
+
elif dtype == "fp8": type_kwargs["torch_dtype"] = torch.float8_e4m3fn
|
|
|
|
|
|
|
|
|
|
|
265 |
|
266 |
new_vae_file = ""
|
267 |
if vae:
|
268 |
+
if is_repo_name(vae): my_vae = AutoencoderKL.from_pretrained(vae, **type_kwargs)
|
|
|
|
|
|
|
|
|
269 |
else:
|
270 |
new_vae_file = get_download_file(temp_dir, vae, civitai_key, hf_token)
|
271 |
+
if new_vae_file: my_vae = AutoencoderKL.from_single_file(new_vae_file, **type_kwargs)
|
272 |
+
kwargs["vae"] = my_vae
|
273 |
+
|
274 |
+
if clip:
|
275 |
+
my_tokenizer = CLIPTokenizer.from_pretrained(clip)
|
276 |
+
my_text_encoder = CLIPTextModel.from_pretrained(clip, **type_kwargs)
|
277 |
+
kwargs["tokenizer"] = my_tokenizer
|
278 |
+
kwargs["text_encoder"] = my_text_encoder
|
279 |
+
|
280 |
+
pipe = None
|
281 |
+
if is_repo_name(url): pipe = StableDiffusionXLPipeline.from_pretrained(new_file, use_safetensors=True, **kwargs, **type_kwargs)
|
282 |
+
else: pipe = StableDiffusionXLPipeline.from_single_file(new_file, use_safetensors=True, **kwargs, **type_kwargs)
|
283 |
|
284 |
pipe = fuse_loras(pipe, lora_dict, temp_dir, civitai_key, hf_token)
|
285 |
|
286 |
sconf = get_scheduler_config(scheduler)
|
287 |
pipe.scheduler = sconf[0].from_config(pipe.scheduler.config, **sconf[1])
|
288 |
|
289 |
+
pipe.save_pretrained(new_repo_name, safe_serialization=True, use_safetensors=True)
|
|
|
|
|
|
|
290 |
|
291 |
+
if Path(new_repo_name).exists(): save_readme_md(new_repo_name, url)
|
|
|
292 |
|
293 |
+
if not is_local:
|
294 |
+
if not is_repo_name(new_file) and is_upload_sf: shutil.move(str(Path(new_file).resolve()), str(Path(new_repo_name, Path(new_file).name).resolve()))
|
295 |
+
else: os.remove(new_file)
|
296 |
+
del pipe
|
297 |
+
torch.cuda.empty_cache()
|
298 |
+
gc.collect()
|
299 |
|
300 |
progress(1, desc="Converted.")
|
301 |
return new_repo_name
|
302 |
|
303 |
|
304 |
def is_repo_exists(repo_id, hf_token):
|
|
|
305 |
api = HfApi(token=hf_token)
|
306 |
try:
|
307 |
if api.repo_exists(repo_id=repo_id): return True
|
308 |
else: return False
|
309 |
except Exception as e:
|
310 |
+
print(f"Error: Failed to connect {repo_id}. {e}")
|
|
|
311 |
return True # for safe
|
312 |
|
313 |
|
314 |
def create_diffusers_repo(new_repo_id, diffusers_folder, is_private, hf_token, progress=gr.Progress(track_tqdm=True)):
|
|
|
315 |
api = HfApi(token=hf_token)
|
316 |
try:
|
317 |
progress(0, desc="Start uploading...")
|
318 |
+
api.create_repo(repo_id=new_repo_id, token=hf_token, private=is_private, exist_ok=True)
|
319 |
for path in Path(diffusers_folder).glob("*"):
|
320 |
if path.is_dir():
|
321 |
api.upload_folder(repo_id=new_repo_id, folder_path=str(path), path_in_repo=path.name, token=hf_token)
|
|
|
324 |
progress(1, desc="Uploaded.")
|
325 |
url = f"https://huggingface.co/{new_repo_id}"
|
326 |
except Exception as e:
|
327 |
+
print(f"Error: Failed to upload to {new_repo_id}. {e}")
|
|
|
328 |
return ""
|
329 |
return url
|
330 |
|
331 |
|
332 |
+
def convert_url_to_diffusers_repo(dl_url, hf_user, hf_repo, hf_token, civitai_key="", is_private=True, is_overwrite=False, is_upload_sf=False,
|
333 |
+
repo_urls=[], dtype="fp16", vae=None, clip="", scheduler="Euler a",
|
334 |
+
lora1=None, lora1s=1.0, lora2=None, lora2s=1.0, lora3=None, lora3s=1.0,
|
335 |
lora4=None, lora4s=1.0, lora5=None, lora5s=1.0, progress=gr.Progress(track_tqdm=True)):
|
336 |
+
if not civitai_key and os.environ.get("CIVITAI_API_KEY"): civitai_key = os.environ.get("CIVITAI_API_KEY") # default Civitai API key
|
337 |
+
if not hf_token and os.environ.get("HF_TOKEN"): hf_token = os.environ.get("HF_TOKEN") # default HF write token
|
338 |
+
if not hf_user and os.environ.get("HF_USER"): hf_user = os.environ.get("HF_USER") # default username
|
339 |
if not hf_user:
|
340 |
print(f"Invalid user name: {hf_user}")
|
341 |
progress(1, desc=f"Invalid user name: {hf_user}")
|
342 |
+
return gr.update(value=repo_urls, choices=repo_urls), gr.update(visible=True)
|
|
|
343 |
lora_dict = {lora1: lora1s, lora2: lora2s, lora3: lora3s, lora4: lora4s, lora5: lora5s}
|
344 |
+
new_path = convert_url_to_diffusers_sdxl(dl_url, civitai_key, hf_token, is_upload_sf, dtype, vae, scheduler, lora_dict, False, clip)
|
345 |
if not new_path: return ""
|
346 |
new_repo_id = f"{hf_user}/{Path(new_path).stem}"
|
347 |
if hf_repo != "": new_repo_id = f"{hf_user}/{hf_repo}"
|
348 |
if not is_repo_name(new_repo_id):
|
349 |
print(f"Invalid repo name: {new_repo_id}")
|
350 |
progress(1, desc=f"Invalid repo name: {new_repo_id}")
|
351 |
+
return gr.update(value=repo_urls, choices=repo_urls), gr.update(visible=True)
|
352 |
+
if not is_overwrite and is_repo_exists(new_repo_id, hf_token):
|
353 |
print(f"Repo already exists: {new_repo_id}")
|
354 |
progress(1, desc=f"Repo already exists: {new_repo_id}")
|
355 |
+
return gr.update(value=repo_urls, choices=repo_urls), gr.update(visible=True)
|
356 |
repo_url = create_diffusers_repo(new_repo_id, new_path, is_private, hf_token)
|
357 |
shutil.rmtree(new_path)
|
358 |
if not repo_urls: repo_urls = []
|
359 |
repo_urls.append(repo_url)
|
360 |
+
md = "### Your new repo:\n"
|
361 |
for u in repo_urls:
|
362 |
md += f"[{str(u).split('/')[-2]}/{str(u).split('/')[-1]}]({str(u)})<br>"
|
363 |
return gr.update(value=repo_urls, choices=repo_urls), gr.update(value=md)
|
|
|
367 |
parser = argparse.ArgumentParser()
|
368 |
|
369 |
parser.add_argument("--url", default=None, type=str, required=True, help="URL of the model to convert.")
|
370 |
+
parser.add_argument("--dtype", default="fp16", type=str, choices=["fp16", "fp32", "bf16", "fp8", "default"], help='Output data type. (Default: "fp16")')
|
371 |
parser.add_argument("--scheduler", default="Euler a", type=str, choices=list(SCHEDULER_CONFIG_MAP.keys()), required=False, help="Scheduler name to use.")
|
372 |
parser.add_argument("--vae", default=None, type=str, required=False, help="URL of the VAE to use.")
|
373 |
parser.add_argument("--civitai_key", default=None, type=str, required=False, help="Civitai API Key (If you want to download file from Civitai).")
|
|
|
392 |
for p in Path(args.loras).glob('**/*.safetensors'):
|
393 |
lora_dict[str(p)] = 1.0
|
394 |
|
395 |
+
convert_url_to_diffusers_sdxl(args.url, args.civitai_key, args.dtype, args.vae, args.scheduler, lora_dict, True)
|