r3gm commited on
Commit
1fdf747
1 Parent(s): c71cd1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +183 -705
app.py CHANGED
@@ -1,336 +1,97 @@
1
  import spaces
2
  import os
3
  from stablepy import Model_Diffusers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  from stablepy.diffusers_vanilla.style_prompt_config import STYLE_NAMES
5
- from stablepy.diffusers_vanilla.constants import FLUX_CN_UNION_MODES
6
  import torch
7
  import re
8
- from huggingface_hub import HfApi
9
  from stablepy import (
10
- CONTROLNET_MODEL_IDS,
11
- VALID_TASKS,
12
- T2I_PREPROCESSOR_NAME,
13
- FLASH_LORA,
14
- SCHEDULER_CONFIG_MAP,
15
  scheduler_names,
16
- IP_ADAPTER_MODELS,
17
  IP_ADAPTERS_SD,
18
  IP_ADAPTERS_SDXL,
19
- REPO_IMAGE_ENCODER,
20
- ALL_PROMPT_WEIGHT_OPTIONS,
21
- SD15_TASKS,
22
- SDXL_TASKS,
23
  )
24
  import time
25
  from PIL import ImageFile
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  # import urllib.parse
27
 
28
  ImageFile.LOAD_TRUNCATED_IMAGES = True
 
29
  print(os.getenv("SPACES_ZERO_GPU"))
30
 
31
- # - **Download SD 1.5 Models**
32
- download_model = "https://civitai.com/api/download/models/574369, https://huggingface.co/TechnoByte/MilkyWonderland/resolve/main/milkyWonderland_v40.safetensors"
33
- # - **Download VAEs**
34
- download_vae = "https://huggingface.co/nubby/blessed-sdxl-vae-fp16-fix/resolve/main/sdxl_vae-fp16fix-c-1.1-b-0.5.safetensors?download=true, https://huggingface.co/nubby/blessed-sdxl-vae-fp16-fix/resolve/main/sdxl_vae-fp16fix-blessed.safetensors?download=true, https://huggingface.co/digiplay/VAE/resolve/main/vividReal_v20.safetensors?download=true, https://huggingface.co/fp16-guy/anything_kl-f8-anime2_vae-ft-mse-840000-ema-pruned_blessed_clearvae_fp16_cleaned/resolve/main/vae-ft-mse-840000-ema-pruned_fp16.safetensors?download=true"
35
- # - **Download LoRAs**
36
- download_lora = "https://civitai.com/api/download/models/28907, https://huggingface.co/Leopain/color/resolve/main/Coloring_book_-_LineArt.safetensors, https://civitai.com/api/download/models/135867, https://civitai.com/api/download/models/145907, https://huggingface.co/Linaqruf/anime-detailer-xl-lora/resolve/main/anime-detailer-xl.safetensors?download=true, https://huggingface.co/Linaqruf/style-enhancer-xl-lora/resolve/main/style-enhancer-xl.safetensors?download=true, https://civitai.com/api/download/models/28609, https://huggingface.co/ByteDance/Hyper-SD/resolve/main/Hyper-SD15-8steps-CFG-lora.safetensors?download=true, https://huggingface.co/ByteDance/Hyper-SD/resolve/main/Hyper-SDXL-8steps-CFG-lora.safetensors?download=true"
37
- load_diffusers_format_model = [
38
- 'stabilityai/stable-diffusion-xl-base-1.0',
39
- 'black-forest-labs/FLUX.1-dev',
40
- 'John6666/blue-pencil-flux1-v021-fp8-flux',
41
- 'John6666/wai-ani-flux-v10forfp8-fp8-flux',
42
- 'John6666/xe-anime-flux-v04-fp8-flux',
43
- 'John6666/lyh-anime-flux-v2a1-fp8-flux',
44
- 'John6666/carnival-unchained-v10-fp8-flux',
45
- 'cagliostrolab/animagine-xl-3.1',
46
- 'John6666/epicrealism-xl-v8kiss-sdxl',
47
- 'misri/epicrealismXL_v7FinalDestination',
48
- 'misri/juggernautXL_juggernautX',
49
- 'misri/zavychromaxl_v80',
50
- 'SG161222/RealVisXL_V4.0',
51
- 'SG161222/RealVisXL_V5.0',
52
- 'misri/newrealityxlAllInOne_Newreality40',
53
- 'eienmojiki/Anything-XL',
54
- 'eienmojiki/Starry-XL-v5.2',
55
- 'gsdf/CounterfeitXL',
56
- 'KBlueLeaf/Kohaku-XL-Zeta',
57
- 'John6666/silvermoon-mix-01xl-v11-sdxl',
58
- 'WhiteAiZ/autismmixSDXL_autismmixConfetti_diffusers',
59
- 'kitty7779/ponyDiffusionV6XL',
60
- 'GraydientPlatformAPI/aniverse-pony',
61
- 'John6666/ras-real-anime-screencap-v1-sdxl',
62
- 'John6666/duchaiten-pony-xl-no-score-v60-sdxl',
63
- 'John6666/mistoon-anime-ponyalpha-sdxl',
64
- 'John6666/3x3x3mixxl-v2-sdxl',
65
- 'John6666/3x3x3mixxl-3dv01-sdxl',
66
- 'John6666/ebara-mfcg-pony-mix-v12-sdxl',
67
- 'John6666/t-ponynai3-v51-sdxl',
68
- 'John6666/t-ponynai3-v65-sdxl',
69
- 'John6666/prefect-pony-xl-v3-sdxl',
70
- 'John6666/mala-anime-mix-nsfw-pony-xl-v5-sdxl',
71
- 'John6666/wai-real-mix-v11-sdxl',
72
- 'John6666/wai-c-v6-sdxl',
73
- 'John6666/iniverse-mix-xl-sfwnsfw-pony-guofeng-v43-sdxl',
74
- 'John6666/photo-realistic-pony-v5-sdxl',
75
- 'John6666/pony-realism-v21main-sdxl',
76
- 'John6666/pony-realism-v22main-sdxl',
77
- 'John6666/cyberrealistic-pony-v63-sdxl',
78
- 'John6666/cyberrealistic-pony-v64-sdxl',
79
- 'GraydientPlatformAPI/realcartoon-pony-diffusion',
80
- 'John6666/nova-anime-xl-pony-v5-sdxl',
81
- 'John6666/autismmix-sdxl-autismmix-pony-sdxl',
82
- 'John6666/aimz-dream-real-pony-mix-v3-sdxl',
83
- 'John6666/duchaiten-pony-real-v11fix-sdxl',
84
- 'John6666/duchaiten-pony-real-v20-sdxl',
85
- 'yodayo-ai/kivotos-xl-2.0',
86
- 'yodayo-ai/holodayo-xl-2.1',
87
- 'yodayo-ai/clandestine-xl-1.0',
88
- 'digiplay/majicMIX_sombre_v2',
89
- 'digiplay/majicMIX_realistic_v6',
90
- 'digiplay/majicMIX_realistic_v7',
91
- 'digiplay/DreamShaper_8',
92
- 'digiplay/BeautifulArt_v1',
93
- 'digiplay/DarkSushi2.5D_v1',
94
- 'digiplay/darkphoenix3D_v1.1',
95
- 'digiplay/BeenYouLiteL11_diffusers',
96
- 'Yntec/RevAnimatedV2Rebirth',
97
- 'youknownothing/cyberrealistic_v50',
98
- 'youknownothing/deliberate-v6',
99
- 'GraydientPlatformAPI/deliberate-cyber3',
100
- 'GraydientPlatformAPI/picx-real',
101
- 'GraydientPlatformAPI/perfectworld6',
102
- 'emilianJR/epiCRealism',
103
- 'votepurchase/counterfeitV30_v30',
104
- 'votepurchase/ChilloutMix',
105
- 'Meina/MeinaMix_V11',
106
- 'Meina/MeinaUnreal_V5',
107
- 'Meina/MeinaPastel_V7',
108
- 'GraydientPlatformAPI/realcartoon3d-17',
109
- 'GraydientPlatformAPI/realcartoon-pixar11',
110
- 'GraydientPlatformAPI/realcartoon-real17',
111
- ]
112
-
113
- DIFFUSERS_FORMAT_LORAS = [
114
- "nerijs/animation2k-flux",
115
- "XLabs-AI/flux-RealismLora",
116
- ]
117
-
118
- CIVITAI_API_KEY = os.environ.get("CIVITAI_API_KEY")
119
- HF_TOKEN = os.environ.get("HF_READ_TOKEN")
120
-
121
- PREPROCESSOR_CONTROLNET = {
122
- "openpose": [
123
- "Openpose",
124
- "None",
125
- ],
126
- "scribble": [
127
- "HED",
128
- "PidiNet",
129
- "None",
130
- ],
131
- "softedge": [
132
- "PidiNet",
133
- "HED",
134
- "HED safe",
135
- "PidiNet safe",
136
- "None",
137
- ],
138
- "segmentation": [
139
- "UPerNet",
140
- "None",
141
- ],
142
- "depth": [
143
- "DPT",
144
- "Midas",
145
- "None",
146
- ],
147
- "normalbae": [
148
- "NormalBae",
149
- "None",
150
- ],
151
- "lineart": [
152
- "Lineart",
153
- "Lineart coarse",
154
- "Lineart (anime)",
155
- "None",
156
- "None (anime)",
157
- ],
158
- "lineart_anime": [
159
- "Lineart",
160
- "Lineart coarse",
161
- "Lineart (anime)",
162
- "None",
163
- "None (anime)",
164
- ],
165
- "shuffle": [
166
- "ContentShuffle",
167
- "None",
168
- ],
169
- "canny": [
170
- "Canny",
171
- "None",
172
- ],
173
- "mlsd": [
174
- "MLSD",
175
- "None",
176
- ],
177
- "ip2p": [
178
- "ip2p"
179
- ],
180
- "recolor": [
181
- "Recolor luminance",
182
- "Recolor intensity",
183
- "None",
184
- ],
185
- "tile": [
186
- "Mild Blur",
187
- "Moderate Blur",
188
- "Heavy Blur",
189
- "None",
190
- ],
191
-
192
- }
193
-
194
- TASK_STABLEPY = {
195
- 'txt2img': 'txt2img',
196
- 'img2img': 'img2img',
197
- 'inpaint': 'inpaint',
198
- # 'canny T2I Adapter': 'sdxl_canny_t2i', # NO HAVE STEP CALLBACK PARAMETERS SO NOT WORKS WITH DIFFUSERS 0.29.0
199
- # 'sketch T2I Adapter': 'sdxl_sketch_t2i',
200
- # 'lineart T2I Adapter': 'sdxl_lineart_t2i',
201
- # 'depth-midas T2I Adapter': 'sdxl_depth-midas_t2i',
202
- # 'openpose T2I Adapter': 'sdxl_openpose_t2i',
203
- 'openpose ControlNet': 'openpose',
204
- 'canny ControlNet': 'canny',
205
- 'mlsd ControlNet': 'mlsd',
206
- 'scribble ControlNet': 'scribble',
207
- 'softedge ControlNet': 'softedge',
208
- 'segmentation ControlNet': 'segmentation',
209
- 'depth ControlNet': 'depth',
210
- 'normalbae ControlNet': 'normalbae',
211
- 'lineart ControlNet': 'lineart',
212
- 'lineart_anime ControlNet': 'lineart_anime',
213
- 'shuffle ControlNet': 'shuffle',
214
- 'ip2p ControlNet': 'ip2p',
215
- 'optical pattern ControlNet': 'pattern',
216
- 'recolor ControlNet': 'recolor',
217
- 'tile ControlNet': 'tile',
218
- }
219
-
220
- TASK_MODEL_LIST = list(TASK_STABLEPY.keys())
221
-
222
- UPSCALER_DICT_GUI = {
223
- None: None,
224
- "Lanczos": "Lanczos",
225
- "Nearest": "Nearest",
226
- 'Latent': 'Latent',
227
- 'Latent (antialiased)': 'Latent (antialiased)',
228
- 'Latent (bicubic)': 'Latent (bicubic)',
229
- 'Latent (bicubic antialiased)': 'Latent (bicubic antialiased)',
230
- 'Latent (nearest)': 'Latent (nearest)',
231
- 'Latent (nearest-exact)': 'Latent (nearest-exact)',
232
- "RealESRGAN_x4plus": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth",
233
- "RealESRNet_x4plus": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.1/RealESRNet_x4plus.pth",
234
- "RealESRGAN_x4plus_anime_6B": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth",
235
- "RealESRGAN_x2plus": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth",
236
- "realesr-animevideov3": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-animevideov3.pth",
237
- "realesr-general-x4v3": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth",
238
- "realesr-general-wdn-x4v3": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-wdn-x4v3.pth",
239
- "4x-UltraSharp": "https://huggingface.co/Shandypur/ESRGAN-4x-UltraSharp/resolve/main/4x-UltraSharp.pth",
240
- "4x_foolhardy_Remacri": "https://huggingface.co/FacehugmanIII/4x_foolhardy_Remacri/resolve/main/4x_foolhardy_Remacri.pth",
241
- "Remacri4xExtraSmoother": "https://huggingface.co/hollowstrawberry/upscalers-backup/resolve/main/ESRGAN/Remacri%204x%20ExtraSmoother.pth",
242
- "AnimeSharp4x": "https://huggingface.co/hollowstrawberry/upscalers-backup/resolve/main/ESRGAN/AnimeSharp%204x.pth",
243
- "lollypop": "https://huggingface.co/hollowstrawberry/upscalers-backup/resolve/main/ESRGAN/lollypop.pth",
244
- "RealisticRescaler4x": "https://huggingface.co/hollowstrawberry/upscalers-backup/resolve/main/ESRGAN/RealisticRescaler%204x.pth",
245
- "NickelbackFS4x": "https://huggingface.co/hollowstrawberry/upscalers-backup/resolve/main/ESRGAN/NickelbackFS%204x.pth"
246
- }
247
-
248
- UPSCALER_KEYS = list(UPSCALER_DICT_GUI.keys())
249
-
250
-
251
- def download_things(directory, url, hf_token="", civitai_api_key=""):
252
- url = url.strip()
253
-
254
- if "drive.google.com" in url:
255
- original_dir = os.getcwd()
256
- os.chdir(directory)
257
- os.system(f"gdown --fuzzy {url}")
258
- os.chdir(original_dir)
259
- elif "huggingface.co" in url:
260
- url = url.replace("?download=true", "")
261
- # url = urllib.parse.quote(url, safe=':/') # fix encoding
262
- if "/blob/" in url:
263
- url = url.replace("/blob/", "/resolve/")
264
- user_header = f'"Authorization: Bearer {hf_token}"'
265
- if hf_token:
266
- os.system(f"aria2c --console-log-level=error --summary-interval=10 --header={user_header} -c -x 16 -k 1M -s 16 {url} -d {directory} -o {url.split('/')[-1]}")
267
- else:
268
- os.system(f"aria2c --optimize-concurrent-downloads --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 {url} -d {directory} -o {url.split('/')[-1]}")
269
- elif "civitai.com" in url:
270
- if "?" in url:
271
- url = url.split("?")[0]
272
- if civitai_api_key:
273
- url = url + f"?token={civitai_api_key}"
274
- os.system(f"aria2c --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 -d {directory} {url}")
275
- else:
276
- print("\033[91mYou need an API key to download Civitai models.\033[0m")
277
- else:
278
- os.system(f"aria2c --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 -d {directory} {url}")
279
-
280
-
281
- def get_model_list(directory_path):
282
- model_list = []
283
- valid_extensions = {'.ckpt', '.pt', '.pth', '.safetensors', '.bin'}
284
-
285
- for filename in os.listdir(directory_path):
286
- if os.path.splitext(filename)[1] in valid_extensions:
287
- # name_without_extension = os.path.splitext(filename)[0]
288
- file_path = os.path.join(directory_path, filename)
289
- # model_list.append((name_without_extension, file_path))
290
- model_list.append(file_path)
291
- print('\033[34mFILE: ' + file_path + '\033[0m')
292
- return model_list
293
-
294
-
295
- directory_models = 'models'
296
- os.makedirs(directory_models, exist_ok=True)
297
- directory_loras = 'loras'
298
- os.makedirs(directory_loras, exist_ok=True)
299
- directory_vaes = 'vaes'
300
- os.makedirs(directory_vaes, exist_ok=True)
301
 
302
  # Download stuffs
303
- for url in [url.strip() for url in download_model.split(',')]:
304
  if not os.path.exists(f"./models/{url.split('/')[-1]}"):
305
- download_things(directory_models, url, HF_TOKEN, CIVITAI_API_KEY)
306
- for url in [url.strip() for url in download_vae.split(',')]:
307
  if not os.path.exists(f"./vaes/{url.split('/')[-1]}"):
308
- download_things(directory_vaes, url, HF_TOKEN, CIVITAI_API_KEY)
309
- for url in [url.strip() for url in download_lora.split(',')]:
310
  if not os.path.exists(f"./loras/{url.split('/')[-1]}"):
311
- download_things(directory_loras, url, HF_TOKEN, CIVITAI_API_KEY)
312
 
313
  # Download Embeddings
314
- directory_embeds = 'embedings'
315
- os.makedirs(directory_embeds, exist_ok=True)
316
- download_embeds = [
317
- 'https://huggingface.co/datasets/Nerfgun3/bad_prompt/blob/main/bad_prompt_version2.pt',
318
- 'https://huggingface.co/embed/negative/resolve/main/EasyNegativeV2.safetensors',
319
- 'https://huggingface.co/embed/negative/resolve/main/bad-hands-5.pt',
320
- ]
321
-
322
- for url_embed in download_embeds:
323
  if not os.path.exists(f"./embedings/{url_embed.split('/')[-1]}"):
324
- download_things(directory_embeds, url_embed, HF_TOKEN, CIVITAI_API_KEY)
325
 
326
  # Build list models
327
- embed_list = get_model_list(directory_embeds)
328
- model_list = get_model_list(directory_models)
329
- model_list = load_diffusers_format_model + model_list
330
- lora_model_list = get_model_list(directory_loras)
331
  lora_model_list.insert(0, "None")
332
  lora_model_list = lora_model_list + DIFFUSERS_FORMAT_LORAS
333
- vae_model_list = get_model_list(directory_vaes)
334
  vae_model_list.insert(0, "None")
335
 
336
  print('\033[33m🏁 Download and listing of valid models completed.\033[0m')
@@ -338,185 +99,110 @@ print('\033[33m🏁 Download and listing of valid models completed.\033[0m')
338
  #######################
339
  # GUI
340
  #######################
341
- import gradio as gr
342
- import logging
343
  logging.getLogger("diffusers").setLevel(logging.ERROR)
344
- import diffusers
345
  diffusers.utils.logging.set_verbosity(40)
346
- import warnings
347
  warnings.filterwarnings(action="ignore", category=FutureWarning, module="diffusers")
348
  warnings.filterwarnings(action="ignore", category=UserWarning, module="diffusers")
349
  warnings.filterwarnings(action="ignore", category=FutureWarning, module="transformers")
350
- from stablepy import logger
351
-
352
  logger.setLevel(logging.DEBUG)
353
 
354
- msg_inc_vae = (
355
- "Use the right VAE for your model to maintain image quality. The wrong"
356
- " VAE can lead to poor results, like blurriness in the generated images."
357
- )
358
-
359
- SDXL_TASK = [k for k, v in TASK_STABLEPY.items() if v in SDXL_TASKS]
360
- SD_TASK = [k for k, v in TASK_STABLEPY.items() if v in SD15_TASKS]
361
- FLUX_TASK = list(TASK_STABLEPY.keys())[:3] + [k for k, v in TASK_STABLEPY.items() if v in FLUX_CN_UNION_MODES.keys()]
362
-
363
- MODEL_TYPE_TASK = {
364
- "SD 1.5": SD_TASK,
365
- "SDXL": SDXL_TASK,
366
- "FLUX": FLUX_TASK,
367
- }
368
-
369
- MODEL_TYPE_CLASS = {
370
- "diffusers:StableDiffusionPipeline": "SD 1.5",
371
- "diffusers:StableDiffusionXLPipeline": "SDXL",
372
- "diffusers:FluxPipeline": "FLUX",
373
- }
374
-
375
- POST_PROCESSING_SAMPLER = ["Use same sampler"] + scheduler_names[:-2]
376
-
377
  CSS = """
378
  .contain { display: flex; flex-direction: column; }
379
  #component-0 { height: 100%; }
380
  #gallery { flex-grow: 1; }
 
381
  """
382
 
383
- SUBTITLE_GUI = (
384
- "### This demo uses [diffusers](https://github.com/huggingface/diffusers)"
385
- " to perform different tasks in image generation."
386
- )
387
-
388
-
389
- def extract_parameters(input_string):
390
- parameters = {}
391
- input_string = input_string.replace("\n", "")
392
-
393
- if "Negative prompt:" not in input_string:
394
- if "Steps:" in input_string:
395
- input_string = input_string.replace("Steps:", "Negative prompt: Steps:")
396
- else:
397
- print("Invalid metadata")
398
- parameters["prompt"] = input_string
399
- return parameters
400
-
401
- parm = input_string.split("Negative prompt:")
402
- parameters["prompt"] = parm[0].strip()
403
- if "Steps:" not in parm[1]:
404
- print("Steps not detected")
405
- parameters["neg_prompt"] = parm[1].strip()
406
- return parameters
407
- parm = parm[1].split("Steps:")
408
- parameters["neg_prompt"] = parm[0].strip()
409
- input_string = "Steps:" + parm[1]
410
-
411
- # Extracting Steps
412
- steps_match = re.search(r'Steps: (\d+)', input_string)
413
- if steps_match:
414
- parameters['Steps'] = int(steps_match.group(1))
415
-
416
- # Extracting Size
417
- size_match = re.search(r'Size: (\d+x\d+)', input_string)
418
- if size_match:
419
- parameters['Size'] = size_match.group(1)
420
- width, height = map(int, parameters['Size'].split('x'))
421
- parameters['width'] = width
422
- parameters['height'] = height
423
-
424
- # Extracting other parameters
425
- other_parameters = re.findall(r'(\w+): (.*?)(?=, \w+|$)', input_string)
426
- for param in other_parameters:
427
- parameters[param[0]] = param[1].strip('"')
428
-
429
- return parameters
430
-
431
-
432
- def get_my_lora(link_url):
433
- for url in [url.strip() for url in link_url.split(',')]:
434
- if not os.path.exists(f"./loras/{url.split('/')[-1]}"):
435
- download_things(directory_loras, url, HF_TOKEN, CIVITAI_API_KEY)
436
- new_lora_model_list = get_model_list(directory_loras)
437
- new_lora_model_list.insert(0, "None")
438
- new_lora_model_list = new_lora_model_list + DIFFUSERS_FORMAT_LORAS
439
-
440
- return gr.update(
441
- choices=new_lora_model_list
442
- ), gr.update(
443
- choices=new_lora_model_list
444
- ), gr.update(
445
- choices=new_lora_model_list
446
- ), gr.update(
447
- choices=new_lora_model_list
448
- ), gr.update(
449
- choices=new_lora_model_list
450
- ),
451
-
452
-
453
- def info_html(json_data, title, subtitle):
454
- return f"""
455
- <div style='padding: 0; border-radius: 10px;'>
456
- <p style='margin: 0; font-weight: bold;'>{title}</p>
457
- <details>
458
- <summary>Details</summary>
459
- <p style='margin: 0; font-weight: bold;'>{subtitle}</p>
460
- </details>
461
- </div>
462
- """
463
-
464
-
465
- def get_model_type(repo_id: str):
466
- api = HfApi(token=os.environ.get("HF_TOKEN")) # if use private or gated model
467
- default = "SD 1.5"
468
- try:
469
- model = api.model_info(repo_id=repo_id, timeout=5.0)
470
- tags = model.tags
471
- for tag in tags:
472
- if tag in MODEL_TYPE_CLASS.keys(): return MODEL_TYPE_CLASS.get(tag, default)
473
- except Exception:
474
- return default
475
- return default
476
-
477
 
478
  class GuiSD:
479
  def __init__(self, stream=True):
480
  self.model = None
481
-
482
- print("Loading model...")
483
- self.model = Model_Diffusers(
484
- base_model_id="Lykon/dreamshaper-8",
485
- task_name="txt2img",
486
- vae_model=None,
487
- type_model_precision=torch.float16,
488
- retain_task_model_in_cache=False,
489
- device="cpu",
490
- )
491
- self.model.load_beta_styles()
492
 
493
  def load_new_model(self, model_name, vae_model, task, progress=gr.Progress(track_tqdm=True)):
494
 
495
- yield f"Loading model: {model_name}"
496
-
497
  vae_model = vae_model if vae_model != "None" else None
498
  model_type = get_model_type(model_name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
499
 
500
  if vae_model:
501
  vae_type = "SDXL" if "sdxl" in vae_model.lower() else "SD 1.5"
502
  if model_type != vae_type:
503
- gr.Warning(msg_inc_vae)
504
 
505
- self.model.device = torch.device("cpu")
506
- dtype_model = torch.bfloat16 if model_type == "FLUX" else torch.float16
507
 
508
- self.model.load_pipe(
509
- model_name,
510
- task_name=TASK_STABLEPY[task],
511
- vae_model=vae_model,
512
- type_model_precision=dtype_model,
513
- retain_task_model_in_cache=False,
514
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
515
 
516
  yield f"Model loaded: {model_name}"
517
 
518
  # @spaces.GPU(duration=59)
519
- @torch.inference_mode()
520
  def generate_pipeline(
521
  self,
522
  prompt,
@@ -622,6 +308,8 @@ class GuiSD:
622
  scale_ip2,
623
  pag_scale,
624
  ):
 
 
625
 
626
  vae_model = vae_model if vae_model != "None" else None
627
  loras_list = [lora1, lora2, lora3, lora4, lora5]
@@ -652,7 +340,8 @@ class GuiSD:
652
  params_ip_mode.append(modeip)
653
  params_ip_scale.append(scaleip)
654
 
655
- self.model.stream_config(concurrency=5, latent_resize_by=1, vae_decoding=False)
 
656
 
657
  if task != "txt2img" and not image_control:
658
  raise ValueError("No control image found: To use this function, you have to upload an image in 'Image ControlNet/Inpaint/Img2img'")
@@ -785,13 +474,15 @@ class GuiSD:
785
  self.model.pipe.transformer.to(self.model.device)
786
  print("transformer to cuda")
787
 
788
- info_state = "PROCESSING "
 
789
  for img, seed, image_path, metadata in self.model(**pipe_params):
790
- info_state += ">"
 
791
  if image_path:
792
- info_state = f"COMPLETE. Seeds: {str(seed)}"
793
  if vae_msg:
794
- info_state = info_state + "<br>" + vae_msg
795
 
796
  for status, lora in zip(self.model.lora_status, self.model.lora_memory):
797
  if status:
@@ -800,9 +491,9 @@ class GuiSD:
800
  msg_lora += f"<br>Error with: {lora}"
801
 
802
  if msg_lora:
803
- info_state += msg_lora
804
 
805
- info_state = info_state + "<br>" + "GENERATION DATA:<br>" + metadata[0].replace("\n", "<br>") + "<br>-------<br>"
806
 
807
  download_links = "<br>".join(
808
  [
@@ -811,22 +502,16 @@ class GuiSD:
811
  ]
812
  )
813
  if save_generated_images:
814
- info_state += f"<br>{download_links}"
815
-
816
- yield img, info_state
817
 
 
818
 
819
- def update_task_options(model_name, task_name):
820
- new_choices = MODEL_TYPE_TASK[get_model_type(model_name)]
821
-
822
- if task_name not in new_choices:
823
- task_name = "txt2img"
824
-
825
- return gr.update(value=task_name, choices=new_choices)
826
 
827
 
828
  def dynamic_gpu_duration(func, duration, *args):
829
 
 
830
  @spaces.GPU(duration=duration)
831
  def wrapped_func():
832
  yield from func(*args)
@@ -856,7 +541,7 @@ def sd_gen_generate_pipeline(*args):
856
  msg_load_lora = "Updating LoRAs in CPU (Slow but saves GPU usage)..."
857
 
858
  if lora_list != sd_gen.model.lora_memory and lora_list != [None] * 5:
859
- yield None, msg_load_lora
860
 
861
  # Load lora in CPU
862
  if load_lora_cpu:
@@ -882,14 +567,15 @@ def sd_gen_generate_pipeline(*args):
882
  )
883
  gr.Info(f"LoRAs in cache: {lora_cache_msg}")
884
 
885
- msg_request = f"Requesting {gpu_duration_arg}s. of GPU time"
 
886
  gr.Info(msg_request)
887
  print(msg_request)
888
-
889
- # yield from sd_gen.generate_pipeline(*generation_args)
890
 
891
  start_time = time.time()
892
 
 
893
  yield from dynamic_gpu_duration(
894
  sd_gen.generate_pipeline,
895
  gpu_duration_arg,
@@ -897,33 +583,19 @@ def sd_gen_generate_pipeline(*args):
897
  )
898
 
899
  end_time = time.time()
 
 
 
 
900
 
901
  if verbose_arg:
902
- execution_time = end_time - start_time
903
- msg_task_complete = (
904
- f"GPU task complete in: {round(execution_time, 0) + 1} seconds"
905
- )
906
  gr.Info(msg_task_complete)
907
  print(msg_task_complete)
908
 
 
909
 
910
- def extract_exif_data(image):
911
- if image is None: return ""
912
 
913
- try:
914
- metadata_keys = ['parameters', 'metadata', 'prompt', 'Comment']
915
-
916
- for key in metadata_keys:
917
- if key in image.info:
918
- return image.info[key]
919
-
920
- return str(image.info)
921
-
922
- except Exception as e:
923
- return f"Error extracting metadata: {str(e)}"
924
-
925
-
926
- @spaces.GPU(duration=20)
927
  def esrgan_upscale(image, upscaler_name, upscaler_size):
928
  if image is None: return None
929
 
@@ -958,6 +630,14 @@ with gr.Blocks(theme="NoCrypt/miku", css=CSS) as app:
958
 
959
  with gr.Column(scale=2):
960
 
 
 
 
 
 
 
 
 
961
  task_gui = gr.Dropdown(label="Task", choices=SDXL_TASK, value=TASK_MODEL_LIST[0])
962
  model_name_gui = gr.Dropdown(label="Model", choices=model_list, value=model_list[0], allow_custom_value=True)
963
  prompt_gui = gr.Textbox(lines=5, placeholder="Enter prompt", label="Prompt")
@@ -974,7 +654,7 @@ with gr.Blocks(theme="NoCrypt/miku", css=CSS) as app:
974
  [task_gui],
975
  )
976
 
977
- load_model_gui = gr.HTML()
978
 
979
  result_images = gr.Gallery(
980
  label="Generated images",
@@ -1093,15 +773,7 @@ with gr.Blocks(theme="NoCrypt/miku", css=CSS) as app:
1093
  )
1094
 
1095
  num_images_gui = gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Images")
1096
- prompt_s_options = [
1097
- ("Compel format: (word)weight", "Compel"),
1098
- ("Classic format: (word:weight)", "Classic"),
1099
- ("Classic-original format: (word:weight)", "Classic-original"),
1100
- ("Classic-no_norm format: (word:weight)", "Classic-no_norm"),
1101
- ("Classic-ignore", "Classic-ignore"),
1102
- ("None", "None"),
1103
- ]
1104
- prompt_syntax_gui = gr.Dropdown(label="Prompt Syntax", choices=prompt_s_options, value=prompt_s_options[1][1])
1105
  vae_model_gui = gr.Dropdown(label="VAE Model", choices=vae_model_list, value=vae_model_list[0])
1106
 
1107
  with gr.Accordion("Hires fix", open=False, visible=True):
@@ -1248,7 +920,7 @@ with gr.Blocks(theme="NoCrypt/miku", css=CSS) as app:
1248
  negative_prompt_ad_a_gui = gr.Textbox(label="Negative prompt", placeholder="Main negative prompt will be use", lines=3)
1249
  strength_ad_a_gui = gr.Number(label="Strength:", value=0.35, step=0.01, minimum=0.01, maximum=1.0)
1250
  face_detector_ad_a_gui = gr.Checkbox(label="Face detector", value=True)
1251
- person_detector_ad_a_gui = gr.Checkbox(label="Person detector", value=True)
1252
  hand_detector_ad_a_gui = gr.Checkbox(label="Hand detector", value=False)
1253
  mask_dilation_a_gui = gr.Number(label="Mask dilation:", value=4, minimum=1)
1254
  mask_blur_a_gui = gr.Number(label="Mask blur:", value=4, minimum=1)
@@ -1260,7 +932,7 @@ with gr.Blocks(theme="NoCrypt/miku", css=CSS) as app:
1260
  prompt_ad_b_gui = gr.Textbox(label="Main prompt", placeholder="Main prompt will be use", lines=3)
1261
  negative_prompt_ad_b_gui = gr.Textbox(label="Negative prompt", placeholder="Main negative prompt will be use", lines=3)
1262
  strength_ad_b_gui = gr.Number(label="Strength:", value=0.35, step=0.01, minimum=0.01, maximum=1.0)
1263
- face_detector_ad_b_gui = gr.Checkbox(label="Face detector", value=True)
1264
  person_detector_ad_b_gui = gr.Checkbox(label="Person detector", value=True)
1265
  hand_detector_ad_b_gui = gr.Checkbox(label="Hand detector", value=False)
1266
  mask_dilation_b_gui = gr.Number(label="Mask dilation:", value=4, minimum=1)
@@ -1278,7 +950,7 @@ with gr.Blocks(theme="NoCrypt/miku", css=CSS) as app:
1278
  retain_task_cache_gui = gr.Checkbox(value=False, label="Retain task model in cache")
1279
  leave_progress_bar_gui = gr.Checkbox(value=True, label="Leave Progress Bar")
1280
  disable_progress_bar_gui = gr.Checkbox(value=False, label="Disable Progress Bar")
1281
- display_images_gui = gr.Checkbox(value=True, label="Display Images")
1282
  image_previews_gui = gr.Checkbox(value=True, label="Image Previews")
1283
  image_storage_location_gui = gr.Textbox(value="./images", label="Image Storage Location")
1284
  retain_compel_previous_load_gui = gr.Checkbox(value=False, label="Retain Compel Previous Load")
@@ -1287,172 +959,10 @@ with gr.Blocks(theme="NoCrypt/miku", css=CSS) as app:
1287
  xformers_memory_efficient_attention_gui = gr.Checkbox(value=False, label="Xformers Memory Efficient Attention")
1288
 
1289
  with gr.Accordion("Examples and help", open=False, visible=True):
1290
- gr.Markdown(
1291
- """### Help:
1292
- - The current space runs on a ZERO GPU which is assigned for approximately 60 seconds; Therefore, if you submit expensive tasks, the operation may be canceled upon reaching the maximum allowed time with 'GPU TASK ABORTED'.
1293
- - Distorted or strange images often result from high prompt weights, so it's best to use low weights and scales, and consider using Classic variants like 'Classic-original'.
1294
- - For better results with Pony Diffusion, try using sampler DPM++ 1s or DPM2 with Compel or Classic prompt weights.
1295
- """
1296
- )
1297
- gr.Markdown(
1298
- """### The following examples perform specific tasks:
1299
- 1. Generation with SDXL and upscale
1300
- 2. Generation with FLUX dev
1301
- 3. ControlNet Canny SDXL
1302
- 4. Optical pattern (Optical illusion) SDXL
1303
- 5. Convert an image to a coloring drawing
1304
- 6. ControlNet OpenPose SD 1.5 and Latent upscale
1305
-
1306
- - Different tasks can be performed, such as img2img or using the IP adapter, to preserve a person's appearance or a specific style based on an image.
1307
- """
1308
- )
1309
  gr.Examples(
1310
- examples=[
1311
- [
1312
- "1girl, souryuu asuka langley, neon genesis evangelion, rebuild of evangelion, lance of longinus, cat hat, plugsuit, pilot suit, red bodysuit, sitting, crossed legs, black eye patch, throne, looking down, from bottom, looking at viewer, outdoors, (masterpiece), (best quality), (ultra-detailed), very aesthetic, illustration, disheveled hair, perfect composition, moist skin, intricate details",
1313
- "nfsw, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry, unfinished, very displeasing, oldest, early, chromatic aberration, artistic error, scan, abstract",
1314
- 28,
1315
- 7.0,
1316
- -1,
1317
- "None",
1318
- 0.33,
1319
- "Euler a",
1320
- 1152,
1321
- 896,
1322
- "cagliostrolab/animagine-xl-3.1",
1323
- "txt2img",
1324
- "image.webp", # img conttol
1325
- 1024, # img resolution
1326
- 0.35, # strength
1327
- 1.0, # cn scale
1328
- 0.0, # cn start
1329
- 1.0, # cn end
1330
- "Classic",
1331
- "Nearest",
1332
- 45,
1333
- False,
1334
- ],
1335
- [
1336
- "a digital illustration of a movie poster titled 'Finding Emo', finding nemo parody poster, featuring a depressed cartoon clownfish with black emo hair, eyeliner, and piercings, bored expression, swimming in a dark underwater scene, in the background, movie title in a dripping, grungy font, moody blue and purple color palette",
1337
- "",
1338
- 24,
1339
- 3.5,
1340
- -1,
1341
- "None",
1342
- 0.33,
1343
- "Euler a",
1344
- 1152,
1345
- 896,
1346
- "black-forest-labs/FLUX.1-dev",
1347
- "txt2img",
1348
- None, # img conttol
1349
- 1024, # img resolution
1350
- 0.35, # strength
1351
- 1.0, # cn scale
1352
- 0.0, # cn start
1353
- 1.0, # cn end
1354
- "Classic",
1355
- None,
1356
- 70,
1357
- True,
1358
- ],
1359
- [
1360
- "((masterpiece)), best quality, blonde disco girl, detailed face, realistic face, realistic hair, dynamic pose, pink pvc, intergalactic disco background, pastel lights, dynamic contrast, airbrush, fine detail, 70s vibe, midriff",
1361
- "(worst quality:1.2), (bad quality:1.2), (poor quality:1.2), (missing fingers:1.2), bad-artist-anime, bad-artist, bad-picture-chill-75v",
1362
- 48,
1363
- 3.5,
1364
- -1,
1365
- "None",
1366
- 0.33,
1367
- "DPM++ 2M SDE Lu",
1368
- 1024,
1369
- 1024,
1370
- "misri/epicrealismXL_v7FinalDestination",
1371
- "canny ControlNet",
1372
- "image.webp", # img conttol
1373
- 1024, # img resolution
1374
- 0.35, # strength
1375
- 1.0, # cn scale
1376
- 0.0, # cn start
1377
- 1.0, # cn end
1378
- "Classic",
1379
- None,
1380
- 44,
1381
- False,
1382
- ],
1383
- [
1384
- "cinematic scenery old city ruins",
1385
- "(worst quality, low quality, illustration, 3d, 2d, painting, cartoons, sketch), (illustration, 3d, 2d, painting, cartoons, sketch, blurry, film grain, noise), (low quality, worst quality:1.2)",
1386
- 50,
1387
- 4.0,
1388
- -1,
1389
- "None",
1390
- 0.33,
1391
- "Euler a",
1392
- 1024,
1393
- 1024,
1394
- "misri/juggernautXL_juggernautX",
1395
- "optical pattern ControlNet",
1396
- "spiral_no_transparent.png", # img conttol
1397
- 1024, # img resolution
1398
- 0.35, # strength
1399
- 1.0, # cn scale
1400
- 0.05, # cn start
1401
- 0.75, # cn end
1402
- "Classic",
1403
- None,
1404
- 35,
1405
- False,
1406
- ],
1407
- [
1408
- "black and white, line art, coloring drawing, clean line art, black strokes, no background, white, black, free lines, black scribbles, on paper, A blend of comic book art and lineart full of black and white color, masterpiece, high-resolution, trending on Pixiv fan box, palette knife, brush strokes, two-dimensional, planar vector, T-shirt design, stickers, and T-shirt design, vector art, fantasy art, Adobe Illustrator, hand-painted, digital painting, low polygon, soft lighting, aerial view, isometric style, retro aesthetics, 8K resolution, black sketch lines, monochrome, invert color",
1409
- "color, red, green, yellow, colored, duplicate, blurry, abstract, disfigured, deformed, animated, toy, figure, framed, 3d, bad art, poorly drawn, extra limbs, close up, b&w, weird colors, blurry, watermark, blur haze, 2 heads, long neck, watermark, elongated body, cropped image, out of frame, draft, deformed hands, twisted fingers, double image, malformed hands, multiple heads, extra limb, ugly, poorly drawn hands, missing limb, cut-off, over satured, grain, lowères, bad anatomy, poorly drawn face, mutation, mutated, floating limbs, disconnected limbs, out of focus, long body, disgusting, extra fingers, groos proportions, missing arms, mutated hands, cloned face, missing legs, ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, extra limbs, disfigured, deformed, body out of frame, blurry, bad anatomy, blurred, watermark, grainy, signature, cut off, draft, deformed, blurry, bad anatomy, disfigured, poorly drawn face, mutation, bluelish, blue",
1410
- 20,
1411
- 4.0,
1412
- -1,
1413
- "loras/Coloring_book_-_LineArt.safetensors",
1414
- 1.0,
1415
- "DPM++ 2M SDE Karras",
1416
- 1024,
1417
- 1024,
1418
- "cagliostrolab/animagine-xl-3.1",
1419
- "lineart ControlNet",
1420
- "color_image.png", # img conttol
1421
- 896, # img resolution
1422
- 0.35, # strength
1423
- 1.0, # cn scale
1424
- 0.0, # cn start
1425
- 1.0, # cn end
1426
- "Compel",
1427
- None,
1428
- 35,
1429
- False,
1430
- ],
1431
- [
1432
- "1girl,face,curly hair,red hair,white background,",
1433
- "(worst quality:2),(low quality:2),(normal quality:2),lowres,watermark,",
1434
- 38,
1435
- 5.0,
1436
- -1,
1437
- "None",
1438
- 0.33,
1439
- "DPM++ 2M SDE Karras",
1440
- 512,
1441
- 512,
1442
- "digiplay/majicMIX_realistic_v7",
1443
- "openpose ControlNet",
1444
- "image.webp", # img conttol
1445
- 1024, # img resolution
1446
- 0.35, # strength
1447
- 1.0, # cn scale
1448
- 0.0, # cn start
1449
- 0.9, # cn end
1450
- "Compel",
1451
- "Latent (antialiased)",
1452
- 46,
1453
- False,
1454
- ],
1455
- ],
1456
  fn=sd_gen.generate_pipeline,
1457
  inputs=[
1458
  prompt_gui,
@@ -1478,45 +988,13 @@ with gr.Blocks(theme="NoCrypt/miku", css=CSS) as app:
1478
  gpu_duration_gui,
1479
  load_lora_cpu_gui,
1480
  ],
1481
- outputs=[result_images, actual_task_info],
1482
  cache_examples=False,
1483
  )
1484
- gr.Markdown(
1485
- """### Resources
1486
- - John6666's space has some great features you might find helpful [link](https://huggingface.co/spaces/John6666/DiffuseCraftMod).
1487
- - You can also try the image generator in Colab’s free tier, which provides free GPU [link](https://github.com/R3gm/SD_diffusers_interactive).
1488
- """
1489
- )
1490
 
1491
  with gr.Tab("Inpaint mask maker", render=True):
1492
 
1493
- def create_mask_now(img, invert):
1494
- import numpy as np
1495
- import time
1496
-
1497
- time.sleep(0.5)
1498
-
1499
- transparent_image = img["layers"][0]
1500
-
1501
- # Extract the alpha channel
1502
- alpha_channel = np.array(transparent_image)[:, :, 3]
1503
-
1504
- # Create a binary mask by thresholding the alpha channel
1505
- binary_mask = alpha_channel > 1
1506
-
1507
- if invert:
1508
- print("Invert")
1509
- # Invert the binary mask so that the drawn shape is white and the rest is black
1510
- binary_mask = np.invert(binary_mask)
1511
-
1512
- # Convert the binary mask to a 3-channel RGB mask
1513
- rgb_mask = np.stack((binary_mask,) * 3, axis=-1)
1514
-
1515
- # Convert the mask to uint8
1516
- rgb_mask = rgb_mask.astype(np.uint8) * 255
1517
-
1518
- return img["background"], rgb_mask
1519
-
1520
  with gr.Row():
1521
  with gr.Column(scale=2):
1522
  image_base = gr.ImageEditor(
@@ -1702,7 +1180,7 @@ with gr.Blocks(theme="NoCrypt/miku", css=CSS) as app:
1702
  verbose_info_gui,
1703
  gpu_duration_gui,
1704
  ],
1705
- outputs=[result_images, actual_task_info],
1706
  queue=True,
1707
  show_progress="minimal",
1708
  )
@@ -1713,4 +1191,4 @@ app.launch(
1713
  show_error=True,
1714
  debug=True,
1715
  allowed_paths=["./images/"],
1716
- )
 
1
  import spaces
2
  import os
3
  from stablepy import Model_Diffusers
4
+ from constants import (
5
+ DIRECTORY_MODELS,
6
+ DIRECTORY_LORAS,
7
+ DIRECTORY_VAES,
8
+ DIRECTORY_EMBEDS,
9
+ DOWNLOAD_MODEL,
10
+ DOWNLOAD_VAE,
11
+ DOWNLOAD_LORA,
12
+ LOAD_DIFFUSERS_FORMAT_MODEL,
13
+ DIFFUSERS_FORMAT_LORAS,
14
+ DOWNLOAD_EMBEDS,
15
+ CIVITAI_API_KEY,
16
+ HF_TOKEN,
17
+ PREPROCESSOR_CONTROLNET,
18
+ TASK_STABLEPY,
19
+ TASK_MODEL_LIST,
20
+ UPSCALER_DICT_GUI,
21
+ UPSCALER_KEYS,
22
+ PROMPT_W_OPTIONS,
23
+ WARNING_MSG_VAE,
24
+ SDXL_TASK,
25
+ MODEL_TYPE_TASK,
26
+ POST_PROCESSING_SAMPLER,
27
+ SUBTITLE_GUI,
28
+ HELP_GUI,
29
+ EXAMPLES_GUI_HELP,
30
+ EXAMPLES_GUI,
31
+ RESOURCES,
32
+ )
33
  from stablepy.diffusers_vanilla.style_prompt_config import STYLE_NAMES
 
34
  import torch
35
  import re
 
36
  from stablepy import (
 
 
 
 
 
37
  scheduler_names,
 
38
  IP_ADAPTERS_SD,
39
  IP_ADAPTERS_SDXL,
 
 
 
 
40
  )
41
  import time
42
  from PIL import ImageFile
43
+ from utils import (
44
+ download_things,
45
+ get_model_list,
46
+ extract_parameters,
47
+ get_my_lora,
48
+ get_model_type,
49
+ extract_exif_data,
50
+ create_mask_now,
51
+ download_diffuser_repo,
52
+ progress_step_bar,
53
+ html_template_message,
54
+ )
55
+ from datetime import datetime
56
+ import gradio as gr
57
+ import logging
58
+ import diffusers
59
+ import warnings
60
+ from stablepy import logger
61
  # import urllib.parse
62
 
63
  ImageFile.LOAD_TRUNCATED_IMAGES = True
64
+ # os.environ["PYTORCH_NO_CUDA_MEMORY_CACHING"] = "1"
65
  print(os.getenv("SPACES_ZERO_GPU"))
66
 
67
+ directories = [DIRECTORY_MODELS, DIRECTORY_LORAS, DIRECTORY_VAES, DIRECTORY_EMBEDS]
68
+ for directory in directories:
69
+ os.makedirs(directory, exist_ok=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  # Download stuffs
72
+ for url in [url.strip() for url in DOWNLOAD_MODEL.split(',')]:
73
  if not os.path.exists(f"./models/{url.split('/')[-1]}"):
74
+ download_things(DIRECTORY_MODELS, url, HF_TOKEN, CIVITAI_API_KEY)
75
+ for url in [url.strip() for url in DOWNLOAD_VAE.split(',')]:
76
  if not os.path.exists(f"./vaes/{url.split('/')[-1]}"):
77
+ download_things(DIRECTORY_VAES, url, HF_TOKEN, CIVITAI_API_KEY)
78
+ for url in [url.strip() for url in DOWNLOAD_LORA.split(',')]:
79
  if not os.path.exists(f"./loras/{url.split('/')[-1]}"):
80
+ download_things(DIRECTORY_LORAS, url, HF_TOKEN, CIVITAI_API_KEY)
81
 
82
  # Download Embeddings
83
+ for url_embed in DOWNLOAD_EMBEDS:
 
 
 
 
 
 
 
 
84
  if not os.path.exists(f"./embedings/{url_embed.split('/')[-1]}"):
85
+ download_things(DIRECTORY_EMBEDS, url_embed, HF_TOKEN, CIVITAI_API_KEY)
86
 
87
  # Build list models
88
+ embed_list = get_model_list(DIRECTORY_EMBEDS)
89
+ model_list = get_model_list(DIRECTORY_MODELS)
90
+ model_list = LOAD_DIFFUSERS_FORMAT_MODEL + model_list
91
+ lora_model_list = get_model_list(DIRECTORY_LORAS)
92
  lora_model_list.insert(0, "None")
93
  lora_model_list = lora_model_list + DIFFUSERS_FORMAT_LORAS
94
+ vae_model_list = get_model_list(DIRECTORY_VAES)
95
  vae_model_list.insert(0, "None")
96
 
97
  print('\033[33m🏁 Download and listing of valid models completed.\033[0m')
 
99
  #######################
100
  # GUI
101
  #######################
 
 
102
  logging.getLogger("diffusers").setLevel(logging.ERROR)
 
103
  diffusers.utils.logging.set_verbosity(40)
 
104
  warnings.filterwarnings(action="ignore", category=FutureWarning, module="diffusers")
105
  warnings.filterwarnings(action="ignore", category=UserWarning, module="diffusers")
106
  warnings.filterwarnings(action="ignore", category=FutureWarning, module="transformers")
 
 
107
  logger.setLevel(logging.DEBUG)
108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  CSS = """
110
  .contain { display: flex; flex-direction: column; }
111
  #component-0 { height: 100%; }
112
  #gallery { flex-grow: 1; }
113
+ #load_model { height: 50px; }
114
  """
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
  class GuiSD:
118
  def __init__(self, stream=True):
119
  self.model = None
120
+ self.status_loading = False
121
+ self.sleep_loading = 4
122
+ self.last_load = datetime.now()
 
 
 
 
 
 
 
 
123
 
124
  def load_new_model(self, model_name, vae_model, task, progress=gr.Progress(track_tqdm=True)):
125
 
 
 
126
  vae_model = vae_model if vae_model != "None" else None
127
  model_type = get_model_type(model_name)
128
+ dtype_model = torch.bfloat16 if model_type == "FLUX" else torch.float16
129
+
130
+ if not os.path.exists(model_name):
131
+ _ = download_diffuser_repo(
132
+ repo_name=model_name,
133
+ model_type=model_type,
134
+ revision="main",
135
+ token=True,
136
+ )
137
+
138
+ for i in range(68):
139
+ if not self.status_loading:
140
+ self.status_loading = True
141
+ if i > 0:
142
+ time.sleep(self.sleep_loading)
143
+ print("Previous model ops...")
144
+ break
145
+ time.sleep(0.5)
146
+ print(f"Waiting queue {i}")
147
+ yield "Waiting queue"
148
+
149
+ self.status_loading = True
150
+
151
+ yield f"Loading model: {model_name}"
152
 
153
  if vae_model:
154
  vae_type = "SDXL" if "sdxl" in vae_model.lower() else "SD 1.5"
155
  if model_type != vae_type:
156
+ gr.Warning(WARNING_MSG_VAE)
157
 
158
+ print("Loading model...")
 
159
 
160
+ try:
161
+ start_time = time.time()
162
+
163
+ if self.model is None:
164
+ self.model = Model_Diffusers(
165
+ base_model_id=model_name,
166
+ task_name=TASK_STABLEPY[task],
167
+ vae_model=vae_model,
168
+ type_model_precision=dtype_model,
169
+ retain_task_model_in_cache=False,
170
+ device="cpu",
171
+ )
172
+ else:
173
+
174
+ if self.model.base_model_id != model_name:
175
+ load_now_time = datetime.now()
176
+ elapsed_time = (load_now_time - self.last_load).total_seconds()
177
+
178
+ if elapsed_time <= 8:
179
+ print("Waiting for the previous model's time ops...")
180
+ time.sleep(8-elapsed_time)
181
+
182
+ self.model.device = torch.device("cpu")
183
+ self.model.load_pipe(
184
+ model_name,
185
+ task_name=TASK_STABLEPY[task],
186
+ vae_model=vae_model,
187
+ type_model_precision=dtype_model,
188
+ retain_task_model_in_cache=False,
189
+ )
190
+
191
+ end_time = time.time()
192
+ self.sleep_loading = max(min(int(end_time - start_time), 10), 4)
193
+ except Exception as e:
194
+ self.last_load = datetime.now()
195
+ self.status_loading = False
196
+ self.sleep_loading = 4
197
+ raise e
198
+
199
+ self.last_load = datetime.now()
200
+ self.status_loading = False
201
 
202
  yield f"Model loaded: {model_name}"
203
 
204
  # @spaces.GPU(duration=59)
205
+ # @torch.inference_mode()
206
  def generate_pipeline(
207
  self,
208
  prompt,
 
308
  scale_ip2,
309
  pag_scale,
310
  ):
311
+ info_state = html_template_message("Navigating latent space...")
312
+ yield info_state, gr.update(), gr.update()
313
 
314
  vae_model = vae_model if vae_model != "None" else None
315
  loras_list = [lora1, lora2, lora3, lora4, lora5]
 
340
  params_ip_mode.append(modeip)
341
  params_ip_scale.append(scaleip)
342
 
343
+ concurrency = 5
344
+ self.model.stream_config(concurrency=concurrency, latent_resize_by=1, vae_decoding=False)
345
 
346
  if task != "txt2img" and not image_control:
347
  raise ValueError("No control image found: To use this function, you have to upload an image in 'Image ControlNet/Inpaint/Img2img'")
 
474
  self.model.pipe.transformer.to(self.model.device)
475
  print("transformer to cuda")
476
 
477
+ actual_progress = 0
478
+ info_images = gr.update()
479
  for img, seed, image_path, metadata in self.model(**pipe_params):
480
+ info_state = progress_step_bar(actual_progress, steps)
481
+ actual_progress += concurrency
482
  if image_path:
483
+ info_images = f"Seeds: {str(seed)}"
484
  if vae_msg:
485
+ info_images = info_images + "<br>" + vae_msg
486
 
487
  for status, lora in zip(self.model.lora_status, self.model.lora_memory):
488
  if status:
 
491
  msg_lora += f"<br>Error with: {lora}"
492
 
493
  if msg_lora:
494
+ info_images += msg_lora
495
 
496
+ info_images = info_images + "<br>" + "GENERATION DATA:<br>" + metadata[0].replace("\n", "<br>") + "<br>-------<br>"
497
 
498
  download_links = "<br>".join(
499
  [
 
502
  ]
503
  )
504
  if save_generated_images:
505
+ info_images += f"<br>{download_links}"
 
 
506
 
507
+ info_state = "COMPLETE"
508
 
509
+ yield info_state, img, info_images
 
 
 
 
 
 
510
 
511
 
512
  def dynamic_gpu_duration(func, duration, *args):
513
 
514
+ @torch.inference_mode()
515
  @spaces.GPU(duration=duration)
516
  def wrapped_func():
517
  yield from func(*args)
 
541
  msg_load_lora = "Updating LoRAs in CPU (Slow but saves GPU usage)..."
542
 
543
  if lora_list != sd_gen.model.lora_memory and lora_list != [None] * 5:
544
+ yield msg_load_lora, gr.update(), gr.update()
545
 
546
  # Load lora in CPU
547
  if load_lora_cpu:
 
567
  )
568
  gr.Info(f"LoRAs in cache: {lora_cache_msg}")
569
 
570
+ msg_request = f"Requesting {gpu_duration_arg}s. of GPU time.<br>Model: {sd_gen.model.base_model_id}"
571
+ if verbose_arg:
572
  gr.Info(msg_request)
573
  print(msg_request)
574
+ yield msg_request, gr.update(), gr.update()
 
575
 
576
  start_time = time.time()
577
 
578
+ # yield from sd_gen.generate_pipeline(*generation_args)
579
  yield from dynamic_gpu_duration(
580
  sd_gen.generate_pipeline,
581
  gpu_duration_arg,
 
583
  )
584
 
585
  end_time = time.time()
586
+ execution_time = end_time - start_time
587
+ msg_task_complete = (
588
+ f"GPU task complete in: {round(execution_time, 0) + 1} seconds"
589
+ )
590
 
591
  if verbose_arg:
 
 
 
 
592
  gr.Info(msg_task_complete)
593
  print(msg_task_complete)
594
 
595
+ yield msg_task_complete, gr.update(), gr.update()
596
 
 
 
597
 
598
+ @spaces.GPU(duration=18)
 
 
 
 
 
 
 
 
 
 
 
 
 
599
  def esrgan_upscale(image, upscaler_name, upscaler_size):
600
  if image is None: return None
601
 
 
630
 
631
  with gr.Column(scale=2):
632
 
633
+ def update_task_options(model_name, task_name):
634
+ new_choices = MODEL_TYPE_TASK[get_model_type(model_name)]
635
+
636
+ if task_name not in new_choices:
637
+ task_name = "txt2img"
638
+
639
+ return gr.update(value=task_name, choices=new_choices)
640
+
641
  task_gui = gr.Dropdown(label="Task", choices=SDXL_TASK, value=TASK_MODEL_LIST[0])
642
  model_name_gui = gr.Dropdown(label="Model", choices=model_list, value=model_list[0], allow_custom_value=True)
643
  prompt_gui = gr.Textbox(lines=5, placeholder="Enter prompt", label="Prompt")
 
654
  [task_gui],
655
  )
656
 
657
+ load_model_gui = gr.HTML(elem_id="load_model", elem_classes="contain")
658
 
659
  result_images = gr.Gallery(
660
  label="Generated images",
 
773
  )
774
 
775
  num_images_gui = gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Images")
776
+ prompt_syntax_gui = gr.Dropdown(label="Prompt Syntax", choices=PROMPT_W_OPTIONS, value=PROMPT_W_OPTIONS[1][1])
 
 
 
 
 
 
 
 
777
  vae_model_gui = gr.Dropdown(label="VAE Model", choices=vae_model_list, value=vae_model_list[0])
778
 
779
  with gr.Accordion("Hires fix", open=False, visible=True):
 
920
  negative_prompt_ad_a_gui = gr.Textbox(label="Negative prompt", placeholder="Main negative prompt will be use", lines=3)
921
  strength_ad_a_gui = gr.Number(label="Strength:", value=0.35, step=0.01, minimum=0.01, maximum=1.0)
922
  face_detector_ad_a_gui = gr.Checkbox(label="Face detector", value=True)
923
+ person_detector_ad_a_gui = gr.Checkbox(label="Person detector", value=False)
924
  hand_detector_ad_a_gui = gr.Checkbox(label="Hand detector", value=False)
925
  mask_dilation_a_gui = gr.Number(label="Mask dilation:", value=4, minimum=1)
926
  mask_blur_a_gui = gr.Number(label="Mask blur:", value=4, minimum=1)
 
932
  prompt_ad_b_gui = gr.Textbox(label="Main prompt", placeholder="Main prompt will be use", lines=3)
933
  negative_prompt_ad_b_gui = gr.Textbox(label="Negative prompt", placeholder="Main negative prompt will be use", lines=3)
934
  strength_ad_b_gui = gr.Number(label="Strength:", value=0.35, step=0.01, minimum=0.01, maximum=1.0)
935
+ face_detector_ad_b_gui = gr.Checkbox(label="Face detector", value=False)
936
  person_detector_ad_b_gui = gr.Checkbox(label="Person detector", value=True)
937
  hand_detector_ad_b_gui = gr.Checkbox(label="Hand detector", value=False)
938
  mask_dilation_b_gui = gr.Number(label="Mask dilation:", value=4, minimum=1)
 
950
  retain_task_cache_gui = gr.Checkbox(value=False, label="Retain task model in cache")
951
  leave_progress_bar_gui = gr.Checkbox(value=True, label="Leave Progress Bar")
952
  disable_progress_bar_gui = gr.Checkbox(value=False, label="Disable Progress Bar")
953
+ display_images_gui = gr.Checkbox(value=False, label="Display Images")
954
  image_previews_gui = gr.Checkbox(value=True, label="Image Previews")
955
  image_storage_location_gui = gr.Textbox(value="./images", label="Image Storage Location")
956
  retain_compel_previous_load_gui = gr.Checkbox(value=False, label="Retain Compel Previous Load")
 
959
  xformers_memory_efficient_attention_gui = gr.Checkbox(value=False, label="Xformers Memory Efficient Attention")
960
 
961
  with gr.Accordion("Examples and help", open=False, visible=True):
962
+ gr.Markdown(HELP_GUI)
963
+ gr.Markdown(EXAMPLES_GUI_HELP)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
964
  gr.Examples(
965
+ examples=EXAMPLES_GUI,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
966
  fn=sd_gen.generate_pipeline,
967
  inputs=[
968
  prompt_gui,
 
988
  gpu_duration_gui,
989
  load_lora_cpu_gui,
990
  ],
991
+ outputs=[load_model_gui, result_images, actual_task_info],
992
  cache_examples=False,
993
  )
994
+ gr.Markdown(RESOURCES)
 
 
 
 
 
995
 
996
  with gr.Tab("Inpaint mask maker", render=True):
997
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
998
  with gr.Row():
999
  with gr.Column(scale=2):
1000
  image_base = gr.ImageEditor(
 
1180
  verbose_info_gui,
1181
  gpu_duration_gui,
1182
  ],
1183
+ outputs=[load_model_gui, result_images, actual_task_info],
1184
  queue=True,
1185
  show_progress="minimal",
1186
  )
 
1191
  show_error=True,
1192
  debug=True,
1193
  allowed_paths=["./images/"],
1194
+ )