Files changed (9) hide show
  1. .gitignore +0 -207
  2. README.md +1 -3
  3. app.py +850 -635
  4. constants.py +0 -611
  5. image_processor.py +0 -130
  6. packages.txt +1 -1
  7. pre-requirements.txt +0 -1
  8. requirements.txt +3 -12
  9. utils.py +0 -562
.gitignore DELETED
@@ -1,207 +0,0 @@
1
- # Byte-compiled / optimized / DLL files
2
- __pycache__/
3
- *.py[codz]
4
- *$py.class
5
-
6
- # C extensions
7
- *.so
8
-
9
- # Distribution / packaging
10
- .Python
11
- build/
12
- develop-eggs/
13
- dist/
14
- downloads/
15
- eggs/
16
- .eggs/
17
- lib/
18
- lib64/
19
- parts/
20
- sdist/
21
- var/
22
- wheels/
23
- share/python-wheels/
24
- *.egg-info/
25
- .installed.cfg
26
- *.egg
27
- MANIFEST
28
-
29
- # PyInstaller
30
- # Usually these files are written by a python script from a template
31
- # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
- *.manifest
33
- *.spec
34
-
35
- # Installer logs
36
- pip-log.txt
37
- pip-delete-this-directory.txt
38
-
39
- # Unit test / coverage reports
40
- htmlcov/
41
- .tox/
42
- .nox/
43
- .coverage
44
- .coverage.*
45
- .cache
46
- nosetests.xml
47
- coverage.xml
48
- *.cover
49
- *.py.cover
50
- .hypothesis/
51
- .pytest_cache/
52
- cover/
53
-
54
- # Translations
55
- *.mo
56
- *.pot
57
-
58
- # Django stuff:
59
- *.log
60
- local_settings.py
61
- db.sqlite3
62
- db.sqlite3-journal
63
-
64
- # Flask stuff:
65
- instance/
66
- .webassets-cache
67
-
68
- # Scrapy stuff:
69
- .scrapy
70
-
71
- # Sphinx documentation
72
- docs/_build/
73
-
74
- # PyBuilder
75
- .pybuilder/
76
- target/
77
-
78
- # Jupyter Notebook
79
- .ipynb_checkpoints
80
-
81
- # IPython
82
- profile_default/
83
- ipython_config.py
84
-
85
- # pyenv
86
- # For a library or package, you might want to ignore these files since the code is
87
- # intended to run in multiple environments; otherwise, check them in:
88
- # .python-version
89
-
90
- # pipenv
91
- # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
- # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
- # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
- # install all needed dependencies.
95
- #Pipfile.lock
96
-
97
- # UV
98
- # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
- # This is especially recommended for binary packages to ensure reproducibility, and is more
100
- # commonly ignored for libraries.
101
- #uv.lock
102
-
103
- # poetry
104
- # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
- # This is especially recommended for binary packages to ensure reproducibility, and is more
106
- # commonly ignored for libraries.
107
- # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
- #poetry.lock
109
- #poetry.toml
110
-
111
- # pdm
112
- # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
- # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
- # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
- #pdm.lock
116
- #pdm.toml
117
- .pdm-python
118
- .pdm-build/
119
-
120
- # pixi
121
- # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
- #pixi.lock
123
- # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
- # in the .venv directory. It is recommended not to include this directory in version control.
125
- .pixi
126
-
127
- # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
- __pypackages__/
129
-
130
- # Celery stuff
131
- celerybeat-schedule
132
- celerybeat.pid
133
-
134
- # SageMath parsed files
135
- *.sage.py
136
-
137
- # Environments
138
- .env
139
- .envrc
140
- .venv
141
- env/
142
- venv/
143
- ENV/
144
- env.bak/
145
- venv.bak/
146
-
147
- # Spyder project settings
148
- .spyderproject
149
- .spyproject
150
-
151
- # Rope project settings
152
- .ropeproject
153
-
154
- # mkdocs documentation
155
- /site
156
-
157
- # mypy
158
- .mypy_cache/
159
- .dmypy.json
160
- dmypy.json
161
-
162
- # Pyre type checker
163
- .pyre/
164
-
165
- # pytype static type analyzer
166
- .pytype/
167
-
168
- # Cython debug symbols
169
- cython_debug/
170
-
171
- # PyCharm
172
- # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
173
- # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
174
- # and can be added to the global gitignore or merged into this file. For a more nuclear
175
- # option (not recommended) you can uncomment the following to ignore the entire idea folder.
176
- #.idea/
177
-
178
- # Abstra
179
- # Abstra is an AI-powered process automation framework.
180
- # Ignore directories containing user credentials, local state, and settings.
181
- # Learn more at https://abstra.io/docs
182
- .abstra/
183
-
184
- # Visual Studio Code
185
- # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
186
- # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
187
- # and can be added to the global gitignore or merged into this file. However, if you prefer,
188
- # you could uncomment the following to ignore the entire vscode folder
189
- # .vscode/
190
-
191
- # Ruff stuff:
192
- .ruff_cache/
193
-
194
- # PyPI configuration file
195
- .pypirc
196
-
197
- # Cursor
198
- # Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
199
- # exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
200
- # refer to https://docs.cursor.com/context/ignore-files
201
- .cursorignore
202
- .cursorindexingignore
203
-
204
- # Marimo
205
- marimo/_static/
206
- marimo/_lsp/
207
- __marimo__/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md CHANGED
@@ -4,13 +4,11 @@ emoji: 🧩🖼️
4
  colorFrom: red
5
  colorTo: pink
6
  sdk: gradio
7
- sdk_version: 5.44.1
8
  app_file: app.py
9
  pinned: true
10
  license: mit
11
  short_description: Stunning images using stable diffusion.
12
- preload_from_hub:
13
- - madebyollin/sdxl-vae-fp16-fix config.json,diffusion_pytorch_model.safetensors
14
  ---
15
 
16
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
4
  colorFrom: red
5
  colorTo: pink
6
  sdk: gradio
7
+ sdk_version: 5.1.0
8
  app_file: app.py
9
  pinned: true
10
  license: mit
11
  short_description: Stunning images using stable diffusion.
 
 
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,296 +1,517 @@
1
  import spaces
2
  import os
3
- from argparse import ArgumentParser
4
- from stablepy import (
5
- Model_Diffusers,
6
- SCHEDULE_TYPE_OPTIONS,
7
- SCHEDULE_PREDICTION_TYPE_OPTIONS,
8
- check_scheduler_compatibility,
9
- TASK_AND_PREPROCESSORS,
10
- FACE_RESTORATION_MODELS,
11
- scheduler_names,
12
- )
13
- from constants import (
14
- DIRECTORY_MODELS,
15
- DIRECTORY_LORAS,
16
- DIRECTORY_VAES,
17
- DIRECTORY_EMBEDS,
18
- DIRECTORY_UPSCALERS,
19
- DOWNLOAD_MODEL,
20
- DOWNLOAD_VAE,
21
- DOWNLOAD_LORA,
22
- LOAD_DIFFUSERS_FORMAT_MODEL,
23
- DIFFUSERS_FORMAT_LORAS,
24
- DOWNLOAD_EMBEDS,
25
- CIVITAI_API_KEY,
26
- HF_TOKEN,
27
- TASK_STABLEPY,
28
- TASK_MODEL_LIST,
29
- UPSCALER_DICT_GUI,
30
- UPSCALER_KEYS,
31
- PROMPT_W_OPTIONS,
32
- WARNING_MSG_VAE,
33
- SDXL_TASK,
34
- MODEL_TYPE_TASK,
35
- POST_PROCESSING_SAMPLER,
36
- SUBTITLE_GUI,
37
- HELP_GUI,
38
- EXAMPLES_GUI_HELP,
39
- EXAMPLES_GUI,
40
- RESOURCES,
41
- DIFFUSERS_CONTROLNET_MODEL,
42
- IP_MODELS,
43
- MODE_IP_OPTIONS,
44
- CACHE_HF_ROOT,
45
- )
46
  from stablepy.diffusers_vanilla.style_prompt_config import STYLE_NAMES
 
47
  import torch
48
  import re
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  import time
50
  from PIL import ImageFile
51
- from utils import (
52
- download_things,
53
- get_model_list,
54
- extract_parameters,
55
- get_my_lora,
56
- get_model_type,
57
- extract_exif_data,
58
- create_mask_now,
59
- download_diffuser_repo,
60
- get_used_storage_gb,
61
- delete_model,
62
- progress_step_bar,
63
- html_template_message,
64
- escape_html,
65
- clear_hf_cache,
66
- )
67
- from image_processor import preprocessor_tab
68
- from datetime import datetime
69
- import gradio as gr
70
- import logging
71
- import diffusers
72
- import warnings
73
- from stablepy import logger
74
- from diffusers import FluxPipeline
75
  # import urllib.parse
76
- import subprocess
77
-
78
- IS_ZERO_GPU = bool(os.getenv("SPACES_ZERO_GPU"))
79
- if IS_ZERO_GPU:
80
- subprocess.run("rm -rf /data-nvme/zerogpu-offload/*", env={}, shell=True)
81
- IS_GPU_MODE = True if IS_ZERO_GPU else (True if torch.cuda.is_available() else False)
82
- img_path = "./images/"
83
- allowed_path = os.path.abspath(img_path)
84
- delete_cache_time = (9600, 9600) if IS_ZERO_GPU else (86400, 86400)
85
 
86
  ImageFile.LOAD_TRUNCATED_IMAGES = True
87
- torch.backends.cuda.matmul.allow_tf32 = True
88
- # os.environ["PYTORCH_NO_CUDA_MEMORY_CACHING"] = "1"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
- directories = [DIRECTORY_MODELS, DIRECTORY_LORAS, DIRECTORY_VAES, DIRECTORY_EMBEDS, DIRECTORY_UPSCALERS]
91
- for directory in directories:
92
- os.makedirs(directory, exist_ok=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
  # Download stuffs
95
- for url in [url.strip() for url in DOWNLOAD_MODEL.split(',')]:
96
- download_things(DIRECTORY_MODELS, url, HF_TOKEN, CIVITAI_API_KEY)
97
- for url in [url.strip() for url in DOWNLOAD_VAE.split(',')]:
98
- download_things(DIRECTORY_VAES, url, HF_TOKEN, CIVITAI_API_KEY)
99
- for url in [url.strip() for url in DOWNLOAD_LORA.split(',')]:
100
- download_things(DIRECTORY_LORAS, url, HF_TOKEN, CIVITAI_API_KEY)
 
 
 
101
 
102
  # Download Embeddings
103
- for url_embed in DOWNLOAD_EMBEDS:
104
- download_things(DIRECTORY_EMBEDS, url_embed, HF_TOKEN, CIVITAI_API_KEY)
 
 
 
 
 
 
 
 
 
105
 
106
  # Build list models
107
- embed_list = get_model_list(DIRECTORY_EMBEDS)
108
- embed_list = [
109
- (os.path.splitext(os.path.basename(emb))[0], emb) for emb in embed_list
110
- ]
111
- single_file_model_list = get_model_list(DIRECTORY_MODELS)
112
- model_list = LOAD_DIFFUSERS_FORMAT_MODEL + single_file_model_list
113
- lora_model_list = get_model_list(DIRECTORY_LORAS)
114
  lora_model_list.insert(0, "None")
115
  lora_model_list = lora_model_list + DIFFUSERS_FORMAT_LORAS
116
- vae_model_list = get_model_list(DIRECTORY_VAES)
117
- vae_model_list.insert(0, "BakedVAE")
118
  vae_model_list.insert(0, "None")
119
 
120
  print('\033[33m🏁 Download and listing of valid models completed.\033[0m')
121
 
122
- components = None
123
- if IS_ZERO_GPU:
124
- flux_repo = "camenduru/FLUX.1-dev-diffusers"
125
- flux_pipe = FluxPipeline.from_pretrained(
126
- flux_repo,
127
- transformer=None,
128
- torch_dtype=torch.bfloat16,
129
- ).to("cuda")
130
- components = flux_pipe.components
131
- delete_model(flux_repo)
132
-
133
  #######################
134
  # GUI
135
  #######################
 
 
136
  logging.getLogger("diffusers").setLevel(logging.ERROR)
 
137
  diffusers.utils.logging.set_verbosity(40)
 
138
  warnings.filterwarnings(action="ignore", category=FutureWarning, module="diffusers")
139
  warnings.filterwarnings(action="ignore", category=UserWarning, module="diffusers")
140
  warnings.filterwarnings(action="ignore", category=FutureWarning, module="transformers")
 
141
 
142
- parser = ArgumentParser(description='DiffuseCraft: Create images from text prompts.', add_help=True)
143
- parser.add_argument("--share", action="store_true", dest="share_enabled", default=False, help="Enable sharing")
144
- parser.add_argument('--theme', type=str, default="NoCrypt/miku", help='Set the theme (default: NoCrypt/miku)')
145
- parser.add_argument("--ssr", action="store_true", help="Enable SSR (Server-Side Rendering)")
146
- parser.add_argument("--log-level", type=str, default="INFO", choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], help="Set logging level (default: INFO)")
147
- args = parser.parse_args()
148
 
149
- logger.setLevel(
150
- "INFO" if IS_ZERO_GPU else getattr(logging, args.log_level.upper())
 
151
  )
152
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  CSS = """
154
  .contain { display: flex; flex-direction: column; }
155
  #component-0 { height: 100%; }
156
  #gallery { flex-grow: 1; }
157
- #load_model { height: 50px; }
158
  """
159
 
 
 
 
 
160
 
161
- def lora_chk(lora_):
162
- if isinstance(lora_, str) and lora_.strip() not in ["", "None"]:
163
- return lora_
164
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
 
166
 
167
  class GuiSD:
168
  def __init__(self, stream=True):
169
  self.model = None
170
- self.status_loading = False
171
- self.sleep_loading = 4
172
- self.last_load = datetime.now()
173
- self.inventory = []
174
-
175
- def update_storage_models(self, storage_floor_gb=24, required_inventory_for_purge=3):
176
- while get_used_storage_gb() > storage_floor_gb:
177
- if len(self.inventory) < required_inventory_for_purge:
178
- break
179
- removal_candidate = self.inventory.pop(0)
180
- delete_model(removal_candidate)
181
-
182
- # Cleanup after 60 seconds of inactivity
183
- lowPrioCleanup = max((datetime.now() - self.last_load).total_seconds(), 0) > 60
184
- if lowPrioCleanup and not self.status_loading and get_used_storage_gb(CACHE_HF_ROOT) > (storage_floor_gb * 2):
185
- print("Cleaning up Hugging Face cache...")
186
- clear_hf_cache()
187
- self.inventory = [
188
- m for m in self.inventory if os.path.exists(m)
189
- ]
190
-
191
- def update_inventory(self, model_name):
192
- if model_name not in single_file_model_list:
193
- self.inventory = [
194
- m for m in self.inventory if m != model_name
195
- ] + [model_name]
196
- print(self.inventory)
197
-
198
- def load_new_model(self, model_name, vae_model, task, controlnet_model, progress=gr.Progress(track_tqdm=True)):
199
-
200
- # download link model > model_name
201
- if model_name.startswith("http"):
202
- yield f"Downloading model: {model_name}"
203
- model_name = download_things(DIRECTORY_MODELS, model_name, HF_TOKEN, CIVITAI_API_KEY)
204
- if not model_name:
205
- raise ValueError("Error retrieving model information from URL")
206
-
207
- if IS_ZERO_GPU:
208
- self.update_storage_models()
209
-
210
- vae_model = vae_model if vae_model != "None" else None
211
- model_type = get_model_type(model_name)
212
- dtype_model = torch.bfloat16 if model_type == "FLUX" else torch.float16
213
-
214
- if not os.path.exists(model_name):
215
- logger.debug(f"model_name={model_name}, vae_model={vae_model}, task={task}, controlnet_model={controlnet_model}")
216
- _ = download_diffuser_repo(
217
- repo_name=model_name,
218
- model_type=model_type,
219
- revision="main",
220
- token=True,
221
- )
222
-
223
- self.update_inventory(model_name)
224
 
225
- for i in range(68):
226
- if not self.status_loading:
227
- self.status_loading = True
228
- if i > 0:
229
- time.sleep(self.sleep_loading)
230
- print("Previous model ops...")
231
- break
232
- time.sleep(0.5)
233
- print(f"Waiting queue {i}")
234
- yield "Waiting queue"
235
 
236
- self.status_loading = True
237
 
238
  yield f"Loading model: {model_name}"
239
 
240
- if vae_model == "BakedVAE":
241
- vae_model = model_name
242
- elif vae_model:
 
243
  vae_type = "SDXL" if "sdxl" in vae_model.lower() else "SD 1.5"
244
  if model_type != vae_type:
245
- gr.Warning(WARNING_MSG_VAE)
246
-
247
- print("Loading model...")
248
-
249
- try:
250
- start_time = time.time()
251
-
252
- if self.model is None:
253
- self.model = Model_Diffusers(
254
- base_model_id=model_name,
255
- task_name=TASK_STABLEPY[task],
256
- vae_model=vae_model,
257
- type_model_precision=dtype_model,
258
- retain_task_model_in_cache=False,
259
- controlnet_model=controlnet_model,
260
- device="cpu" if IS_ZERO_GPU else None,
261
- env_components=components,
262
- )
263
- self.model.advanced_params(image_preprocessor_cuda_active=IS_GPU_MODE)
264
- else:
265
- if self.model.base_model_id != model_name:
266
- load_now_time = datetime.now()
267
- elapsed_time = max((load_now_time - self.last_load).total_seconds(), 0)
268
-
269
- if elapsed_time <= 9:
270
- print("Waiting for the previous model's time ops...")
271
- time.sleep(9 - elapsed_time)
272
-
273
- if IS_ZERO_GPU:
274
- self.model.device = torch.device("cpu")
275
- self.model.load_pipe(
276
- model_name,
277
- task_name=TASK_STABLEPY[task],
278
- vae_model=vae_model,
279
- type_model_precision=dtype_model,
280
- retain_task_model_in_cache=False,
281
- controlnet_model=controlnet_model,
282
- )
283
 
284
- end_time = time.time()
285
- self.sleep_loading = max(min(int(end_time - start_time), 10), 4)
286
- except Exception as e:
287
- self.last_load = datetime.now()
288
- self.status_loading = False
289
- self.sleep_loading = 4
290
- raise e
291
 
292
- self.last_load = datetime.now()
293
- self.status_loading = False
 
 
 
 
 
294
 
295
  yield f"Model loaded: {model_name}"
296
 
@@ -315,13 +536,7 @@ class GuiSD:
315
  lora_scale4,
316
  lora5,
317
  lora_scale5,
318
- lora6,
319
- lora_scale6,
320
- lora7,
321
- lora_scale7,
322
  sampler,
323
- schedule_type,
324
- schedule_prediction_type,
325
  img_height,
326
  img_width,
327
  model_name,
@@ -339,8 +554,6 @@ class GuiSD:
339
  high_threshold,
340
  value_threshold,
341
  distance_threshold,
342
- recolor_gamma_correction,
343
- tile_blur_sigma,
344
  controlnet_output_scaling_in_unet,
345
  controlnet_start_threshold,
346
  controlnet_stop_threshold,
@@ -348,8 +561,8 @@ class GuiSD:
348
  syntax_weights,
349
  upscaler_model_path,
350
  upscaler_increases_size,
351
- upscaler_tile_size,
352
- upscaler_tile_overlap,
353
  hires_steps,
354
  hires_denoising_strength,
355
  hires_sampler,
@@ -357,16 +570,12 @@ class GuiSD:
357
  hires_negative_prompt,
358
  hires_before_adetailer,
359
  hires_after_adetailer,
360
- hires_schedule_type,
361
- hires_guidance_scale,
362
- controlnet_model,
363
  loop_generation,
364
  leave_progress_bar,
365
  disable_progress_bar,
366
  image_previews,
367
  display_images,
368
  save_generated_images,
369
- filename_pattern,
370
  image_storage_location,
371
  retain_compel_previous_load,
372
  retain_detailfix_model_previous_load,
@@ -401,7 +610,6 @@ class GuiSD:
401
  mask_blur_b,
402
  mask_padding_b,
403
  retain_task_cache_gui,
404
- guidance_rescale,
405
  image_ip1,
406
  mask_ip1,
407
  model_ip1,
@@ -413,19 +621,14 @@ class GuiSD:
413
  mode_ip2,
414
  scale_ip2,
415
  pag_scale,
416
- face_restoration_model,
417
- face_restoration_visibility,
418
- face_restoration_weight,
419
  ):
420
- info_state = html_template_message("Navigating latent space...")
421
- yield info_state, gr.update(), gr.update()
422
 
423
  vae_model = vae_model if vae_model != "None" else None
424
- loras_list = [lora1, lora2, lora3, lora4, lora5, lora6, lora7]
425
  vae_msg = f"VAE: {vae_model}" if vae_model else ""
426
  msg_lora = ""
427
 
428
- logger.debug(f"Config model: {model_name}, {vae_model}, {loras_list}")
429
 
430
  task = TASK_STABLEPY[task]
431
 
@@ -440,34 +643,35 @@ class GuiSD:
440
  (image_ip2, mask_ip2, model_ip2, mode_ip2, scale_ip2),
441
  ]
442
 
443
- if not hasattr(self.model.pipe, "transformer"):
444
- for imgip, mskip, modelip, modeip, scaleip in all_adapters:
445
- if imgip:
446
- params_ip_img.append(imgip)
447
- if mskip:
448
- params_ip_msk.append(mskip)
449
- params_ip_model.append(modelip)
450
- params_ip_mode.append(modeip)
451
- params_ip_scale.append(scaleip)
452
 
453
- concurrency = 5
454
- self.model.stream_config(concurrency=concurrency, latent_resize_by=1, vae_decoding=False)
455
 
456
  if task != "txt2img" and not image_control:
457
- raise ValueError("Reference image is required. Please upload one in 'Image ControlNet/Inpaint/Img2img'.")
458
 
459
- if task in ["inpaint", "repaint"] and not image_mask:
460
- raise ValueError("Mask image not found. Upload one in 'Image Mask' to proceed.")
461
 
462
- if "https://" not in str(UPSCALER_DICT_GUI[upscaler_model_path]):
463
  upscaler_model = upscaler_model_path
464
  else:
 
 
 
465
  url_upscaler = UPSCALER_DICT_GUI[upscaler_model_path]
466
 
467
- if not os.path.exists(f"./{DIRECTORY_UPSCALERS}/{url_upscaler.split('/')[-1]}"):
468
- download_things(DIRECTORY_UPSCALERS, url_upscaler, HF_TOKEN)
469
 
470
- upscaler_model = f"./{DIRECTORY_UPSCALERS}/{url_upscaler.split('/')[-1]}"
471
 
472
  logging.getLogger("ultralytics").setLevel(logging.INFO if adetailer_verbose else logging.ERROR)
473
 
@@ -521,27 +725,19 @@ class GuiSD:
521
  "high_threshold": high_threshold,
522
  "value_threshold": value_threshold,
523
  "distance_threshold": distance_threshold,
524
- "recolor_gamma_correction": float(recolor_gamma_correction),
525
- "tile_blur_sigma": int(tile_blur_sigma),
526
- "lora_A": lora_chk(lora1),
527
  "lora_scale_A": lora_scale1,
528
- "lora_B": lora_chk(lora2),
529
  "lora_scale_B": lora_scale2,
530
- "lora_C": lora_chk(lora3),
531
  "lora_scale_C": lora_scale3,
532
- "lora_D": lora_chk(lora4),
533
  "lora_scale_D": lora_scale4,
534
- "lora_E": lora_chk(lora5),
535
  "lora_scale_E": lora_scale5,
536
- "lora_F": lora_chk(lora6),
537
- "lora_scale_F": lora_scale6,
538
- "lora_G": lora_chk(lora7),
539
- "lora_scale_G": lora_scale7,
540
- "textual_inversion": embed_list if textual_inversion else [],
541
  "syntax_weights": syntax_weights, # "Classic"
542
  "sampler": sampler,
543
- "schedule_type": schedule_type,
544
- "schedule_prediction_type": schedule_prediction_type,
545
  "xformers_memory_efficient_attention": xformers_memory_efficient_attention,
546
  "gui_active": True,
547
  "loop_generation": loop_generation,
@@ -559,7 +755,6 @@ class GuiSD:
559
  "image_previews": image_previews,
560
  "display_images": display_images,
561
  "save_generated_images": save_generated_images,
562
- "filename_pattern": filename_pattern,
563
  "image_storage_location": image_storage_location,
564
  "retain_compel_previous_load": retain_compel_previous_load,
565
  "retain_detailfix_model_previous_load": retain_detailfix_model_previous_load,
@@ -569,8 +764,8 @@ class GuiSD:
569
  "t2i_adapter_conditioning_factor": float(t2i_adapter_conditioning_factor),
570
  "upscaler_model_path": upscaler_model,
571
  "upscaler_increases_size": upscaler_increases_size,
572
- "upscaler_tile_size": upscaler_tile_size,
573
- "upscaler_tile_overlap": upscaler_tile_overlap,
574
  "hires_steps": hires_steps,
575
  "hires_denoising_strength": hires_denoising_strength,
576
  "hires_prompt": hires_prompt,
@@ -578,41 +773,25 @@ class GuiSD:
578
  "hires_sampler": hires_sampler,
579
  "hires_before_adetailer": hires_before_adetailer,
580
  "hires_after_adetailer": hires_after_adetailer,
581
- "hires_schedule_type": hires_schedule_type,
582
- "hires_guidance_scale": hires_guidance_scale,
583
  "ip_adapter_image": params_ip_img,
584
  "ip_adapter_mask": params_ip_msk,
585
  "ip_adapter_model": params_ip_model,
586
  "ip_adapter_mode": params_ip_mode,
587
  "ip_adapter_scale": params_ip_scale,
588
- "face_restoration_model": face_restoration_model,
589
- "face_restoration_visibility": face_restoration_visibility,
590
- "face_restoration_weight": face_restoration_weight,
591
  }
592
 
593
- # kwargs for diffusers pipeline
594
- if guidance_rescale:
595
- pipe_params["guidance_rescale"] = guidance_rescale
596
- if IS_ZERO_GPU:
597
- self.model.device = torch.device("cuda:0")
598
- if hasattr(self.model.pipe, "transformer") and loras_list != ["None"] * self.model.num_loras:
599
- self.model.pipe.transformer.to(self.model.device)
600
- logger.debug("transformer to cuda")
601
-
602
- actual_progress = 0
603
- info_images = gr.update()
604
- for img, [seed, image_path, metadata] in self.model(**pipe_params):
605
- info_state = progress_step_bar(actual_progress, steps)
606
- actual_progress += concurrency
607
  if image_path:
608
- info_images = f"Seeds: {str(seed)}"
609
  if vae_msg:
610
- info_images = info_images + "<br>" + vae_msg
611
-
612
- if "Cannot copy out of meta tensor; no data!" in self.model.last_lora_error:
613
- msg_ram = "Unable to process the LoRAs due to high RAM usage; please try again later."
614
- print(msg_ram)
615
- msg_lora += f"<br>{msg_ram}"
616
 
617
  for status, lora in zip(self.model.lora_status, self.model.lora_memory):
618
  if status:
@@ -621,27 +800,33 @@ class GuiSD:
621
  msg_lora += f"<br>Error with: {lora}"
622
 
623
  if msg_lora:
624
- info_images += msg_lora
625
 
626
- info_images = info_images + "<br>" + "GENERATION DATA:<br>" + escape_html(metadata[-1]) + "<br>-------<br>"
627
 
628
  download_links = "<br>".join(
629
  [
630
- f'<a href="{path.replace("/images/", f"/gradio_api/file={allowed_path}/")}" download="{os.path.basename(path)}">Download Image {i + 1}</a>'
631
  for i, path in enumerate(image_path)
632
  ]
633
  )
634
  if save_generated_images:
635
- info_images += f"<br>{download_links}"
 
 
 
 
 
 
636
 
637
- info_state = "COMPLETE"
 
638
 
639
- yield info_state, img, info_images
640
 
641
 
642
  def dynamic_gpu_duration(func, duration, *args):
643
 
644
- # @torch.inference_mode()
645
  @spaces.GPU(duration=duration)
646
  def wrapped_func():
647
  yield from func(*args)
@@ -655,44 +840,35 @@ def dummy_gpu():
655
 
656
 
657
  def sd_gen_generate_pipeline(*args):
 
658
  gpu_duration_arg = int(args[-1]) if args[-1] else 59
659
  verbose_arg = int(args[-2])
660
  load_lora_cpu = args[-3]
661
  generation_args = args[:-3]
662
  lora_list = [
663
  None if item == "None" else item
664
- for item in [args[7], args[9], args[11], args[13], args[15], args[17], args[19]]
665
  ]
666
- lora_status = [None] * sd_gen.model.num_loras
667
 
668
  msg_load_lora = "Updating LoRAs in GPU..."
669
  if load_lora_cpu:
670
- msg_load_lora = "Updating LoRAs in CPU..."
671
 
672
- if lora_list != sd_gen.model.lora_memory and lora_list != [None] * sd_gen.model.num_loras:
673
- yield msg_load_lora, gr.update(), gr.update()
674
 
675
  # Load lora in CPU
676
  if load_lora_cpu:
677
- lora_status = sd_gen.model.load_lora_on_the_fly(
678
  lora_A=lora_list[0], lora_scale_A=args[8],
679
  lora_B=lora_list[1], lora_scale_B=args[10],
680
  lora_C=lora_list[2], lora_scale_C=args[12],
681
  lora_D=lora_list[3], lora_scale_D=args[14],
682
  lora_E=lora_list[4], lora_scale_E=args[16],
683
- lora_F=lora_list[5], lora_scale_F=args[18],
684
- lora_G=lora_list[6], lora_scale_G=args[20],
685
  )
686
  print(lora_status)
687
 
688
- sampler_name = args[21]
689
- schedule_type_name = args[22]
690
- _, _, msg_sampler = check_scheduler_compatibility(
691
- sd_gen.model.class_name, sampler_name, schedule_type_name
692
- )
693
- if msg_sampler:
694
- gr.Warning(msg_sampler)
695
-
696
  if verbose_arg:
697
  for status, lora in zip(lora_status, lora_list):
698
  if status:
@@ -700,21 +876,20 @@ def sd_gen_generate_pipeline(*args):
700
  elif status is not None:
701
  gr.Warning(f"Failed to load LoRA: {lora}")
702
 
703
- if lora_status == [None] * sd_gen.model.num_loras and sd_gen.model.lora_memory != [None] * sd_gen.model.num_loras and load_lora_cpu:
704
  lora_cache_msg = ", ".join(
705
  str(x) for x in sd_gen.model.lora_memory if x is not None
706
  )
707
  gr.Info(f"LoRAs in cache: {lora_cache_msg}")
708
 
709
- msg_request = f"Requesting {gpu_duration_arg}s. of GPU time.\nModel: {sd_gen.model.base_model_id}"
710
- if verbose_arg:
711
  gr.Info(msg_request)
712
  print(msg_request)
713
- yield msg_request.replace("\n", "<br>"), gr.update(), gr.update()
 
714
 
715
  start_time = time.time()
716
 
717
- # yield from sd_gen.generate_pipeline(*generation_args)
718
  yield from dynamic_gpu_duration(
719
  sd_gen.generate_pipeline,
720
  gpu_duration_arg,
@@ -722,52 +897,60 @@ def sd_gen_generate_pipeline(*args):
722
  )
723
 
724
  end_time = time.time()
725
- execution_time = end_time - start_time
726
- msg_task_complete = (
727
- f"GPU task complete in: {int(round(execution_time, 0) + 1)} seconds"
728
- )
729
 
730
  if verbose_arg:
 
 
 
 
731
  gr.Info(msg_task_complete)
732
  print(msg_task_complete)
733
 
734
- yield msg_task_complete, gr.update(), gr.update()
735
 
 
 
736
 
737
- @spaces.GPU(duration=15)
738
- def process_upscale(image, upscaler_name, upscaler_size):
739
- if image is None:
740
- return None
741
 
742
- from stablepy.diffusers_vanilla.utils import save_pil_image_with_metadata
743
- from stablepy import load_upscaler_model
 
744
 
745
- image = image.convert("RGB")
746
- exif_image = extract_exif_data(image)
 
 
747
 
748
- name_upscaler = UPSCALER_DICT_GUI[upscaler_name]
749
 
750
- if "https://" in str(name_upscaler):
 
 
 
 
 
751
 
752
- if not os.path.exists(f"./{DIRECTORY_UPSCALERS}/{name_upscaler.split('/')[-1]}"):
753
- download_things(DIRECTORY_UPSCALERS, name_upscaler, HF_TOKEN)
754
 
755
- name_upscaler = f"./{DIRECTORY_UPSCALERS}/{name_upscaler.split('/')[-1]}"
 
 
 
 
756
 
757
- scaler_beta = load_upscaler_model(model=name_upscaler, tile=(0 if IS_ZERO_GPU else 192), tile_overlap=8, device=("cuda" if IS_GPU_MODE else "cpu"), half=IS_GPU_MODE)
758
- image_up = scaler_beta.upscale(image, upscaler_size, True)
759
 
760
  image_path = save_pil_image_with_metadata(image_up, f'{os.getcwd()}/up_images', exif_image)
761
 
762
  return image_path
763
 
764
 
765
- # https://huggingface.co/spaces/BestWishYsh/ConsisID-preview-Space/discussions/1#674969a022b99c122af5d407
766
- # dynamic_gpu_duration.zerogpu = True
767
- # sd_gen_generate_pipeline.zerogpu = True
768
  sd_gen = GuiSD()
769
 
770
- with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as app:
771
  gr.Markdown("# 🧩 DiffuseCraft")
772
  gr.Markdown(SUBTITLE_GUI)
773
  with gr.Tab("Generation"):
@@ -775,18 +958,10 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
775
 
776
  with gr.Column(scale=2):
777
 
778
- def update_task_options(model_name, task_name):
779
- new_choices = MODEL_TYPE_TASK[get_model_type(model_name)]
780
-
781
- if task_name not in new_choices:
782
- task_name = "txt2img"
783
-
784
- return gr.update(value=task_name, choices=new_choices)
785
-
786
  task_gui = gr.Dropdown(label="Task", choices=SDXL_TASK, value=TASK_MODEL_LIST[0])
787
  model_name_gui = gr.Dropdown(label="Model", choices=model_list, value=model_list[0], allow_custom_value=True)
788
  prompt_gui = gr.Textbox(lines=5, placeholder="Enter prompt", label="Prompt")
789
- neg_prompt_gui = gr.Textbox(lines=3, placeholder="Enter Neg prompt", label="Negative prompt", value="lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, worst quality, low quality, very displeasing, (bad)")
790
  with gr.Row(equal_height=False):
791
  set_params_gui = gr.Button(value="↙️", variant="secondary", size="sm")
792
  clear_prompt_gui = gr.Button(value="🗑️", variant="secondary", size="sm")
@@ -799,7 +974,7 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
799
  [task_gui],
800
  )
801
 
802
- load_model_gui = gr.HTML(elem_id="load_model", elem_classes="contain")
803
 
804
  result_images = gr.Gallery(
805
  label="Generated images",
@@ -816,17 +991,16 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
816
 
817
  actual_task_info = gr.HTML()
818
 
819
- with gr.Row(equal_height=False, variant="default", visible=IS_ZERO_GPU):
820
  gpu_duration_gui = gr.Number(minimum=5, maximum=240, value=59, show_label=False, container=False, info="GPU time duration (seconds)")
821
  with gr.Column():
822
  verbose_info_gui = gr.Checkbox(value=False, container=False, label="Status info")
823
- load_lora_cpu_gui = gr.Checkbox(value=False, container=False, label="Load LoRAs on CPU")
824
 
825
  with gr.Column(scale=1):
826
- steps_gui = gr.Slider(minimum=1, maximum=100, step=1, value=28, label="Steps")
827
  cfg_gui = gr.Slider(minimum=0, maximum=30, step=0.5, value=7., label="CFG")
828
- sampler_gui = gr.Dropdown(label="Sampler", choices=scheduler_names, value="Euler")
829
- schedule_type_gui = gr.Dropdown(label="Schedule type", choices=SCHEDULE_TYPE_OPTIONS, value=SCHEDULE_TYPE_OPTIONS[0])
830
  img_width_gui = gr.Slider(minimum=64, maximum=4096, step=8, value=1024, label="Img Width")
831
  img_height_gui = gr.Slider(minimum=64, maximum=4096, step=8, value=1024, label="Img Height")
832
  seed_gui = gr.Number(minimum=-1, maximum=9999999999, value=-1, label="Seed")
@@ -845,72 +1019,15 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
845
  "width": gr.update(value=1024),
846
  "height": gr.update(value=1024),
847
  "Seed": gr.update(value=-1),
848
- "Sampler": gr.update(value="Euler"),
849
- "CFG scale": gr.update(value=7.), # cfg
850
- "Clip skip": gr.update(value=True),
851
  "Model": gr.update(value=name_model),
852
- "Schedule type": gr.update(value="Automatic"),
853
- "PAG": gr.update(value=.0),
854
- "FreeU": gr.update(value=False),
855
- "Hires upscaler": gr.update(),
856
- "Hires upscale": gr.update(),
857
- "Hires steps": gr.update(),
858
- "Hires denoising strength": gr.update(),
859
- "Hires CFG": gr.update(),
860
- "Hires sampler": gr.update(),
861
- "Hires schedule type": gr.update(),
862
- "Image resolution": gr.update(value=1024),
863
- "Strength": gr.update(),
864
  }
865
-
866
- # Generate up to 7 LoRAs
867
- for i in range(1, 8):
868
- valid_receptors[f"Lora_{i}"] = gr.update()
869
- valid_receptors[f"Lora_scale_{i}"] = gr.update()
870
-
871
  valid_keys = list(valid_receptors.keys())
872
 
873
  parameters = extract_parameters(base_prompt)
874
- # print(parameters)
875
-
876
- if "Sampler" in parameters:
877
- value_sampler = parameters["Sampler"]
878
- for s_type in SCHEDULE_TYPE_OPTIONS:
879
- if s_type in value_sampler:
880
- value_sampler = value_sampler.replace(s_type, "").strip()
881
- parameters["Sampler"] = value_sampler
882
- parameters["Schedule type"] = s_type
883
-
884
- params_lora = []
885
- if ">" in parameters["prompt"] and "<" in parameters["prompt"]:
886
- params_lora = re.findall(r'<lora:[^>]+>', parameters["prompt"])
887
- if "Loras" in parameters:
888
- params_lora += re.findall(r'<lora:[^>]+>', parameters["Loras"])
889
-
890
- if params_lora:
891
- parsed_params = []
892
- for tag_l in params_lora:
893
- try:
894
- inner = tag_l.strip("<>") # remove < >
895
- _, data_l = inner.split(":", 1) # remove the "lora:" part
896
- parts_l = data_l.split(":")
897
-
898
- name_l = parts_l[0]
899
- weight_l = float(parts_l[1]) if len(parts_l) > 1 else 1.0 # default weight = 1.0
900
-
901
- parsed_params.append((name_l, weight_l))
902
- except Exception as e:
903
- print(f"Error parsing LoRA tag {tag_l}: {e}")
904
 
905
- num_lora = 1
906
- for parsed_l, parsed_s in parsed_params:
907
- filtered_loras = [m for m in lora_model_list if parsed_l in m]
908
- if filtered_loras:
909
- parameters[f"Lora_{num_lora}"] = filtered_loras[0]
910
- parameters[f"Lora_scale_{num_lora}"] = parsed_s
911
- num_lora += 1
912
-
913
- # continue = discard new value
914
  for key, val in parameters.items():
915
  # print(val)
916
  if key in valid_keys:
@@ -918,28 +1035,20 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
918
  if key == "Sampler":
919
  if val not in scheduler_names:
920
  continue
921
- if key in ["Schedule type", "Hires schedule type"]:
922
- if val not in SCHEDULE_TYPE_OPTIONS:
923
- continue
924
- if key == "Hires sampler":
925
- if val not in POST_PROCESSING_SAMPLER:
926
- continue
927
- elif key == "Clip skip":
928
  if "," in str(val):
929
  val = val.replace(",", "")
930
  if int(val) >= 2:
931
  val = True
932
  if key == "prompt":
933
  if ">" in val and "<" in val:
934
- val = re.sub(r'<[^>]+>', '', val) # Delete html and loras
935
  print("Removed LoRA written in the prompt")
936
  if key in ["prompt", "neg_prompt"]:
937
  val = re.sub(r'\s+', ' ', re.sub(r',+', ',', val)).strip()
938
- if key in ["Steps", "width", "height", "Seed", "Hires steps", "Image resolution"]:
939
  val = int(val)
940
- if key == "FreeU":
941
- val = True
942
- if key in ["CFG scale", "PAG", "Hires upscale", "Hires denoising strength", "Hires CFG", "Strength"]:
943
  val = float(val)
944
  if key == "Model":
945
  filtered_models = [m for m in model_list if val in m]
@@ -947,12 +1056,8 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
947
  val = filtered_models[0]
948
  else:
949
  val = name_model
950
- if key == "Hires upscaler":
951
- if val not in UPSCALER_KEYS:
952
- continue
953
  if key == "Seed":
954
  continue
955
-
956
  valid_receptors[key] = gr.update(value=val)
957
  # print(val, type(val))
958
  # print(valid_receptors)
@@ -960,6 +1065,21 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
960
  print(str(e))
961
  return [value for value in valid_receptors.values()]
962
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
963
  def run_clear_prompt_gui():
964
  return gr.update(value=""), gr.update(value="")
965
  clear_prompt_gui.click(
@@ -972,33 +1092,37 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
972
  run_set_random_seed, [], seed_gui
973
  )
974
 
975
- num_images_gui = gr.Slider(minimum=1, maximum=(5 if IS_ZERO_GPU else 20), step=1, value=1, label="Images")
976
- prompt_syntax_gui = gr.Dropdown(label="Prompt Syntax", choices=PROMPT_W_OPTIONS, value=PROMPT_W_OPTIONS[1][1])
 
 
 
 
 
 
 
 
977
  vae_model_gui = gr.Dropdown(label="VAE Model", choices=vae_model_list, value=vae_model_list[0])
978
 
979
  with gr.Accordion("Hires fix", open=False, visible=True):
980
 
981
  upscaler_model_path_gui = gr.Dropdown(label="Upscaler", choices=UPSCALER_KEYS, value=UPSCALER_KEYS[0])
982
  upscaler_increases_size_gui = gr.Slider(minimum=1.1, maximum=4., step=0.1, value=1.2, label="Upscale by")
983
- upscaler_tile_size_gui = gr.Slider(minimum=0, maximum=512, step=16, value=(0 if IS_ZERO_GPU else 192), label="Upscaler Tile Size", info="0 = no tiling")
984
- upscaler_tile_overlap_gui = gr.Slider(minimum=0, maximum=48, step=1, value=8, label="Upscaler Tile Overlap")
985
  hires_steps_gui = gr.Slider(minimum=0, value=30, maximum=100, step=1, label="Hires Steps")
986
  hires_denoising_strength_gui = gr.Slider(minimum=0.1, maximum=1.0, step=0.01, value=0.55, label="Hires Denoising Strength")
987
  hires_sampler_gui = gr.Dropdown(label="Hires Sampler", choices=POST_PROCESSING_SAMPLER, value=POST_PROCESSING_SAMPLER[0])
988
- hires_schedule_list = ["Use same schedule type"] + SCHEDULE_TYPE_OPTIONS
989
- hires_schedule_type_gui = gr.Dropdown(label="Hires Schedule type", choices=hires_schedule_list, value=hires_schedule_list[0])
990
- hires_guidance_scale_gui = gr.Slider(minimum=-1., maximum=30., step=0.5, value=-1., label="Hires CFG", info="If the value is -1, the main CFG will be used")
991
  hires_prompt_gui = gr.Textbox(label="Hires Prompt", placeholder="Main prompt will be use", lines=3)
992
  hires_negative_prompt_gui = gr.Textbox(label="Hires Negative Prompt", placeholder="Main negative prompt will be use", lines=3)
993
 
994
  with gr.Accordion("LoRA", open=False, visible=True):
995
 
996
- def lora_dropdown(label, visible=True):
997
- return gr.Dropdown(label=label, choices=lora_model_list, value="None", allow_custom_value=True, visible=visible)
998
 
999
- def lora_scale_slider(label, visible=True):
1000
- val_lora = 2 if IS_ZERO_GPU else 8
1001
- return gr.Slider(minimum=-val_lora, maximum=val_lora, step=0.01, value=0.33, label=label, visible=visible)
1002
 
1003
  lora1_gui = lora_dropdown("Lora1")
1004
  lora_scale_1_gui = lora_scale_slider("Lora Scale 1")
@@ -1010,37 +1134,21 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
1010
  lora_scale_4_gui = lora_scale_slider("Lora Scale 4")
1011
  lora5_gui = lora_dropdown("Lora5")
1012
  lora_scale_5_gui = lora_scale_slider("Lora Scale 5")
1013
- lora6_gui = lora_dropdown("Lora6", visible=(not IS_ZERO_GPU))
1014
- lora_scale_6_gui = lora_scale_slider("Lora Scale 6", visible=(not IS_ZERO_GPU))
1015
- lora7_gui = lora_dropdown("Lora7", visible=(not IS_ZERO_GPU))
1016
- lora_scale_7_gui = lora_scale_slider("Lora Scale 7", visible=(not IS_ZERO_GPU))
1017
 
1018
  with gr.Accordion("From URL", open=False, visible=True):
1019
- text_lora = gr.Textbox(
1020
- label="LoRA's download URL",
1021
- placeholder="https://civitai.com/api/download/models/28907",
1022
- lines=1,
1023
- info="It has to be .safetensors files, and you can also download them from Hugging Face.",
1024
- )
1025
- romanize_text = gr.Checkbox(value=False, label="Transliterate name", visible=(not IS_ZERO_GPU))
1026
- button_lora = gr.Button("Get and Refresh the LoRA Lists")
1027
- new_lora_status = gr.HTML()
1028
  button_lora.click(
1029
  get_my_lora,
1030
- [text_lora, romanize_text],
1031
- [lora1_gui, lora2_gui, lora3_gui, lora4_gui, lora5_gui, lora6_gui, lora7_gui, new_lora_status]
1032
  )
1033
 
1034
- with gr.Accordion("Face restoration", open=False, visible=True):
1035
-
1036
- face_rest_options = [None] + FACE_RESTORATION_MODELS
1037
-
1038
- face_restoration_model_gui = gr.Dropdown(label="Face restoration model", choices=face_rest_options, value=face_rest_options[0])
1039
- face_restoration_visibility_gui = gr.Slider(minimum=0., maximum=1., step=0.001, value=1., label="Visibility")
1040
- face_restoration_weight_gui = gr.Slider(minimum=0., maximum=1., step=0.001, value=.5, label="Weight", info="(0 = maximum effect, 1 = minimum effect)")
1041
-
1042
  with gr.Accordion("IP-Adapter", open=False, visible=True):
1043
 
 
 
 
1044
  with gr.Accordion("IP-Adapter 1", open=False, visible=True):
1045
  image_ip1 = gr.Image(label="IP Image", type="filepath")
1046
  mask_ip1 = gr.Image(label="IP Mask", type="filepath")
@@ -1059,38 +1167,32 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
1059
  image_mask_gui = gr.Image(label="Image Mask", type="filepath")
1060
  strength_gui = gr.Slider(
1061
  minimum=0.01, maximum=1.0, step=0.01, value=0.55, label="Strength",
1062
- info="This option adjusts the level of changes for img2img, repaint and inpaint."
1063
  )
1064
- image_resolution_gui = gr.Slider(
1065
- minimum=64, maximum=2048, step=64, value=1024, label="Image Resolution",
1066
- info="The maximum proportional size of the generated image based on the uploaded image."
1067
- )
1068
- controlnet_model_gui = gr.Dropdown(label="ControlNet model", choices=DIFFUSERS_CONTROLNET_MODEL, value=DIFFUSERS_CONTROLNET_MODEL[0], allow_custom_value=True)
1069
- control_net_output_scaling_gui = gr.Slider(minimum=0, maximum=5.0, step=0.1, value=1, label="ControlNet Output Scaling in UNet")
1070
- control_net_start_threshold_gui = gr.Slider(minimum=0, maximum=1, step=0.01, value=0, label="ControlNet Start Threshold (%)")
1071
- control_net_stop_threshold_gui = gr.Slider(minimum=0, maximum=1, step=0.01, value=1, label="ControlNet Stop Threshold (%)")
1072
- preprocessor_name_gui = gr.Dropdown(label="Preprocessor Name", choices=TASK_AND_PREPROCESSORS["canny"])
1073
 
1074
  def change_preprocessor_choices(task):
1075
  task = TASK_STABLEPY[task]
1076
- if task in TASK_AND_PREPROCESSORS.keys():
1077
- choices_task = TASK_AND_PREPROCESSORS[task]
1078
  else:
1079
- choices_task = TASK_AND_PREPROCESSORS["canny"]
1080
  return gr.update(choices=choices_task, value=choices_task[0])
 
1081
  task_gui.change(
1082
  change_preprocessor_choices,
1083
  [task_gui],
1084
  [preprocessor_name_gui],
1085
  )
1086
-
1087
- preprocess_resolution_gui = gr.Slider(minimum=64, maximum=2048, step=64, value=512, label="Preprocessor Resolution")
1088
- low_threshold_gui = gr.Slider(minimum=1, maximum=255, step=1, value=100, label="'CANNY' low threshold")
1089
- high_threshold_gui = gr.Slider(minimum=1, maximum=255, step=1, value=200, label="'CANNY' high threshold")
1090
- value_threshold_gui = gr.Slider(minimum=0.0, maximum=2.0, step=0.01, value=0.1, label="'MLSD' Hough value threshold")
1091
- distance_threshold_gui = gr.Slider(minimum=0.0, maximum=20.0, step=0.01, value=0.1, label="'MLSD' Hough distance threshold")
1092
- recolor_gamma_correction_gui = gr.Number(minimum=0., maximum=25., value=1., step=0.001, label="'RECOLOR' gamma correction")
1093
- tile_blur_sigma_gui = gr.Number(minimum=0, maximum=100, value=9, step=1, label="'TILE' blur sigma")
1094
 
1095
  with gr.Accordion("T2I adapter", open=False, visible=False):
1096
  t2i_adapter_preprocessor_gui = gr.Checkbox(value=True, label="T2i Adapter Preprocessor")
@@ -1123,7 +1225,7 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
1123
  gr.Info(f"{len(sd_gen.model.STYLE_NAMES)} styles loaded")
1124
  return gr.update(value=None, choices=sd_gen.model.STYLE_NAMES)
1125
 
1126
- style_button.click(load_json_style_file, [style_json_gui], [style_prompt_gui])
1127
 
1128
  with gr.Accordion("Textual inversion", open=False, visible=False):
1129
  active_textual_inversion_gui = gr.Checkbox(value=False, label="Active Textual Inversion in prompt")
@@ -1146,7 +1248,7 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
1146
  negative_prompt_ad_a_gui = gr.Textbox(label="Negative prompt", placeholder="Main negative prompt will be use", lines=3)
1147
  strength_ad_a_gui = gr.Number(label="Strength:", value=0.35, step=0.01, minimum=0.01, maximum=1.0)
1148
  face_detector_ad_a_gui = gr.Checkbox(label="Face detector", value=True)
1149
- person_detector_ad_a_gui = gr.Checkbox(label="Person detector", value=False)
1150
  hand_detector_ad_a_gui = gr.Checkbox(label="Hand detector", value=False)
1151
  mask_dilation_a_gui = gr.Number(label="Mask dilation:", value=4, minimum=1)
1152
  mask_blur_a_gui = gr.Number(label="Mask blur:", value=4, minimum=1)
@@ -1158,7 +1260,7 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
1158
  prompt_ad_b_gui = gr.Textbox(label="Main prompt", placeholder="Main prompt will be use", lines=3)
1159
  negative_prompt_ad_b_gui = gr.Textbox(label="Negative prompt", placeholder="Main negative prompt will be use", lines=3)
1160
  strength_ad_b_gui = gr.Number(label="Strength:", value=0.35, step=0.01, minimum=0.01, maximum=1.0)
1161
- face_detector_ad_b_gui = gr.Checkbox(label="Face detector", value=False)
1162
  person_detector_ad_b_gui = gr.Checkbox(label="Person detector", value=True)
1163
  hand_detector_ad_b_gui = gr.Checkbox(label="Hand detector", value=False)
1164
  mask_dilation_b_gui = gr.Number(label="Mask dilation:", value=4, minimum=1)
@@ -1166,74 +1268,191 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
1166
  mask_padding_b_gui = gr.Number(label="Mask padding:", value=32, minimum=1)
1167
 
1168
  with gr.Accordion("Other settings", open=False, visible=True):
1169
- schedule_prediction_type_gui = gr.Dropdown(label="Discrete Sampling Type", choices=SCHEDULE_PREDICTION_TYPE_OPTIONS, value=SCHEDULE_PREDICTION_TYPE_OPTIONS[0])
1170
- guidance_rescale_gui = gr.Number(label="CFG rescale:", value=0., step=0.01, minimum=0., maximum=1.5)
1171
  save_generated_images_gui = gr.Checkbox(value=True, label="Create a download link for the images")
1172
- filename_pattern_gui = gr.Textbox(label="Filename pattern", value="model,seed", placeholder="model,seed,sampler,schedule_type,img_width,img_height,guidance_scale,num_steps,vae,prompt_section,neg_prompt_section", lines=1)
1173
  hires_before_adetailer_gui = gr.Checkbox(value=False, label="Hires Before Adetailer")
1174
  hires_after_adetailer_gui = gr.Checkbox(value=True, label="Hires After Adetailer")
1175
  generator_in_cpu_gui = gr.Checkbox(value=False, label="Generator in CPU")
1176
- with gr.Column(visible=(not IS_ZERO_GPU)):
1177
- image_storage_location_gui = gr.Textbox(value=img_path, label="Image Storage Location")
1178
- disable_progress_bar_gui = gr.Checkbox(value=False, label="Disable Progress Bar")
1179
- leave_progress_bar_gui = gr.Checkbox(value=True, label="Leave Progress Bar")
1180
 
1181
  with gr.Accordion("More settings", open=False, visible=False):
1182
  loop_generation_gui = gr.Slider(minimum=1, value=1, label="Loop Generation")
1183
  retain_task_cache_gui = gr.Checkbox(value=False, label="Retain task model in cache")
1184
- display_images_gui = gr.Checkbox(value=False, label="Display Images")
 
 
1185
  image_previews_gui = gr.Checkbox(value=True, label="Image Previews")
 
1186
  retain_compel_previous_load_gui = gr.Checkbox(value=False, label="Retain Compel Previous Load")
1187
  retain_detailfix_model_previous_load_gui = gr.Checkbox(value=False, label="Retain Detailfix Model Previous Load")
1188
  retain_hires_model_previous_load_gui = gr.Checkbox(value=False, label="Retain Hires Model Previous Load")
1189
  xformers_memory_efficient_attention_gui = gr.Checkbox(value=False, label="Xformers Memory Efficient Attention")
1190
 
1191
- set_params_gui.click(
1192
- run_set_params_gui, [prompt_gui, model_name_gui], [
1193
- prompt_gui,
1194
- neg_prompt_gui,
1195
- steps_gui,
1196
- img_width_gui,
1197
- img_height_gui,
1198
- seed_gui,
1199
- sampler_gui,
1200
- cfg_gui,
1201
- clip_skip_gui,
1202
- model_name_gui,
1203
- schedule_type_gui,
1204
- pag_scale_gui,
1205
- free_u_gui,
1206
- upscaler_model_path_gui,
1207
- upscaler_increases_size_gui,
1208
- hires_steps_gui,
1209
- hires_denoising_strength_gui,
1210
- hires_guidance_scale_gui,
1211
- hires_sampler_gui,
1212
- hires_schedule_type_gui,
1213
- image_resolution_gui,
1214
- strength_gui,
1215
- lora1_gui,
1216
- lora_scale_1_gui,
1217
- lora2_gui,
1218
- lora_scale_2_gui,
1219
- lora3_gui,
1220
- lora_scale_3_gui,
1221
- lora4_gui,
1222
- lora_scale_4_gui,
1223
- lora5_gui,
1224
- lora_scale_5_gui,
1225
- lora6_gui,
1226
- lora_scale_6_gui,
1227
- lora7_gui,
1228
- lora_scale_7_gui,
1229
- ],
1230
- )
1231
-
1232
  with gr.Accordion("Examples and help", open=False, visible=True):
1233
- gr.Markdown(HELP_GUI)
1234
- gr.Markdown(EXAMPLES_GUI_HELP)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1235
  gr.Examples(
1236
- examples=EXAMPLES_GUI,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1237
  fn=sd_gen.generate_pipeline,
1238
  inputs=[
1239
  prompt_gui,
@@ -1259,13 +1478,45 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
1259
  gpu_duration_gui,
1260
  load_lora_cpu_gui,
1261
  ],
1262
- outputs=[load_model_gui, result_images, actual_task_info],
1263
  cache_examples=False,
1264
  )
1265
- gr.Markdown(RESOURCES)
 
 
 
 
 
1266
 
1267
  with gr.Tab("Inpaint mask maker", render=True):
1268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1269
  with gr.Row():
1270
  with gr.Column(scale=2):
1271
  image_base = gr.ImageEditor(
@@ -1274,31 +1525,20 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
1274
  # enable crop (or disable it)
1275
  # transforms=["crop"],
1276
  brush=gr.Brush(
1277
- default_size="16", # or leave it as 'auto'
1278
- color_mode="fixed", # 'fixed' hides the user swatches and colorpicker, 'defaults' shows it
1279
- # default_color="black", # html names are supported
1280
- colors=[
1281
- "rgba(0, 0, 0, 1)", # rgb(a)
1282
- "rgba(0, 0, 0, 0.1)",
1283
- "rgba(255, 255, 255, 0.1)",
1284
- # "hsl(360, 120, 120)" # in fact any valid colorstring
1285
- ]
1286
  ),
1287
- eraser=gr.Eraser(default_size="16"),
1288
- render=True,
1289
- visible=False,
1290
- interactive=False,
1291
  )
1292
-
1293
- show_canvas = gr.Button("SHOW INPAINT CANVAS")
1294
-
1295
- def change_visibility_canvas():
1296
- return gr.update(visible=True, interactive=True), gr.update(visible=False)
1297
- show_canvas.click(change_visibility_canvas, [], [image_base, show_canvas])
1298
-
1299
  invert_mask = gr.Checkbox(value=False, label="Invert mask")
1300
  btn = gr.Button("Create mask")
1301
-
1302
  with gr.Column(scale=1):
1303
  img_source = gr.Image(interactive=False)
1304
  img_result = gr.Image(label="Mask image", show_label=True, interactive=False)
@@ -1329,11 +1569,8 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
1329
 
1330
  with gr.Row():
1331
  with gr.Column():
1332
-
1333
- USCALER_TAB_KEYS = [name for name in UPSCALER_KEYS[9:]]
1334
-
1335
  image_up_tab = gr.Image(label="Image", type="pil", sources=["upload"])
1336
- upscaler_tab = gr.Dropdown(label="Upscaler", choices=USCALER_TAB_KEYS, value=USCALER_TAB_KEYS[5])
1337
  upscaler_size_tab = gr.Slider(minimum=1., maximum=4., step=0.1, value=1.1, label="Upscale by")
1338
  generate_button_up_tab = gr.Button(value="START UPSCALE", variant="primary")
1339
 
@@ -1341,21 +1578,17 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
1341
  result_up_tab = gr.Image(label="Result", type="pil", interactive=False, format="png")
1342
 
1343
  generate_button_up_tab.click(
1344
- fn=process_upscale,
1345
  inputs=[image_up_tab, upscaler_tab, upscaler_size_tab],
1346
  outputs=[result_up_tab],
1347
  )
1348
 
1349
- with gr.Tab("Preprocessor", render=True):
1350
- preprocessor_tab()
1351
-
1352
  generate_button.click(
1353
  fn=sd_gen.load_new_model,
1354
  inputs=[
1355
  model_name_gui,
1356
  vae_model_gui,
1357
- task_gui,
1358
- controlnet_model_gui,
1359
  ],
1360
  outputs=[load_model_gui],
1361
  queue=True,
@@ -1380,13 +1613,7 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
1380
  lora_scale_4_gui,
1381
  lora5_gui,
1382
  lora_scale_5_gui,
1383
- lora6_gui,
1384
- lora_scale_6_gui,
1385
- lora7_gui,
1386
- lora_scale_7_gui,
1387
  sampler_gui,
1388
- schedule_type_gui,
1389
- schedule_prediction_type_gui,
1390
  img_height_gui,
1391
  img_width_gui,
1392
  model_name_gui,
@@ -1404,8 +1631,6 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
1404
  high_threshold_gui,
1405
  value_threshold_gui,
1406
  distance_threshold_gui,
1407
- recolor_gamma_correction_gui,
1408
- tile_blur_sigma_gui,
1409
  control_net_output_scaling_gui,
1410
  control_net_start_threshold_gui,
1411
  control_net_stop_threshold_gui,
@@ -1413,8 +1638,8 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
1413
  prompt_syntax_gui,
1414
  upscaler_model_path_gui,
1415
  upscaler_increases_size_gui,
1416
- upscaler_tile_size_gui,
1417
- upscaler_tile_overlap_gui,
1418
  hires_steps_gui,
1419
  hires_denoising_strength_gui,
1420
  hires_sampler_gui,
@@ -1422,16 +1647,12 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
1422
  hires_negative_prompt_gui,
1423
  hires_before_adetailer_gui,
1424
  hires_after_adetailer_gui,
1425
- hires_schedule_type_gui,
1426
- hires_guidance_scale_gui,
1427
- controlnet_model_gui,
1428
  loop_generation_gui,
1429
  leave_progress_bar_gui,
1430
  disable_progress_bar_gui,
1431
  image_previews_gui,
1432
  display_images_gui,
1433
  save_generated_images_gui,
1434
- filename_pattern_gui,
1435
  image_storage_location_gui,
1436
  retain_compel_previous_load_gui,
1437
  retain_detailfix_model_previous_load_gui,
@@ -1466,7 +1687,6 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
1466
  mask_blur_b_gui,
1467
  mask_padding_b_gui,
1468
  retain_task_cache_gui,
1469
- guidance_rescale_gui,
1470
  image_ip1,
1471
  mask_ip1,
1472
  model_ip1,
@@ -1478,24 +1698,19 @@ with gr.Blocks(theme=args.theme, css=CSS, fill_width=True, fill_height=False) as
1478
  mode_ip2,
1479
  scale_ip2,
1480
  pag_scale_gui,
1481
- face_restoration_model_gui,
1482
- face_restoration_visibility_gui,
1483
- face_restoration_weight_gui,
1484
  load_lora_cpu_gui,
1485
  verbose_info_gui,
1486
  gpu_duration_gui,
1487
  ],
1488
- outputs=[load_model_gui, result_images, actual_task_info],
1489
  queue=True,
1490
  show_progress="minimal",
1491
  )
1492
 
1493
- if __name__ == "__main__":
1494
- app.queue()
1495
- app.launch(
1496
- show_error=True,
1497
- share=args.share_enabled,
1498
- debug=True,
1499
- ssr_mode=args.ssr,
1500
- allowed_paths=[allowed_path],
1501
- )
 
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')
337
 
 
 
 
 
 
 
 
 
 
 
 
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
 
 
536
  lora_scale4,
537
  lora5,
538
  lora_scale5,
 
 
 
 
539
  sampler,
 
 
540
  img_height,
541
  img_width,
542
  model_name,
 
554
  high_threshold,
555
  value_threshold,
556
  distance_threshold,
 
 
557
  controlnet_output_scaling_in_unet,
558
  controlnet_start_threshold,
559
  controlnet_stop_threshold,
 
561
  syntax_weights,
562
  upscaler_model_path,
563
  upscaler_increases_size,
564
+ esrgan_tile,
565
+ esrgan_tile_overlap,
566
  hires_steps,
567
  hires_denoising_strength,
568
  hires_sampler,
 
570
  hires_negative_prompt,
571
  hires_before_adetailer,
572
  hires_after_adetailer,
 
 
 
573
  loop_generation,
574
  leave_progress_bar,
575
  disable_progress_bar,
576
  image_previews,
577
  display_images,
578
  save_generated_images,
 
579
  image_storage_location,
580
  retain_compel_previous_load,
581
  retain_detailfix_model_previous_load,
 
610
  mask_blur_b,
611
  mask_padding_b,
612
  retain_task_cache_gui,
 
613
  image_ip1,
614
  mask_ip1,
615
  model_ip1,
 
621
  mode_ip2,
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]
628
  vae_msg = f"VAE: {vae_model}" if vae_model else ""
629
  msg_lora = ""
630
 
631
+ print("Config model:", model_name, vae_model, loras_list)
632
 
633
  task = TASK_STABLEPY[task]
634
 
 
643
  (image_ip2, mask_ip2, model_ip2, mode_ip2, scale_ip2),
644
  ]
645
 
646
+ for imgip, mskip, modelip, modeip, scaleip in all_adapters:
647
+ if imgip:
648
+ params_ip_img.append(imgip)
649
+ if mskip:
650
+ params_ip_msk.append(mskip)
651
+ params_ip_model.append(modelip)
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'")
659
 
660
+ if task == "inpaint" and not image_mask:
661
+ raise ValueError("No mask image found: Specify one in 'Image Mask'")
662
 
663
+ if upscaler_model_path in UPSCALER_KEYS[:9]:
664
  upscaler_model = upscaler_model_path
665
  else:
666
+ directory_upscalers = 'upscalers'
667
+ os.makedirs(directory_upscalers, exist_ok=True)
668
+
669
  url_upscaler = UPSCALER_DICT_GUI[upscaler_model_path]
670
 
671
+ if not os.path.exists(f"./upscalers/{url_upscaler.split('/')[-1]}"):
672
+ download_things(directory_upscalers, url_upscaler, HF_TOKEN)
673
 
674
+ upscaler_model = f"./upscalers/{url_upscaler.split('/')[-1]}"
675
 
676
  logging.getLogger("ultralytics").setLevel(logging.INFO if adetailer_verbose else logging.ERROR)
677
 
 
725
  "high_threshold": high_threshold,
726
  "value_threshold": value_threshold,
727
  "distance_threshold": distance_threshold,
728
+ "lora_A": lora1 if lora1 != "None" else None,
 
 
729
  "lora_scale_A": lora_scale1,
730
+ "lora_B": lora2 if lora2 != "None" else None,
731
  "lora_scale_B": lora_scale2,
732
+ "lora_C": lora3 if lora3 != "None" else None,
733
  "lora_scale_C": lora_scale3,
734
+ "lora_D": lora4 if lora4 != "None" else None,
735
  "lora_scale_D": lora_scale4,
736
+ "lora_E": lora5 if lora5 != "None" else None,
737
  "lora_scale_E": lora_scale5,
738
+ "textual_inversion": embed_list if textual_inversion and self.model.class_name != "StableDiffusionXLPipeline" else [],
 
 
 
 
739
  "syntax_weights": syntax_weights, # "Classic"
740
  "sampler": sampler,
 
 
741
  "xformers_memory_efficient_attention": xformers_memory_efficient_attention,
742
  "gui_active": True,
743
  "loop_generation": loop_generation,
 
755
  "image_previews": image_previews,
756
  "display_images": display_images,
757
  "save_generated_images": save_generated_images,
 
758
  "image_storage_location": image_storage_location,
759
  "retain_compel_previous_load": retain_compel_previous_load,
760
  "retain_detailfix_model_previous_load": retain_detailfix_model_previous_load,
 
764
  "t2i_adapter_conditioning_factor": float(t2i_adapter_conditioning_factor),
765
  "upscaler_model_path": upscaler_model,
766
  "upscaler_increases_size": upscaler_increases_size,
767
+ "esrgan_tile": esrgan_tile,
768
+ "esrgan_tile_overlap": esrgan_tile_overlap,
769
  "hires_steps": hires_steps,
770
  "hires_denoising_strength": hires_denoising_strength,
771
  "hires_prompt": hires_prompt,
 
773
  "hires_sampler": hires_sampler,
774
  "hires_before_adetailer": hires_before_adetailer,
775
  "hires_after_adetailer": hires_after_adetailer,
 
 
776
  "ip_adapter_image": params_ip_img,
777
  "ip_adapter_mask": params_ip_msk,
778
  "ip_adapter_model": params_ip_model,
779
  "ip_adapter_mode": params_ip_mode,
780
  "ip_adapter_scale": params_ip_scale,
 
 
 
781
  }
782
 
783
+ self.model.device = torch.device("cuda:0")
784
+ if hasattr(self.model.pipe, "transformer") and loras_list != ["None"] * 5:
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
  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
  [
809
+ f'<a href="{path.replace("/images/", "/file=/home/user/app/images/")}" download="{os.path.basename(path)}">Download Image {i + 1}</a>'
810
  for i, path in enumerate(image_path)
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)
 
840
 
841
 
842
  def sd_gen_generate_pipeline(*args):
843
+
844
  gpu_duration_arg = int(args[-1]) if args[-1] else 59
845
  verbose_arg = int(args[-2])
846
  load_lora_cpu = args[-3]
847
  generation_args = args[:-3]
848
  lora_list = [
849
  None if item == "None" else item
850
+ for item in [args[7], args[9], args[11], args[13], args[15]]
851
  ]
852
+ lora_status = [None] * 5
853
 
854
  msg_load_lora = "Updating LoRAs in GPU..."
855
  if load_lora_cpu:
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:
863
+ lora_status = sd_gen.model.lora_merge(
864
  lora_A=lora_list[0], lora_scale_A=args[8],
865
  lora_B=lora_list[1], lora_scale_B=args[10],
866
  lora_C=lora_list[2], lora_scale_C=args[12],
867
  lora_D=lora_list[3], lora_scale_D=args[14],
868
  lora_E=lora_list[4], lora_scale_E=args[16],
 
 
869
  )
870
  print(lora_status)
871
 
 
 
 
 
 
 
 
 
872
  if verbose_arg:
873
  for status, lora in zip(lora_status, lora_list):
874
  if status:
 
876
  elif status is not None:
877
  gr.Warning(f"Failed to load LoRA: {lora}")
878
 
879
+ if lora_status == [None] * 5 and sd_gen.model.lora_memory != [None] * 5 and load_lora_cpu:
880
  lora_cache_msg = ", ".join(
881
  str(x) for x in sd_gen.model.lora_memory if x is not None
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
  )
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
+
930
+ from stablepy.diffusers_vanilla.utils import save_pil_image_with_metadata
931
+ from stablepy import UpscalerESRGAN
932
 
933
+ exif_image = extract_exif_data(image)
 
934
 
935
+ url_upscaler = UPSCALER_DICT_GUI[upscaler_name]
936
+ directory_upscalers = 'upscalers'
937
+ os.makedirs(directory_upscalers, exist_ok=True)
938
+ if not os.path.exists(f"./upscalers/{url_upscaler.split('/')[-1]}"):
939
+ download_things(directory_upscalers, url_upscaler, HF_TOKEN)
940
 
941
+ scaler_beta = UpscalerESRGAN(0, 0)
942
+ image_up = scaler_beta.upscale(image, upscaler_size, f"./upscalers/{url_upscaler.split('/')[-1]}")
943
 
944
  image_path = save_pil_image_with_metadata(image_up, f'{os.getcwd()}/up_images', exif_image)
945
 
946
  return image_path
947
 
948
 
949
+ dynamic_gpu_duration.zerogpu = True
950
+ sd_gen_generate_pipeline.zerogpu = True
 
951
  sd_gen = GuiSD()
952
 
953
+ with gr.Blocks(theme="NoCrypt/miku", css=CSS) as app:
954
  gr.Markdown("# 🧩 DiffuseCraft")
955
  gr.Markdown(SUBTITLE_GUI)
956
  with gr.Tab("Generation"):
 
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")
964
+ neg_prompt_gui = gr.Textbox(lines=3, placeholder="Enter Neg prompt", label="Negative prompt")
965
  with gr.Row(equal_height=False):
966
  set_params_gui = gr.Button(value="↙️", variant="secondary", size="sm")
967
  clear_prompt_gui = gr.Button(value="🗑️", variant="secondary", size="sm")
 
974
  [task_gui],
975
  )
976
 
977
+ load_model_gui = gr.HTML()
978
 
979
  result_images = gr.Gallery(
980
  label="Generated images",
 
991
 
992
  actual_task_info = gr.HTML()
993
 
994
+ with gr.Row(equal_height=False, variant="default"):
995
  gpu_duration_gui = gr.Number(minimum=5, maximum=240, value=59, show_label=False, container=False, info="GPU time duration (seconds)")
996
  with gr.Column():
997
  verbose_info_gui = gr.Checkbox(value=False, container=False, label="Status info")
998
+ load_lora_cpu_gui = gr.Checkbox(value=False, container=False, label="Load LoRAs on CPU (Save GPU time)")
999
 
1000
  with gr.Column(scale=1):
1001
+ steps_gui = gr.Slider(minimum=1, maximum=100, step=1, value=30, label="Steps")
1002
  cfg_gui = gr.Slider(minimum=0, maximum=30, step=0.5, value=7., label="CFG")
1003
+ sampler_gui = gr.Dropdown(label="Sampler", choices=scheduler_names, value="Euler a")
 
1004
  img_width_gui = gr.Slider(minimum=64, maximum=4096, step=8, value=1024, label="Img Width")
1005
  img_height_gui = gr.Slider(minimum=64, maximum=4096, step=8, value=1024, label="Img Height")
1006
  seed_gui = gr.Number(minimum=-1, maximum=9999999999, value=-1, label="Seed")
 
1019
  "width": gr.update(value=1024),
1020
  "height": gr.update(value=1024),
1021
  "Seed": gr.update(value=-1),
1022
+ "Sampler": gr.update(value="Euler a"),
1023
+ "scale": gr.update(value=7.), # cfg
1024
+ "skip": gr.update(value=True),
1025
  "Model": gr.update(value=name_model),
 
 
 
 
 
 
 
 
 
 
 
 
1026
  }
 
 
 
 
 
 
1027
  valid_keys = list(valid_receptors.keys())
1028
 
1029
  parameters = extract_parameters(base_prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1030
 
 
 
 
 
 
 
 
 
 
1031
  for key, val in parameters.items():
1032
  # print(val)
1033
  if key in valid_keys:
 
1035
  if key == "Sampler":
1036
  if val not in scheduler_names:
1037
  continue
1038
+ elif key == "skip":
 
 
 
 
 
 
1039
  if "," in str(val):
1040
  val = val.replace(",", "")
1041
  if int(val) >= 2:
1042
  val = True
1043
  if key == "prompt":
1044
  if ">" in val and "<" in val:
1045
+ val = re.sub(r'<[^>]+>', '', val)
1046
  print("Removed LoRA written in the prompt")
1047
  if key in ["prompt", "neg_prompt"]:
1048
  val = re.sub(r'\s+', ' ', re.sub(r',+', ',', val)).strip()
1049
+ if key in ["Steps", "width", "height", "Seed"]:
1050
  val = int(val)
1051
+ if key == "scale":
 
 
1052
  val = float(val)
1053
  if key == "Model":
1054
  filtered_models = [m for m in model_list if val in m]
 
1056
  val = filtered_models[0]
1057
  else:
1058
  val = name_model
 
 
 
1059
  if key == "Seed":
1060
  continue
 
1061
  valid_receptors[key] = gr.update(value=val)
1062
  # print(val, type(val))
1063
  # print(valid_receptors)
 
1065
  print(str(e))
1066
  return [value for value in valid_receptors.values()]
1067
 
1068
+ set_params_gui.click(
1069
+ run_set_params_gui, [prompt_gui, model_name_gui], [
1070
+ prompt_gui,
1071
+ neg_prompt_gui,
1072
+ steps_gui,
1073
+ img_width_gui,
1074
+ img_height_gui,
1075
+ seed_gui,
1076
+ sampler_gui,
1077
+ cfg_gui,
1078
+ clip_skip_gui,
1079
+ model_name_gui,
1080
+ ],
1081
+ )
1082
+
1083
  def run_clear_prompt_gui():
1084
  return gr.update(value=""), gr.update(value="")
1085
  clear_prompt_gui.click(
 
1092
  run_set_random_seed, [], seed_gui
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):
1108
 
1109
  upscaler_model_path_gui = gr.Dropdown(label="Upscaler", choices=UPSCALER_KEYS, value=UPSCALER_KEYS[0])
1110
  upscaler_increases_size_gui = gr.Slider(minimum=1.1, maximum=4., step=0.1, value=1.2, label="Upscale by")
1111
+ esrgan_tile_gui = gr.Slider(minimum=0, value=0, maximum=500, step=1, label="ESRGAN Tile")
1112
+ esrgan_tile_overlap_gui = gr.Slider(minimum=1, maximum=200, step=1, value=8, label="ESRGAN Tile Overlap")
1113
  hires_steps_gui = gr.Slider(minimum=0, value=30, maximum=100, step=1, label="Hires Steps")
1114
  hires_denoising_strength_gui = gr.Slider(minimum=0.1, maximum=1.0, step=0.01, value=0.55, label="Hires Denoising Strength")
1115
  hires_sampler_gui = gr.Dropdown(label="Hires Sampler", choices=POST_PROCESSING_SAMPLER, value=POST_PROCESSING_SAMPLER[0])
 
 
 
1116
  hires_prompt_gui = gr.Textbox(label="Hires Prompt", placeholder="Main prompt will be use", lines=3)
1117
  hires_negative_prompt_gui = gr.Textbox(label="Hires Negative Prompt", placeholder="Main negative prompt will be use", lines=3)
1118
 
1119
  with gr.Accordion("LoRA", open=False, visible=True):
1120
 
1121
+ def lora_dropdown(label):
1122
+ return gr.Dropdown(label=label, choices=lora_model_list, value="None", allow_custom_value=True)
1123
 
1124
+ def lora_scale_slider(label):
1125
+ return gr.Slider(minimum=-2, maximum=2, step=0.01, value=0.33, label=label)
 
1126
 
1127
  lora1_gui = lora_dropdown("Lora1")
1128
  lora_scale_1_gui = lora_scale_slider("Lora Scale 1")
 
1134
  lora_scale_4_gui = lora_scale_slider("Lora Scale 4")
1135
  lora5_gui = lora_dropdown("Lora5")
1136
  lora_scale_5_gui = lora_scale_slider("Lora Scale 5")
 
 
 
 
1137
 
1138
  with gr.Accordion("From URL", open=False, visible=True):
1139
+ text_lora = gr.Textbox(label="LoRA URL", placeholder="https://civitai.com/api/download/models/28907", lines=1)
1140
+ button_lora = gr.Button("Get and update lists of LoRAs")
 
 
 
 
 
 
 
1141
  button_lora.click(
1142
  get_my_lora,
1143
+ [text_lora],
1144
+ [lora1_gui, lora2_gui, lora3_gui, lora4_gui, lora5_gui]
1145
  )
1146
 
 
 
 
 
 
 
 
 
1147
  with gr.Accordion("IP-Adapter", open=False, visible=True):
1148
 
1149
+ IP_MODELS = sorted(list(set(IP_ADAPTERS_SD + IP_ADAPTERS_SDXL)))
1150
+ MODE_IP_OPTIONS = ["original", "style", "layout", "style+layout"]
1151
+
1152
  with gr.Accordion("IP-Adapter 1", open=False, visible=True):
1153
  image_ip1 = gr.Image(label="IP Image", type="filepath")
1154
  mask_ip1 = gr.Image(label="IP Mask", type="filepath")
 
1167
  image_mask_gui = gr.Image(label="Image Mask", type="filepath")
1168
  strength_gui = gr.Slider(
1169
  minimum=0.01, maximum=1.0, step=0.01, value=0.55, label="Strength",
1170
+ info="This option adjusts the level of changes for img2img and inpainting."
1171
  )
1172
+ image_resolution_gui = gr.Slider(minimum=64, maximum=2048, step=64, value=1024, label="Image Resolution")
1173
+ preprocessor_name_gui = gr.Dropdown(label="Preprocessor Name", choices=PREPROCESSOR_CONTROLNET["canny"])
 
 
 
 
 
 
 
1174
 
1175
  def change_preprocessor_choices(task):
1176
  task = TASK_STABLEPY[task]
1177
+ if task in PREPROCESSOR_CONTROLNET.keys():
1178
+ choices_task = PREPROCESSOR_CONTROLNET[task]
1179
  else:
1180
+ choices_task = PREPROCESSOR_CONTROLNET["canny"]
1181
  return gr.update(choices=choices_task, value=choices_task[0])
1182
+
1183
  task_gui.change(
1184
  change_preprocessor_choices,
1185
  [task_gui],
1186
  [preprocessor_name_gui],
1187
  )
1188
+ preprocess_resolution_gui = gr.Slider(minimum=64, maximum=2048, step=64, value=512, label="Preprocess Resolution")
1189
+ low_threshold_gui = gr.Slider(minimum=1, maximum=255, step=1, value=100, label="Canny low threshold")
1190
+ high_threshold_gui = gr.Slider(minimum=1, maximum=255, step=1, value=200, label="Canny high threshold")
1191
+ value_threshold_gui = gr.Slider(minimum=1, maximum=2.0, step=0.01, value=0.1, label="Hough value threshold (MLSD)")
1192
+ distance_threshold_gui = gr.Slider(minimum=1, maximum=20.0, step=0.01, value=0.1, label="Hough distance threshold (MLSD)")
1193
+ control_net_output_scaling_gui = gr.Slider(minimum=0, maximum=5.0, step=0.1, value=1, label="ControlNet Output Scaling in UNet")
1194
+ control_net_start_threshold_gui = gr.Slider(minimum=0, maximum=1, step=0.01, value=0, label="ControlNet Start Threshold (%)")
1195
+ control_net_stop_threshold_gui = gr.Slider(minimum=0, maximum=1, step=0.01, value=1, label="ControlNet Stop Threshold (%)")
1196
 
1197
  with gr.Accordion("T2I adapter", open=False, visible=False):
1198
  t2i_adapter_preprocessor_gui = gr.Checkbox(value=True, label="T2i Adapter Preprocessor")
 
1225
  gr.Info(f"{len(sd_gen.model.STYLE_NAMES)} styles loaded")
1226
  return gr.update(value=None, choices=sd_gen.model.STYLE_NAMES)
1227
 
1228
+ style_button.click(load_json_style_file, [style_json_gui], [style_prompt_gui])
1229
 
1230
  with gr.Accordion("Textual inversion", open=False, visible=False):
1231
  active_textual_inversion_gui = gr.Checkbox(value=False, label="Active Textual Inversion in prompt")
 
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
  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)
 
1268
  mask_padding_b_gui = gr.Number(label="Mask padding:", value=32, minimum=1)
1269
 
1270
  with gr.Accordion("Other settings", open=False, visible=True):
 
 
1271
  save_generated_images_gui = gr.Checkbox(value=True, label="Create a download link for the images")
 
1272
  hires_before_adetailer_gui = gr.Checkbox(value=False, label="Hires Before Adetailer")
1273
  hires_after_adetailer_gui = gr.Checkbox(value=True, label="Hires After Adetailer")
1274
  generator_in_cpu_gui = gr.Checkbox(value=False, label="Generator in CPU")
 
 
 
 
1275
 
1276
  with gr.Accordion("More settings", open=False, visible=False):
1277
  loop_generation_gui = gr.Slider(minimum=1, value=1, label="Loop Generation")
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")
1285
  retain_detailfix_model_previous_load_gui = gr.Checkbox(value=False, label="Retain Detailfix Model Previous Load")
1286
  retain_hires_model_previous_load_gui = gr.Checkbox(value=False, label="Retain Hires Model Previous Load")
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
  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(
 
1525
  # enable crop (or disable it)
1526
  # transforms=["crop"],
1527
  brush=gr.Brush(
1528
+ default_size="16", # or leave it as 'auto'
1529
+ color_mode="fixed", # 'fixed' hides the user swatches and colorpicker, 'defaults' shows it
1530
+ # default_color="black", # html names are supported
1531
+ colors=[
1532
+ "rgba(0, 0, 0, 1)", # rgb(a)
1533
+ "rgba(0, 0, 0, 0.1)",
1534
+ "rgba(255, 255, 255, 0.1)",
1535
+ # "hsl(360, 120, 120)" # in fact any valid colorstring
1536
+ ]
1537
  ),
1538
+ eraser=gr.Eraser(default_size="16")
 
 
 
1539
  )
 
 
 
 
 
 
 
1540
  invert_mask = gr.Checkbox(value=False, label="Invert mask")
1541
  btn = gr.Button("Create mask")
 
1542
  with gr.Column(scale=1):
1543
  img_source = gr.Image(interactive=False)
1544
  img_result = gr.Image(label="Mask image", show_label=True, interactive=False)
 
1569
 
1570
  with gr.Row():
1571
  with gr.Column():
 
 
 
1572
  image_up_tab = gr.Image(label="Image", type="pil", sources=["upload"])
1573
+ upscaler_tab = gr.Dropdown(label="Upscaler", choices=UPSCALER_KEYS[9:], value=UPSCALER_KEYS[11])
1574
  upscaler_size_tab = gr.Slider(minimum=1., maximum=4., step=0.1, value=1.1, label="Upscale by")
1575
  generate_button_up_tab = gr.Button(value="START UPSCALE", variant="primary")
1576
 
 
1578
  result_up_tab = gr.Image(label="Result", type="pil", interactive=False, format="png")
1579
 
1580
  generate_button_up_tab.click(
1581
+ fn=esrgan_upscale,
1582
  inputs=[image_up_tab, upscaler_tab, upscaler_size_tab],
1583
  outputs=[result_up_tab],
1584
  )
1585
 
 
 
 
1586
  generate_button.click(
1587
  fn=sd_gen.load_new_model,
1588
  inputs=[
1589
  model_name_gui,
1590
  vae_model_gui,
1591
+ task_gui
 
1592
  ],
1593
  outputs=[load_model_gui],
1594
  queue=True,
 
1613
  lora_scale_4_gui,
1614
  lora5_gui,
1615
  lora_scale_5_gui,
 
 
 
 
1616
  sampler_gui,
 
 
1617
  img_height_gui,
1618
  img_width_gui,
1619
  model_name_gui,
 
1631
  high_threshold_gui,
1632
  value_threshold_gui,
1633
  distance_threshold_gui,
 
 
1634
  control_net_output_scaling_gui,
1635
  control_net_start_threshold_gui,
1636
  control_net_stop_threshold_gui,
 
1638
  prompt_syntax_gui,
1639
  upscaler_model_path_gui,
1640
  upscaler_increases_size_gui,
1641
+ esrgan_tile_gui,
1642
+ esrgan_tile_overlap_gui,
1643
  hires_steps_gui,
1644
  hires_denoising_strength_gui,
1645
  hires_sampler_gui,
 
1647
  hires_negative_prompt_gui,
1648
  hires_before_adetailer_gui,
1649
  hires_after_adetailer_gui,
 
 
 
1650
  loop_generation_gui,
1651
  leave_progress_bar_gui,
1652
  disable_progress_bar_gui,
1653
  image_previews_gui,
1654
  display_images_gui,
1655
  save_generated_images_gui,
 
1656
  image_storage_location_gui,
1657
  retain_compel_previous_load_gui,
1658
  retain_detailfix_model_previous_load_gui,
 
1687
  mask_blur_b_gui,
1688
  mask_padding_b_gui,
1689
  retain_task_cache_gui,
 
1690
  image_ip1,
1691
  mask_ip1,
1692
  model_ip1,
 
1698
  mode_ip2,
1699
  scale_ip2,
1700
  pag_scale_gui,
 
 
 
1701
  load_lora_cpu_gui,
1702
  verbose_info_gui,
1703
  gpu_duration_gui,
1704
  ],
1705
+ outputs=[result_images, actual_task_info],
1706
  queue=True,
1707
  show_progress="minimal",
1708
  )
1709
 
1710
+ app.queue()
1711
+
1712
+ app.launch(
1713
+ show_error=True,
1714
+ debug=True,
1715
+ allowed_paths=["./images/"],
1716
+ )
 
 
constants.py DELETED
@@ -1,611 +0,0 @@
1
- import os
2
- from stablepy.diffusers_vanilla.constants import FLUX_CN_UNION_MODES
3
- from stablepy import (
4
- scheduler_names,
5
- SD15_TASKS,
6
- SDXL_TASKS,
7
- ALL_BUILTIN_UPSCALERS,
8
- IP_ADAPTERS_SD,
9
- IP_ADAPTERS_SDXL,
10
- )
11
-
12
- IS_ZERO_GPU = bool(os.getenv("SPACES_ZERO_GPU"))
13
-
14
- # - **Download Models**
15
- DOWNLOAD_MODEL = "https://huggingface.co/TechnoByte/MilkyWonderland/resolve/main/milkyWonderland_v40.safetensors"
16
-
17
- # - **Download VAEs**
18
- DOWNLOAD_VAE = "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"
19
-
20
- # - **Download LoRAs**
21
- DOWNLOAD_LORA = "https://huggingface.co/Leopain/color/resolve/main/Coloring_book_-_LineArt.safetensors, https://civitai.com/api/download/models/135867, 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://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"
22
-
23
- LOAD_DIFFUSERS_FORMAT_MODEL = [
24
- 'stabilityai/stable-diffusion-xl-base-1.0',
25
- 'Laxhar/noobai-XL-1.1',
26
- 'Laxhar/noobai-XL-Vpred-1.0',
27
- 'black-forest-labs/FLUX.1-dev',
28
- 'black-forest-labs/FLUX.1-Krea-dev',
29
- 'John6666/blue-pencil-flux1-v021-fp8-flux',
30
- 'John6666/wai-ani-flux-v10forfp8-fp8-flux',
31
- 'John6666/xe-anime-flux-v04-fp8-flux',
32
- 'John6666/lyh-anime-flux-v2a1-fp8-flux',
33
- 'John6666/carnival-unchained-v10-fp8-flux',
34
- 'Freepik/flux.1-lite-8B-alpha',
35
- 'shauray/FluxDev-HyperSD-merged',
36
- 'mikeyandfriends/PixelWave_FLUX.1-dev_03',
37
- 'terminusresearch/FluxBooru-v0.3',
38
- 'black-forest-labs/FLUX.1-schnell',
39
- # 'ostris/OpenFLUX.1',
40
- 'shuttleai/shuttle-3-diffusion',
41
- 'Laxhar/noobai-XL-1.0',
42
- 'Laxhar/noobai-XL-0.77',
43
- 'John6666/noobai-xl-nai-xl-epsilonpred075version-sdxl',
44
- 'Laxhar/noobai-XL-0.6',
45
- 'John6666/noobai-xl-nai-xl-epsilonpred05version-sdxl',
46
- 'John6666/noobai-cyberfix-v10-sdxl',
47
- 'John6666/noobaiiter-xl-vpred-v075-sdxl',
48
- 'John6666/ripplemix-noob-vpred10-illustrious01-v14-sdxl',
49
- 'John6666/sigmaih-15-sdxl',
50
- 'John6666/ntr-mix-illustrious-xl-noob-xl-xi-sdxl',
51
- 'John6666/ntr-mix-illustrious-xl-noob-xl-xii-sdxl',
52
- 'John6666/ntr-mix-illustrious-xl-noob-xl-xiii-sdxl',
53
- 'John6666/mistoon-anime-v10illustrious-sdxl',
54
- 'John6666/hassaku-xl-illustrious-v22-sdxl',
55
- 'John6666/haruki-mix-illustrious-v10-sdxl',
56
- 'John6666/noobreal-v10-sdxl',
57
- 'John6666/complicated-noobai-merge-vprediction-sdxl',
58
- 'Laxhar/noobai-XL-Vpred-0.9r',
59
- 'Laxhar/noobai-XL-Vpred-0.75s',
60
- 'Laxhar/noobai-XL-Vpred-0.75',
61
- 'Laxhar/noobai-XL-Vpred-0.65s',
62
- 'Laxhar/noobai-XL-Vpred-0.65',
63
- 'Laxhar/noobai-XL-Vpred-0.6',
64
- 'John6666/cat-tower-noobai-xl-checkpoint-v14vpred-sdxl',
65
- 'John6666/cat-tower-noobai-xl-checkpoint-v15vpred-sdxl',
66
- 'John6666/noobai-xl-nai-xl-vpred05version-sdxl',
67
- 'John6666/noobai-fusion2-vpred-itercomp-v1-sdxl',
68
- 'John6666/noobai-xl-nai-xl-vpredtestversion-sdxl',
69
- 'John6666/chadmix-noobai075-illustrious01-v10-sdxl',
70
- 'OnomaAIResearch/Illustrious-xl-early-release-v0',
71
- 'John6666/illustriousxl-mmmix-v50-sdxl',
72
- 'John6666/illustrious-pencil-xl-v200-sdxl',
73
- 'John6666/obsession-illustriousxl-v21-sdxl',
74
- 'John6666/obsession-illustriousxl-v30-sdxl',
75
- 'John6666/obsession-illustriousxl-v31-sdxl',
76
- 'John6666/one-obsession-13-sdxl',
77
- 'John6666/one-obsession-14-24d-sdxl',
78
- 'John6666/one-obsession-15-noobai-sdxl',
79
- 'John6666/one-obsession-v16-noobai-sdxl',
80
- 'John6666/prefect-illustrious-xl-v3-sdxl',
81
- 'John6666/wai-nsfw-illustrious-v70-sdxl',
82
- 'John6666/wai-nsfw-illustrious-sdxl-v140-sdxl',
83
- 'John6666/illustrious-pony-mix-v3-sdxl',
84
- 'John6666/nova-anime-xl-il-v90-sdxl',
85
- 'John6666/nova-anime-xl-il-v110-sdxl',
86
- 'John6666/nova-orange-xl-re-v10-sdxl',
87
- 'John6666/nova-orange-xl-v110-sdxl',
88
- 'John6666/nova-orange-xl-re-v20-sdxl',
89
- 'John6666/nova-unreal-xl-v60-sdxl',
90
- 'John6666/nova-unreal-xl-v70-sdxl',
91
- 'John6666/nova-unreal-xl-v80-sdxl',
92
- 'John6666/nova-cartoon-xl-v40-sdxl',
93
- 'John6666/silvermoon-mix03-illustrious-v10-sdxl',
94
- 'eienmojiki/Anything-XL',
95
- 'eienmojiki/Starry-XL-v5.2',
96
- 'votepurchase/plantMilkModelSuite_walnut',
97
- 'John6666/meinaxl-v2-sdxl',
98
- 'Eugeoter/artiwaifu-diffusion-2.0',
99
- 'comin/IterComp',
100
- 'John6666/epicrealism-xl-v8kiss-sdxl',
101
- 'John6666/epicrealism-xl-v10kiss2-sdxl',
102
- 'John6666/epicrealism-xl-vxiabeast-sdxl',
103
- 'John6666/epicrealism-xl-vxvii-crystal-clear-realism-sdxl',
104
- 'misri/zavychromaxl_v80',
105
- 'SG161222/RealVisXL_V4.0',
106
- 'SG161222/RealVisXL_V5.0',
107
- 'misri/newrealityxlAllInOne_Newreality40',
108
- 'gsdf/CounterfeitXL',
109
- 'WhiteAiZ/autismmixSDXL_autismmixConfetti_diffusers',
110
- 'kitty7779/ponyDiffusionV6XL',
111
- 'GraydientPlatformAPI/aniverse-pony',
112
- 'John6666/ras-real-anime-screencap-v1-sdxl',
113
- 'John6666/duchaiten-pony-xl-no-score-v60-sdxl',
114
- 'John6666/mistoon-anime-ponyalpha-sdxl',
115
- 'John6666/mistoon-xl-copper-v20fast-sdxl',
116
- 'John6666/ebara-mfcg-pony-mix-v12-sdxl',
117
- 'John6666/t-ponynai3-v51-sdxl',
118
- 'John6666/t-ponynai3-v65-sdxl',
119
- 'John6666/t-ponynai3-v7-sdxl',
120
- 'John6666/prefect-pony-xl-v3-sdxl',
121
- 'John6666/prefect-pony-xl-v4-sdxl',
122
- 'John6666/prefect-pony-xl-v50-sdxl',
123
- 'John6666/mala-anime-mix-nsfw-pony-xl-v5-sdxl',
124
- 'John6666/wai-ani-nsfw-ponyxl-v10-sdxl',
125
- 'John6666/wai-real-mix-v11-sdxl',
126
- 'John6666/wai-shuffle-pdxl-v2-sdxl',
127
- 'John6666/wai-c-v6-sdxl',
128
- 'John6666/iniverse-mix-xl-sfwnsfw-pony-guofeng-v43-sdxl',
129
- 'John6666/sifw-annihilation-xl-v2-sdxl',
130
- 'John6666/sifw-annihilation-xl-v305illustrious-beta-sdxl',
131
- 'John6666/photo-realistic-pony-v5-sdxl',
132
- 'John6666/pony-realism-v21main-sdxl',
133
- 'John6666/pony-realism-v22main-sdxl',
134
- 'John6666/pony-realism-v23-ultra-sdxl',
135
- 'John6666/cyberrealistic-pony-v65-sdxl',
136
- 'John6666/cyberrealistic-pony-v7-sdxl',
137
- 'John6666/cyberrealistic-pony-v127-alternative-sdxl',
138
- 'GraydientPlatformAPI/realcartoon-pony-diffusion',
139
- 'John6666/nova-anime-xl-pony-v5-sdxl',
140
- 'John6666/autismmix-sdxl-autismmix-pony-sdxl',
141
- 'John6666/aimz-dream-real-pony-mix-v3-sdxl',
142
- 'John6666/prefectious-xl-nsfw-v10-sdxl',
143
- 'GraydientPlatformAPI/iniverseponyRealGuofeng49',
144
- 'John6666/duchaiten-pony-real-v11fix-sdxl',
145
- 'John6666/duchaiten-pony-real-v20-sdxl',
146
- 'John6666/duchaiten-pony-xl-no-score-v70-sdxl',
147
- 'KBlueLeaf/Kohaku-XL-Zeta',
148
- 'cagliostrolab/animagine-xl-3.1',
149
- 'cagliostrolab/animagine-xl-4.0',
150
- 'yodayo-ai/kivotos-xl-2.0',
151
- 'yodayo-ai/holodayo-xl-2.1',
152
- 'yodayo-ai/clandestine-xl-1.0',
153
- 'https://huggingface.co/chemwolf/Karmix-XL-v0/resolve/main/Karmix-XL-v0.safetensors?download=true',
154
- 'https://civitai.com/api/download/models/128713?type=Model&format=SafeTensor&size=pruned&fp=fp16',
155
- 'https://civitai.com/models/30240?modelVersionId=125771',
156
- 'digiplay/majicMIX_sombre_v2',
157
- 'digiplay/majicMIX_realistic_v6',
158
- 'digiplay/majicMIX_realistic_v7',
159
- 'digiplay/DreamShaper_8',
160
- 'digiplay/BeautifulArt_v1',
161
- 'digiplay/DarkSushi2.5D_v1',
162
- 'digiplay/darkphoenix3D_v1.1',
163
- 'digiplay/BeenYouLiteL11_diffusers',
164
- 'GraydientPlatformAPI/rev-animated2',
165
- 'myxlmynx/cyberrealistic_classic40',
166
- 'GraydientPlatformAPI/cyberreal6',
167
- 'GraydientPlatformAPI/cyberreal5',
168
- 'youknownothing/deliberate-v6',
169
- 'GraydientPlatformAPI/deliberate-cyber3',
170
- 'GraydientPlatformAPI/picx-real',
171
- 'GraydientPlatformAPI/perfectworld6',
172
- 'emilianJR/epiCRealism',
173
- 'votepurchase/counterfeitV30_v30',
174
- 'votepurchase/ChilloutMix',
175
- 'Meina/MeinaMix_V11',
176
- 'Meina/MeinaUnreal_V5',
177
- 'Meina/MeinaPastel_V7',
178
- 'GraydientPlatformAPI/realcartoon3d-17',
179
- 'GraydientPlatformAPI/realcartoon-pixar11',
180
- 'GraydientPlatformAPI/realcartoon-real17',
181
- ]
182
-
183
-
184
- DIFFUSERS_FORMAT_LORAS = [
185
- "nerijs/animation2k-flux",
186
- "XLabs-AI/flux-RealismLora",
187
- "Shakker-Labs/FLUX.1-dev-LoRA-Logo-Design",
188
- ]
189
-
190
- DOWNLOAD_EMBEDS = [
191
- 'https://huggingface.co/datasets/Nerfgun3/bad_prompt/blob/main/bad_prompt_version2.pt',
192
- # 'https://huggingface.co/embed/negative/resolve/main/EasyNegativeV2.safetensors',
193
- # 'https://huggingface.co/embed/negative/resolve/main/bad-hands-5.pt',
194
- ]
195
-
196
- CIVITAI_API_KEY = os.environ.get("CIVITAI_API_KEY")
197
- HF_TOKEN = os.environ.get("HF_READ_TOKEN")
198
-
199
- DIRECTORY_MODELS = 'models'
200
- DIRECTORY_LORAS = 'loras'
201
- DIRECTORY_VAES = 'vaes'
202
- DIRECTORY_EMBEDS = 'embedings'
203
- DIRECTORY_UPSCALERS = 'upscalers'
204
-
205
- STORAGE_ROOT = "/home/user/"
206
- CACHE_HF_ROOT = os.path.expanduser("~/.cache/huggingface")
207
- CACHE_HF = os.path.join(CACHE_HF_ROOT, "hub")
208
- if IS_ZERO_GPU:
209
- os.environ["HF_HOME"] = CACHE_HF
210
-
211
- TASK_STABLEPY = {
212
- 'txt2img': 'txt2img',
213
- 'img2img': 'img2img',
214
- 'inpaint': 'inpaint',
215
- # 'canny T2I Adapter': 'sdxl_canny_t2i', # NO HAVE STEP CALLBACK PARAMETERS SO NOT WORKS WITH DIFFUSERS 0.29.0
216
- # 'sketch T2I Adapter': 'sdxl_sketch_t2i',
217
- # 'lineart T2I Adapter': 'sdxl_lineart_t2i',
218
- # 'depth-midas T2I Adapter': 'sdxl_depth-midas_t2i',
219
- # 'openpose T2I Adapter': 'sdxl_openpose_t2i',
220
- 'openpose ControlNet': 'openpose',
221
- 'canny ControlNet': 'canny',
222
- 'mlsd ControlNet': 'mlsd',
223
- 'scribble ControlNet': 'scribble',
224
- 'softedge ControlNet': 'softedge',
225
- 'segmentation ControlNet': 'segmentation',
226
- 'depth ControlNet': 'depth',
227
- 'normalbae ControlNet': 'normalbae',
228
- 'lineart ControlNet': 'lineart',
229
- 'lineart_anime ControlNet': 'lineart_anime',
230
- 'shuffle ControlNet': 'shuffle',
231
- 'ip2p ControlNet': 'ip2p',
232
- 'optical pattern ControlNet': 'pattern',
233
- 'recolor ControlNet': 'recolor',
234
- 'tile ControlNet': 'tile',
235
- 'repaint ControlNet': 'repaint',
236
- }
237
-
238
- TASK_MODEL_LIST = list(TASK_STABLEPY.keys())
239
-
240
- UPSCALER_DICT_GUI = {
241
- None: None,
242
- **{bu: bu for bu in ALL_BUILTIN_UPSCALERS if bu not in ["HAT x4", "DAT x4", "DAT x3", "DAT x2", "SwinIR 4x"]},
243
- # "RealESRGAN_x4plus": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth",
244
- "RealESRNet_x4plus": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.1/RealESRNet_x4plus.pth",
245
- # "RealESRGAN_x4plus_anime_6B": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth",
246
- # "RealESRGAN_x2plus": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth",
247
- # "realesr-animevideov3": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-animevideov3.pth",
248
- # "realesr-general-x4v3": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth",
249
- # "realesr-general-wdn-x4v3": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-wdn-x4v3.pth",
250
- "4x-UltraSharp": "https://huggingface.co/Shandypur/ESRGAN-4x-UltraSharp/resolve/main/4x-UltraSharp.pth",
251
- "Real-ESRGAN-Anime-finetuning": "https://huggingface.co/danhtran2mind/Real-ESRGAN-Anime-finetuning/resolve/main/Real-ESRGAN-Anime-finetuning.pth",
252
- "4x_foolhardy_Remacri": "https://huggingface.co/FacehugmanIII/4x_foolhardy_Remacri/resolve/main/4x_foolhardy_Remacri.pth",
253
- "Remacri4xExtraSmoother": "https://huggingface.co/hollowstrawberry/upscalers-backup/resolve/main/ESRGAN/Remacri%204x%20ExtraSmoother.pth",
254
- "AnimeSharp4x": "https://huggingface.co/hollowstrawberry/upscalers-backup/resolve/main/ESRGAN/AnimeSharp%204x.pth",
255
- "lollypop": "https://huggingface.co/hollowstrawberry/upscalers-backup/resolve/main/ESRGAN/lollypop.pth",
256
- "RealisticRescaler4x": "https://huggingface.co/hollowstrawberry/upscalers-backup/resolve/main/ESRGAN/RealisticRescaler%204x.pth",
257
- "NickelbackFS4x": "https://huggingface.co/hollowstrawberry/upscalers-backup/resolve/main/ESRGAN/NickelbackFS%204x.pth"
258
- }
259
-
260
- UPSCALER_KEYS = list(UPSCALER_DICT_GUI.keys())
261
-
262
- DIFFUSERS_CONTROLNET_MODEL = [
263
- "Automatic",
264
-
265
- "brad-twinkl/controlnet-union-sdxl-1.0-promax",
266
- "xinsir/controlnet-union-sdxl-1.0",
267
- "xinsir/anime-painter",
268
- "Eugeoter/noob-sdxl-controlnet-canny",
269
- "Eugeoter/noob-sdxl-controlnet-lineart_anime",
270
- "Eugeoter/noob-sdxl-controlnet-depth",
271
- "Eugeoter/noob-sdxl-controlnet-normal",
272
- "Eugeoter/noob-sdxl-controlnet-softedge_hed",
273
- "Eugeoter/noob-sdxl-controlnet-scribble_pidinet",
274
- "Eugeoter/noob-sdxl-controlnet-scribble_hed",
275
- "Eugeoter/noob-sdxl-controlnet-manga_line",
276
- "Eugeoter/noob-sdxl-controlnet-lineart_realistic",
277
- "Eugeoter/noob-sdxl-controlnet-depth_midas-v1-1",
278
- "dimitribarbot/controlnet-openpose-sdxl-1.0-safetensors",
279
- "r3gm/controlnet-openpose-sdxl-1.0-fp16",
280
- "r3gm/controlnet-canny-scribble-integrated-sdxl-v2-fp16",
281
- "r3gm/controlnet-union-sdxl-1.0-fp16",
282
- "r3gm/controlnet-lineart-anime-sdxl-fp16",
283
- "r3gm/control_v1p_sdxl_qrcode_monster_fp16",
284
- "r3gm/controlnet-tile-sdxl-1.0-fp16",
285
- "r3gm/controlnet-recolor-sdxl-fp16",
286
- "r3gm/controlnet-openpose-twins-sdxl-1.0-fp16",
287
- "r3gm/controlnet-qr-pattern-sdxl-fp16",
288
- "Yakonrus/SDXL_Controlnet_Tile_Realistic_v2",
289
- "TheMistoAI/MistoLine",
290
- "briaai/BRIA-2.3-ControlNet-Recoloring",
291
- "briaai/BRIA-2.3-ControlNet-Canny",
292
-
293
- "lllyasviel/control_v11p_sd15_openpose",
294
- "lllyasviel/control_v11p_sd15_canny",
295
- "lllyasviel/control_v11p_sd15_mlsd",
296
- "lllyasviel/control_v11p_sd15_scribble",
297
- "lllyasviel/control_v11p_sd15_softedge",
298
- "lllyasviel/control_v11p_sd15_seg",
299
- "lllyasviel/control_v11f1p_sd15_depth",
300
- "lllyasviel/control_v11p_sd15_normalbae",
301
- "lllyasviel/control_v11p_sd15_lineart",
302
- "lllyasviel/control_v11p_sd15s2_lineart_anime",
303
- "lllyasviel/control_v11e_sd15_shuffle",
304
- "lllyasviel/control_v11e_sd15_ip2p",
305
- "lllyasviel/control_v11p_sd15_inpaint",
306
- "monster-labs/control_v1p_sd15_qrcode_monster",
307
- "lllyasviel/control_v11f1e_sd15_tile",
308
- "latentcat/control_v1p_sd15_brightness",
309
- "yuanqiuye/qrcode_controlnet_v3",
310
-
311
- "Shakker-Labs/FLUX.1-dev-ControlNet-Union-Pro",
312
- # "Shakker-Labs/FLUX.1-dev-ControlNet-Pose",
313
- # "Shakker-Labs/FLUX.1-dev-ControlNet-Depth",
314
- # "jasperai/Flux.1-dev-Controlnet-Upscaler",
315
- # "jasperai/Flux.1-dev-Controlnet-Depth",
316
- # "jasperai/Flux.1-dev-Controlnet-Surface-Normals",
317
- # "XLabs-AI/flux-controlnet-canny-diffusers",
318
- # "XLabs-AI/flux-controlnet-hed-diffusers",
319
- # "XLabs-AI/flux-controlnet-depth-diffusers",
320
- # "InstantX/FLUX.1-dev-Controlnet-Union",
321
- # "InstantX/FLUX.1-dev-Controlnet-Canny",
322
- ]
323
-
324
- PROMPT_W_OPTIONS = [
325
- ("Compel format: (word)weight", "Compel"),
326
- ("Classic format: (word:weight)", "Classic"),
327
- ("Classic-original format: (word:weight)", "Classic-original"),
328
- ("Classic-no_norm format: (word:weight)", "Classic-no_norm"),
329
- ("Classic-sd_embed format: (word:weight)", "Classic-sd_embed"),
330
- ("Classic-ignore", "Classic-ignore"),
331
- ("None", "None"),
332
- ]
333
-
334
- WARNING_MSG_VAE = (
335
- "Use the right VAE for your model to maintain image quality. The wrong"
336
- " VAE can lead to poor results, like blurriness in the generated images."
337
- )
338
-
339
- SDXL_TASK = [k for k, v in TASK_STABLEPY.items() if v in SDXL_TASKS]
340
- SD_TASK = [k for k, v in TASK_STABLEPY.items() if v in SD15_TASKS]
341
- FLUX_TASK = list(TASK_STABLEPY.keys())[:3] + [k for k, v in TASK_STABLEPY.items() if v in FLUX_CN_UNION_MODES.keys()]
342
-
343
- MODEL_TYPE_TASK = {
344
- "SD 1.5": SD_TASK,
345
- "SDXL": SDXL_TASK,
346
- "FLUX": FLUX_TASK,
347
- }
348
-
349
- MODEL_TYPE_CLASS = {
350
- "diffusers:StableDiffusionPipeline": "SD 1.5",
351
- "diffusers:StableDiffusionXLPipeline": "SDXL",
352
- "diffusers:FluxPipeline": "FLUX",
353
- }
354
-
355
- DIFFUSECRAFT_CHECKPOINT_NAME = {
356
- "sd1.5": "SD 1.5",
357
- "sdxl": "SDXL",
358
- "flux-dev": "FLUX",
359
- "flux-schnell": "FLUX",
360
- }
361
-
362
- POST_PROCESSING_SAMPLER = ["Use same sampler"] + [
363
- name_s for name_s in scheduler_names if "Auto-Loader" not in name_s
364
- ]
365
-
366
- IP_MODELS = []
367
- ALL_IPA = sorted(set(IP_ADAPTERS_SD + IP_ADAPTERS_SDXL))
368
-
369
- for origin_name in ALL_IPA:
370
- suffixes = []
371
- if origin_name in IP_ADAPTERS_SD:
372
- suffixes.append("sd1.5")
373
- if origin_name in IP_ADAPTERS_SDXL:
374
- suffixes.append("sdxl")
375
- ref_name = f"{origin_name} ({'/'.join(suffixes)})"
376
- IP_MODELS.append((ref_name, origin_name))
377
-
378
- MODE_IP_OPTIONS = ["original", "style", "layout", "style+layout"]
379
-
380
- SUBTITLE_GUI = (
381
- "### This demo uses [diffusers](https://github.com/huggingface/diffusers)"
382
- " to perform different tasks in image generation."
383
- )
384
-
385
- msg_zero = "" if not IS_ZERO_GPU else "- 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'."
386
-
387
- HELP_GUI = (
388
- f"""### Help:
389
- {msg_zero}
390
- - 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'.
391
- - For better results with Pony Diffusion, try using sampler DPM++ 1s or DPM2 with Compel or Classic prompt weights.
392
- """
393
- )
394
-
395
- EXAMPLES_GUI_HELP = (
396
- """### The following examples perform specific tasks:
397
- 1. Generation with SDXL and upscale
398
- 2. Generation with FLUX dev
399
- 3. ControlNet Canny SDXL
400
- 4. Optical pattern (Optical illusion) SDXL
401
- 5. Convert an image to a coloring drawing
402
- 6. V prediction model inference
403
- 7. V prediction model sd_embed variant inference
404
- 8. ControlNet OpenPose SD 1.5 and Latent upscale
405
-
406
- - 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.
407
- """
408
- )
409
-
410
- EXAMPLES_GUI = [
411
- [
412
- "splatter paint theme, 1girl, frame center, pretty face, face with artistic paint artwork, feminism, long hair, upper body view, futuristic expression illustrative painted background, origami, stripes, explosive paint splashes behind her, hand on cheek pose, strobe lighting, masterpiece photography creative artwork, golden morning light, highly detailed, masterpiece, best quality, very aesthetic, absurdres",
413
- "logo, artist name, (worst quality, normal quality), bad-artist, ((bad anatomy)), ((bad hands)), ((bad proportions)), ((duplicate limbs)), ((fused limbs)), ((interlocking fingers)), ((poorly drawn face)), high contrast., score_6, score_5, score_4, lowres, (bad), text, error, fewer, extra, missing, worst quality, jpeg artifacts, low quality, watermark, unfinished, displeasing, oldest, early, chromatic aberration, signature, extra digits, artistic error, username, scan, [abstract]",
414
- 28,
415
- 5.0,
416
- -1,
417
- "None",
418
- 0.33,
419
- "DPM++ 2M SDE",
420
- 1152,
421
- 896,
422
- "John6666/noobai-xl-nai-xl-epsilonpred10version-sdxl",
423
- "txt2img",
424
- "image.webp", # img conttol
425
- 1024, # img resolution
426
- 0.35, # strength
427
- 1.0, # cn scale
428
- 0.0, # cn start
429
- 1.0, # cn end
430
- "Classic-no_norm",
431
- "Nearest",
432
- 45,
433
- False,
434
- ],
435
- [
436
- "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",
437
- "",
438
- 24,
439
- 3.5,
440
- -1,
441
- "None",
442
- 0.33,
443
- "FlowMatch Euler",
444
- 1152,
445
- 896,
446
- "black-forest-labs/FLUX.1-dev",
447
- "txt2img",
448
- None, # img conttol
449
- 1024, # img resolution
450
- 0.35, # strength
451
- 1.0, # cn scale
452
- 0.0, # cn start
453
- 1.0, # cn end
454
- "Classic",
455
- None,
456
- 70,
457
- True,
458
- ],
459
- [
460
- "((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",
461
- "(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",
462
- 48,
463
- 3.5,
464
- -1,
465
- "None",
466
- 0.33,
467
- "DPM++ 2M SDE Ef",
468
- 1024,
469
- 1024,
470
- "John6666/epicrealism-xl-v10kiss2-sdxl",
471
- "canny ControlNet",
472
- "image.webp", # img conttol
473
- 1024, # img resolution
474
- 0.35, # strength
475
- 1.0, # cn scale
476
- 0.0, # cn start
477
- 1.0, # cn end
478
- "Classic",
479
- None,
480
- 44,
481
- False,
482
- ],
483
- [
484
- "cinematic scenery old city ruins",
485
- "(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)",
486
- 50,
487
- 4.0,
488
- -1,
489
- "None",
490
- 0.33,
491
- "Euler a",
492
- 1024,
493
- 1024,
494
- "SG161222/RealVisXL_V5.0",
495
- "optical pattern ControlNet",
496
- "spiral_no_transparent.png", # img conttol
497
- 1024, # img resolution
498
- 0.35, # strength
499
- 1.0, # cn scale
500
- 0.05, # cn start
501
- 0.8, # cn end
502
- "Classic",
503
- None,
504
- 35,
505
- False,
506
- ],
507
- [
508
- "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",
509
- "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",
510
- 20,
511
- 4.0,
512
- -1,
513
- ("loras/Coloring_book_-_LineArt.safetensors" if os.path.exists("loras/Coloring_book_-_LineArt.safetensors") else "None"),
514
- 1.0,
515
- "DPM++ 2M SDE",
516
- 1024,
517
- 1024,
518
- "eienmojiki/Anything-XL",
519
- "lineart ControlNet",
520
- "color_image.png", # img conttol
521
- 896, # img resolution
522
- 0.35, # strength
523
- 1.0, # cn scale
524
- 0.0, # cn start
525
- 1.0, # cn end
526
- "Compel",
527
- None,
528
- 35,
529
- False,
530
- ],
531
- [
532
- "[mochizuki_shiina], [syuri22], newest, reimu, solo, outdoors, water, flower, lantern",
533
- "worst quality, normal quality, old, sketch,",
534
- 28,
535
- 7.0,
536
- -1,
537
- "None",
538
- 0.33,
539
- "DPM 3M Ef",
540
- 1600,
541
- 1024,
542
- "Laxhar/noobai-XL-Vpred-1.0",
543
- "txt2img",
544
- "color_image.png", # img conttol
545
- 1024, # img resolution
546
- 0.35, # strength
547
- 1.0, # cn scale
548
- 0.0, # cn start
549
- 1.0, # cn end
550
- "Classic",
551
- None,
552
- 30,
553
- False,
554
- ],
555
- [
556
- "[mochizuki_shiina], [syuri22], newest, multiple girls, 2girls, earrings, jewelry, gloves, purple eyes, black hair, looking at viewer, nail polish, hat, smile, open mouth, fingerless gloves, sleeveless, :d, upper body, blue eyes, closed mouth, black gloves, hands up, long hair, shirt, bare shoulders, white headwear, blush, black headwear, blue nails, upper teeth only, short hair, white gloves, white shirt, teeth, rabbit hat, star earrings, purple nails, pink hair, detached sleeves, fingernails, fake animal ears, animal hat, sleeves past wrists, black shirt, medium hair, fur trim, sleeveless shirt, turtleneck, long sleeves, rabbit ears, star \\(symbol\\)",
557
- "worst quality, normal quality, old, sketch,",
558
- 28,
559
- 7.0,
560
- -1,
561
- "None",
562
- 0.33,
563
- "DPM 3M Ef",
564
- 1600,
565
- 1024,
566
- "Laxhar/noobai-XL-Vpred-1.0",
567
- "txt2img",
568
- "color_image.png", # img conttol
569
- 1024, # img resolution
570
- 0.35, # strength
571
- 1.0, # cn scale
572
- 0.0, # cn start
573
- 1.0, # cn end
574
- "Classic-sd_embed",
575
- None,
576
- 30,
577
- False,
578
- ],
579
- [
580
- "1girl,face,curly hair,red hair,white background,",
581
- "(worst quality:2),(low quality:2),(normal quality:2),lowres,watermark,",
582
- 38,
583
- 5.0,
584
- -1,
585
- "None",
586
- 0.33,
587
- "DPM++ 2M SDE",
588
- 512,
589
- 512,
590
- "digiplay/majicMIX_realistic_v7",
591
- "openpose ControlNet",
592
- "image.webp", # img conttol
593
- 1024, # img resolution
594
- 0.35, # strength
595
- 1.0, # cn scale
596
- 0.0, # cn start
597
- 0.9, # cn end
598
- "Classic-original",
599
- "Latent (antialiased)",
600
- 46,
601
- False,
602
- ],
603
- ]
604
-
605
- RESOURCES = (
606
- """### Resources
607
- - John6666's space has some great features you might find helpful [link](https://huggingface.co/spaces/John6666/DiffuseCraftMod).
608
- - Try the image generator in Colab’s free tier, which provides free GPU [link](https://github.com/R3gm/SD_diffusers_interactive).
609
- - `DiffuseCraft` in Colab:[link](https://github.com/R3gm/DiffuseCraft?tab=readme-ov-file#diffusecraft).
610
- """
611
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
image_processor.py DELETED
@@ -1,130 +0,0 @@
1
- import spaces
2
- import gradio as gr
3
- from stablepy import Preprocessor
4
-
5
- PREPROCESSOR_TASKS_LIST = [
6
- "Canny",
7
- "Openpose",
8
- "DPT",
9
- "Midas",
10
- "ZoeDepth",
11
- "DepthAnything",
12
- "HED",
13
- "PidiNet",
14
- "TEED",
15
- "Lineart",
16
- "LineartAnime",
17
- "Anyline",
18
- "Lineart standard",
19
- "SegFormer",
20
- "UPerNet",
21
- "ContentShuffle",
22
- "Recolor",
23
- "Blur",
24
- "MLSD",
25
- "NormalBae",
26
- ]
27
-
28
- preprocessor = Preprocessor()
29
-
30
-
31
- def process_inputs(
32
- image,
33
- name,
34
- resolution,
35
- precessor_resolution,
36
- low_threshold,
37
- high_threshold,
38
- value_threshod,
39
- distance_threshold,
40
- recolor_mode,
41
- recolor_gamma_correction,
42
- blur_k_size,
43
- pre_openpose_extra,
44
- hed_scribble,
45
- pre_pidinet_safe,
46
- pre_lineart_coarse,
47
- use_cuda,
48
- ):
49
- if not image:
50
- raise ValueError("To use this, simply upload an image.")
51
-
52
- preprocessor.load(name, False)
53
-
54
- params = dict(
55
- image_resolution=resolution,
56
- detect_resolution=precessor_resolution,
57
- low_threshold=low_threshold,
58
- high_threshold=high_threshold,
59
- thr_v=value_threshod,
60
- thr_d=distance_threshold,
61
- mode=recolor_mode,
62
- gamma_correction=recolor_gamma_correction,
63
- blur_sigma=blur_k_size,
64
- hand_and_face=pre_openpose_extra,
65
- scribble=hed_scribble,
66
- safe=pre_pidinet_safe,
67
- coarse=pre_lineart_coarse,
68
- )
69
-
70
- if use_cuda:
71
- @spaces.GPU(duration=15)
72
- def wrapped_func():
73
- preprocessor.to("cuda")
74
- return preprocessor(image, **params)
75
- return wrapped_func()
76
-
77
- return preprocessor(image, **params)
78
-
79
-
80
- def preprocessor_tab():
81
- with gr.Row():
82
- with gr.Column():
83
- pre_image = gr.Image(label="Image", type="pil", sources=["upload"])
84
- pre_options = gr.Dropdown(label="Preprocessor", choices=PREPROCESSOR_TASKS_LIST, value=PREPROCESSOR_TASKS_LIST[0])
85
- pre_img_resolution = gr.Slider(
86
- minimum=64, maximum=4096, step=64, value=1024, label="Image Resolution",
87
- info="The maximum proportional size of the generated image based on the uploaded image."
88
- )
89
- pre_start = gr.Button(value="PROCESS IMAGE", variant="primary")
90
- with gr.Accordion("Advanced Settings", open=False):
91
- with gr.Column():
92
- pre_processor_resolution = gr.Slider(minimum=64, maximum=2048, step=64, value=512, label="Preprocessor Resolution")
93
- pre_low_threshold = gr.Slider(minimum=1, maximum=255, step=1, value=100, label="'CANNY' low threshold")
94
- pre_high_threshold = gr.Slider(minimum=1, maximum=255, step=1, value=200, label="'CANNY' high threshold")
95
- pre_value_threshold = gr.Slider(minimum=0., maximum=2.0, step=0.01, value=0.1, label="'MLSD' Hough value threshold")
96
- pre_distance_threshold = gr.Slider(minimum=0., maximum=20.0, step=0.01, value=0.1, label="'MLSD' Hough distance threshold")
97
- pre_recolor_mode = gr.Dropdown(label="'RECOLOR' mode", choices=["luminance", "intensity"], value="luminance")
98
- pre_recolor_gamma_correction = gr.Number(minimum=0., maximum=25., value=1., step=0.001, label="'RECOLOR' gamma correction")
99
- pre_blur_k_size = gr.Number(minimum=0, maximum=100, value=9, step=1, label="'BLUR' sigma")
100
- pre_openpose_extra = gr.Checkbox(value=True, label="'OPENPOSE' face and hand")
101
- pre_hed_scribble = gr.Checkbox(value=False, label="'HED' scribble")
102
- pre_pidinet_safe = gr.Checkbox(value=False, label="'PIDINET' safe")
103
- pre_lineart_coarse = gr.Checkbox(value=False, label="'LINEART' coarse")
104
- pre_use_cuda = gr.Checkbox(value=False, label="Use CUDA")
105
-
106
- with gr.Column():
107
- pre_result = gr.Image(label="Result", type="pil", interactive=False, format="png")
108
-
109
- pre_start.click(
110
- fn=process_inputs,
111
- inputs=[
112
- pre_image,
113
- pre_options,
114
- pre_img_resolution,
115
- pre_processor_resolution,
116
- pre_low_threshold,
117
- pre_high_threshold,
118
- pre_value_threshold,
119
- pre_distance_threshold,
120
- pre_recolor_mode,
121
- pre_recolor_gamma_correction,
122
- pre_blur_k_size,
123
- pre_openpose_extra,
124
- pre_hed_scribble,
125
- pre_pidinet_safe,
126
- pre_lineart_coarse,
127
- pre_use_cuda,
128
- ],
129
- outputs=[pre_result],
130
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
packages.txt CHANGED
@@ -1,3 +1,3 @@
1
  git-lfs
2
- aria2
3
  ffmpeg
 
1
  git-lfs
2
+ aria2 -y
3
  ffmpeg
pre-requirements.txt DELETED
@@ -1 +0,0 @@
1
- pip>=23.0.0
 
 
requirements.txt CHANGED
@@ -1,13 +1,4 @@
1
- stablepy==0.6.2
2
- torch==2.5.1
3
- diffusers
4
  gdown
5
- opencv-python
6
- unidecode
7
- pydantic==2.10.6
8
- huggingface_hub
9
- hf_transfer
10
- hf_xet
11
- spaces
12
- gradio==5.44.1
13
- matplotlib-inline
 
1
+ git+https://github.com/R3gm/stablepy.git@flux_beta
2
+ torch==2.2.0
 
3
  gdown
4
+ opencv-python
 
 
 
 
 
 
 
 
utils.py DELETED
@@ -1,562 +0,0 @@
1
- import os
2
- import re
3
- import gradio as gr
4
- from constants import (
5
- DIFFUSERS_FORMAT_LORAS,
6
- CIVITAI_API_KEY,
7
- HF_TOKEN,
8
- MODEL_TYPE_CLASS,
9
- DIRECTORY_LORAS,
10
- DIRECTORY_MODELS,
11
- DIFFUSECRAFT_CHECKPOINT_NAME,
12
- CACHE_HF_ROOT,
13
- CACHE_HF,
14
- STORAGE_ROOT,
15
- )
16
- from huggingface_hub import HfApi
17
- from huggingface_hub import snapshot_download
18
- from diffusers import DiffusionPipeline
19
- from huggingface_hub import model_info as model_info_data
20
- from diffusers.pipelines.pipeline_loading_utils import variant_compatible_siblings
21
- from stablepy.diffusers_vanilla.utils import checkpoint_model_type
22
- from pathlib import PosixPath
23
- from unidecode import unidecode
24
- import urllib.parse
25
- import copy
26
- import requests
27
- from requests.adapters import HTTPAdapter
28
- from urllib3.util import Retry
29
- import shutil
30
- import subprocess
31
-
32
- IS_ZERO_GPU = bool(os.getenv("SPACES_ZERO_GPU"))
33
- USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:127.0) Gecko/20100101 Firefox/127.0'
34
-
35
-
36
- def request_json_data(url):
37
- model_version_id = url.split('/')[-1]
38
- if "?modelVersionId=" in model_version_id:
39
- match = re.search(r'modelVersionId=(\d+)', url)
40
- model_version_id = match.group(1)
41
-
42
- endpoint_url = f"https://civitai.com/api/v1/model-versions/{model_version_id}"
43
-
44
- params = {}
45
- headers = {'User-Agent': USER_AGENT, 'content-type': 'application/json'}
46
- session = requests.Session()
47
- retries = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
48
- session.mount("https://", HTTPAdapter(max_retries=retries))
49
-
50
- try:
51
- result = session.get(endpoint_url, params=params, headers=headers, stream=True, timeout=(3.0, 15))
52
- result.raise_for_status()
53
- json_data = result.json()
54
- return json_data if json_data else None
55
- except Exception as e:
56
- print(f"Error: {e}")
57
- return None
58
-
59
-
60
- class ModelInformation:
61
- def __init__(self, json_data):
62
- self.model_version_id = json_data.get("id", "")
63
- self.model_id = json_data.get("modelId", "")
64
- self.download_url = json_data.get("downloadUrl", "")
65
- self.model_url = f"https://civitai.com/models/{self.model_id}?modelVersionId={self.model_version_id}"
66
- self.filename_url = next(
67
- (v.get("name", "") for v in json_data.get("files", []) if str(self.model_version_id) in v.get("downloadUrl", "") and v.get("type", "Model") == "Model"), ""
68
- )
69
- self.filename_url = self.filename_url if self.filename_url else ""
70
- self.description = json_data.get("description", "")
71
- if self.description is None:
72
- self.description = ""
73
- self.model_name = json_data.get("model", {}).get("name", "")
74
- self.model_type = json_data.get("model", {}).get("type", "")
75
- self.nsfw = json_data.get("model", {}).get("nsfw", False)
76
- self.poi = json_data.get("model", {}).get("poi", False)
77
- self.images = [img.get("url", "") for img in json_data.get("images", [])]
78
- self.example_prompt = json_data.get("trainedWords", [""])[0] if json_data.get("trainedWords") else ""
79
- self.original_json = copy.deepcopy(json_data)
80
-
81
-
82
- def get_civit_params(url):
83
- try:
84
- json_data = request_json_data(url)
85
- mdc = ModelInformation(json_data)
86
- if mdc.download_url and mdc.filename_url:
87
- return mdc.download_url, mdc.filename_url, mdc.model_url
88
- else:
89
- ValueError("Invalid Civitai model URL")
90
- except Exception as e:
91
- print(f"Error retrieving Civitai metadata: {e} — fallback to direct download")
92
- return url, None, None
93
-
94
-
95
- def civ_redirect_down(url, dir_, civitai_api_key, romanize, alternative_name):
96
- filename_base = filename = None
97
-
98
- if alternative_name:
99
- output_path = os.path.join(dir_, alternative_name)
100
- if os.path.exists(output_path):
101
- return output_path, alternative_name
102
-
103
- # Follow the redirect to get the actual download URL
104
- curl_command = (
105
- f'curl -L -sI --connect-timeout 5 --max-time 5 '
106
- f'-H "Content-Type: application/json" '
107
- f'-H "Authorization: Bearer {civitai_api_key}" "{url}"'
108
- )
109
-
110
- headers = os.popen(curl_command).read()
111
-
112
- # Look for the redirected "Location" URL
113
- location_match = re.search(r'location: (.+)', headers, re.IGNORECASE)
114
-
115
- if location_match:
116
- redirect_url = location_match.group(1).strip()
117
-
118
- # Extract the filename from the redirect URL's "Content-Disposition"
119
- filename_match = re.search(r'filename%3D%22(.+?)%22', redirect_url)
120
- if filename_match:
121
- encoded_filename = filename_match.group(1)
122
- # Decode the URL-encoded filename
123
- decoded_filename = urllib.parse.unquote(encoded_filename)
124
-
125
- filename = unidecode(decoded_filename) if romanize else decoded_filename
126
- # print(f"Filename redirect: {filename}")
127
-
128
- filename_base = alternative_name if alternative_name else filename
129
- if not filename_base:
130
- return None, None
131
- elif os.path.exists(os.path.join(dir_, filename_base)):
132
- return os.path.join(dir_, filename_base), filename_base
133
-
134
- aria2_command = (
135
- f'aria2c --console-log-level=error --summary-interval=10 -c -x 16 '
136
- f'-k 1M -s 16 -d "{dir_}" -o "{filename_base}" "{redirect_url}"'
137
- )
138
- r_code = os.system(aria2_command) # noqa
139
-
140
- # if r_code != 0:
141
- # raise RuntimeError(f"Failed to download file: {filename_base}. Error code: {r_code}")
142
-
143
- output_path = os.path.join(dir_, filename_base)
144
- if not os.path.exists(output_path):
145
- return None, filename_base
146
-
147
- return output_path, filename_base
148
-
149
-
150
- def civ_api_down(url, dir_, civitai_api_key, civ_filename):
151
- """
152
- This method is susceptible to being blocked because it generates a lot of temp redirect links with aria2c.
153
- If an API key limit is reached, generating a new API key and using it can fix the issue.
154
- """
155
- output_path = None
156
-
157
- url_dl = url + f"?token={civitai_api_key}"
158
- if not civ_filename:
159
- aria2_command = f'aria2c -c -x 1 -s 1 -d "{dir_}" "{url_dl}"'
160
- os.system(aria2_command)
161
- else:
162
- output_path = os.path.join(dir_, civ_filename)
163
- if not os.path.exists(output_path):
164
- aria2_command = (
165
- f'aria2c --console-log-level=error --summary-interval=10 -c -x 16 '
166
- f'-k 1M -s 16 -d "{dir_}" -o "{civ_filename}" "{url_dl}"'
167
- )
168
- os.system(aria2_command)
169
-
170
- return output_path
171
-
172
-
173
- def drive_down(url, dir_):
174
- import gdown
175
-
176
- output_path = None
177
-
178
- drive_id, _ = gdown.parse_url.parse_url(url, warning=False)
179
- dir_files = os.listdir(dir_)
180
-
181
- for dfile in dir_files:
182
- if drive_id in dfile:
183
- output_path = os.path.join(dir_, dfile)
184
- break
185
-
186
- if not output_path:
187
- original_path = gdown.download(url, f"{dir_}/", fuzzy=True)
188
-
189
- dir_name, base_name = os.path.split(original_path)
190
- name, ext = base_name.rsplit(".", 1)
191
- new_name = f"{name}_{drive_id}.{ext}"
192
- output_path = os.path.join(dir_name, new_name)
193
-
194
- os.rename(original_path, output_path)
195
-
196
- return output_path
197
-
198
-
199
- def hf_down(url, dir_, hf_token, romanize):
200
- url = url.replace("?download=true", "")
201
- # url = urllib.parse.quote(url, safe=':/') # fix encoding
202
-
203
- filename = unidecode(url.split('/')[-1]) if romanize else url.split('/')[-1]
204
- output_path = os.path.join(dir_, filename)
205
-
206
- if os.path.exists(output_path):
207
- return output_path
208
-
209
- if "/blob/" in url:
210
- url = url.replace("/blob/", "/resolve/")
211
-
212
- if hf_token:
213
- user_header = f'"Authorization: Bearer {hf_token}"'
214
- os.system(f"aria2c --console-log-level=error --summary-interval=10 --header={user_header} -c -x 16 -k 1M -s 16 {url} -d {dir_} -o {filename}")
215
- else:
216
- os.system(f"aria2c --optimize-concurrent-downloads --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 {url} -d {dir_} -o {filename}")
217
-
218
- return output_path
219
-
220
-
221
- def download_things(directory, url, hf_token="", civitai_api_key="", romanize=False):
222
- url = url.strip()
223
- downloaded_file_path = None
224
-
225
- if "drive.google.com" in url:
226
- downloaded_file_path = drive_down(url, directory)
227
- elif "huggingface.co" in url:
228
- downloaded_file_path = hf_down(url, directory, hf_token, romanize)
229
- elif "civitai.com" in url:
230
- if not civitai_api_key:
231
- msg = "You need an API key to download Civitai models."
232
- print(f"\033[91m{msg}\033[0m")
233
- gr.Warning(msg)
234
- return None
235
-
236
- url, civ_filename, civ_page = get_civit_params(url)
237
- if civ_page and not IS_ZERO_GPU:
238
- print(f"\033[92mCivitai model: {civ_filename} [page: {civ_page}]\033[0m")
239
-
240
- downloaded_file_path, civ_filename = civ_redirect_down(url, directory, civitai_api_key, romanize, civ_filename)
241
-
242
- if not downloaded_file_path:
243
- msg = (
244
- "Download failed.\n"
245
- "If this is due to an API limit, generating a new API key may resolve the issue.\n"
246
- "Attempting to download using the old method..."
247
- )
248
- print(msg)
249
- gr.Warning(msg)
250
- downloaded_file_path = civ_api_down(url, directory, civitai_api_key, civ_filename)
251
- else:
252
- os.system(f"aria2c --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 -d {directory} {url}")
253
-
254
- return downloaded_file_path
255
-
256
-
257
- def get_model_list(directory_path):
258
- model_list = []
259
- valid_extensions = {'.ckpt', '.pt', '.pth', '.safetensors', '.bin'}
260
-
261
- for filename in os.listdir(directory_path):
262
- if os.path.splitext(filename)[1] in valid_extensions:
263
- # name_without_extension = os.path.splitext(filename)[0]
264
- file_path = os.path.join(directory_path, filename)
265
- # model_list.append((name_without_extension, file_path))
266
- model_list.append(file_path)
267
- print('\033[34mFILE: ' + file_path + '\033[0m')
268
- return model_list
269
-
270
-
271
- def extract_parameters(input_string):
272
- parameters = {}
273
- input_string = input_string.replace("\n", "")
274
-
275
- if "Negative prompt:" not in input_string:
276
- if "Steps:" in input_string:
277
- input_string = input_string.replace("Steps:", "Negative prompt: Steps:")
278
- else:
279
- msg = "Generation data is invalid."
280
- gr.Warning(msg)
281
- print(msg)
282
- parameters["prompt"] = input_string
283
- return parameters
284
-
285
- parm = input_string.split("Negative prompt:")
286
- parameters["prompt"] = parm[0].strip()
287
- if "Steps:" not in parm[1]:
288
- parameters["neg_prompt"] = parm[1].strip()
289
- return parameters
290
- parm = parm[1].split("Steps:")
291
- parameters["neg_prompt"] = parm[0].strip()
292
- input_string = "Steps:" + parm[1]
293
-
294
- # Extracting Steps
295
- steps_match = re.search(r'Steps: (\d+)', input_string)
296
- if steps_match:
297
- parameters['Steps'] = int(steps_match.group(1))
298
-
299
- # Extracting Size
300
- size_match = re.search(r'Size: (\d+x\d+)', input_string)
301
- if size_match:
302
- parameters['Size'] = size_match.group(1)
303
- width, height = map(int, parameters['Size'].split('x'))
304
- parameters['width'] = width
305
- parameters['height'] = height
306
-
307
- # Extracting other parameters
308
- other_parameters = re.findall(r'([^,:]+): (.*?)(?=, [^,:]+:|$)', input_string)
309
- for param in other_parameters:
310
- parameters[param[0].strip()] = param[1].strip('"')
311
-
312
- return parameters
313
-
314
-
315
- def get_my_lora(link_url, romanize):
316
- l_name = ""
317
- for url in [url.strip() for url in link_url.split(',')]:
318
- if not os.path.exists(f"./loras/{url.split('/')[-1]}"):
319
- l_name = download_things(DIRECTORY_LORAS, url, HF_TOKEN, CIVITAI_API_KEY, romanize)
320
- new_lora_model_list = get_model_list(DIRECTORY_LORAS)
321
- new_lora_model_list.insert(0, "None")
322
- new_lora_model_list = new_lora_model_list + DIFFUSERS_FORMAT_LORAS
323
- msg_lora = "Downloaded"
324
- if l_name:
325
- msg_lora += f": <b>{l_name}</b>"
326
- print(msg_lora)
327
-
328
- return gr.update(
329
- choices=new_lora_model_list
330
- ), gr.update(
331
- choices=new_lora_model_list
332
- ), gr.update(
333
- choices=new_lora_model_list
334
- ), gr.update(
335
- choices=new_lora_model_list
336
- ), gr.update(
337
- choices=new_lora_model_list
338
- ), gr.update(
339
- choices=new_lora_model_list
340
- ), gr.update(
341
- choices=new_lora_model_list
342
- ), gr.update(
343
- value=msg_lora
344
- )
345
-
346
-
347
- def info_html(json_data, title, subtitle):
348
- return f"""
349
- <div style='padding: 0; border-radius: 10px;'>
350
- <p style='margin: 0; font-weight: bold;'>{title}</p>
351
- <details>
352
- <summary>Details</summary>
353
- <p style='margin: 0; font-weight: bold;'>{subtitle}</p>
354
- </details>
355
- </div>
356
- """
357
-
358
-
359
- def get_model_type(repo_id: str):
360
- api = HfApi(token=os.environ.get("HF_TOKEN")) # if use private or gated model
361
- default = "SD 1.5"
362
- try:
363
- if os.path.exists(repo_id):
364
- tag, _, _, _ = checkpoint_model_type(repo_id)
365
- return DIFFUSECRAFT_CHECKPOINT_NAME[tag]
366
- else:
367
- model = api.model_info(repo_id=repo_id, timeout=5.0)
368
- tags = model.tags
369
- for tag in tags:
370
- if tag in MODEL_TYPE_CLASS.keys():
371
- return MODEL_TYPE_CLASS.get(tag, default)
372
-
373
- except Exception:
374
- return default
375
- return default
376
-
377
-
378
- def restart_space(repo_id: str, factory_reboot: bool):
379
- api = HfApi(token=os.environ.get("HF_TOKEN"))
380
- try:
381
- runtime = api.get_space_runtime(repo_id=repo_id)
382
- if runtime.stage == "RUNNING":
383
- api.restart_space(repo_id=repo_id, factory_reboot=factory_reboot)
384
- print(f"Restarting space: {repo_id}")
385
- else:
386
- print(f"Space {repo_id} is in stage: {runtime.stage}")
387
- except Exception as e:
388
- print(e)
389
-
390
-
391
- def extract_exif_data(image):
392
- if image is None:
393
- return ""
394
-
395
- try:
396
- metadata_keys = ['parameters', 'metadata', 'prompt', 'Comment']
397
-
398
- for key in metadata_keys:
399
- if key in image.info:
400
- return image.info[key]
401
-
402
- return str(image.info)
403
-
404
- except Exception as e:
405
- return f"Error extracting metadata: {str(e)}"
406
-
407
-
408
- def create_mask_now(img, invert):
409
- import numpy as np
410
- import time
411
-
412
- time.sleep(0.5)
413
-
414
- transparent_image = img["layers"][0]
415
-
416
- # Extract the alpha channel
417
- alpha_channel = np.array(transparent_image)[:, :, 3]
418
-
419
- # Create a binary mask by thresholding the alpha channel
420
- binary_mask = alpha_channel > 1
421
-
422
- if invert:
423
- print("Invert")
424
- # Invert the binary mask so that the drawn shape is white and the rest is black
425
- binary_mask = np.invert(binary_mask)
426
-
427
- # Convert the binary mask to a 3-channel RGB mask
428
- rgb_mask = np.stack((binary_mask,) * 3, axis=-1)
429
-
430
- # Convert the mask to uint8
431
- rgb_mask = rgb_mask.astype(np.uint8) * 255
432
-
433
- return img["background"], rgb_mask
434
-
435
-
436
- def download_diffuser_repo(repo_name: str, model_type: str, revision: str = "main", token=True):
437
-
438
- variant = None
439
- if token is True and not os.environ.get("HF_TOKEN"):
440
- token = None
441
-
442
- if model_type == "SDXL":
443
- info = model_info_data(
444
- repo_name,
445
- token=token,
446
- revision=revision,
447
- timeout=5.0,
448
- )
449
-
450
- filenames = {sibling.rfilename for sibling in info.siblings}
451
- model_filenames, variant_filenames = variant_compatible_siblings(
452
- filenames, variant="fp16"
453
- )
454
-
455
- if len(variant_filenames):
456
- variant = "fp16"
457
-
458
- if model_type == "FLUX":
459
- cached_folder = snapshot_download(
460
- repo_id=repo_name,
461
- allow_patterns="transformer/*"
462
- )
463
- else:
464
- cached_folder = DiffusionPipeline.download(
465
- pretrained_model_name=repo_name,
466
- force_download=False,
467
- token=token,
468
- revision=revision,
469
- # mirror="https://hf-mirror.com",
470
- variant=variant,
471
- use_safetensors=True,
472
- trust_remote_code=False,
473
- timeout=5.0,
474
- )
475
-
476
- if isinstance(cached_folder, PosixPath):
477
- cached_folder = cached_folder.as_posix()
478
-
479
- # Task model
480
- # from huggingface_hub import hf_hub_download
481
- # hf_hub_download(
482
- # task_model,
483
- # filename="diffusion_pytorch_model.safetensors", # fix fp16 variant
484
- # )
485
-
486
- return cached_folder
487
-
488
-
489
- def get_folder_size_gb(folder_path):
490
- result = subprocess.run(["du", "-s", folder_path], capture_output=True, text=True)
491
-
492
- total_size_kb = int(result.stdout.split()[0])
493
- total_size_gb = total_size_kb / (1024 ** 2)
494
-
495
- return total_size_gb
496
-
497
-
498
- def get_used_storage_gb(path_storage=STORAGE_ROOT):
499
- try:
500
- used_gb = get_folder_size_gb(path_storage)
501
- print(f"Used Storage: {used_gb:.2f} GB")
502
- except Exception as e:
503
- used_gb = 999
504
- print(f"Error while retrieving the used storage: {e}.")
505
-
506
- return used_gb
507
-
508
-
509
- def delete_model(removal_candidate):
510
- print(f"Removing: {removal_candidate}")
511
-
512
- if os.path.exists(removal_candidate):
513
- os.remove(removal_candidate)
514
- else:
515
- diffusers_model = f"{CACHE_HF}{DIRECTORY_MODELS}--{removal_candidate.replace('/', '--')}"
516
- if os.path.isdir(diffusers_model):
517
- shutil.rmtree(diffusers_model)
518
-
519
-
520
- def clear_hf_cache():
521
- """
522
- Clears the entire Hugging Face cache at ~/.cache/huggingface.
523
- Hugging Face will re-download models as needed later.
524
- """
525
- try:
526
- if os.path.exists(CACHE_HF_ROOT):
527
- shutil.rmtree(CACHE_HF_ROOT, ignore_errors=True)
528
- print(f"Hugging Face cache cleared: {CACHE_HF_ROOT}")
529
- else:
530
- print(f"No Hugging Face cache found at: {CACHE_HF_ROOT}")
531
- except Exception as e:
532
- print(f"Error clearing Hugging Face cache: {e}")
533
-
534
-
535
- def progress_step_bar(step, total):
536
- # Calculate the percentage for the progress bar width
537
- percentage = min(100, ((step / total) * 100))
538
-
539
- return f"""
540
- <div style="position: relative; width: 100%; background-color: gray; border-radius: 5px; overflow: hidden;">
541
- <div style="width: {percentage}%; height: 17px; background-color: #800080; transition: width 0.5s;"></div>
542
- <div style="position: absolute; width: 100%; text-align: center; color: white; top: 0; line-height: 19px; font-size: 13px;">
543
- {int(percentage)}%
544
- </div>
545
- </div>
546
- """
547
-
548
-
549
- def html_template_message(msg):
550
- return f"""
551
- <div style="position: relative; width: 100%; background-color: gray; border-radius: 5px; overflow: hidden;">
552
- <div style="width: 0%; height: 17px; background-color: #800080; transition: width 0.5s;"></div>
553
- <div style="position: absolute; width: 100%; text-align: center; color: white; top: 0; line-height: 19px; font-size: 14px; font-weight: bold; text-shadow: 1px 1px 2px black;">
554
- {msg}
555
- </div>
556
- </div>
557
- """
558
-
559
-
560
- def escape_html(text):
561
- """Escapes HTML special characters in the input text."""
562
- return text.replace("<", "&lt;").replace(">", "&gt;").replace("\n", "<br>")