Upload 13 files
Browse files- app -nonyieldlast.py +696 -0
- app.py +90 -63
- mod.py +12 -1
- modutils.py +11 -0
- requirements.txt +2 -1
- tagger/tagger.py +18 -23
app -nonyieldlast.py
ADDED
@@ -0,0 +1,696 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import gradio as gr
|
3 |
+
import json
|
4 |
+
import logging
|
5 |
+
import torch
|
6 |
+
from PIL import Image
|
7 |
+
from diffusers import DiffusionPipeline
|
8 |
+
from diffusers import FluxControlNetPipeline, FluxControlNetModel, FluxMultiControlNetModel
|
9 |
+
from huggingface_hub import hf_hub_download, HfFileSystem, ModelCard, snapshot_download
|
10 |
+
import copy
|
11 |
+
import random
|
12 |
+
import time
|
13 |
+
|
14 |
+
|
15 |
+
from mod import (models, clear_cache, get_repo_safetensors, is_repo_name, is_repo_exists,
|
16 |
+
description_ui, num_loras, compose_lora_json, is_valid_lora, fuse_loras,
|
17 |
+
get_trigger_word, enhance_prompt, deselect_lora, num_cns, set_control_union_image,
|
18 |
+
get_control_union_mode, set_control_union_mode, get_control_params)
|
19 |
+
from flux import (search_civitai_lora, select_civitai_lora, search_civitai_lora_json,
|
20 |
+
download_my_lora, get_all_lora_tupled_list, apply_lora_prompt,
|
21 |
+
update_loras, get_t2i_model_info)
|
22 |
+
from tagger.tagger import predict_tags_wd, compose_prompt_to_copy
|
23 |
+
from tagger.fl2flux import predict_tags_fl2_flux
|
24 |
+
|
25 |
+
|
26 |
+
dtype = torch.bfloat16
|
27 |
+
#dtype = torch.float8_e4m3fn
|
28 |
+
# Initialize the base model
|
29 |
+
base_model = models[0]
|
30 |
+
controlnet_model_union_repo = 'InstantX/FLUX.1-dev-Controlnet-Union'
|
31 |
+
#controlnet_model_union_repo = 'InstantX/FLUX.1-dev-Controlnet-Union-alpha'
|
32 |
+
pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=dtype)
|
33 |
+
controlnet_union = None
|
34 |
+
controlnet = None
|
35 |
+
last_model = models[0]
|
36 |
+
last_cn_on = False
|
37 |
+
|
38 |
+
# https://huggingface.co/InstantX/FLUX.1-dev-Controlnet-Union
|
39 |
+
# https://huggingface.co/spaces/jiuface/FLUX.1-dev-Controlnet-Union
|
40 |
+
def change_base_model(repo_id: str, cn_on: bool, progress=gr.Progress(track_tqdm=True)):
|
41 |
+
global pipe
|
42 |
+
global controlnet_union
|
43 |
+
global controlnet
|
44 |
+
global last_model
|
45 |
+
global last_cn_on
|
46 |
+
global dtype
|
47 |
+
try:
|
48 |
+
if (repo_id == last_model and cn_on is last_cn_on) or not is_repo_name(repo_id) or not is_repo_exists(repo_id): return gr.update(visible=True)
|
49 |
+
if cn_on:
|
50 |
+
#progress(0, desc=f"Loading model: {repo_id} / Loading ControlNet: {controlnet_model_union_repo}")
|
51 |
+
print(f"Loading model: {repo_id} / Loading ControlNet: {controlnet_model_union_repo}")
|
52 |
+
clear_cache()
|
53 |
+
controlnet_union = FluxControlNetModel.from_pretrained(controlnet_model_union_repo, torch_dtype=dtype)
|
54 |
+
controlnet = FluxMultiControlNetModel([controlnet_union])
|
55 |
+
pipe = FluxControlNetPipeline.from_pretrained(repo_id, controlnet=controlnet, torch_dtype=dtype)
|
56 |
+
last_model = repo_id
|
57 |
+
last_cn_on = cn_on
|
58 |
+
#progress(1, desc=f"Model loaded: {repo_id} / ControlNet Loaded: {controlnet_model_union_repo}")
|
59 |
+
print(f"Model loaded: {repo_id} / ControlNet Loaded: {controlnet_model_union_repo}")
|
60 |
+
else:
|
61 |
+
#progress(0, desc=f"Loading model: {repo_id}")
|
62 |
+
print(f"Loading model: {repo_id}")
|
63 |
+
clear_cache()
|
64 |
+
pipe = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=dtype)
|
65 |
+
last_model = repo_id
|
66 |
+
last_cn_on = cn_on
|
67 |
+
#progress(1, desc=f"Model loaded: {repo_id}")
|
68 |
+
print(f"Model loaded: {repo_id}")
|
69 |
+
except Exception as e:
|
70 |
+
print(f"Model load Error: {e}")
|
71 |
+
raise gr.Error(f"Model load Error: {e}") from e
|
72 |
+
return gr.update(visible=True)
|
73 |
+
|
74 |
+
change_base_model.zerogpu = True
|
75 |
+
|
76 |
+
# Load LoRAs from JSON file
|
77 |
+
with open('loras.json', 'r') as f:
|
78 |
+
loras = json.load(f)
|
79 |
+
|
80 |
+
MAX_SEED = 2**32-1
|
81 |
+
|
82 |
+
class calculateDuration:
|
83 |
+
def __init__(self, activity_name=""):
|
84 |
+
self.activity_name = activity_name
|
85 |
+
|
86 |
+
def __enter__(self):
|
87 |
+
self.start_time = time.time()
|
88 |
+
return self
|
89 |
+
|
90 |
+
def __exit__(self, exc_type, exc_value, traceback):
|
91 |
+
self.end_time = time.time()
|
92 |
+
self.elapsed_time = self.end_time - self.start_time
|
93 |
+
if self.activity_name:
|
94 |
+
print(f"Elapsed time for {self.activity_name}: {self.elapsed_time:.6f} seconds")
|
95 |
+
else:
|
96 |
+
print(f"Elapsed time: {self.elapsed_time:.6f} seconds")
|
97 |
+
|
98 |
+
def update_selection(evt: gr.SelectData, width, height):
|
99 |
+
selected_lora = loras[evt.index]
|
100 |
+
new_placeholder = f"Type a prompt for {selected_lora['title']}"
|
101 |
+
lora_repo = selected_lora["repo"]
|
102 |
+
updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) ✨"
|
103 |
+
if "aspect" in selected_lora:
|
104 |
+
if selected_lora["aspect"] == "portrait":
|
105 |
+
width = 768
|
106 |
+
height = 1024
|
107 |
+
elif selected_lora["aspect"] == "landscape":
|
108 |
+
width = 1024
|
109 |
+
height = 768
|
110 |
+
else:
|
111 |
+
width = 1024
|
112 |
+
height = 1024
|
113 |
+
return (
|
114 |
+
gr.update(placeholder=new_placeholder),
|
115 |
+
updated_text,
|
116 |
+
evt.index,
|
117 |
+
width,
|
118 |
+
height,
|
119 |
+
)
|
120 |
+
|
121 |
+
@spaces.GPU(duration=70)
|
122 |
+
def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, cn_on, progress=gr.Progress(track_tqdm=True)):
|
123 |
+
global pipe
|
124 |
+
global controlnet
|
125 |
+
global controlnet_union
|
126 |
+
try:
|
127 |
+
pipe.to("cuda")
|
128 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
129 |
+
|
130 |
+
with calculateDuration("Generating image"):
|
131 |
+
# Generate image
|
132 |
+
modes, images, scales = get_control_params()
|
133 |
+
if not cn_on or len(modes) == 0:
|
134 |
+
progress(0, desc="Start Inference.")
|
135 |
+
image = pipe(
|
136 |
+
prompt=prompt_mash,
|
137 |
+
num_inference_steps=steps,
|
138 |
+
guidance_scale=cfg_scale,
|
139 |
+
width=width,
|
140 |
+
height=height,
|
141 |
+
generator=generator,
|
142 |
+
joint_attention_kwargs={"scale": lora_scale},
|
143 |
+
).images[0]
|
144 |
+
else:
|
145 |
+
progress(0, desc="Start Inference with ControlNet.")
|
146 |
+
if controlnet is not None: controlnet.to("cuda")
|
147 |
+
if controlnet_union is not None: controlnet_union.to("cuda")
|
148 |
+
image = pipe(
|
149 |
+
prompt=prompt_mash,
|
150 |
+
control_image=images,
|
151 |
+
control_mode=modes,
|
152 |
+
num_inference_steps=steps,
|
153 |
+
guidance_scale=cfg_scale,
|
154 |
+
width=width,
|
155 |
+
height=height,
|
156 |
+
controlnet_conditioning_scale=scales,
|
157 |
+
generator=generator,
|
158 |
+
joint_attention_kwargs={"scale": lora_scale},
|
159 |
+
).images[0]
|
160 |
+
except Exception as e:
|
161 |
+
print(e)
|
162 |
+
raise gr.Error(f"Inference Error {e}") from e
|
163 |
+
return image
|
164 |
+
|
165 |
+
def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height,
|
166 |
+
lora_scale, lora_json, cn_on, progress=gr.Progress(track_tqdm=True)):
|
167 |
+
global pipe
|
168 |
+
if selected_index is None and not is_valid_lora(lora_json):
|
169 |
+
gr.Info("LoRA isn't selected.")
|
170 |
+
# raise gr.Error("You must select a LoRA before proceeding.")
|
171 |
+
progress(0, desc="Preparing Inference.")
|
172 |
+
|
173 |
+
prompt_mash = prompt
|
174 |
+
if is_valid_lora(lora_json):
|
175 |
+
with calculateDuration("Loading LoRA weights"):
|
176 |
+
fuse_loras(pipe, lora_json)
|
177 |
+
trigger_word = get_trigger_word(lora_json)
|
178 |
+
prompt_mash = f"{prompt} {trigger_word}"
|
179 |
+
if selected_index is not None:
|
180 |
+
selected_lora = loras[selected_index]
|
181 |
+
lora_path = selected_lora["repo"]
|
182 |
+
trigger_word = selected_lora["trigger_word"]
|
183 |
+
if(trigger_word):
|
184 |
+
if "trigger_position" in selected_lora:
|
185 |
+
if selected_lora["trigger_position"] == "prepend":
|
186 |
+
prompt_mash = f"{trigger_word} {prompt}"
|
187 |
+
else:
|
188 |
+
prompt_mash = f"{prompt} {trigger_word}"
|
189 |
+
else:
|
190 |
+
prompt_mash = f"{trigger_word} {prompt}"
|
191 |
+
else:
|
192 |
+
prompt_mash = prompt
|
193 |
+
# Load LoRA weights
|
194 |
+
with calculateDuration(f"Loading LoRA weights for {selected_lora['title']}"):
|
195 |
+
if "weights" in selected_lora:
|
196 |
+
pipe.load_lora_weights(lora_path, weight_name=selected_lora["weights"])
|
197 |
+
else:
|
198 |
+
pipe.load_lora_weights(lora_path)
|
199 |
+
|
200 |
+
# Set random seed for reproducibility
|
201 |
+
with calculateDuration("Randomizing seed"):
|
202 |
+
if randomize_seed:
|
203 |
+
seed = random.randint(0, MAX_SEED)
|
204 |
+
|
205 |
+
progress(0, desc="Running Inference.")
|
206 |
+
|
207 |
+
image = generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, cn_on, progress)
|
208 |
+
if is_valid_lora(lora_json):
|
209 |
+
pipe.unfuse_lora()
|
210 |
+
pipe.unload_lora_weights()
|
211 |
+
if selected_index is not None: pipe.unload_lora_weights()
|
212 |
+
pipe.to("cpu")
|
213 |
+
if controlnet is not None: controlnet.to("cpu")
|
214 |
+
if controlnet_union is not None: controlnet_union.to("cpu")
|
215 |
+
clear_cache()
|
216 |
+
return image, seed
|
217 |
+
|
218 |
+
def get_huggingface_safetensors(link):
|
219 |
+
split_link = link.split("/")
|
220 |
+
if(len(split_link) == 2):
|
221 |
+
model_card = ModelCard.load(link)
|
222 |
+
base_model = model_card.data.get("base_model")
|
223 |
+
print(base_model)
|
224 |
+
if((base_model != "black-forest-labs/FLUX.1-dev") and (base_model != "black-forest-labs/FLUX.1-schnell")):
|
225 |
+
raise Exception("Not a FLUX LoRA!")
|
226 |
+
image_path = model_card.data.get("widget", [{}])[0].get("output", {}).get("url", None)
|
227 |
+
trigger_word = model_card.data.get("instance_prompt", "")
|
228 |
+
image_url = f"https://huggingface.co/{link}/resolve/main/{image_path}" if image_path else None
|
229 |
+
fs = HfFileSystem()
|
230 |
+
try:
|
231 |
+
list_of_files = fs.ls(link, detail=False)
|
232 |
+
for file in list_of_files:
|
233 |
+
if(file.endswith(".safetensors")):
|
234 |
+
safetensors_name = file.split("/")[-1]
|
235 |
+
if (not image_url and file.lower().endswith((".jpg", ".jpeg", ".png", ".webp"))):
|
236 |
+
image_elements = file.split("/")
|
237 |
+
image_url = f"https://huggingface.co/{link}/resolve/main/{image_elements[-1]}"
|
238 |
+
except Exception as e:
|
239 |
+
print(e)
|
240 |
+
gr.Warning(f"You didn't include a link neither a valid Hugging Face repository with a *.safetensors LoRA")
|
241 |
+
raise Exception(f"You didn't include a link neither a valid Hugging Face repository with a *.safetensors LoRA")
|
242 |
+
return split_link[1], link, safetensors_name, trigger_word, image_url
|
243 |
+
|
244 |
+
def check_custom_model(link):
|
245 |
+
if(link.startswith("https://")):
|
246 |
+
if(link.startswith("https://huggingface.co") or link.startswith("https://www.huggingface.co")):
|
247 |
+
link_split = link.split("huggingface.co/")
|
248 |
+
return get_huggingface_safetensors(link_split[1])
|
249 |
+
else:
|
250 |
+
return get_huggingface_safetensors(link)
|
251 |
+
|
252 |
+
def add_custom_lora(custom_lora):
|
253 |
+
global loras
|
254 |
+
if(custom_lora):
|
255 |
+
try:
|
256 |
+
title, repo, path, trigger_word, image = check_custom_model(custom_lora)
|
257 |
+
print(f"Loaded custom LoRA: {repo}")
|
258 |
+
card = f'''
|
259 |
+
<div class="custom_lora_card">
|
260 |
+
<span>Loaded custom LoRA:</span>
|
261 |
+
<div class="card_internal">
|
262 |
+
<img src="{image}" />
|
263 |
+
<div>
|
264 |
+
<h3>{title}</h3>
|
265 |
+
<small>{"Using: <code><b>"+trigger_word+"</code></b> as the trigger word" if trigger_word else "No trigger word found. If there's a trigger word, include it in your prompt"}<br></small>
|
266 |
+
</div>
|
267 |
+
</div>
|
268 |
+
</div>
|
269 |
+
'''
|
270 |
+
existing_item_index = next((index for (index, item) in enumerate(loras) if item['repo'] == repo), None)
|
271 |
+
if(not existing_item_index):
|
272 |
+
new_item = {
|
273 |
+
"image": image,
|
274 |
+
"title": title,
|
275 |
+
"repo": repo,
|
276 |
+
"weights": path,
|
277 |
+
"trigger_word": trigger_word
|
278 |
+
}
|
279 |
+
print(new_item)
|
280 |
+
existing_item_index = len(loras)
|
281 |
+
loras.append(new_item)
|
282 |
+
|
283 |
+
return gr.update(visible=True, value=card), gr.update(visible=True), gr.Gallery(selected_index=None), f"Custom: {path}", existing_item_index, trigger_word
|
284 |
+
except Exception as e:
|
285 |
+
gr.Warning(f"Invalid LoRA: either you entered an invalid link, or a non-FLUX LoRA")
|
286 |
+
return gr.update(visible=True, value=f"Invalid LoRA: either you entered an invalid link, a non-FLUX LoRA"), gr.update(visible=True), gr.update(), "", None, ""
|
287 |
+
else:
|
288 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(), "", None, ""
|
289 |
+
|
290 |
+
def remove_custom_lora():
|
291 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(), "", None, ""
|
292 |
+
|
293 |
+
run_lora.zerogpu = True
|
294 |
+
|
295 |
+
css = '''
|
296 |
+
#gen_btn{height: 100%}
|
297 |
+
#title{text-align: center}
|
298 |
+
#title h1{font-size: 3em; display:inline-flex; align-items:center}
|
299 |
+
#title img{width: 100px; margin-right: 0.5em}
|
300 |
+
#gallery .grid-wrap{height: 10vh}
|
301 |
+
#lora_list{background: var(--block-background-fill);padding: 0 1em .3em; font-size: 90%}
|
302 |
+
.card_internal{display: flex;height: 100px;margin-top: .5em}
|
303 |
+
.card_internal img{margin-right: 1em}
|
304 |
+
.styler{--form-gap-width: 0px !important}
|
305 |
+
#progress{height:30px}
|
306 |
+
#progress .generating{display:none}
|
307 |
+
.progress-container {width: 100%;height: 30px;background-color: #f0f0f0;border-radius: 15px;overflow: hidden;margin-bottom: 20px}
|
308 |
+
.progress-bar {height: 100%;background-color: #4f46e5;width: calc(var(--current) / var(--total) * 100%);transition: width 0.5s ease-in-out}
|
309 |
+
#model-info {text-align: center; !important}
|
310 |
+
'''
|
311 |
+
with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=css, delete_cache=(60, 3600)) as app:
|
312 |
+
with gr.Tab("FLUX LoRA the Explorer"):
|
313 |
+
title = gr.HTML(
|
314 |
+
"""<h1><img src="https://huggingface.co/spaces/multimodalart/flux-lora-the-explorer/resolve/main/flux_lora.png" alt="LoRA">FLUX LoRA the Explorer Mod</h1>""",
|
315 |
+
elem_id="title",
|
316 |
+
)
|
317 |
+
selected_index = gr.State(None)
|
318 |
+
with gr.Row():
|
319 |
+
with gr.Column(scale=3):
|
320 |
+
with gr.Group():
|
321 |
+
with gr.Accordion("Generate Prompt from Image", open=False):
|
322 |
+
tagger_image = gr.Image(label="Input image", type="pil", sources=["upload", "clipboard"], height=256)
|
323 |
+
with gr.Accordion(label="Advanced options", open=False):
|
324 |
+
tagger_general_threshold = gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.3, step=0.01, interactive=True)
|
325 |
+
tagger_character_threshold = gr.Slider(label="Character threshold", minimum=0.0, maximum=1.0, value=0.8, step=0.01, interactive=True)
|
326 |
+
neg_prompt = gr.Text(label="Negative Prompt", lines=1, max_lines=8, placeholder="", visible=False)
|
327 |
+
v2_character = gr.Textbox(label="Character", placeholder="hatsune miku", scale=2, visible=False)
|
328 |
+
v2_series = gr.Textbox(label="Series", placeholder="vocaloid", scale=2, visible=False)
|
329 |
+
v2_copy = gr.Button(value="Copy to clipboard", size="sm", interactive=False, visible=False)
|
330 |
+
tagger_algorithms = gr.CheckboxGroup(["Use WD Tagger", "Use Florence-2-Flux"], label="Algorithms", value=["Use WD Tagger"])
|
331 |
+
tagger_generate_from_image = gr.Button(value="Generate Prompt from Image")
|
332 |
+
prompt = gr.Textbox(label="Prompt", lines=1, max_lines=8, placeholder="Type a prompt")
|
333 |
+
prompt_enhance = gr.Button(value="Enhance your prompt", variant="secondary")
|
334 |
+
with gr.Column(scale=1, elem_id="gen_column"):
|
335 |
+
generate_button = gr.Button("Generate", variant="primary", elem_id="gen_btn")
|
336 |
+
with gr.Row():
|
337 |
+
with gr.Column():
|
338 |
+
selected_info = gr.Markdown("")
|
339 |
+
gallery = gr.Gallery(
|
340 |
+
[(item["image"], item["title"]) for item in loras],
|
341 |
+
label="LoRA Gallery",
|
342 |
+
allow_preview=False,
|
343 |
+
columns=3,
|
344 |
+
elem_id="gallery"
|
345 |
+
)
|
346 |
+
with gr.Group():
|
347 |
+
custom_lora = gr.Textbox(label="Custom LoRA", info="LoRA Hugging Face path", placeholder="multimodalart/vintage-ads-flux")
|
348 |
+
gr.Markdown("[Check the list of FLUX LoRas](https://huggingface.co/models?other=base_model:adapter:black-forest-labs/FLUX.1-dev)", elem_id="lora_list")
|
349 |
+
custom_lora_info = gr.HTML(visible=False)
|
350 |
+
custom_lora_button = gr.Button("Remove custom LoRA", visible=False)
|
351 |
+
deselect_lora_button = gr.Button("Deselect LoRA", variant="secondary")
|
352 |
+
with gr.Column():
|
353 |
+
result = gr.Image(label="Generated Image", format="png", show_share_button=False)
|
354 |
+
with gr.Group():
|
355 |
+
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)
|
356 |
+
model_info = gr.Markdown(elem_id="model-info")
|
357 |
+
with gr.Row():
|
358 |
+
with gr.Accordion("Advanced Settings", open=False):
|
359 |
+
with gr.Column():
|
360 |
+
with gr.Row():
|
361 |
+
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=3.5)
|
362 |
+
steps = gr.Slider(label="Steps", minimum=1, maximum=50, step=1, value=28)
|
363 |
+
with gr.Row():
|
364 |
+
width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=1024)
|
365 |
+
height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=1024)
|
366 |
+
with gr.Row():
|
367 |
+
randomize_seed = gr.Checkbox(True, label="Randomize seed")
|
368 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
|
369 |
+
lora_scale = gr.Slider(label="LoRA Scale", minimum=-3, maximum=3, step=0.01, value=0.95)
|
370 |
+
with gr.Accordion("External LoRA", open=True):
|
371 |
+
with gr.Column():
|
372 |
+
lora_repo_json = gr.JSON(value=[{}] * num_loras, visible=False)
|
373 |
+
lora_repo = [None] * num_loras
|
374 |
+
lora_weights = [None] * num_loras
|
375 |
+
lora_trigger = [None] * num_loras
|
376 |
+
lora_wt = [None] * num_loras
|
377 |
+
lora_info = [None] * num_loras
|
378 |
+
lora_copy = [None] * num_loras
|
379 |
+
lora_md = [None] * num_loras
|
380 |
+
lora_num = [None] * num_loras
|
381 |
+
with gr.Row():
|
382 |
+
for i in range(num_loras):
|
383 |
+
with gr.Column():
|
384 |
+
lora_repo[i] = gr.Dropdown(label=f"LoRA {int(i+1)} Repo", choices=get_all_lora_tupled_list(), info="Input LoRA Repo ID", value="", allow_custom_value=True)
|
385 |
+
with gr.Row():
|
386 |
+
lora_weights[i] = gr.Dropdown(label=f"LoRA {int(i+1)} Filename", choices=[], info="Optional", value="", allow_custom_value=True)
|
387 |
+
lora_trigger[i] = gr.Textbox(label=f"LoRA {int(i+1)} Trigger Prompt", lines=1, max_lines=4, value="")
|
388 |
+
lora_wt[i] = gr.Slider(label=f"LoRA {int(i+1)} Scale", minimum=-3, maximum=3, step=0.01, value=1.00)
|
389 |
+
with gr.Row():
|
390 |
+
lora_info[i] = gr.Textbox(label="", info="Example of prompt:", value="", show_copy_button=True, interactive=False, visible=False)
|
391 |
+
lora_copy[i] = gr.Button(value="Copy example to prompt", visible=False)
|
392 |
+
lora_md[i] = gr.Markdown(value="", visible=False)
|
393 |
+
lora_num[i] = gr.Number(i, visible=False)
|
394 |
+
with gr.Accordion("From URL", open=True, visible=True):
|
395 |
+
with gr.Row():
|
396 |
+
lora_search_civitai_query = gr.Textbox(label="Query", placeholder="flux", lines=1)
|
397 |
+
lora_search_civitai_submit = gr.Button("Search on Civitai")
|
398 |
+
lora_search_civitai_basemodel = gr.CheckboxGroup(label="Search LoRA for", choices=["Flux.1 D", "Flux.1 S"], value=["Flux.1 D", "Flux.1 S"])
|
399 |
+
with gr.Row():
|
400 |
+
lora_search_civitai_json = gr.JSON(value={}, visible=False)
|
401 |
+
lora_search_civitai_desc = gr.Markdown(value="", visible=False)
|
402 |
+
lora_search_civitai_result = gr.Dropdown(label="Search Results", choices=[("", "")], value="", allow_custom_value=True, visible=False)
|
403 |
+
lora_download_url = gr.Textbox(label="URL", placeholder="http://...my_lora_url.safetensors", lines=1)
|
404 |
+
with gr.Row():
|
405 |
+
lora_download = [None] * num_loras
|
406 |
+
for i in range(num_loras):
|
407 |
+
lora_download[i] = gr.Button(f"Get and set LoRA to {int(i+1)}")
|
408 |
+
with gr.Accordion("ControlNet (🚧Under construction...🚧)", open=False, visible=False):
|
409 |
+
with gr.Column():
|
410 |
+
cn_on = gr.Checkbox(False, label="Use ControlNet")
|
411 |
+
cn_mode = [None] * num_cns
|
412 |
+
cn_scale = [None] * num_cns
|
413 |
+
cn_image = [None] * num_cns
|
414 |
+
cn_image_ref = [None] * num_cns
|
415 |
+
cn_res = [None] * num_cns
|
416 |
+
cn_num = [None] * num_cns
|
417 |
+
with gr.Row():
|
418 |
+
for i in range(num_cns):
|
419 |
+
with gr.Column():
|
420 |
+
with gr.Row():
|
421 |
+
cn_mode[i] = gr.Dropdown(label=f"ControlNet {int(i+1)} Mode", choices=get_control_union_mode(), value=get_control_union_mode()[0], allow_custom_value=False)
|
422 |
+
cn_scale[i] = gr.Slider(label=f"ControlNet {int(i+1)} Weight", minimum=0.0, maximum=1.0, step=0.01, value=0.75)
|
423 |
+
cn_res[i] = gr.Slider(label=f"ControlNet {int(i+1)} Preprocess resolution", minimum=128, maximum=512, value=384, step=1)
|
424 |
+
cn_num[i] = gr.Number(i, visible=False)
|
425 |
+
with gr.Row():
|
426 |
+
cn_image_ref[i] = gr.Image(label="Image Reference", type="pil", format="png", height=256, sources=["upload", "clipboard"], show_share_button=False)
|
427 |
+
cn_image[i] = gr.Image(label="Control Image", type="pil", format="png", height=256, show_share_button=False, interactive=False)
|
428 |
+
|
429 |
+
gallery.select(
|
430 |
+
update_selection,
|
431 |
+
inputs=[width, height],
|
432 |
+
outputs=[prompt, selected_info, selected_index, width, height],
|
433 |
+
queue=False,
|
434 |
+
show_api=False,
|
435 |
+
)
|
436 |
+
custom_lora.input(
|
437 |
+
add_custom_lora,
|
438 |
+
inputs=[custom_lora],
|
439 |
+
outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, prompt],
|
440 |
+
queue=False,
|
441 |
+
show_api=False,
|
442 |
+
)
|
443 |
+
custom_lora_button.click(
|
444 |
+
remove_custom_lora,
|
445 |
+
outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, custom_lora],
|
446 |
+
queue=False,
|
447 |
+
show_api=False,
|
448 |
+
)
|
449 |
+
gr.on(
|
450 |
+
triggers=[generate_button.click, prompt.submit],
|
451 |
+
fn=change_base_model,
|
452 |
+
inputs=[model_name, cn_on],
|
453 |
+
outputs=[result],
|
454 |
+
queue=True,
|
455 |
+
show_api=False,
|
456 |
+
trigger_mode="once",
|
457 |
+
).success(
|
458 |
+
fn=run_lora,
|
459 |
+
inputs=[prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height,
|
460 |
+
lora_scale, lora_repo_json, cn_on],
|
461 |
+
outputs=[result, seed],
|
462 |
+
queue=True,
|
463 |
+
show_api=True,
|
464 |
+
)
|
465 |
+
|
466 |
+
deselect_lora_button.click(deselect_lora, None, [prompt, selected_info, selected_index, width, height], queue=False, show_api=False)
|
467 |
+
"""gr.on(
|
468 |
+
triggers=[model_name.change, cn_on.change],
|
469 |
+
fn=change_base_model,
|
470 |
+
inputs=[model_name, cn_on],
|
471 |
+
outputs=[result],
|
472 |
+
queue=True,
|
473 |
+
show_api=False,
|
474 |
+
trigger_mode="once",
|
475 |
+
).then(get_t2i_model_info, [model_name], [model_info], queue=True, show_api=False)"""
|
476 |
+
gr.on(
|
477 |
+
triggers=[model_name.change, cn_on.change],
|
478 |
+
fn=get_t2i_model_info,
|
479 |
+
inputs=[model_name],
|
480 |
+
outputs=[model_info],
|
481 |
+
queue=False,
|
482 |
+
show_api=False,
|
483 |
+
trigger_mode="once",
|
484 |
+
).then(change_base_model, [model_name, cn_on], [result], queue=True, show_api=False)
|
485 |
+
prompt_enhance.click(enhance_prompt, [prompt], [prompt], queue=False, show_api=False)
|
486 |
+
|
487 |
+
gr.on(
|
488 |
+
triggers=[lora_search_civitai_submit.click, lora_search_civitai_query.submit],
|
489 |
+
fn=search_civitai_lora,
|
490 |
+
inputs=[lora_search_civitai_query, lora_search_civitai_basemodel],
|
491 |
+
outputs=[lora_search_civitai_result, lora_search_civitai_desc, lora_search_civitai_submit, lora_search_civitai_query],
|
492 |
+
scroll_to_output=True,
|
493 |
+
queue=True,
|
494 |
+
show_api=False,
|
495 |
+
)
|
496 |
+
lora_search_civitai_json.change(search_civitai_lora_json, [lora_search_civitai_query, lora_search_civitai_basemodel], [lora_search_civitai_json], queue=True, show_api=True) # fn for api
|
497 |
+
lora_search_civitai_result.change(select_civitai_lora, [lora_search_civitai_result], [lora_download_url, lora_search_civitai_desc], scroll_to_output=True, queue=False, show_api=False)
|
498 |
+
|
499 |
+
for i, l in enumerate(lora_repo):
|
500 |
+
deselect_lora_button.click(lambda: ("", 1.0), None, [lora_repo[i], lora_wt[i]], queue=False, show_api=False)
|
501 |
+
gr.on(
|
502 |
+
triggers=[lora_download[i].click],
|
503 |
+
fn=download_my_lora,
|
504 |
+
inputs=[lora_download_url, lora_repo[i]],
|
505 |
+
outputs=[lora_repo[i]],
|
506 |
+
scroll_to_output=True,
|
507 |
+
queue=True,
|
508 |
+
show_api=False,
|
509 |
+
)
|
510 |
+
gr.on(
|
511 |
+
triggers=[lora_repo[i].change, lora_wt[i].change],
|
512 |
+
fn=update_loras,
|
513 |
+
inputs=[prompt, lora_repo[i], lora_wt[i]],
|
514 |
+
outputs=[prompt, lora_repo[i], lora_wt[i], lora_info[i], lora_md[i]],
|
515 |
+
queue=False,
|
516 |
+
trigger_mode="once",
|
517 |
+
show_api=False,
|
518 |
+
).success(get_repo_safetensors, [lora_repo[i]], [lora_weights[i]], queue=False, show_api=False
|
519 |
+
).success(apply_lora_prompt, [lora_info[i]], [lora_trigger[i]], queue=False, show_api=False
|
520 |
+
).success(compose_lora_json, [lora_repo_json, lora_num[i], lora_repo[i], lora_wt[i], lora_weights[i], lora_trigger[i]], [lora_repo_json], queue=False, show_api=False)
|
521 |
+
|
522 |
+
for i, m in enumerate(cn_mode):
|
523 |
+
gr.on(
|
524 |
+
triggers=[cn_mode[i].change, cn_scale[i].change],
|
525 |
+
fn=set_control_union_mode,
|
526 |
+
inputs=[cn_num[i], cn_mode[i], cn_scale[i]],
|
527 |
+
outputs=[cn_on],
|
528 |
+
queue=True,
|
529 |
+
show_api=False,
|
530 |
+
).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)
|
531 |
+
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)
|
532 |
+
|
533 |
+
tagger_generate_from_image.click(lambda: ("", "", ""), None, [v2_series, v2_character, prompt], queue=False, show_api=False,
|
534 |
+
).success(
|
535 |
+
predict_tags_wd,
|
536 |
+
[tagger_image, prompt, tagger_algorithms, tagger_general_threshold, tagger_character_threshold],
|
537 |
+
[v2_series, v2_character, prompt, v2_copy],
|
538 |
+
show_api=False,
|
539 |
+
).success(predict_tags_fl2_flux, [tagger_image, prompt, tagger_algorithms], [prompt], show_api=False,
|
540 |
+
).success(compose_prompt_to_copy, [v2_character, v2_series, prompt], [prompt], queue=False, show_api=False)
|
541 |
+
|
542 |
+
with gr.Tab("FLUX Prompt Generator"):
|
543 |
+
from prompt import (PromptGenerator, HuggingFaceInferenceNode, florence_caption,
|
544 |
+
ARTFORM, PHOTO_TYPE, ROLES, HAIRSTYLES, LIGHTING, COMPOSITION, POSE, BACKGROUND,
|
545 |
+
PHOTOGRAPHY_STYLES, DEVICE, PHOTOGRAPHER, ARTIST, DIGITAL_ARTFORM, PLACE,
|
546 |
+
FEMALE_DEFAULT_TAGS, MALE_DEFAULT_TAGS, FEMALE_BODY_TYPES, MALE_BODY_TYPES,
|
547 |
+
FEMALE_CLOTHING, MALE_CLOTHING, FEMALE_ADDITIONAL_DETAILS, MALE_ADDITIONAL_DETAILS, pg_title)
|
548 |
+
|
549 |
+
prompt_generator = PromptGenerator()
|
550 |
+
huggingface_node = HuggingFaceInferenceNode()
|
551 |
+
|
552 |
+
gr.HTML(pg_title)
|
553 |
+
|
554 |
+
with gr.Row():
|
555 |
+
with gr.Column(scale=2):
|
556 |
+
with gr.Accordion("Basic Settings"):
|
557 |
+
pg_custom = gr.Textbox(label="Custom Input Prompt (optional)")
|
558 |
+
pg_subject = gr.Textbox(label="Subject (optional)")
|
559 |
+
pg_gender = gr.Radio(["female", "male"], label="Gender", value="female")
|
560 |
+
|
561 |
+
# Add the radio button for global option selection
|
562 |
+
pg_global_option = gr.Radio(
|
563 |
+
["Disabled", "Random", "No Figure Rand"],
|
564 |
+
label="Set all options to:",
|
565 |
+
value="Disabled"
|
566 |
+
)
|
567 |
+
|
568 |
+
with gr.Accordion("Artform and Photo Type", open=False):
|
569 |
+
pg_artform = gr.Dropdown(["disabled", "random"] + ARTFORM, label="Artform", value="disabled")
|
570 |
+
pg_photo_type = gr.Dropdown(["disabled", "random"] + PHOTO_TYPE, label="Photo Type", value="disabled")
|
571 |
+
|
572 |
+
with gr.Accordion("Character Details", open=False):
|
573 |
+
pg_body_types = gr.Dropdown(["disabled", "random"] + FEMALE_BODY_TYPES + MALE_BODY_TYPES, label="Body Types", value="disabled")
|
574 |
+
pg_default_tags = gr.Dropdown(["disabled", "random"] + FEMALE_DEFAULT_TAGS + MALE_DEFAULT_TAGS, label="Default Tags", value="disabled")
|
575 |
+
pg_roles = gr.Dropdown(["disabled", "random"] + ROLES, label="Roles", value="disabled")
|
576 |
+
pg_hairstyles = gr.Dropdown(["disabled", "random"] + HAIRSTYLES, label="Hairstyles", value="disabled")
|
577 |
+
pg_clothing = gr.Dropdown(["disabled", "random"] + FEMALE_CLOTHING + MALE_CLOTHING, label="Clothing", value="disabled")
|
578 |
+
|
579 |
+
with gr.Accordion("Scene Details", open=False):
|
580 |
+
pg_place = gr.Dropdown(["disabled", "random"] + PLACE, label="Place", value="disabled")
|
581 |
+
pg_lighting = gr.Dropdown(["disabled", "random"] + LIGHTING, label="Lighting", value="disabled")
|
582 |
+
pg_composition = gr.Dropdown(["disabled", "random"] + COMPOSITION, label="Composition", value="disabled")
|
583 |
+
pg_pose = gr.Dropdown(["disabled", "random"] + POSE, label="Pose", value="disabled")
|
584 |
+
pg_background = gr.Dropdown(["disabled", "random"] + BACKGROUND, label="Background", value="disabled")
|
585 |
+
|
586 |
+
with gr.Accordion("Style and Artist", open=False):
|
587 |
+
pg_additional_details = gr.Dropdown(["disabled", "random"] + FEMALE_ADDITIONAL_DETAILS + MALE_ADDITIONAL_DETAILS, label="Additional Details", value="disabled")
|
588 |
+
pg_photography_styles = gr.Dropdown(["disabled", "random"] + PHOTOGRAPHY_STYLES, label="Photography Styles", value="disabled")
|
589 |
+
pg_device = gr.Dropdown(["disabled", "random"] + DEVICE, label="Device", value="disabled")
|
590 |
+
pg_photographer = gr.Dropdown(["disabled", "random"] + PHOTOGRAPHER, label="Photographer", value="disabled")
|
591 |
+
pg_artist = gr.Dropdown(["disabled", "random"] + ARTIST, label="Artist", value="disabled")
|
592 |
+
pg_digital_artform = gr.Dropdown(["disabled", "random"] + DIGITAL_ARTFORM, label="Digital Artform", value="disabled")
|
593 |
+
|
594 |
+
pg_generate_button = gr.Button("Generate Prompt")
|
595 |
+
|
596 |
+
with gr.Column(scale=2):
|
597 |
+
with gr.Accordion("Image and Caption", open=False):
|
598 |
+
pg_input_image = gr.Image(label="Input Image (optional)")
|
599 |
+
pg_caption_output = gr.Textbox(label="Generated Caption", lines=3)
|
600 |
+
pg_create_caption_button = gr.Button("Create Caption")
|
601 |
+
pg_add_caption_button = gr.Button("Add Caption to Prompt")
|
602 |
+
|
603 |
+
with gr.Accordion("Prompt Generation", open=True):
|
604 |
+
pg_output = gr.Textbox(label="Generated Prompt / Input Text", lines=4)
|
605 |
+
pg_t5xxl_output = gr.Textbox(label="T5XXL Output", visible=True)
|
606 |
+
pg_clip_l_output = gr.Textbox(label="CLIP L Output", visible=True)
|
607 |
+
pg_clip_g_output = gr.Textbox(label="CLIP G Output", visible=True)
|
608 |
+
|
609 |
+
with gr.Column(scale=2):
|
610 |
+
with gr.Accordion("Prompt Generation with LLM", open=False):
|
611 |
+
pg_happy_talk = gr.Checkbox(label="Happy Talk", value=True)
|
612 |
+
pg_compress = gr.Checkbox(label="Compress", value=True)
|
613 |
+
pg_compression_level = gr.Radio(["soft", "medium", "hard"], label="Compression Level", value="hard")
|
614 |
+
pg_poster = gr.Checkbox(label="Poster", value=False)
|
615 |
+
pg_custom_base_prompt = gr.Textbox(label="Custom Base Prompt", lines=5)
|
616 |
+
pg_generate_text_button = gr.Button("Generate Prompt with LLM (Llama 3.1 70B)")
|
617 |
+
pg_text_output = gr.Textbox(label="Generated Text", lines=10)
|
618 |
+
|
619 |
+
description_ui()
|
620 |
+
|
621 |
+
def create_caption(image):
|
622 |
+
if image is not None:
|
623 |
+
return florence_caption(image)
|
624 |
+
return ""
|
625 |
+
|
626 |
+
pg_create_caption_button.click(
|
627 |
+
create_caption,
|
628 |
+
inputs=[pg_input_image],
|
629 |
+
outputs=[pg_caption_output]
|
630 |
+
)
|
631 |
+
|
632 |
+
def generate_prompt_with_dynamic_seed(*args):
|
633 |
+
# Generate a new random seed
|
634 |
+
dynamic_seed = random.randint(0, 1000000)
|
635 |
+
|
636 |
+
# Call the generate_prompt function with the dynamic seed
|
637 |
+
result = prompt_generator.generate_prompt(dynamic_seed, *args)
|
638 |
+
|
639 |
+
# Return the result along with the used seed
|
640 |
+
return [dynamic_seed] + list(result)
|
641 |
+
|
642 |
+
pg_generate_button.click(
|
643 |
+
generate_prompt_with_dynamic_seed,
|
644 |
+
inputs=[pg_custom, pg_subject, pg_gender, pg_artform, pg_photo_type, pg_body_types, pg_default_tags, pg_roles, pg_hairstyles,
|
645 |
+
pg_additional_details, pg_photography_styles, pg_device, pg_photographer, pg_artist, pg_digital_artform,
|
646 |
+
pg_place, pg_lighting, pg_clothing, pg_composition, pg_pose, pg_background, pg_input_image],
|
647 |
+
outputs=[gr.Number(label="Used Seed", visible=False), pg_output, gr.Number(visible=False), pg_t5xxl_output, pg_clip_l_output, pg_clip_g_output]
|
648 |
+
) #
|
649 |
+
|
650 |
+
pg_add_caption_button.click(
|
651 |
+
prompt_generator.add_caption_to_prompt,
|
652 |
+
inputs=[pg_output, pg_caption_output],
|
653 |
+
outputs=[pg_output]
|
654 |
+
)
|
655 |
+
|
656 |
+
pg_generate_text_button.click(
|
657 |
+
huggingface_node.generate,
|
658 |
+
inputs=[pg_output, pg_happy_talk, pg_compress, pg_compression_level, pg_poster, pg_custom_base_prompt],
|
659 |
+
outputs=pg_text_output
|
660 |
+
)
|
661 |
+
|
662 |
+
def update_all_options(choice):
|
663 |
+
updates = {}
|
664 |
+
if choice == "Disabled":
|
665 |
+
for dropdown in [
|
666 |
+
pg_artform, pg_photo_type, pg_body_types, pg_default_tags, pg_roles, pg_hairstyles, pg_clothing,
|
667 |
+
pg_place, pg_lighting, pg_composition, pg_pose, pg_background, pg_additional_details,
|
668 |
+
pg_photography_styles, pg_device, pg_photographer, pg_artist, pg_digital_artform
|
669 |
+
]:
|
670 |
+
updates[dropdown] = gr.update(value="disabled")
|
671 |
+
elif choice == "Random":
|
672 |
+
for dropdown in [
|
673 |
+
pg_artform, pg_photo_type, pg_body_types, pg_default_tags, pg_roles, pg_hairstyles, pg_clothing,
|
674 |
+
pg_place, pg_lighting, pg_composition, pg_pose, pg_background, pg_additional_details,
|
675 |
+
pg_photography_styles, pg_device, pg_photographer, pg_artist, pg_digital_artform
|
676 |
+
]:
|
677 |
+
updates[dropdown] = gr.update(value="random")
|
678 |
+
else: # No Figure Random
|
679 |
+
for dropdown in [pg_photo_type, pg_body_types, pg_default_tags, pg_roles, pg_hairstyles, pg_clothing, pg_pose, pg_additional_details]:
|
680 |
+
updates[dropdown] = gr.update(value="disabled")
|
681 |
+
for dropdown in [pg_artform, pg_place, pg_lighting, pg_composition, pg_background, pg_photography_styles, pg_device, pg_photographer, pg_artist, pg_digital_artform]:
|
682 |
+
updates[dropdown] = gr.update(value="random")
|
683 |
+
return updates
|
684 |
+
|
685 |
+
pg_global_option.change(
|
686 |
+
update_all_options,
|
687 |
+
inputs=[pg_global_option],
|
688 |
+
outputs=[
|
689 |
+
pg_artform, pg_photo_type, pg_body_types, pg_default_tags, pg_roles, pg_hairstyles, pg_clothing,
|
690 |
+
pg_place, pg_lighting, pg_composition, pg_pose, pg_background, pg_additional_details,
|
691 |
+
pg_photography_styles, pg_device, pg_photographer, pg_artist, pg_digital_artform
|
692 |
+
]
|
693 |
+
)
|
694 |
+
|
695 |
+
app.queue()
|
696 |
+
app.launch()
|
app.py
CHANGED
@@ -1,44 +1,54 @@
|
|
1 |
import spaces
|
2 |
import gradio as gr
|
3 |
import json
|
4 |
-
import logging
|
5 |
import torch
|
6 |
-
from
|
7 |
-
from
|
8 |
from diffusers import FluxControlNetPipeline, FluxControlNetModel, FluxMultiControlNetModel
|
9 |
-
from huggingface_hub import
|
10 |
-
import copy
|
11 |
import random
|
12 |
import time
|
13 |
|
14 |
-
|
15 |
from mod import (models, clear_cache, get_repo_safetensors, is_repo_name, is_repo_exists,
|
16 |
description_ui, num_loras, compose_lora_json, is_valid_lora, fuse_loras,
|
17 |
get_trigger_word, enhance_prompt, deselect_lora, num_cns, set_control_union_image,
|
18 |
-
get_control_union_mode, set_control_union_mode, get_control_params)
|
19 |
from flux import (search_civitai_lora, select_civitai_lora, search_civitai_lora_json,
|
20 |
download_my_lora, get_all_lora_tupled_list, apply_lora_prompt,
|
21 |
update_loras, get_t2i_model_info)
|
22 |
from tagger.tagger import predict_tags_wd, compose_prompt_to_copy
|
23 |
from tagger.fl2flux import predict_tags_fl2_flux
|
24 |
|
|
|
|
|
|
|
25 |
|
26 |
-
dtype = torch.bfloat16
|
27 |
-
#dtype = torch.float8_e4m3fn
|
28 |
# Initialize the base model
|
29 |
base_model = models[0]
|
30 |
controlnet_model_union_repo = 'InstantX/FLUX.1-dev-Controlnet-Union'
|
31 |
#controlnet_model_union_repo = 'InstantX/FLUX.1-dev-Controlnet-Union-alpha'
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
33 |
controlnet_union = None
|
34 |
controlnet = None
|
35 |
last_model = models[0]
|
36 |
last_cn_on = False
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# https://huggingface.co/InstantX/FLUX.1-dev-Controlnet-Union
|
39 |
# https://huggingface.co/spaces/jiuface/FLUX.1-dev-Controlnet-Union
|
40 |
-
|
|
|
41 |
global pipe
|
|
|
|
|
42 |
global controlnet_union
|
43 |
global controlnet
|
44 |
global last_model
|
@@ -46,25 +56,29 @@ def change_base_model(repo_id: str, cn_on: bool, progress=gr.Progress(track_tqdm
|
|
46 |
global dtype
|
47 |
try:
|
48 |
if (repo_id == last_model and cn_on is last_cn_on) or not is_repo_name(repo_id) or not is_repo_exists(repo_id): return gr.update(visible=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
if cn_on:
|
50 |
-
|
51 |
print(f"Loading model: {repo_id} / Loading ControlNet: {controlnet_model_union_repo}")
|
52 |
-
clear_cache()
|
53 |
controlnet_union = FluxControlNetModel.from_pretrained(controlnet_model_union_repo, torch_dtype=dtype)
|
54 |
controlnet = FluxMultiControlNetModel([controlnet_union])
|
55 |
pipe = FluxControlNetPipeline.from_pretrained(repo_id, controlnet=controlnet, torch_dtype=dtype)
|
56 |
last_model = repo_id
|
57 |
last_cn_on = cn_on
|
58 |
-
|
59 |
print(f"Model loaded: {repo_id} / ControlNet Loaded: {controlnet_model_union_repo}")
|
60 |
else:
|
61 |
-
|
62 |
print(f"Loading model: {repo_id}")
|
63 |
-
clear_cache()
|
64 |
pipe = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=dtype)
|
65 |
last_model = repo_id
|
66 |
last_cn_on = cn_on
|
67 |
-
|
68 |
print(f"Model loaded: {repo_id}")
|
69 |
except Exception as e:
|
70 |
print(f"Model load Error: {e}")
|
@@ -73,12 +87,6 @@ def change_base_model(repo_id: str, cn_on: bool, progress=gr.Progress(track_tqdm
|
|
73 |
|
74 |
change_base_model.zerogpu = True
|
75 |
|
76 |
-
# Load LoRAs from JSON file
|
77 |
-
with open('loras.json', 'r') as f:
|
78 |
-
loras = json.load(f)
|
79 |
-
|
80 |
-
MAX_SEED = 2**32-1
|
81 |
-
|
82 |
class calculateDuration:
|
83 |
def __init__(self, activity_name=""):
|
84 |
self.activity_name = activity_name
|
@@ -121,18 +129,24 @@ def update_selection(evt: gr.SelectData, width, height):
|
|
121 |
@spaces.GPU(duration=70)
|
122 |
def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, cn_on, progress=gr.Progress(track_tqdm=True)):
|
123 |
global pipe
|
|
|
|
|
124 |
global controlnet
|
125 |
global controlnet_union
|
126 |
try:
|
127 |
-
|
|
|
128 |
generator = torch.Generator(device="cuda").manual_seed(seed)
|
129 |
|
130 |
with calculateDuration("Generating image"):
|
131 |
# Generate image
|
132 |
modes, images, scales = get_control_params()
|
133 |
if not cn_on or len(modes) == 0:
|
|
|
|
|
|
|
134 |
progress(0, desc="Start Inference.")
|
135 |
-
|
136 |
prompt=prompt_mash,
|
137 |
num_inference_steps=steps,
|
138 |
guidance_scale=cfg_scale,
|
@@ -140,12 +154,18 @@ def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scal
|
|
140 |
height=height,
|
141 |
generator=generator,
|
142 |
joint_attention_kwargs={"scale": lora_scale},
|
143 |
-
|
|
|
|
|
|
|
144 |
else:
|
145 |
-
progress(0, desc="Start Inference with ControlNet.")
|
146 |
if controlnet is not None: controlnet.to("cuda")
|
147 |
if controlnet_union is not None: controlnet_union.to("cuda")
|
148 |
-
|
|
|
|
|
|
|
|
|
149 |
prompt=prompt_mash,
|
150 |
control_image=images,
|
151 |
control_mode=modes,
|
@@ -156,23 +176,35 @@ def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scal
|
|
156 |
controlnet_conditioning_scale=scales,
|
157 |
generator=generator,
|
158 |
joint_attention_kwargs={"scale": lora_scale},
|
159 |
-
).images
|
|
|
160 |
except Exception as e:
|
161 |
print(e)
|
162 |
-
raise gr.Error(f"Inference Error {e}") from e
|
163 |
-
return image
|
164 |
|
165 |
def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height,
|
166 |
-
lora_scale, lora_json, cn_on, progress=gr.Progress(track_tqdm=True)):
|
167 |
global pipe
|
168 |
if selected_index is None and not is_valid_lora(lora_json):
|
169 |
gr.Info("LoRA isn't selected.")
|
170 |
# raise gr.Error("You must select a LoRA before proceeding.")
|
171 |
progress(0, desc="Preparing Inference.")
|
172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
prompt_mash = prompt
|
174 |
if is_valid_lora(lora_json):
|
175 |
-
|
|
|
176 |
fuse_loras(pipe, lora_json)
|
177 |
trigger_word = get_trigger_word(lora_json)
|
178 |
prompt_mash = f"{prompt} {trigger_word}"
|
@@ -203,17 +235,17 @@ def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, wid
|
|
203 |
seed = random.randint(0, MAX_SEED)
|
204 |
|
205 |
progress(0, desc="Running Inference.")
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
|
218 |
def get_huggingface_safetensors(link):
|
219 |
split_link = link.split("/")
|
@@ -306,7 +338,7 @@ css = '''
|
|
306 |
#progress .generating{display:none}
|
307 |
.progress-container {width: 100%;height: 30px;background-color: #f0f0f0;border-radius: 15px;overflow: hidden;margin-bottom: 20px}
|
308 |
.progress-bar {height: 100%;background-color: #4f46e5;width: calc(var(--current) / var(--total) * 100%);transition: width 0.5s ease-in-out}
|
309 |
-
|
310 |
'''
|
311 |
with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=css, delete_cache=(60, 3600)) as app:
|
312 |
with gr.Tab("FLUX LoRA the Explorer"):
|
@@ -329,8 +361,10 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=css, delete_cache
|
|
329 |
v2_copy = gr.Button(value="Copy to clipboard", size="sm", interactive=False, visible=False)
|
330 |
tagger_algorithms = gr.CheckboxGroup(["Use WD Tagger", "Use Florence-2-Flux"], label="Algorithms", value=["Use WD Tagger"])
|
331 |
tagger_generate_from_image = gr.Button(value="Generate Prompt from Image")
|
332 |
-
prompt = gr.Textbox(label="Prompt", lines=1, max_lines=8, placeholder="Type a prompt")
|
333 |
-
|
|
|
|
|
334 |
with gr.Column(scale=1, elem_id="gen_column"):
|
335 |
generate_button = gr.Button("Generate", variant="primary", elem_id="gen_btn")
|
336 |
with gr.Row():
|
@@ -350,19 +384,20 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=css, delete_cache
|
|
350 |
custom_lora_button = gr.Button("Remove custom LoRA", visible=False)
|
351 |
deselect_lora_button = gr.Button("Deselect LoRA", variant="secondary")
|
352 |
with gr.Column():
|
|
|
353 |
result = gr.Image(label="Generated Image", format="png", show_share_button=False)
|
354 |
with gr.Group():
|
355 |
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)
|
356 |
-
model_info = gr.Markdown(
|
357 |
with gr.Row():
|
358 |
with gr.Accordion("Advanced Settings", open=False):
|
359 |
with gr.Column():
|
360 |
-
with gr.Row():
|
361 |
-
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=3.5)
|
362 |
-
steps = gr.Slider(label="Steps", minimum=1, maximum=50, step=1, value=28)
|
363 |
with gr.Row():
|
364 |
width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=1024)
|
365 |
height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=1024)
|
|
|
|
|
|
|
366 |
with gr.Row():
|
367 |
randomize_seed = gr.Checkbox(True, label="Randomize seed")
|
368 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
|
@@ -417,8 +452,8 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=css, delete_cache
|
|
417 |
with gr.Row():
|
418 |
for i in range(num_cns):
|
419 |
with gr.Column():
|
|
|
420 |
with gr.Row():
|
421 |
-
cn_mode[i] = gr.Dropdown(label=f"ControlNet {int(i+1)} Mode", choices=get_control_union_mode(), value=get_control_union_mode()[0], allow_custom_value=False)
|
422 |
cn_scale[i] = gr.Slider(label=f"ControlNet {int(i+1)} Weight", minimum=0.0, maximum=1.0, step=0.01, value=0.75)
|
423 |
cn_res[i] = gr.Slider(label=f"ControlNet {int(i+1)} Preprocess resolution", minimum=128, maximum=512, value=384, step=1)
|
424 |
cn_num[i] = gr.Number(i, visible=False)
|
@@ -432,6 +467,7 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=css, delete_cache
|
|
432 |
outputs=[prompt, selected_info, selected_index, width, height],
|
433 |
queue=False,
|
434 |
show_api=False,
|
|
|
435 |
)
|
436 |
custom_lora.input(
|
437 |
add_custom_lora,
|
@@ -457,22 +493,13 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=css, delete_cache
|
|
457 |
).success(
|
458 |
fn=run_lora,
|
459 |
inputs=[prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height,
|
460 |
-
lora_scale, lora_repo_json, cn_on],
|
461 |
-
outputs=[result, seed],
|
462 |
queue=True,
|
463 |
show_api=True,
|
464 |
)
|
465 |
|
466 |
deselect_lora_button.click(deselect_lora, None, [prompt, selected_info, selected_index, width, height], queue=False, show_api=False)
|
467 |
-
"""gr.on(
|
468 |
-
triggers=[model_name.change, cn_on.change],
|
469 |
-
fn=change_base_model,
|
470 |
-
inputs=[model_name, cn_on],
|
471 |
-
outputs=[result],
|
472 |
-
queue=True,
|
473 |
-
show_api=False,
|
474 |
-
trigger_mode="once",
|
475 |
-
).then(get_t2i_model_info, [model_name], [model_info], queue=True, show_api=False)"""
|
476 |
gr.on(
|
477 |
triggers=[model_name.change, cn_on.change],
|
478 |
fn=get_t2i_model_info,
|
|
|
1 |
import spaces
|
2 |
import gradio as gr
|
3 |
import json
|
|
|
4 |
import torch
|
5 |
+
from diffusers import DiffusionPipeline, AutoencoderTiny, AutoencoderKL
|
6 |
+
from live_preview_helpers import flux_pipe_call_that_returns_an_iterable_of_images
|
7 |
from diffusers import FluxControlNetPipeline, FluxControlNetModel, FluxMultiControlNetModel
|
8 |
+
from huggingface_hub import HfFileSystem, ModelCard
|
|
|
9 |
import random
|
10 |
import time
|
11 |
|
|
|
12 |
from mod import (models, clear_cache, get_repo_safetensors, is_repo_name, is_repo_exists,
|
13 |
description_ui, num_loras, compose_lora_json, is_valid_lora, fuse_loras,
|
14 |
get_trigger_word, enhance_prompt, deselect_lora, num_cns, set_control_union_image,
|
15 |
+
get_control_union_mode, set_control_union_mode, get_control_params, translate_to_en)
|
16 |
from flux import (search_civitai_lora, select_civitai_lora, search_civitai_lora_json,
|
17 |
download_my_lora, get_all_lora_tupled_list, apply_lora_prompt,
|
18 |
update_loras, get_t2i_model_info)
|
19 |
from tagger.tagger import predict_tags_wd, compose_prompt_to_copy
|
20 |
from tagger.fl2flux import predict_tags_fl2_flux
|
21 |
|
22 |
+
# Load LoRAs from JSON file
|
23 |
+
with open('loras.json', 'r') as f:
|
24 |
+
loras = json.load(f)
|
25 |
|
|
|
|
|
26 |
# Initialize the base model
|
27 |
base_model = models[0]
|
28 |
controlnet_model_union_repo = 'InstantX/FLUX.1-dev-Controlnet-Union'
|
29 |
#controlnet_model_union_repo = 'InstantX/FLUX.1-dev-Controlnet-Union-alpha'
|
30 |
+
dtype = torch.bfloat16
|
31 |
+
#dtype = torch.float8_e4m3fn
|
32 |
+
#device = "cuda" if torch.cuda.is_available() else "cpu"
|
33 |
+
taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype)
|
34 |
+
good_vae = AutoencoderKL.from_pretrained(base_model, subfolder="vae", torch_dtype=dtype)
|
35 |
+
pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=dtype, vae=taef1)
|
36 |
controlnet_union = None
|
37 |
controlnet = None
|
38 |
last_model = models[0]
|
39 |
last_cn_on = False
|
40 |
+
#controlnet_union = FluxControlNetModel.from_pretrained(controlnet_model_union_repo, torch_dtype=dtype)
|
41 |
+
#controlnet = FluxMultiControlNetModel([controlnet_union])
|
42 |
+
|
43 |
+
MAX_SEED = 2**32-1
|
44 |
|
45 |
# https://huggingface.co/InstantX/FLUX.1-dev-Controlnet-Union
|
46 |
# https://huggingface.co/spaces/jiuface/FLUX.1-dev-Controlnet-Union
|
47 |
+
#@spaces.GPU()
|
48 |
+
def change_base_model(repo_id: str, cn_on: bool, progress=gr.Progress(track_tqdm=True)): # , progress=gr.Progress(track_tqdm=True) # gradio.exceptions.Error: 'Model load Error: too many values to unpack (expected 2)'
|
49 |
global pipe
|
50 |
+
global taef1
|
51 |
+
global good_vae
|
52 |
global controlnet_union
|
53 |
global controlnet
|
54 |
global last_model
|
|
|
56 |
global dtype
|
57 |
try:
|
58 |
if (repo_id == last_model and cn_on is last_cn_on) or not is_repo_name(repo_id) or not is_repo_exists(repo_id): return gr.update(visible=True)
|
59 |
+
pipe.to("cpu")
|
60 |
+
good_vae.to("cpu")
|
61 |
+
taef1.to("cpu")
|
62 |
+
if controlnet is not None: controlnet.to("cpu")
|
63 |
+
if controlnet_union is not None: controlnet_union.to("cpu")
|
64 |
+
clear_cache()
|
65 |
if cn_on:
|
66 |
+
progress(0, desc=f"Loading model: {repo_id} / Loading ControlNet: {controlnet_model_union_repo}")
|
67 |
print(f"Loading model: {repo_id} / Loading ControlNet: {controlnet_model_union_repo}")
|
|
|
68 |
controlnet_union = FluxControlNetModel.from_pretrained(controlnet_model_union_repo, torch_dtype=dtype)
|
69 |
controlnet = FluxMultiControlNetModel([controlnet_union])
|
70 |
pipe = FluxControlNetPipeline.from_pretrained(repo_id, controlnet=controlnet, torch_dtype=dtype)
|
71 |
last_model = repo_id
|
72 |
last_cn_on = cn_on
|
73 |
+
progress(1, desc=f"Model loaded: {repo_id} / ControlNet Loaded: {controlnet_model_union_repo}")
|
74 |
print(f"Model loaded: {repo_id} / ControlNet Loaded: {controlnet_model_union_repo}")
|
75 |
else:
|
76 |
+
progress(0, desc=f"Loading model: {repo_id}")
|
77 |
print(f"Loading model: {repo_id}")
|
|
|
78 |
pipe = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=dtype)
|
79 |
last_model = repo_id
|
80 |
last_cn_on = cn_on
|
81 |
+
progress(1, desc=f"Model loaded: {repo_id}")
|
82 |
print(f"Model loaded: {repo_id}")
|
83 |
except Exception as e:
|
84 |
print(f"Model load Error: {e}")
|
|
|
87 |
|
88 |
change_base_model.zerogpu = True
|
89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
class calculateDuration:
|
91 |
def __init__(self, activity_name=""):
|
92 |
self.activity_name = activity_name
|
|
|
129 |
@spaces.GPU(duration=70)
|
130 |
def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, cn_on, progress=gr.Progress(track_tqdm=True)):
|
131 |
global pipe
|
132 |
+
global taef1
|
133 |
+
global good_vae
|
134 |
global controlnet
|
135 |
global controlnet_union
|
136 |
try:
|
137 |
+
good_vae.to("cuda")
|
138 |
+
taef1.to("cuda")
|
139 |
generator = torch.Generator(device="cuda").manual_seed(seed)
|
140 |
|
141 |
with calculateDuration("Generating image"):
|
142 |
# Generate image
|
143 |
modes, images, scales = get_control_params()
|
144 |
if not cn_on or len(modes) == 0:
|
145 |
+
pipe.vae = taef1
|
146 |
+
pipe.to("cuda")
|
147 |
+
pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
|
148 |
progress(0, desc="Start Inference.")
|
149 |
+
for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
|
150 |
prompt=prompt_mash,
|
151 |
num_inference_steps=steps,
|
152 |
guidance_scale=cfg_scale,
|
|
|
154 |
height=height,
|
155 |
generator=generator,
|
156 |
joint_attention_kwargs={"scale": lora_scale},
|
157 |
+
output_type="pil",
|
158 |
+
good_vae=good_vae,
|
159 |
+
):
|
160 |
+
yield img
|
161 |
else:
|
|
|
162 |
if controlnet is not None: controlnet.to("cuda")
|
163 |
if controlnet_union is not None: controlnet_union.to("cuda")
|
164 |
+
pipe.vae = good_vae
|
165 |
+
pipe.controlnet = controlnet
|
166 |
+
pipe.to("cuda")
|
167 |
+
progress(0, desc="Start Inference with ControlNet.")
|
168 |
+
for img in pipe(
|
169 |
prompt=prompt_mash,
|
170 |
control_image=images,
|
171 |
control_mode=modes,
|
|
|
176 |
controlnet_conditioning_scale=scales,
|
177 |
generator=generator,
|
178 |
joint_attention_kwargs={"scale": lora_scale},
|
179 |
+
).images:
|
180 |
+
yield img
|
181 |
except Exception as e:
|
182 |
print(e)
|
183 |
+
raise gr.Error(f"Inference Error: {e}") from e
|
|
|
184 |
|
185 |
def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height,
|
186 |
+
lora_scale, lora_json, cn_on, translate_on, progress=gr.Progress(track_tqdm=True)):
|
187 |
global pipe
|
188 |
if selected_index is None and not is_valid_lora(lora_json):
|
189 |
gr.Info("LoRA isn't selected.")
|
190 |
# raise gr.Error("You must select a LoRA before proceeding.")
|
191 |
progress(0, desc="Preparing Inference.")
|
192 |
|
193 |
+
with calculateDuration("Unloading LoRA"):
|
194 |
+
try:
|
195 |
+
pipe.unfuse_lora()
|
196 |
+
pipe.unload_lora_weights()
|
197 |
+
except Exception as e:
|
198 |
+
print(e)
|
199 |
+
|
200 |
+
clear_cache() #
|
201 |
+
|
202 |
+
if translate_on: prompt = translate_to_en(prompt)
|
203 |
+
|
204 |
prompt_mash = prompt
|
205 |
if is_valid_lora(lora_json):
|
206 |
+
# Load External LoRA weights
|
207 |
+
with calculateDuration("Loading External LoRA weights"):
|
208 |
fuse_loras(pipe, lora_json)
|
209 |
trigger_word = get_trigger_word(lora_json)
|
210 |
prompt_mash = f"{prompt} {trigger_word}"
|
|
|
235 |
seed = random.randint(0, MAX_SEED)
|
236 |
|
237 |
progress(0, desc="Running Inference.")
|
238 |
+
image_generator = generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, cn_on, progress)
|
239 |
+
# Consume the generator to get the final image
|
240 |
+
final_image = None
|
241 |
+
step_counter = 0
|
242 |
+
for image in image_generator:
|
243 |
+
step_counter+=1
|
244 |
+
final_image = image
|
245 |
+
progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
|
246 |
+
yield image, seed, gr.update(value=progress_bar, visible=True)
|
247 |
+
|
248 |
+
yield final_image, seed, gr.update(value=progress_bar, visible=False)
|
249 |
|
250 |
def get_huggingface_safetensors(link):
|
251 |
split_link = link.split("/")
|
|
|
338 |
#progress .generating{display:none}
|
339 |
.progress-container {width: 100%;height: 30px;background-color: #f0f0f0;border-radius: 15px;overflow: hidden;margin-bottom: 20px}
|
340 |
.progress-bar {height: 100%;background-color: #4f46e5;width: calc(var(--current) / var(--total) * 100%);transition: width 0.5s ease-in-out}
|
341 |
+
.info {text-align:center; display:inline-flex; align-items:center !important}
|
342 |
'''
|
343 |
with gr.Blocks(theme='Nymbo/Nymbo_Theme', fill_width=True, css=css, delete_cache=(60, 3600)) as app:
|
344 |
with gr.Tab("FLUX LoRA the Explorer"):
|
|
|
361 |
v2_copy = gr.Button(value="Copy to clipboard", size="sm", interactive=False, visible=False)
|
362 |
tagger_algorithms = gr.CheckboxGroup(["Use WD Tagger", "Use Florence-2-Flux"], label="Algorithms", value=["Use WD Tagger"])
|
363 |
tagger_generate_from_image = gr.Button(value="Generate Prompt from Image")
|
364 |
+
prompt = gr.Textbox(label="Prompt", lines=1, max_lines=8, placeholder="Type a prompt", show_copy_button=True)
|
365 |
+
with gr.Row():
|
366 |
+
prompt_enhance = gr.Button(value="Enhance your prompt", variant="secondary")
|
367 |
+
auto_trans = gr.Checkbox(label="Auto translate to English", value=True, elem_classes="info")
|
368 |
with gr.Column(scale=1, elem_id="gen_column"):
|
369 |
generate_button = gr.Button("Generate", variant="primary", elem_id="gen_btn")
|
370 |
with gr.Row():
|
|
|
384 |
custom_lora_button = gr.Button("Remove custom LoRA", visible=False)
|
385 |
deselect_lora_button = gr.Button("Deselect LoRA", variant="secondary")
|
386 |
with gr.Column():
|
387 |
+
progress_bar = gr.Markdown(elem_id="progress",visible=False)
|
388 |
result = gr.Image(label="Generated Image", format="png", show_share_button=False)
|
389 |
with gr.Group():
|
390 |
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)
|
391 |
+
model_info = gr.Markdown(elem_classes="info")
|
392 |
with gr.Row():
|
393 |
with gr.Accordion("Advanced Settings", open=False):
|
394 |
with gr.Column():
|
|
|
|
|
|
|
395 |
with gr.Row():
|
396 |
width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=1024)
|
397 |
height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=1024)
|
398 |
+
with gr.Row():
|
399 |
+
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=3.5)
|
400 |
+
steps = gr.Slider(label="Steps", minimum=1, maximum=50, step=1, value=28)
|
401 |
with gr.Row():
|
402 |
randomize_seed = gr.Checkbox(True, label="Randomize seed")
|
403 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
|
|
|
452 |
with gr.Row():
|
453 |
for i in range(num_cns):
|
454 |
with gr.Column():
|
455 |
+
cn_mode[i] = gr.Radio(label=f"ControlNet {int(i+1)} Mode", choices=get_control_union_mode(), value=get_control_union_mode()[0])
|
456 |
with gr.Row():
|
|
|
457 |
cn_scale[i] = gr.Slider(label=f"ControlNet {int(i+1)} Weight", minimum=0.0, maximum=1.0, step=0.01, value=0.75)
|
458 |
cn_res[i] = gr.Slider(label=f"ControlNet {int(i+1)} Preprocess resolution", minimum=128, maximum=512, value=384, step=1)
|
459 |
cn_num[i] = gr.Number(i, visible=False)
|
|
|
467 |
outputs=[prompt, selected_info, selected_index, width, height],
|
468 |
queue=False,
|
469 |
show_api=False,
|
470 |
+
trigger_mode="once",
|
471 |
)
|
472 |
custom_lora.input(
|
473 |
add_custom_lora,
|
|
|
493 |
).success(
|
494 |
fn=run_lora,
|
495 |
inputs=[prompt, cfg_scale, steps, selected_index, randomize_seed, seed, width, height,
|
496 |
+
lora_scale, lora_repo_json, cn_on, auto_trans],
|
497 |
+
outputs=[result, seed, progress_bar],
|
498 |
queue=True,
|
499 |
show_api=True,
|
500 |
)
|
501 |
|
502 |
deselect_lora_button.click(deselect_lora, None, [prompt, selected_info, selected_index, width, height], queue=False, show_api=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
503 |
gr.on(
|
504 |
triggers=[model_name.change, cn_on.change],
|
505 |
fn=get_t2i_model_info,
|
mod.py
CHANGED
@@ -74,6 +74,17 @@ def is_repo_exists(repo_id):
|
|
74 |
return True # for safe
|
75 |
|
76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
def clear_cache():
|
78 |
try:
|
79 |
torch.cuda.empty_cache()
|
@@ -349,7 +360,7 @@ enhancer_flux = load_prompt_enhancer()
|
|
349 |
|
350 |
@spaces.GPU(duration=30)
|
351 |
def enhance_prompt(input_prompt):
|
352 |
-
result = enhancer_flux("enhance prompt: " + input_prompt, max_length = 256)
|
353 |
enhanced_text = result[0]['generated_text']
|
354 |
return enhanced_text
|
355 |
|
|
|
74 |
return True # for safe
|
75 |
|
76 |
|
77 |
+
from translatepy import Translator
|
78 |
+
translator = Translator()
|
79 |
+
def translate_to_en(input: str):
|
80 |
+
try:
|
81 |
+
output = str(translator.translate(input, 'English'))
|
82 |
+
except Exception as e:
|
83 |
+
output = input
|
84 |
+
print(e)
|
85 |
+
return output
|
86 |
+
|
87 |
+
|
88 |
def clear_cache():
|
89 |
try:
|
90 |
torch.cuda.empty_cache()
|
|
|
360 |
|
361 |
@spaces.GPU(duration=30)
|
362 |
def enhance_prompt(input_prompt):
|
363 |
+
result = enhancer_flux("enhance prompt: " + translate_to_en(input_prompt), max_length = 256)
|
364 |
enhanced_text = result[0]['generated_text']
|
365 |
return enhanced_text
|
366 |
|
modutils.py
CHANGED
@@ -27,6 +27,17 @@ def list_sub(a, b):
|
|
27 |
return [e for e in a if e not in b]
|
28 |
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
def get_local_model_list(dir_path):
|
31 |
model_list = []
|
32 |
valid_extensions = ('.ckpt', '.pt', '.pth', '.safetensors', '.bin')
|
|
|
27 |
return [e for e in a if e not in b]
|
28 |
|
29 |
|
30 |
+
from translatepy import Translator
|
31 |
+
translator = Translator()
|
32 |
+
def translate_to_en(input: str):
|
33 |
+
try:
|
34 |
+
output = str(translator.translate(input, 'English'))
|
35 |
+
except Exception as e:
|
36 |
+
output = input
|
37 |
+
print(e)
|
38 |
+
return output
|
39 |
+
|
40 |
+
|
41 |
def get_local_model_list(dir_path):
|
42 |
model_list = []
|
43 |
valid_extensions = ('.ckpt', '.pt', '.pth', '.safetensors', '.bin')
|
requirements.txt
CHANGED
@@ -14,4 +14,5 @@ numpy
|
|
14 |
opencv-python
|
15 |
deepspeed
|
16 |
mediapipe
|
17 |
-
openai==1.37.0
|
|
|
|
14 |
opencv-python
|
15 |
deepspeed
|
16 |
mediapipe
|
17 |
+
openai==1.37.0
|
18 |
+
translatepy
|
tagger/tagger.py
CHANGED
@@ -1,11 +1,8 @@
|
|
|
|
1 |
from PIL import Image
|
2 |
import torch
|
3 |
import gradio as gr
|
4 |
-
import
|
5 |
-
from transformers import (
|
6 |
-
AutoImageProcessor,
|
7 |
-
AutoModelForImageClassification,
|
8 |
-
)
|
9 |
from pathlib import Path
|
10 |
|
11 |
|
@@ -14,9 +11,13 @@ WD_MODEL_NAME = WD_MODEL_NAMES[0]
|
|
14 |
|
15 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
16 |
default_device = device
|
17 |
-
wd_model = AutoModelForImageClassification.from_pretrained(WD_MODEL_NAME, trust_remote_code=True).to(default_device).eval()
|
18 |
-
wd_processor = AutoImageProcessor.from_pretrained(WD_MODEL_NAME, trust_remote_code=True)
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
def _people_tag(noun: str, minimum: int = 1, maximum: int = 5):
|
22 |
return (
|
@@ -186,18 +187,16 @@ def convert_danbooru_to_e621_prompt(input_prompt: str = "", prompt_type: str = "
|
|
186 |
return output_prompt
|
187 |
|
188 |
|
|
|
|
|
189 |
def translate_prompt(prompt: str = ""):
|
190 |
-
def translate_to_english(
|
191 |
-
import httpcore
|
192 |
-
setattr(httpcore, 'SyncHTTPTransport', 'AsyncHTTPProxy')
|
193 |
-
from googletrans import Translator
|
194 |
-
translator = Translator()
|
195 |
try:
|
196 |
-
|
197 |
-
return translated_prompt
|
198 |
except Exception as e:
|
|
|
199 |
print(e)
|
200 |
-
|
201 |
|
202 |
def is_japanese(s):
|
203 |
import unicodedata
|
@@ -220,17 +219,13 @@ def translate_prompt(prompt: str = ""):
|
|
220 |
|
221 |
|
222 |
def translate_prompt_to_ja(prompt: str = ""):
|
223 |
-
def translate_to_japanese(
|
224 |
-
import httpcore
|
225 |
-
setattr(httpcore, 'SyncHTTPTransport', 'AsyncHTTPProxy')
|
226 |
-
from googletrans import Translator
|
227 |
-
translator = Translator()
|
228 |
try:
|
229 |
-
|
230 |
-
return translated_prompt
|
231 |
except Exception as e:
|
|
|
232 |
print(e)
|
233 |
-
|
234 |
|
235 |
def is_japanese(s):
|
236 |
import unicodedata
|
|
|
1 |
+
import spaces
|
2 |
from PIL import Image
|
3 |
import torch
|
4 |
import gradio as gr
|
5 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
|
|
|
|
|
|
|
|
6 |
from pathlib import Path
|
7 |
|
8 |
|
|
|
11 |
|
12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
default_device = device
|
|
|
|
|
14 |
|
15 |
+
try:
|
16 |
+
wd_model = AutoModelForImageClassification.from_pretrained(WD_MODEL_NAME, trust_remote_code=True).to(default_device).eval()
|
17 |
+
wd_processor = AutoImageProcessor.from_pretrained(WD_MODEL_NAME, trust_remote_code=True)
|
18 |
+
except Exception as e:
|
19 |
+
print(e)
|
20 |
+
wd_model = wd_processor = None
|
21 |
|
22 |
def _people_tag(noun: str, minimum: int = 1, maximum: int = 5):
|
23 |
return (
|
|
|
187 |
return output_prompt
|
188 |
|
189 |
|
190 |
+
from translatepy import Translator
|
191 |
+
translator = Translator()
|
192 |
def translate_prompt(prompt: str = ""):
|
193 |
+
def translate_to_english(input: str):
|
|
|
|
|
|
|
|
|
194 |
try:
|
195 |
+
output = str(translator.translate(input, 'English'))
|
|
|
196 |
except Exception as e:
|
197 |
+
output = input
|
198 |
print(e)
|
199 |
+
return output
|
200 |
|
201 |
def is_japanese(s):
|
202 |
import unicodedata
|
|
|
219 |
|
220 |
|
221 |
def translate_prompt_to_ja(prompt: str = ""):
|
222 |
+
def translate_to_japanese(input: str):
|
|
|
|
|
|
|
|
|
223 |
try:
|
224 |
+
output = str(translator.translate(input, 'Japanese'))
|
|
|
225 |
except Exception as e:
|
226 |
+
output = input
|
227 |
print(e)
|
228 |
+
return output
|
229 |
|
230 |
def is_japanese(s):
|
231 |
import unicodedata
|