John6666 commited on
Commit
a03d34d
1 Parent(s): 19a7408

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +9 -10
  2. flux.py +11 -54
  3. modutils.py +28 -24
app.py CHANGED
@@ -17,7 +17,7 @@ from mod import (models, clear_cache, get_repo_safetensors, is_repo_name, is_rep
17
  get_control_union_mode, set_control_union_mode, get_control_params)
18
  from flux import (search_civitai_lora, select_civitai_lora, search_civitai_lora_json,
19
  download_my_lora, get_all_lora_tupled_list, apply_lora_prompt,
20
- update_loras)
21
  from tagger.tagger import predict_tags_wd, compose_prompt_to_copy
22
  from tagger.fl2flux import predict_tags_fl2_flux
23
 
@@ -296,6 +296,7 @@ css = '''
296
  .card_internal{display: flex;height: 100px;margin-top: .5em}
297
  .card_internal img{margin-right: 1em}
298
  .styler{--form-gap-width: 0px !important}
 
299
  '''
300
  with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=css) as app:
301
  with gr.Tab("FLUX LoRA the Explorer"):
@@ -340,7 +341,9 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=css) as app:
340
  deselect_lora_button = gr.Button("Deselect LoRA", variant="secondary")
341
  with gr.Column(scale=4):
342
  result = gr.Image(label="Generated Image", format="png", show_share_button=False)
343
- model_name = gr.Dropdown(label="Base Model", info="You can enter a huggingface model repo_id to want to use.", choices=models, value=models[0], allow_custom_value=True)
 
 
344
  with gr.Row():
345
  with gr.Accordion("Advanced Settings", open=False):
346
  with gr.Column():
@@ -457,7 +460,7 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=css) as app:
457
  outputs=[result],
458
  queue=True,
459
  show_api=False,
460
- )
461
  prompt_enhance.click(enhance_prompt, [prompt], [prompt], queue=False, show_api=False)
462
 
463
  gr.on(
@@ -506,18 +509,14 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=css) as app:
506
  ).success(set_control_union_image, [cn_num[i], cn_mode[i], cn_image_ref[i], height, width, cn_res[i]], [cn_image[i]], queue=False, show_api=False)
507
  cn_image_ref[i].upload(set_control_union_image, [cn_num[i], cn_mode[i], cn_image_ref[i], height, width, cn_res[i]], [cn_image[i]], queue=False, show_api=False)
508
 
509
- tagger_generate_from_image.click(
510
- lambda: ("", "", ""), None, [v2_series, v2_character, prompt], queue=False, show_api=False,
511
  ).success(
512
  predict_tags_wd,
513
  [tagger_image, prompt, tagger_algorithms, tagger_general_threshold, tagger_character_threshold],
514
  [v2_series, v2_character, prompt, v2_copy],
515
  show_api=False,
516
- ).success(
517
- predict_tags_fl2_flux, [tagger_image, prompt, tagger_algorithms], [prompt], show_api=False,
518
- ).success(
519
- compose_prompt_to_copy, [v2_character, v2_series, prompt], [prompt], queue=False, show_api=False,
520
- )
521
 
522
  with gr.Tab("FLUX Prompt Generator"):
523
  from prompt import (PromptGenerator, HuggingFaceInferenceNode, florence_caption,
 
17
  get_control_union_mode, set_control_union_mode, get_control_params)
18
  from flux import (search_civitai_lora, select_civitai_lora, search_civitai_lora_json,
19
  download_my_lora, get_all_lora_tupled_list, apply_lora_prompt,
20
+ update_loras, get_t2i_model_info)
21
  from tagger.tagger import predict_tags_wd, compose_prompt_to_copy
22
  from tagger.fl2flux import predict_tags_fl2_flux
23
 
 
296
  .card_internal{display: flex;height: 100px;margin-top: .5em}
297
  .card_internal img{margin-right: 1em}
298
  .styler{--form-gap-width: 0px !important}
299
+ #model-info {text-align: center; !important}
300
  '''
301
  with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=css) as app:
302
  with gr.Tab("FLUX LoRA the Explorer"):
 
341
  deselect_lora_button = gr.Button("Deselect LoRA", variant="secondary")
342
  with gr.Column(scale=4):
343
  result = gr.Image(label="Generated Image", format="png", show_share_button=False)
344
+ with gr.Group():
345
+ model_name = gr.Dropdown(label="Base Model", info="You can enter a huggingface model repo_id to want to use.", choices=models, value=models[0], allow_custom_value=True)
346
+ model_info = gr.Markdown(elem_id="model-info")
347
  with gr.Row():
348
  with gr.Accordion("Advanced Settings", open=False):
349
  with gr.Column():
 
460
  outputs=[result],
461
  queue=True,
462
  show_api=False,
463
+ ).success(get_t2i_model_info, [model_name], [model_info], queue=False, show_api=False)
464
  prompt_enhance.click(enhance_prompt, [prompt], [prompt], queue=False, show_api=False)
465
 
466
  gr.on(
 
509
  ).success(set_control_union_image, [cn_num[i], cn_mode[i], cn_image_ref[i], height, width, cn_res[i]], [cn_image[i]], queue=False, show_api=False)
510
  cn_image_ref[i].upload(set_control_union_image, [cn_num[i], cn_mode[i], cn_image_ref[i], height, width, cn_res[i]], [cn_image[i]], queue=False, show_api=False)
511
 
512
+ tagger_generate_from_image.click(lambda: ("", "", ""), None, [v2_series, v2_character, prompt], queue=False, show_api=False,
 
513
  ).success(
514
  predict_tags_wd,
515
  [tagger_image, prompt, tagger_algorithms, tagger_general_threshold, tagger_character_threshold],
516
  [v2_series, v2_character, prompt, v2_copy],
517
  show_api=False,
518
+ ).success(predict_tags_fl2_flux, [tagger_image, prompt, tagger_algorithms], [prompt], show_api=False,
519
+ ).success(compose_prompt_to_copy, [v2_character, v2_series, prompt], [prompt], queue=False, show_api=False)
 
 
 
520
 
521
  with gr.Tab("FLUX Prompt Generator"):
522
  from prompt import (PromptGenerator, HuggingFaceInferenceNode, florence_caption,
flux.py CHANGED
@@ -11,41 +11,14 @@ warnings.filterwarnings(action="ignore", category=FutureWarning, module="diffuse
11
  warnings.filterwarnings(action="ignore", category=UserWarning, module="diffusers")
12
  warnings.filterwarnings(action="ignore", category=FutureWarning, module="transformers")
13
  from pathlib import Path
14
- from env import (
15
- hf_token,
16
- hf_read_token, # to use only for private repos
17
- CIVITAI_API_KEY,
18
- HF_LORA_PRIVATE_REPOS1,
19
- HF_LORA_PRIVATE_REPOS2,
20
- HF_LORA_ESSENTIAL_PRIVATE_REPO,
21
- HF_VAE_PRIVATE_REPO,
22
- directory_models,
23
- directory_loras,
24
- directory_vaes,
25
- download_model_list,
26
- download_lora_list,
27
- download_vae_list,
28
- )
29
- from modutils import (
30
- to_list,
31
- list_uniq,
32
- list_sub,
33
- get_lora_model_list,
34
- download_private_repo,
35
- safe_float,
36
- escape_lora_basename,
37
- to_lora_key,
38
- to_lora_path,
39
- get_local_model_list,
40
- get_private_lora_model_lists,
41
- get_valid_lora_name,
42
- get_valid_lora_path,
43
- get_valid_lora_wt,
44
- get_lora_info,
45
- normalize_prompt_list,
46
- get_civitai_info,
47
- search_lora_on_civitai,
48
- )
49
 
50
 
51
  def download_things(directory, url, hf_token="", civitai_api_key=""):
@@ -136,7 +109,9 @@ def get_t2i_model_info(repo_id: str):
136
  info = []
137
  url = f"https://huggingface.co/{repo_id}/"
138
  if not 'diffusers' in tags: return ""
139
- if 'diffusers:StableDiffusionXLPipeline' in tags:
 
 
140
  info.append("SDXL")
141
  elif 'diffusers:StableDiffusionPipeline' in tags:
142
  info.append("SD1.5")
@@ -265,25 +240,7 @@ def apply_lora_prompt(lora_info: str):
265
 
266
 
267
  def update_loras(prompt, lora, lora_wt):
268
- import re
269
  on, label, tag, md = get_lora_info(lora)
270
- """prompts = prompt.split(",") if prompt else []
271
- output_prompts = []
272
- for p in prompts:
273
- p = str(p).strip()
274
- if "<lora" in p:
275
- result = re.findall(r'<lora:(.+?):(.+?)>', p)
276
- if not result: continue
277
- key = result[0][0]
278
- wt = result[0][1]
279
- path = to_lora_path(key)
280
- if not key in loras_dict.keys() or not path: continue
281
- if Path(path).exists(): output_prompts.append(f"<lora:{to_lora_key(path)}:{safe_float(wt):.2f}>")
282
- elif p:
283
- output_prompts.append(p)
284
- lora_prompts = []
285
- if on: lora_prompts.append(f"<lora:{to_lora_key(lora)}:{lora_wt:.2f}>")
286
- output_prompt = ", ".join(list_uniq(output_prompts + lora_prompts))"""
287
  choices = get_all_lora_tupled_list()
288
  return gr.update(value=prompt), gr.update(value=lora, choices=choices), gr.update(value=lora_wt),\
289
  gr.update(value=tag, label=label, visible=on), gr.update(value=md, visible=on)
 
11
  warnings.filterwarnings(action="ignore", category=UserWarning, module="diffusers")
12
  warnings.filterwarnings(action="ignore", category=FutureWarning, module="transformers")
13
  from pathlib import Path
14
+ from env import (hf_token, hf_read_token, # to use only for private repos
15
+ CIVITAI_API_KEY, HF_LORA_PRIVATE_REPOS1, HF_LORA_PRIVATE_REPOS2, HF_LORA_ESSENTIAL_PRIVATE_REPO,
16
+ HF_VAE_PRIVATE_REPO, directory_models, directory_loras, directory_vaes,
17
+ download_model_list, download_lora_list, download_vae_list)
18
+ from modutils import (to_list, list_uniq, list_sub, get_lora_model_list, download_private_repo,
19
+ safe_float, escape_lora_basename, to_lora_key, to_lora_path, get_local_model_list,
20
+ get_private_lora_model_lists, get_valid_lora_name, get_valid_lora_path, get_valid_lora_wt,
21
+ get_lora_info, normalize_prompt_list, get_civitai_info, search_lora_on_civitai)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
 
24
  def download_things(directory, url, hf_token="", civitai_api_key=""):
 
109
  info = []
110
  url = f"https://huggingface.co/{repo_id}/"
111
  if not 'diffusers' in tags: return ""
112
+ if 'diffusers:FluxPipeline' in tags:
113
+ info.append("FLUX.1")
114
+ elif 'diffusers:StableDiffusionXLPipeline' in tags:
115
  info.append("SDXL")
116
  elif 'diffusers:StableDiffusionPipeline' in tags:
117
  info.append("SD1.5")
 
240
 
241
 
242
  def update_loras(prompt, lora, lora_wt):
 
243
  on, label, tag, md = get_lora_info(lora)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
  choices = get_all_lora_tupled_list()
245
  return gr.update(value=prompt), gr.update(value=lora, choices=choices), gr.update(value=lora_wt),\
246
  gr.update(value=tag, label=label, visible=on), gr.update(value=md, visible=on)
modutils.py CHANGED
@@ -1,19 +1,14 @@
 
1
  import json
2
  import gradio as gr
3
  from huggingface_hub import HfApi
4
  import os
5
  from pathlib import Path
6
 
7
- from env import (
8
- HF_LORA_PRIVATE_REPOS1,
9
- HF_LORA_PRIVATE_REPOS2,
10
- HF_MODEL_USER_EX,
11
- HF_MODEL_USER_LIKES,
12
- directory_loras,
13
- hf_read_token,
14
- hf_token,
15
- CIVITAI_API_KEY,
16
- )
17
 
18
 
19
  def get_user_agent():
@@ -112,8 +107,8 @@ def save_gallery_images(images, progress=gr.Progress(track_tqdm=True)):
112
  try:
113
  if oldpath.exists():
114
  newpath = oldpath.resolve().rename(Path(filename).resolve())
115
- except Exception:
116
- pass
117
  finally:
118
  output_paths.append(str(newpath))
119
  output_images.append((str(newpath), str(filename)))
@@ -127,7 +122,8 @@ def download_private_repo(repo_id, dir_path, is_replace):
127
  try:
128
  snapshot_download(repo_id=repo_id, local_dir=dir_path, allow_patterns=['*.ckpt', '*.pt', '*.pth', '*.safetensors', '*.bin'], use_auth_token=hf_read_token)
129
  except Exception as e:
130
- print(f"Error: Failed to download {repo_id}. ")
 
131
  return
132
  if is_replace:
133
  for file in Path(dir_path).glob("*"):
@@ -146,7 +142,8 @@ def get_private_model_list(repo_id, dir_path):
146
  try:
147
  files = api.list_repo_files(repo_id, token=hf_read_token)
148
  except Exception as e:
149
- print(f"Error: Failed to list {repo_id}. ")
 
150
  return []
151
  model_list = []
152
  for file in files:
@@ -168,7 +165,8 @@ def download_private_file(repo_id, path, is_replace):
168
  try:
169
  hf_hub_download(repo_id=repo_id, filename=filename, local_dir=dirname, use_auth_token=hf_read_token)
170
  except Exception as e:
171
- print(f"Error: Failed to download {filename}. ")
 
172
  return
173
  if is_replace:
174
  file.resolve().rename(newpath.resolve())
@@ -194,7 +192,8 @@ def get_model_id_list():
194
  for author in HF_MODEL_USER_EX:
195
  models_ex = api.list_models(author=author, cardData=True, sort="last_modified")
196
  except Exception as e:
197
- print(f"Error: Failed to list {author}'s models. ")
 
198
  return model_ids
199
  for model in models_likes:
200
  model_ids.append(model.id) if not model.private else ""
@@ -218,17 +217,17 @@ def get_t2i_model_info(repo_id: str):
218
  if " " in repo_id or not api.repo_exists(repo_id): return ""
219
  model = api.model_info(repo_id=repo_id)
220
  except Exception as e:
221
- print(f"Error: Failed to get {repo_id}'s info. ")
 
222
  return ""
223
  if model.private or model.gated: return ""
224
  tags = model.tags
225
  info = []
226
  url = f"https://huggingface.co/{repo_id}/"
227
  if not 'diffusers' in tags: return ""
228
- if 'diffusers:StableDiffusionXLPipeline' in tags:
229
- info.append("SDXL")
230
- elif 'diffusers:StableDiffusionPipeline' in tags:
231
- info.append("SD1.5")
232
  if model.card_data and model.card_data.tags:
233
  info.extend(list_sub(model.card_data.tags, ['text-to-image', 'stable-diffusion', 'stable-diffusion-api', 'safetensors', 'stable-diffusion-xl']))
234
  info.append(f"DLs: {model.downloads}")
@@ -247,6 +246,7 @@ def get_tupled_model_list(model_list):
247
  if not api.repo_exists(repo_id): continue
248
  model = api.model_info(repo_id=repo_id)
249
  except Exception as e:
 
250
  continue
251
  if model.private or model.gated: continue
252
  tags = model.tags
@@ -273,8 +273,8 @@ try:
273
  d = json.load(f)
274
  for k, v in d.items():
275
  private_lora_dict[escape_lora_basename(k)] = v
276
- except Exception:
277
- pass
278
  loras_dict = {"None": ["", "", "", "", ""], "": ["", "", "", "", ""]} | private_lora_dict.copy()
279
  civitai_not_exists_list = []
280
  loras_url_to_path_dict = {} # {"URL to download": "local filepath", ...}
@@ -322,6 +322,7 @@ def get_civitai_info(path):
322
  try:
323
  r = session.get(url, params=params, headers=headers, stream=True, timeout=(3.0, 15))
324
  except Exception as e:
 
325
  return ["", "", "", "", ""]
326
  if not r.ok: return None
327
  json = r.json()
@@ -420,7 +421,8 @@ def copy_lora(path: str, new_path: str):
420
  if cpath.exists():
421
  try:
422
  shutil.copy(str(cpath.resolve()), str(npath.resolve()))
423
- except Exception:
 
424
  return None
425
  update_lora_dict(str(npath))
426
  return new_path
@@ -1215,6 +1217,8 @@ def get_model_pipeline(repo_id: str):
1215
  if model.private or model.gated: return default
1216
  tags = model.tags
1217
  if not 'diffusers' in tags: return default
 
 
1218
  if 'diffusers:StableDiffusionXLPipeline' in tags:
1219
  return "StableDiffusionXLPipeline"
1220
  elif 'diffusers:StableDiffusionPipeline' in tags:
 
1
+ import spaces
2
  import json
3
  import gradio as gr
4
  from huggingface_hub import HfApi
5
  import os
6
  from pathlib import Path
7
 
8
+
9
+ from env import (HF_LORA_PRIVATE_REPOS1, HF_LORA_PRIVATE_REPOS2,
10
+ HF_MODEL_USER_EX, HF_MODEL_USER_LIKES,
11
+ directory_loras, hf_read_token, hf_token, CIVITAI_API_KEY)
 
 
 
 
 
 
12
 
13
 
14
  def get_user_agent():
 
107
  try:
108
  if oldpath.exists():
109
  newpath = oldpath.resolve().rename(Path(filename).resolve())
110
+ except Exception as e:
111
+ print(e)
112
  finally:
113
  output_paths.append(str(newpath))
114
  output_images.append((str(newpath), str(filename)))
 
122
  try:
123
  snapshot_download(repo_id=repo_id, local_dir=dir_path, allow_patterns=['*.ckpt', '*.pt', '*.pth', '*.safetensors', '*.bin'], use_auth_token=hf_read_token)
124
  except Exception as e:
125
+ print(f"Error: Failed to download {repo_id}.")
126
+ print(e)
127
  return
128
  if is_replace:
129
  for file in Path(dir_path).glob("*"):
 
142
  try:
143
  files = api.list_repo_files(repo_id, token=hf_read_token)
144
  except Exception as e:
145
+ print(f"Error: Failed to list {repo_id}.")
146
+ print(e)
147
  return []
148
  model_list = []
149
  for file in files:
 
165
  try:
166
  hf_hub_download(repo_id=repo_id, filename=filename, local_dir=dirname, use_auth_token=hf_read_token)
167
  except Exception as e:
168
+ print(f"Error: Failed to download {filename}.")
169
+ print(e)
170
  return
171
  if is_replace:
172
  file.resolve().rename(newpath.resolve())
 
192
  for author in HF_MODEL_USER_EX:
193
  models_ex = api.list_models(author=author, cardData=True, sort="last_modified")
194
  except Exception as e:
195
+ print(f"Error: Failed to list {author}'s models.")
196
+ print(e)
197
  return model_ids
198
  for model in models_likes:
199
  model_ids.append(model.id) if not model.private else ""
 
217
  if " " in repo_id or not api.repo_exists(repo_id): return ""
218
  model = api.model_info(repo_id=repo_id)
219
  except Exception as e:
220
+ print(f"Error: Failed to get {repo_id}'s info.")
221
+ print(e)
222
  return ""
223
  if model.private or model.gated: return ""
224
  tags = model.tags
225
  info = []
226
  url = f"https://huggingface.co/{repo_id}/"
227
  if not 'diffusers' in tags: return ""
228
+ if 'diffusers:FluxPipeline' in tags: info.append("FLUX.1")
229
+ elif 'diffusers:StableDiffusionXLPipeline' in tags: info.append("SDXL")
230
+ elif 'diffusers:StableDiffusionPipeline' in tags: info.append("SD1.5")
 
231
  if model.card_data and model.card_data.tags:
232
  info.extend(list_sub(model.card_data.tags, ['text-to-image', 'stable-diffusion', 'stable-diffusion-api', 'safetensors', 'stable-diffusion-xl']))
233
  info.append(f"DLs: {model.downloads}")
 
246
  if not api.repo_exists(repo_id): continue
247
  model = api.model_info(repo_id=repo_id)
248
  except Exception as e:
249
+ print(e)
250
  continue
251
  if model.private or model.gated: continue
252
  tags = model.tags
 
273
  d = json.load(f)
274
  for k, v in d.items():
275
  private_lora_dict[escape_lora_basename(k)] = v
276
+ except Exception as e:
277
+ print(e)
278
  loras_dict = {"None": ["", "", "", "", ""], "": ["", "", "", "", ""]} | private_lora_dict.copy()
279
  civitai_not_exists_list = []
280
  loras_url_to_path_dict = {} # {"URL to download": "local filepath", ...}
 
322
  try:
323
  r = session.get(url, params=params, headers=headers, stream=True, timeout=(3.0, 15))
324
  except Exception as e:
325
+ print(e)
326
  return ["", "", "", "", ""]
327
  if not r.ok: return None
328
  json = r.json()
 
421
  if cpath.exists():
422
  try:
423
  shutil.copy(str(cpath.resolve()), str(npath.resolve()))
424
+ except Exception as e:
425
+ print(e)
426
  return None
427
  update_lora_dict(str(npath))
428
  return new_path
 
1217
  if model.private or model.gated: return default
1218
  tags = model.tags
1219
  if not 'diffusers' in tags: return default
1220
+ if 'diffusers:FluxPipeline' in tags:
1221
+ return "FluxPipeline"
1222
  if 'diffusers:StableDiffusionXLPipeline' in tags:
1223
  return "StableDiffusionXLPipeline"
1224
  elif 'diffusers:StableDiffusionPipeline' in tags: