John6666 commited on
Commit
563adff
1 Parent(s): 2ae2bdc

Upload 7 files

Browse files
README.md CHANGED
@@ -1,12 +1,12 @@
1
- ---
2
- title: Convert Repo To Safetensors Sdxl Lora
3
- emoji: 🏢
4
- colorFrom: indigo
5
- colorTo: red
6
- sdk: gradio
7
- sdk_version: 4.39.0
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ ---
2
+ title: Convert diffusers SDXL LoRA repo to WebUI single Safetensors
3
+ emoji: 🐶
4
+ colorFrom: yellow
5
+ colorTo: red
6
+ sdk: gradio
7
+ sdk_version: 4.38.1
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from convert_repo_to_safetensors_sdxl_lora_gr import convert_repo_to_safetensors_sdxl_lora_multi
4
+
5
+ css = """"""
6
+
7
+ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", css=css) as demo:
8
+ with gr.Column():
9
+ repo_id = gr.Textbox(label="Repo ID", placeholder="author/model", value="", lines=1)
10
+ run_button = gr.Button(value="Convert")
11
+ st_file = gr.Files(label="Output", interactive=False)
12
+
13
+ gr.on(
14
+ triggers=[repo_id.submit, run_button.click],
15
+ fn=convert_repo_to_safetensors_sdxl_lora_multi,
16
+ inputs=[repo_id, st_file],
17
+ outputs=[st_file],
18
+ )
19
+
20
+ demo.queue()
21
+ demo.launch()
convert_repo_to_safetensors_sdxl_lora.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Script for converting a Hugging Face Diffusers trained SDXL LoRAs to Kohya format
2
+ # This means that you can input your diffusers-trained LoRAs and
3
+ # Get the output to work with WebUIs such as AUTOMATIC1111, ComfyUI, SD.Next and others.
4
+
5
+ # To get started you can find some cool `diffusers` trained LoRAs such as this cute Corgy
6
+ # https://huggingface.co/ignasbud/corgy_dog_LoRA/, download its `pytorch_lora_weights.safetensors` file
7
+ # and run the script:
8
+ # python convert_diffusers_sdxl_lora_to_webui.py --input_lora pytorch_lora_weights.safetensors --output_lora corgy.safetensors
9
+ # now you can use corgy.safetensors in your WebUI of choice!
10
+
11
+ # To train your own, here are some diffusers training scripts and utils that you can use and then convert:
12
+ # LoRA Ease - no code SDXL Dreambooth LoRA trainer: https://huggingface.co/spaces/multimodalart/lora-ease
13
+ # Dreambooth Advanced Training Script - state of the art techniques such as pivotal tuning and prodigy optimizer:
14
+ # - Script: https://github.com/huggingface/diffusers/blob/main/examples/advanced_diffusion_training/train_dreambooth_lora_sdxl_advanced.py
15
+ # - Colab (only on Pro): https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/SDXL_Dreambooth_LoRA_advanced_example.ipynb
16
+ # Canonical diffusers training scripts:
17
+ # - Script: https://github.com/huggingface/diffusers/blob/main/examples/dreambooth/train_dreambooth_lora_sdxl.py
18
+ # - Colab (runs on free tier): https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/SDXL_DreamBooth_LoRA_.ipynb
19
+
20
+ import argparse
21
+ import os
22
+
23
+ from safetensors.torch import load_file, save_file
24
+ from diffusers.utils import convert_all_state_dict_to_peft, convert_state_dict_to_kohya
25
+ from pathlib import Path
26
+
27
+ def convert_and_save(input_lora, output_lora=None):
28
+ if output_lora is None:
29
+ base_name = os.path.splitext(input_lora)[0]
30
+ output_lora = f"{base_name}_webui.safetensors"
31
+
32
+ diffusers_state_dict = load_file(input_lora)
33
+ peft_state_dict = convert_all_state_dict_to_peft(diffusers_state_dict)
34
+ kohya_state_dict = convert_state_dict_to_kohya(peft_state_dict)
35
+ save_file(kohya_state_dict, output_lora)
36
+
37
+
38
+ def download_repo_lora(repo_id, local_file):
39
+ from huggingface_hub import hf_hub_download, HfApi
40
+ lora_filename = "pytorch_lora_weights.safetensors"
41
+ lora_path = Path(lora_filename)
42
+ api = HfApi()
43
+ try:
44
+ if not api.file_exists(repo_id=repo_id, filename=lora_filename):
45
+ print(f"Error: This repo isn't diffusers LoRA repo: {repo_id}. ")
46
+ return None
47
+ if lora_path.exists():
48
+ print(f"Error: Download file already exists: {lora_filename}. ")
49
+ return None
50
+ hf_hub_download(repo_id=repo_id, filename=lora_filename, local_dir=".")
51
+ if lora_path.exists(): lora_path.rename(Path(local_file))
52
+ except Exception as e:
53
+ print(f"Error: Failed to download from {repo_id}. ")
54
+ return local_file
55
+
56
+
57
+ def convert_repo_to_safetensors_sdxl_lora(repo_id):
58
+ download_filename = f"{repo_id.split('/')[0]}_{repo_id.split('/')[-1]}_diffusers.safetensors"
59
+ output_filename = f"{repo_id.split('/')[0]}_{repo_id.split('/')[-1]}_webui.safetensors"
60
+ download_repo_lora(repo_id, download_filename)
61
+ convert_and_save(download_filename, output_filename)
62
+ return output_filename
63
+
64
+
65
+ if __name__ == "__main__":
66
+ parser = argparse.ArgumentParser(description="Convert LoRA model to PEFT and then to Kohya format from Repo.")
67
+ parser.add_argument("--repo_id", type=str, required=True, help="URL to the Repo of input LoRA model in the diffusers format.")
68
+
69
+ args = parser.parse_args()
70
+
71
+ convert_repo_to_safetensors_sdxl_lora(args.repo_id)
72
+
73
+
74
+ # Usage: python convert_repo_to_safetensors_sdxl_lora.py --repo_id nroggendorff/zelda-lora
convert_repo_to_safetensors_sdxl_lora_gr.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Script for converting a Hugging Face Diffusers trained SDXL LoRAs to Kohya format
2
+ # This means that you can input your diffusers-trained LoRAs and
3
+ # Get the output to work with WebUIs such as AUTOMATIC1111, ComfyUI, SD.Next and others.
4
+
5
+ # To get started you can find some cool `diffusers` trained LoRAs such as this cute Corgy
6
+ # https://huggingface.co/ignasbud/corgy_dog_LoRA/, download its `pytorch_lora_weights.safetensors` file
7
+ # and run the script:
8
+ # python convert_diffusers_sdxl_lora_to_webui.py --input_lora pytorch_lora_weights.safetensors --output_lora corgy.safetensors
9
+ # now you can use corgy.safetensors in your WebUI of choice!
10
+
11
+ # To train your own, here are some diffusers training scripts and utils that you can use and then convert:
12
+ # LoRA Ease - no code SDXL Dreambooth LoRA trainer: https://huggingface.co/spaces/multimodalart/lora-ease
13
+ # Dreambooth Advanced Training Script - state of the art techniques such as pivotal tuning and prodigy optimizer:
14
+ # - Script: https://github.com/huggingface/diffusers/blob/main/examples/advanced_diffusion_training/train_dreambooth_lora_sdxl_advanced.py
15
+ # - Colab (only on Pro): https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/SDXL_Dreambooth_LoRA_advanced_example.ipynb
16
+ # Canonical diffusers training scripts:
17
+ # - Script: https://github.com/huggingface/diffusers/blob/main/examples/dreambooth/train_dreambooth_lora_sdxl.py
18
+ # - Colab (runs on free tier): https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/SDXL_DreamBooth_LoRA_.ipynb
19
+
20
+ import argparse
21
+ import os
22
+
23
+ from safetensors.torch import load_file, save_file
24
+ from diffusers.utils import convert_all_state_dict_to_peft, convert_state_dict_to_kohya
25
+ from pathlib import Path
26
+ import gradio as gr
27
+
28
+ def convert_and_save(input_lora, output_lora=None):
29
+ if output_lora is None:
30
+ base_name = os.path.splitext(input_lora)[0]
31
+ output_lora = f"{base_name}_webui.safetensors"
32
+
33
+ diffusers_state_dict = load_file(input_lora)
34
+ peft_state_dict = convert_all_state_dict_to_peft(diffusers_state_dict)
35
+ kohya_state_dict = convert_state_dict_to_kohya(peft_state_dict)
36
+ save_file(kohya_state_dict, output_lora)
37
+
38
+
39
+ def download_repo_lora(repo_id, local_file, progress=gr.Progress(track_tqdm=True)):
40
+ from huggingface_hub import hf_hub_download, HfApi
41
+ lora_filename = "pytorch_lora_weights.safetensors"
42
+ lora_path = Path(lora_filename)
43
+ api = HfApi()
44
+ try:
45
+ if not api.file_exists(repo_id=repo_id, filename=lora_filename):
46
+ print(f"Error: This repo isn't diffusers LoRA repo: {repo_id}. ")
47
+ return None
48
+ if lora_path.exists():
49
+ print(f"Error: Download file already exists: {lora_filename}. ")
50
+ return None
51
+ hf_hub_download(repo_id=repo_id, filename=lora_filename, local_dir=".")
52
+ if lora_path.exists(): lora_path.rename(Path(local_file))
53
+ except Exception as e:
54
+ print(f"Error: Failed to download from {repo_id}. ")
55
+ return local_file
56
+
57
+
58
+ def convert_repo_to_safetensors_sdxl_lora(repo_id, progress=gr.Progress(track_tqdm=True)):
59
+ download_filename = f"{repo_id.split('/')[0]}_{repo_id.split('/')[-1]}_diffusers.safetensors"
60
+ output_filename = f"{repo_id.split('/')[0]}_{repo_id.split('/')[-1]}_webui.safetensors"
61
+ download_repo_lora(repo_id, download_filename)
62
+ convert_and_save(download_filename, output_filename)
63
+ return output_filename
64
+
65
+
66
+ def convert_repo_to_safetensors_sdxl_lora_multi(repo_id, files, progress=gr.Progress(track_tqdm=True)):
67
+ file = convert_repo_to_safetensors_sdxl_lora(repo_id)
68
+ if not files: files = []
69
+ files.append(file)
70
+ return gr.update(value=files)
71
+
72
+
73
+ if __name__ == "__main__":
74
+ parser = argparse.ArgumentParser(description="Convert LoRA model to PEFT and then to Kohya format from Repo.")
75
+ parser.add_argument("--repo_id", type=str, required=True, help="URL to the Repo of input LoRA model in the diffusers format.")
76
+
77
+ args = parser.parse_args()
78
+
79
+ convert_repo_to_safetensors_sdxl_lora(args.repo_id)
80
+
81
+
82
+ # Usage: python convert_repo_to_safetensors_sdxl_lora.py --repo_id nroggendorff/zelda-lora
local/convert_repo_to_safetensors_sdxl_lora.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Script for converting a Hugging Face Diffusers trained SDXL LoRAs to Kohya format
2
+ # This means that you can input your diffusers-trained LoRAs and
3
+ # Get the output to work with WebUIs such as AUTOMATIC1111, ComfyUI, SD.Next and others.
4
+
5
+ # To get started you can find some cool `diffusers` trained LoRAs such as this cute Corgy
6
+ # https://huggingface.co/ignasbud/corgy_dog_LoRA/, download its `pytorch_lora_weights.safetensors` file
7
+ # and run the script:
8
+ # python convert_diffusers_sdxl_lora_to_webui.py --input_lora pytorch_lora_weights.safetensors --output_lora corgy.safetensors
9
+ # now you can use corgy.safetensors in your WebUI of choice!
10
+
11
+ # To train your own, here are some diffusers training scripts and utils that you can use and then convert:
12
+ # LoRA Ease - no code SDXL Dreambooth LoRA trainer: https://huggingface.co/spaces/multimodalart/lora-ease
13
+ # Dreambooth Advanced Training Script - state of the art techniques such as pivotal tuning and prodigy optimizer:
14
+ # - Script: https://github.com/huggingface/diffusers/blob/main/examples/advanced_diffusion_training/train_dreambooth_lora_sdxl_advanced.py
15
+ # - Colab (only on Pro): https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/SDXL_Dreambooth_LoRA_advanced_example.ipynb
16
+ # Canonical diffusers training scripts:
17
+ # - Script: https://github.com/huggingface/diffusers/blob/main/examples/dreambooth/train_dreambooth_lora_sdxl.py
18
+ # - Colab (runs on free tier): https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/SDXL_DreamBooth_LoRA_.ipynb
19
+
20
+ import argparse
21
+ import os
22
+
23
+ from safetensors.torch import load_file, save_file
24
+ from diffusers.utils import convert_all_state_dict_to_peft, convert_state_dict_to_kohya
25
+ from pathlib import Path
26
+
27
+ def convert_and_save(input_lora, output_lora=None):
28
+ if output_lora is None:
29
+ base_name = os.path.splitext(input_lora)[0]
30
+ output_lora = f"{base_name}_webui.safetensors"
31
+
32
+ diffusers_state_dict = load_file(input_lora)
33
+ peft_state_dict = convert_all_state_dict_to_peft(diffusers_state_dict)
34
+ kohya_state_dict = convert_state_dict_to_kohya(peft_state_dict)
35
+ save_file(kohya_state_dict, output_lora)
36
+
37
+
38
+ def download_repo_lora(repo_id, local_file):
39
+ from huggingface_hub import hf_hub_download, HfApi
40
+ lora_filename = "pytorch_lora_weights.safetensors"
41
+ lora_path = Path(lora_filename)
42
+ api = HfApi()
43
+ try:
44
+ if not api.file_exists(repo_id=repo_id, filename=lora_filename):
45
+ print(f"Error: This repo isn't diffusers LoRA repo: {repo_id}. ")
46
+ return None
47
+ if lora_path.exists():
48
+ print(f"Error: Download file already exists: {lora_filename}. ")
49
+ return None
50
+ hf_hub_download(repo_id=repo_id, filename=lora_filename, local_dir=".")
51
+ if lora_path.exists(): lora_path.rename(Path(local_file))
52
+ except Exception as e:
53
+ print(f"Error: Failed to download from {repo_id}. ")
54
+ return local_file
55
+
56
+
57
+ def convert_repo_to_safetensors_sdxl_lora(repo_id):
58
+ download_filename = f"{repo_id.split('/')[0]}_{repo_id.split('/')[-1]}_diffusers.safetensors"
59
+ output_filename = f"{repo_id.split('/')[0]}_{repo_id.split('/')[-1]}_webui.safetensors"
60
+ download_repo_lora(repo_id, download_filename)
61
+ convert_and_save(download_filename, output_filename)
62
+ return output_filename
63
+
64
+
65
+ if __name__ == "__main__":
66
+ parser = argparse.ArgumentParser(description="Convert LoRA model to PEFT and then to Kohya format from Repo.")
67
+ parser.add_argument("--repo_id", type=str, required=True, help="URL to the Repo of input LoRA model in the diffusers format.")
68
+
69
+ args = parser.parse_args()
70
+
71
+ convert_repo_to_safetensors_sdxl_lora(args.repo_id)
72
+
73
+
74
+ # Usage: python convert_repo_to_safetensors_sdxl_lora.py --repo_id nroggendorff/zelda-lora
local/requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ accelerate
2
+ diffusers
3
+ torch
4
+ transformers
5
+ peft
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ accelerate
2
+ diffusers
3
+ torch
4
+ transformers
5
+ peft