File size: 13,461 Bytes
d2ce16f
 
 
 
 
 
 
 
 
 
f575d14
 
 
 
 
7f70a45
f575d14
7f70a45
 
f575d14
d2ce16f
f575d14
 
d2ce16f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f575d14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0ff983
 
 
 
f575d14
 
 
 
 
 
 
 
f0ff983
 
 
 
f575d14
 
 
 
 
 
 
 
 
d2ce16f
be33247
f575d14
 
 
eb7d0ba
d2ce16f
f575d14
 
 
 
 
d2ce16f
f575d14
6f5ad59
5a502f7
f575d14
 
 
3155132
f575d14
 
d2ce16f
 
f575d14
d2ce16f
 
f575d14
 
 
d2ce16f
f575d14
7f70a45
d2ce16f
 
f575d14
 
 
7f70a45
f575d14
d2ce16f
 
 
7f70a45
 
 
d2ce16f
7f70a45
d2ce16f
7f70a45
 
 
 
f575d14
d2ce16f
f575d14
 
 
 
d2ce16f
f575d14
d2ce16f
f575d14
7f70a45
d2ce16f
7f70a45
 
 
 
d9ae74a
f575d14
d2ce16f
f575d14
 
7f70a45
d2ce16f
7f70a45
f575d14
d2ce16f
7f70a45
 
 
d2ce16f
 
 
5519c1a
d2ce16f
5519c1a
 
 
d2ce16f
 
 
76b9a66
d2ce16f
 
7f70a45
d2ce16f
15704fd
d2ce16f
f575d14
 
 
 
 
 
7f70a45
fbc3a29
f575d14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2ce16f
f575d14
 
 
 
d2ce16f
f575d14
d2ce16f
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
import os
if os.environ.get("SPACES_ZERO_GPU") is not None:
    import spaces
else:
    class spaces:
        @staticmethod
        def GPU(func):
            def wrapper(*args, **kwargs):
                return func(*args, **kwargs)
            return wrapper
import argparse
from pathlib import Path
import os
import torch
from diffusers import StableDiffusionXLPipeline, AutoencoderKL
from transformers import CLIPTokenizer, CLIPTextModel
import gradio as gr
import shutil
import gc
# also requires aria, gdown, peft, huggingface_hub, safetensors, transformers, accelerate, pytorch_lightning
from utils import (set_token, is_repo_exists, is_repo_name, get_download_file, upload_repo)


@spaces.GPU
def fake_gpu():
    pass


TEMP_DIR = "."


DTYPE_DICT = {
    "fp16": torch.float16,
    "bf16": torch.bfloat16,
    "fp32": torch.float32,
    "fp8": torch.float8_e4m3fn
}


def get_dtype(dtype: str):
    return DTYPE_DICT.get(dtype, torch.float16)


from diffusers import (
    DPMSolverMultistepScheduler,
    DPMSolverSinglestepScheduler,
    KDPM2DiscreteScheduler,
    EulerDiscreteScheduler,
    EulerAncestralDiscreteScheduler,
    HeunDiscreteScheduler,
    LMSDiscreteScheduler,
    DDIMScheduler,
    DEISMultistepScheduler,
    UniPCMultistepScheduler,
    LCMScheduler,
    PNDMScheduler,
    KDPM2AncestralDiscreteScheduler,
    DPMSolverSDEScheduler,
    EDMDPMSolverMultistepScheduler,
    DDPMScheduler,
    EDMEulerScheduler,
    TCDScheduler,
)


SCHEDULER_CONFIG_MAP = {
    "DPM++ 2M": (DPMSolverMultistepScheduler, {"use_karras_sigmas": False}),
    "DPM++ 2M Karras": (DPMSolverMultistepScheduler, {"use_karras_sigmas": True}),
    "DPM++ 2M SDE": (DPMSolverMultistepScheduler, {"use_karras_sigmas": False, "algorithm_type": "sde-dpmsolver++"}),
    "DPM++ 2M SDE Karras": (DPMSolverMultistepScheduler, {"use_karras_sigmas": True, "algorithm_type": "sde-dpmsolver++"}),
    "DPM++ 2S": (DPMSolverSinglestepScheduler, {"use_karras_sigmas": False}),
    "DPM++ 2S Karras": (DPMSolverSinglestepScheduler, {"use_karras_sigmas": True}),
    "DPM++ 1S": (DPMSolverMultistepScheduler, {"solver_order": 1}),
    "DPM++ 1S Karras": (DPMSolverMultistepScheduler, {"solver_order": 1, "use_karras_sigmas": True}),
    "DPM++ 3M": (DPMSolverMultistepScheduler, {"solver_order": 3}),
    "DPM++ 3M Karras": (DPMSolverMultistepScheduler, {"solver_order": 3, "use_karras_sigmas": True}),
    "DPM++ SDE": (DPMSolverSDEScheduler, {"use_karras_sigmas": False}),
    "DPM++ SDE Karras": (DPMSolverSDEScheduler, {"use_karras_sigmas": True}),
    "DPM2": (KDPM2DiscreteScheduler, {}),
    "DPM2 Karras": (KDPM2DiscreteScheduler, {"use_karras_sigmas": True}),
    "DPM2 a": (KDPM2AncestralDiscreteScheduler, {}),
    "DPM2 a Karras": (KDPM2AncestralDiscreteScheduler, {"use_karras_sigmas": True}),
    "Euler": (EulerDiscreteScheduler, {}),
    "Euler a": (EulerAncestralDiscreteScheduler, {}),
    "Euler trailing": (EulerDiscreteScheduler, {"timestep_spacing": "trailing", "prediction_type": "sample"}),
    "Euler a trailing": (EulerAncestralDiscreteScheduler, {"timestep_spacing": "trailing"}),
    "Heun": (HeunDiscreteScheduler, {}),
    "Heun Karras": (HeunDiscreteScheduler, {"use_karras_sigmas": True}),
    "LMS": (LMSDiscreteScheduler, {}),
    "LMS Karras": (LMSDiscreteScheduler, {"use_karras_sigmas": True}),
    "DDIM": (DDIMScheduler, {}),
    "DDIM trailing": (DDIMScheduler, {"timestep_spacing": "trailing"}),
    "DEIS": (DEISMultistepScheduler, {}),
    "UniPC": (UniPCMultistepScheduler, {}),
    "UniPC Karras": (UniPCMultistepScheduler, {"use_karras_sigmas": True}),
    "PNDM": (PNDMScheduler, {}),
    "Euler EDM": (EDMEulerScheduler, {}),
    "Euler EDM Karras": (EDMEulerScheduler, {"use_karras_sigmas": True}),
    "DPM++ 2M EDM": (EDMDPMSolverMultistepScheduler, {"solver_order": 2, "solver_type": "midpoint", "final_sigmas_type": "zero", "algorithm_type": "dpmsolver++"}),
    "DPM++ 2M EDM Karras": (EDMDPMSolverMultistepScheduler, {"use_karras_sigmas": True, "solver_order": 2, "solver_type": "midpoint", "final_sigmas_type": "zero", "algorithm_type": "dpmsolver++"}),
    "DDPM": (DDPMScheduler, {}),

    "DPM++ 2M Lu": (DPMSolverMultistepScheduler, {"use_lu_lambdas": True}),
    "DPM++ 2M Ef": (DPMSolverMultistepScheduler, {"euler_at_final": True}),
    "DPM++ 2M SDE Lu": (DPMSolverMultistepScheduler, {"use_lu_lambdas": True, "algorithm_type": "sde-dpmsolver++"}),
    "DPM++ 2M SDE Ef": (DPMSolverMultistepScheduler, {"algorithm_type": "sde-dpmsolver++", "euler_at_final": True}),

    "LCM": (LCMScheduler, {}),
    "TCD": (TCDScheduler, {}),
    "LCM trailing": (LCMScheduler, {"timestep_spacing": "trailing"}),
    "TCD trailing": (TCDScheduler, {"timestep_spacing": "trailing"}),
    "LCM Auto-Loader": (LCMScheduler, {}),
    "TCD Auto-Loader": (TCDScheduler, {}),
}


def get_scheduler_config(name):
    if not name in SCHEDULER_CONFIG_MAP.keys(): return SCHEDULER_CONFIG_MAP["Euler a"]
    return SCHEDULER_CONFIG_MAP[name]


def save_readme_md(dir, url):
    orig_url = ""
    orig_name = ""
    if is_repo_name(url): 
        orig_name = url
        orig_url = f"https://huggingface.co/{url}/"
    elif "http" in url:
        orig_name = url
        orig_url = url
    if orig_name and orig_url:
       md = f"""---

license: other

language:

- en

library_name: diffusers

pipeline_tag: text-to-image

tags:

- text-to-image

---

Converted from [{orig_name}]({orig_url}).

"""
    else:
        md = f"""---

license: other

language:

- en

library_name: diffusers

pipeline_tag: text-to-image

tags:

- text-to-image

---

"""
    path = str(Path(dir, "README.md"))
    with open(path, mode='w', encoding="utf-8") as f:
        f.write(md)


def fuse_loras(pipe, lora_dict={}, temp_dir=TEMP_DIR, civitai_key=""):
    if not lora_dict or not isinstance(lora_dict, dict): return pipe
    a_list = []
    w_list = []
    for k, v in lora_dict.items():
        if not k: continue
        new_lora_file = get_download_file(temp_dir, k, civitai_key)
        if not new_lora_file or not Path(new_lora_file).exists():
            print(f"LoRA not found: {k}")
            continue
        w_name = Path(new_lora_file).name
        a_name = Path(new_lora_file).stem
        pipe.load_lora_weights(new_lora_file, weight_name=w_name, adapter_name=a_name)
        a_list.append(a_name)
        w_list.append(v)
    if not a_list: return pipe
    pipe.set_adapters(a_list, adapter_weights=w_list)
    pipe.fuse_lora(adapter_names=a_list, lora_scale=1.0)
    pipe.unload_lora_weights()
    return pipe


def convert_url_to_diffusers_sdxl(url, civitai_key="", is_upload_sf=False, dtype="fp16", vae="", clip="",

                                  scheduler="Euler a", lora_dict={}, is_local=True, progress=gr.Progress(track_tqdm=True)):
    progress(0, desc="Start converting...")
    temp_dir = TEMP_DIR
    new_file = get_download_file(temp_dir, url, civitai_key)
    if not new_file:
        print(f"Not found: {url}")
        return ""
    new_dir = Path(new_file).stem.replace(" ", "_").replace(",", "_").replace(".", "_") #

    kwargs = {}
    type_kwargs = {}
    if dtype != "default": type_kwargs["torch_dtype"] = get_dtype(dtype)

    new_vae_file = ""
    if vae:
        if is_repo_name(vae): my_vae = AutoencoderKL.from_pretrained(vae, **type_kwargs)
        else:
            new_vae_file = get_download_file(temp_dir, vae, civitai_key)
            my_vae = AutoencoderKL.from_single_file(new_vae_file, **type_kwargs) if new_vae_file else None
        if my_vae: kwargs["vae"] = my_vae

    if clip:
        my_tokenizer = CLIPTokenizer.from_pretrained(clip)
        if my_tokenizer: kwargs["tokenizer"] = my_tokenizer
        my_text_encoder = CLIPTextModel.from_pretrained(clip, **type_kwargs)
        if my_text_encoder: kwargs["text_encoder"] = my_text_encoder

    pipe = None
    if is_repo_name(url): pipe = StableDiffusionXLPipeline.from_pretrained(new_file, use_safetensors=True, **kwargs, **type_kwargs)
    else: pipe = StableDiffusionXLPipeline.from_single_file(new_file, use_safetensors=True, **kwargs, **type_kwargs)

    pipe = fuse_loras(pipe, lora_dict, temp_dir, civitai_key)

    sconf = get_scheduler_config(scheduler)
    pipe.scheduler = sconf[0].from_config(pipe.scheduler.config, **sconf[1])

    pipe.save_pretrained(new_dir, safe_serialization=True, use_safetensors=True)

    if Path(new_dir).exists(): save_readme_md(new_dir, url)

    if not is_local:
        if not is_repo_name(new_file) and is_upload_sf: shutil.move(str(Path(new_file).resolve()), str(Path(new_dir, Path(new_file).name).resolve()))
        else: os.remove(new_file)
    del pipe
    torch.cuda.empty_cache()
    gc.collect()

    progress(1, desc="Converted.")
    return new_dir


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,

                                  urls=[], dtype="fp16", vae="", clip="", scheduler="Euler a",

                                  lora1=None, lora1s=1.0, lora2=None, lora2s=1.0, lora3=None, lora3s=1.0,

                                  lora4=None, lora4s=1.0, lora5=None, lora5s=1.0, progress=gr.Progress(track_tqdm=True)):
    is_local = False
    if not civitai_key and os.environ.get("CIVITAI_API_KEY"): civitai_key = os.environ.get("CIVITAI_API_KEY") # default Civitai API key
    if not hf_token and os.environ.get("HF_TOKEN"): hf_token = os.environ.get("HF_TOKEN") # default HF write token
    if not hf_user and os.environ.get("HF_USER"): hf_user = os.environ.get("HF_USER") # default username
    if not hf_user: raise gr.Error(f"Invalid user name: {hf_user}")
    if not hf_repo and os.environ.get("HF_REPO"): hf_repo = os.environ.get("HF_REPO") # default reponame
    set_token(hf_token)
    lora_dict = {lora1: lora1s, lora2: lora2s, lora3: lora3s, lora4: lora4s, lora5: lora5s}
    new_path = convert_url_to_diffusers_sdxl(dl_url, civitai_key, is_upload_sf, dtype, vae, clip, scheduler, lora_dict, is_local)
    if not new_path: return ""
    new_repo_id = f"{hf_user}/{Path(new_path).stem}"
    if hf_repo != "": new_repo_id = f"{hf_user}/{hf_repo}"
    if not is_repo_name(new_repo_id): raise gr.Error(f"Invalid repo name: {new_repo_id}")
    if not is_overwrite and is_repo_exists(new_repo_id): raise gr.Error(f"Repo already exists: {new_repo_id}")
    repo_url = upload_repo(new_repo_id, new_path, is_private)
    shutil.rmtree(new_path)
    if not urls: urls = []
    urls.append(repo_url)
    md = "### Your new repo:\n"
    for u in urls:
        md += f"[{str(u).split('/')[-2]}/{str(u).split('/')[-1]}]({str(u)})<br>"
    return gr.update(value=urls, choices=urls), gr.update(value=md)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()

    parser.add_argument("--url", default=None, type=str, required=True, help="URL of the model to convert.")
    parser.add_argument("--dtype", default="fp16", type=str, choices=["fp16", "fp32", "bf16", "fp8", "default"], help='Output data type. (Default: "fp16")')
    parser.add_argument("--scheduler", default="Euler a", type=str, choices=list(SCHEDULER_CONFIG_MAP.keys()), required=False, help="Scheduler name to use.")
    parser.add_argument("--vae", default=None, type=str, required=False, help="URL of the VAE to use.")
    parser.add_argument("--civitai_key", default=None, type=str, required=False, help="Civitai API Key (If you want to download file from Civitai).")
    parser.add_argument("--lora1", default=None, type=str, required=False, help="URL of the LoRA to use.")
    parser.add_argument("--lora1s", default=1.0, type=float, required=False, help="LoRA weight scale of --lora1.")
    parser.add_argument("--lora2", default=None, type=str, required=False, help="URL of the LoRA to use.")
    parser.add_argument("--lora2s", default=1.0, type=float, required=False, help="LoRA weight scale of --lora2.")
    parser.add_argument("--lora3", default=None, type=str, required=False, help="URL of the LoRA to use.")
    parser.add_argument("--lora3s", default=1.0, type=float, required=False, help="LoRA weight scale of --lora3.")
    parser.add_argument("--lora4", default=None, type=str, required=False, help="URL of the LoRA to use.")
    parser.add_argument("--lora4s", default=1.0, type=float, required=False, help="LoRA weight scale of --lora4.")
    parser.add_argument("--lora5", default=None, type=str, required=False, help="URL of the LoRA to use.")
    parser.add_argument("--lora5s", default=1.0, type=float, required=False, help="LoRA weight scale of --lora5.")
    parser.add_argument("--loras", default=None, type=str, required=False, help="Folder of the LoRA to use.")

    args = parser.parse_args()
    assert args.url is not None, "Must provide a URL!"

    is_local = True
    lora_dict = {args.lora1: args.lora1s, args.lora2: args.lora2s, args.lora3: args.lora3s, args.lora4: args.lora4s, args.lora5: args.lora5s}
    if args.loras and Path(args.loras).exists():
        for p in Path(args.loras).glob('**/*.safetensors'):
            lora_dict[str(p)] = 1.0
    clip = ""

    convert_url_to_diffusers_sdxl(args.url, args.civitai_key, args.dtype, args.vae, clip, args.scheduler, lora_dict, is_local)