Spaces:
Paused
Paused
Update app-backup.py
Browse files- app-backup.py +169 -297
app-backup.py
CHANGED
|
@@ -5,8 +5,7 @@ import logging
|
|
| 5 |
import torch
|
| 6 |
from PIL import Image
|
| 7 |
import spaces
|
| 8 |
-
from diffusers import DiffusionPipeline, AutoencoderTiny, AutoencoderKL, AutoPipelineForImage2Image
|
| 9 |
-
from diffusers.pipelines import FluxControlNetPipeline
|
| 10 |
from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
|
| 11 |
from diffusers.utils import load_image
|
| 12 |
from huggingface_hub import hf_hub_download, HfFileSystem, ModelCard, snapshot_download
|
|
@@ -21,7 +20,7 @@ import numpy as np
|
|
| 21 |
import warnings
|
| 22 |
|
| 23 |
|
| 24 |
-
huggingface_token = os.getenv("
|
| 25 |
|
| 26 |
|
| 27 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en", device="cpu")
|
|
@@ -61,23 +60,6 @@ pipe_i2i = AutoPipelineForImage2Image.from_pretrained(
|
|
| 61 |
torch_dtype=dtype
|
| 62 |
).to(device)
|
| 63 |
|
| 64 |
-
# Upscale을 위한 ControlNet 설정
|
| 65 |
-
controlnet = FluxControlNetModel.from_pretrained(
|
| 66 |
-
"jasperai/Flux.1-dev-Controlnet-Upscaler", torch_dtype=torch.bfloat16
|
| 67 |
-
).to(device)
|
| 68 |
-
|
| 69 |
-
# Upscale 파이프라인 설정 (기존 pipe 재사용)
|
| 70 |
-
pipe_upscale = FluxControlNetPipeline(
|
| 71 |
-
vae=pipe.vae,
|
| 72 |
-
text_encoder=pipe.text_encoder,
|
| 73 |
-
text_encoder_2=pipe.text_encoder_2,
|
| 74 |
-
tokenizer=pipe.tokenizer,
|
| 75 |
-
tokenizer_2=pipe.tokenizer_2,
|
| 76 |
-
transformer=pipe.transformer,
|
| 77 |
-
scheduler=pipe.scheduler,
|
| 78 |
-
controlnet=controlnet
|
| 79 |
-
).to(device)
|
| 80 |
-
|
| 81 |
MAX_SEED = 2**32 - 1
|
| 82 |
MAX_PIXEL_BUDGET = 1024 * 1024
|
| 83 |
|
|
@@ -118,25 +100,30 @@ def download_file(url, directory=None):
|
|
| 118 |
file.write(response.content)
|
| 119 |
|
| 120 |
return filepath
|
| 121 |
-
|
| 122 |
def update_selection(evt: gr.SelectData, selected_indices, loras_state, width, height):
|
| 123 |
selected_index = evt.index
|
| 124 |
selected_indices = selected_indices or []
|
| 125 |
if selected_index in selected_indices:
|
| 126 |
selected_indices.remove(selected_index)
|
| 127 |
else:
|
| 128 |
-
if len(selected_indices) <
|
| 129 |
selected_indices.append(selected_index)
|
| 130 |
else:
|
| 131 |
-
gr.Warning("You can select up to
|
| 132 |
-
return gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), width, height, gr.update(), gr.update()
|
| 133 |
|
| 134 |
-
selected_info_1 = "Select
|
| 135 |
-
selected_info_2 = "Select
|
|
|
|
|
|
|
| 136 |
lora_scale_1 = 1.15
|
| 137 |
lora_scale_2 = 1.15
|
|
|
|
| 138 |
lora_image_1 = None
|
| 139 |
lora_image_2 = None
|
|
|
|
|
|
|
| 140 |
if len(selected_indices) >= 1:
|
| 141 |
lora1 = loras_state[selected_indices[0]]
|
| 142 |
selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}](https://huggingface.co/{lora1['repo']}) ✨"
|
|
@@ -145,71 +132,78 @@ def update_selection(evt: gr.SelectData, selected_indices, loras_state, width, h
|
|
| 145 |
lora2 = loras_state[selected_indices[1]]
|
| 146 |
selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}](https://huggingface.co/{lora2['repo']}) ✨"
|
| 147 |
lora_image_2 = lora2['image']
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
if selected_indices:
|
| 150 |
last_selected_lora = loras_state[selected_indices[-1]]
|
| 151 |
new_placeholder = f"Type a prompt for {last_selected_lora['title']}"
|
| 152 |
else:
|
| 153 |
new_placeholder = "Type a prompt after selecting a LoRA"
|
| 154 |
|
| 155 |
-
return gr.update(placeholder=new_placeholder), selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, width, height, lora_image_1, lora_image_2
|
| 156 |
-
|
| 157 |
-
def remove_lora_1(selected_indices, loras_state):
|
| 158 |
-
if len(selected_indices) >= 1:
|
| 159 |
-
selected_indices.pop(0)
|
| 160 |
-
selected_info_1 = "Select a LoRA 1"
|
| 161 |
-
selected_info_2 = "Select a LoRA 2"
|
| 162 |
-
lora_scale_1 = 1.15
|
| 163 |
-
lora_scale_2 = 1.15
|
| 164 |
-
lora_image_1 = None
|
| 165 |
-
lora_image_2 = None
|
| 166 |
-
if len(selected_indices) >= 1:
|
| 167 |
-
lora1 = loras_state[selected_indices[0]]
|
| 168 |
-
selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}]({lora1['repo']}) ✨"
|
| 169 |
-
lora_image_1 = lora1['image']
|
| 170 |
-
if len(selected_indices) >= 2:
|
| 171 |
-
lora2 = loras_state[selected_indices[1]]
|
| 172 |
-
selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}]({lora2['repo']}) ✨"
|
| 173 |
-
lora_image_2 = lora2['image']
|
| 174 |
-
return selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2
|
| 175 |
|
| 176 |
-
def
|
| 177 |
-
if len(selected_indices)
|
| 178 |
-
selected_indices.pop(
|
|
|
|
| 179 |
selected_info_1 = "Select LoRA 1"
|
| 180 |
selected_info_2 = "Select LoRA 2"
|
|
|
|
| 181 |
lora_scale_1 = 1.15
|
| 182 |
lora_scale_2 = 1.15
|
|
|
|
| 183 |
lora_image_1 = None
|
| 184 |
lora_image_2 = None
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
def randomize_loras(selected_indices, loras_state):
|
| 196 |
try:
|
| 197 |
-
if len(loras_state) <
|
| 198 |
raise gr.Error("Not enough LoRAs to randomize.")
|
| 199 |
-
selected_indices = random.sample(range(len(loras_state)),
|
| 200 |
lora1 = loras_state[selected_indices[0]]
|
| 201 |
lora2 = loras_state[selected_indices[1]]
|
|
|
|
| 202 |
selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}](https://huggingface.co/{lora1['repo']}) ✨"
|
| 203 |
selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}](https://huggingface.co/{lora2['repo']}) ✨"
|
|
|
|
| 204 |
lora_scale_1 = 1.15
|
| 205 |
lora_scale_2 = 1.15
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
| 208 |
random_prompt = random.choice(prompt_values)
|
| 209 |
-
return selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2, random_prompt
|
| 210 |
except Exception as e:
|
| 211 |
print(f"Error in randomize_loras: {str(e)}")
|
| 212 |
-
return "Error", "Error", [], 1.15, 1.15,
|
| 213 |
|
| 214 |
def add_custom_lora(custom_lora, selected_indices, current_loras):
|
| 215 |
if custom_lora:
|
|
@@ -234,18 +228,21 @@ def add_custom_lora(custom_lora, selected_indices, current_loras):
|
|
| 234 |
# Update gallery
|
| 235 |
gallery_items = [(item["image"], item["title"]) for item in current_loras]
|
| 236 |
# Update selected_indices if there's room
|
| 237 |
-
if len(selected_indices) <
|
| 238 |
selected_indices.append(existing_item_index)
|
| 239 |
else:
|
| 240 |
-
gr.Warning("You can select up to
|
| 241 |
|
| 242 |
# Update selected_info and images
|
| 243 |
selected_info_1 = "Select a LoRA 1"
|
| 244 |
selected_info_2 = "Select a LoRA 2"
|
|
|
|
| 245 |
lora_scale_1 = 1.15
|
| 246 |
lora_scale_2 = 1.15
|
|
|
|
| 247 |
lora_image_1 = None
|
| 248 |
lora_image_2 = None
|
|
|
|
| 249 |
if len(selected_indices) >= 1:
|
| 250 |
lora1 = current_loras[selected_indices[0]]
|
| 251 |
selected_info_1 = f"### LoRA 1 Selected: {lora1['title']} ✨"
|
|
@@ -254,24 +251,31 @@ def add_custom_lora(custom_lora, selected_indices, current_loras):
|
|
| 254 |
lora2 = current_loras[selected_indices[1]]
|
| 255 |
selected_info_2 = f"### LoRA 2 Selected: {lora2['title']} ✨"
|
| 256 |
lora_image_2 = lora2['image'] if lora2['image'] else None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
print("Finished adding custom LoRA")
|
| 258 |
return (
|
| 259 |
current_loras,
|
| 260 |
gr.update(value=gallery_items),
|
| 261 |
selected_info_1,
|
| 262 |
selected_info_2,
|
|
|
|
| 263 |
selected_indices,
|
| 264 |
lora_scale_1,
|
| 265 |
lora_scale_2,
|
|
|
|
| 266 |
lora_image_1,
|
| 267 |
-
lora_image_2
|
|
|
|
| 268 |
)
|
| 269 |
except Exception as e:
|
| 270 |
print(e)
|
| 271 |
gr.Warning(str(e))
|
| 272 |
-
return current_loras, gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update()
|
| 273 |
else:
|
| 274 |
-
return current_loras, gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update()
|
| 275 |
|
| 276 |
def remove_custom_lora(selected_indices, current_loras):
|
| 277 |
if current_loras:
|
|
@@ -287,10 +291,13 @@ def remove_custom_lora(selected_indices, current_loras):
|
|
| 287 |
# Update selected_info and images
|
| 288 |
selected_info_1 = "Select a LoRA 1"
|
| 289 |
selected_info_2 = "Select a LoRA 2"
|
|
|
|
| 290 |
lora_scale_1 = 1.15
|
| 291 |
lora_scale_2 = 1.15
|
|
|
|
| 292 |
lora_image_1 = None
|
| 293 |
lora_image_2 = None
|
|
|
|
| 294 |
if len(selected_indices) >= 1:
|
| 295 |
lora1 = current_loras[selected_indices[0]]
|
| 296 |
selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}]({lora1['repo']}) ✨"
|
|
@@ -299,16 +306,23 @@ def remove_custom_lora(selected_indices, current_loras):
|
|
| 299 |
lora2 = current_loras[selected_indices[1]]
|
| 300 |
selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}]({lora2['repo']}) ✨"
|
| 301 |
lora_image_2 = lora2['image']
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
return (
|
| 303 |
current_loras,
|
| 304 |
gr.update(value=gallery_items),
|
| 305 |
selected_info_1,
|
| 306 |
selected_info_2,
|
|
|
|
| 307 |
selected_indices,
|
| 308 |
lora_scale_1,
|
| 309 |
lora_scale_2,
|
|
|
|
| 310 |
lora_image_1,
|
| 311 |
-
lora_image_2
|
|
|
|
| 312 |
)
|
| 313 |
|
| 314 |
@spaces.GPU(duration=75)
|
|
@@ -350,9 +364,9 @@ def generate_image_to_image(prompt_mash, image_input_path, image_strength, steps
|
|
| 350 |
).images[0]
|
| 351 |
return final_image
|
| 352 |
|
| 353 |
-
def run_lora(prompt, image_input, image_strength, cfg_scale, steps, selected_indices, lora_scale_1, lora_scale_2, randomize_seed, seed, width, height, loras_state, progress=gr.Progress(track_tqdm=True)):
|
| 354 |
try:
|
| 355 |
-
# 한글 감지 및 번역
|
| 356 |
if any('\u3131' <= char <= '\u318E' or '\uAC00' <= char <= '\uD7A3' for char in prompt):
|
| 357 |
translated = translator(prompt, max_length=512)[0]['translation_text']
|
| 358 |
print(f"Original prompt: {prompt}")
|
|
@@ -364,7 +378,7 @@ def run_lora(prompt, image_input, image_strength, cfg_scale, steps, selected_ind
|
|
| 364 |
|
| 365 |
selected_loras = [loras_state[idx] for idx in selected_indices]
|
| 366 |
|
| 367 |
-
# Build the prompt with trigger words
|
| 368 |
prepends = []
|
| 369 |
appends = []
|
| 370 |
for lora in selected_loras:
|
|
@@ -382,41 +396,52 @@ def run_lora(prompt, image_input, image_strength, cfg_scale, steps, selected_ind
|
|
| 382 |
pipe.unload_lora_weights()
|
| 383 |
pipe_i2i.unload_lora_weights()
|
| 384 |
|
| 385 |
-
print(pipe.get_active_adapters())
|
|
|
|
| 386 |
# Load LoRA weights with respective scales
|
| 387 |
lora_names = []
|
| 388 |
lora_weights = []
|
| 389 |
with calculateDuration("Loading LoRA weights"):
|
| 390 |
for idx, lora in enumerate(selected_loras):
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
|
|
|
| 400 |
else:
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
|
|
|
|
|
|
|
|
|
| 407 |
print("Loaded LoRAs:", lora_names)
|
| 408 |
print("Adapter weights:", lora_weights)
|
| 409 |
-
|
| 410 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 411 |
else:
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
|
|
|
|
|
|
|
|
|
| 415 |
with calculateDuration("Randomizing seed"):
|
| 416 |
if randomize_seed:
|
| 417 |
seed = random.randint(0, MAX_SEED)
|
| 418 |
|
| 419 |
-
# Generate image
|
| 420 |
if image_input is not None:
|
| 421 |
final_image = generate_image_to_image(prompt_mash, image_input, image_strength, steps, cfg_scale, width, height, seed)
|
| 422 |
else:
|
|
@@ -428,19 +453,16 @@ def run_lora(prompt, image_input, image_strength, cfg_scale, steps, selected_ind
|
|
| 428 |
final_image = image
|
| 429 |
progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
|
| 430 |
yield image, seed, gr.update(value=progress_bar, visible=True)
|
| 431 |
-
|
| 432 |
-
|
| 433 |
|
| 434 |
if final_image is None:
|
| 435 |
raise Exception("Failed to generate image")
|
| 436 |
|
| 437 |
return final_image, seed, gr.update(visible=False)
|
|
|
|
| 438 |
except Exception as e:
|
| 439 |
print(f"Error in run_lora: {str(e)}")
|
| 440 |
return None, seed, gr.update(visible=False)
|
| 441 |
|
| 442 |
-
|
| 443 |
-
|
| 444 |
run_lora.zerogpu = True
|
| 445 |
|
| 446 |
def get_huggingface_safetensors(link):
|
|
@@ -524,119 +546,32 @@ css = '''
|
|
| 524 |
footer {visibility: hidden;}
|
| 525 |
'''
|
| 526 |
|
| 527 |
-
# 업스케일 관련 함수 추가
|
| 528 |
-
def process_input(input_image, upscale_factor, **kwargs):
|
| 529 |
-
w, h = input_image.size
|
| 530 |
-
w_original, h_original = w, h
|
| 531 |
-
aspect_ratio = w / h
|
| 532 |
-
|
| 533 |
-
was_resized = False
|
| 534 |
-
|
| 535 |
-
max_size = int(np.sqrt(MAX_PIXEL_BUDGET / (upscale_factor ** 2)))
|
| 536 |
-
if w > max_size or h > max_size:
|
| 537 |
-
if w > h:
|
| 538 |
-
w_new = max_size
|
| 539 |
-
h_new = int(w_new / aspect_ratio)
|
| 540 |
-
else:
|
| 541 |
-
h_new = max_size
|
| 542 |
-
w_new = int(h_new * aspect_ratio)
|
| 543 |
-
|
| 544 |
-
input_image = input_image.resize((w_new, h_new), Image.LANCZOS)
|
| 545 |
-
was_resized = True
|
| 546 |
-
gr.Info(f"Input image resized to {w_new}x{h_new} to fit within pixel budget after upscaling.")
|
| 547 |
-
|
| 548 |
-
# resize to multiple of 8
|
| 549 |
-
w, h = input_image.size
|
| 550 |
-
w = w - w % 8
|
| 551 |
-
h = h - h % 8
|
| 552 |
-
|
| 553 |
-
return input_image.resize((w, h)), w_original, h_original, was_resized
|
| 554 |
-
|
| 555 |
-
from PIL import Image
|
| 556 |
-
import numpy as np
|
| 557 |
-
|
| 558 |
-
@spaces.GPU
|
| 559 |
-
def infer_upscale(
|
| 560 |
-
seed,
|
| 561 |
-
randomize_seed,
|
| 562 |
-
input_image,
|
| 563 |
-
num_inference_steps,
|
| 564 |
-
upscale_factor,
|
| 565 |
-
controlnet_conditioning_scale,
|
| 566 |
-
progress=gr.Progress(track_tqdm=True),
|
| 567 |
-
):
|
| 568 |
-
if input_image is None:
|
| 569 |
-
return None, seed, gr.update(), gr.update(), gr.update(), gr.update(), gr.update(visible=True, value="Please upload an image for upscaling.")
|
| 570 |
-
|
| 571 |
-
try:
|
| 572 |
-
if randomize_seed:
|
| 573 |
-
seed = random.randint(0, MAX_SEED)
|
| 574 |
-
|
| 575 |
-
input_image, w_original, h_original, was_resized = process_input(input_image, upscale_factor)
|
| 576 |
-
|
| 577 |
-
# rescale with upscale factor
|
| 578 |
-
w, h = input_image.size
|
| 579 |
-
control_image = input_image.resize((w * upscale_factor, h * upscale_factor), Image.LANCZOS)
|
| 580 |
-
|
| 581 |
-
generator = torch.Generator(device=device).manual_seed(seed)
|
| 582 |
-
|
| 583 |
-
gr.Info("Upscaling image...")
|
| 584 |
-
# 모든 텐서를 동일한 디바이스로 이동
|
| 585 |
-
pipe_upscale.to(device)
|
| 586 |
-
|
| 587 |
-
# Ensure the image is in RGB format
|
| 588 |
-
if control_image.mode != 'RGB':
|
| 589 |
-
control_image = control_image.convert('RGB')
|
| 590 |
-
|
| 591 |
-
# Convert to tensor and add batch dimension
|
| 592 |
-
control_image = torch.from_numpy(np.array(control_image)).permute(2, 0, 1).float().unsqueeze(0).to(device) / 255.0
|
| 593 |
-
|
| 594 |
-
with torch.no_grad():
|
| 595 |
-
image = pipe_upscale(
|
| 596 |
-
prompt="",
|
| 597 |
-
control_image=control_image,
|
| 598 |
-
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
| 599 |
-
num_inference_steps=num_inference_steps,
|
| 600 |
-
guidance_scale=3.5,
|
| 601 |
-
generator=generator,
|
| 602 |
-
).images[0]
|
| 603 |
-
|
| 604 |
-
# Convert the image back to PIL Image
|
| 605 |
-
if isinstance(image, torch.Tensor):
|
| 606 |
-
image = image.cpu().permute(1, 2, 0).numpy()
|
| 607 |
-
|
| 608 |
-
# Ensure the image data is in the correct range
|
| 609 |
-
image = np.clip(image * 255, 0, 255).astype(np.uint8)
|
| 610 |
-
image = Image.fromarray(image)
|
| 611 |
-
|
| 612 |
-
if was_resized:
|
| 613 |
-
gr.Info(
|
| 614 |
-
f"Resizing output image to targeted {w_original * upscale_factor}x{h_original * upscale_factor} size."
|
| 615 |
-
)
|
| 616 |
-
image = image.resize((w_original * upscale_factor, h_original * upscale_factor), Image.LANCZOS)
|
| 617 |
-
|
| 618 |
-
return image, seed, num_inference_steps, upscale_factor, controlnet_conditioning_scale, gr.update(), gr.update(visible=False)
|
| 619 |
-
except Exception as e:
|
| 620 |
-
print(f"Error in infer_upscale: {str(e)}")
|
| 621 |
-
import traceback
|
| 622 |
-
traceback.print_exc()
|
| 623 |
-
return None, seed, gr.update(), gr.update(), gr.update(), gr.update(), gr.update(visible=True, value=f"Error: {str(e)}")
|
| 624 |
-
|
| 625 |
-
def check_upscale_input(input_image, *args):
|
| 626 |
-
if input_image is None:
|
| 627 |
-
return gr.update(interactive=False), *args, gr.update(visible=True, value="Please upload an image for upscaling.")
|
| 628 |
-
return gr.update(interactive=True), *args, gr.update(visible=False)
|
| 629 |
-
|
| 630 |
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, delete_cache=(60, 3600)) as app:
|
| 631 |
loras_state = gr.State(loras)
|
| 632 |
selected_indices = gr.State([])
|
| 633 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 634 |
with gr.Row():
|
| 635 |
with gr.Column(scale=3):
|
| 636 |
prompt = gr.Textbox(label="Prompt", lines=1, placeholder="Type a prompt after selecting a LoRA")
|
| 637 |
with gr.Column(scale=1):
|
| 638 |
generate_button = gr.Button("Generate", variant="primary", elem_classes=["button_total"])
|
| 639 |
-
|
|
|
|
|
|
|
|
|
|
| 640 |
with gr.Row(elem_id="loaded_loras"):
|
| 641 |
with gr.Column(scale=1, min_width=25):
|
| 642 |
randomize_button = gr.Button("🎲", variant="secondary", scale=1, elem_id="random_btn")
|
|
@@ -650,6 +585,7 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, delete_cache=(60, 3600)) as a
|
|
| 650 |
lora_scale_1 = gr.Slider(label="LoRA 1 Scale", minimum=0, maximum=3, step=0.01, value=1.15)
|
| 651 |
with gr.Row():
|
| 652 |
remove_button_1 = gr.Button("Remove", size="sm")
|
|
|
|
| 653 |
with gr.Column(scale=8):
|
| 654 |
with gr.Row():
|
| 655 |
with gr.Column(scale=0, min_width=50):
|
|
@@ -660,6 +596,17 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, delete_cache=(60, 3600)) as a
|
|
| 660 |
lora_scale_2 = gr.Slider(label="LoRA 2 Scale", minimum=0, maximum=3, step=0.01, value=1.15)
|
| 661 |
with gr.Row():
|
| 662 |
remove_button_2 = gr.Button("Remove", size="sm")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 663 |
|
| 664 |
with gr.Row():
|
| 665 |
with gr.Column():
|
|
@@ -698,85 +645,52 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, delete_cache=(60, 3600)) as a
|
|
| 698 |
randomize_seed = gr.Checkbox(True, label="Randomize seed")
|
| 699 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
|
| 700 |
|
| 701 |
-
# 업스케일 관련 UI 추가
|
| 702 |
-
with gr.Row():
|
| 703 |
-
upscale_button = gr.Button("Upscale", interactive=False)
|
| 704 |
-
|
| 705 |
-
with gr.Row():
|
| 706 |
-
with gr.Column(scale=4):
|
| 707 |
-
upscale_input = gr.Image(label="Input Image for Upscaling", type="pil")
|
| 708 |
-
with gr.Column(scale=1):
|
| 709 |
-
upscale_steps = gr.Slider(
|
| 710 |
-
label="Number of Inference Steps for Upscaling",
|
| 711 |
-
minimum=8,
|
| 712 |
-
maximum=50,
|
| 713 |
-
step=1,
|
| 714 |
-
value=28,
|
| 715 |
-
)
|
| 716 |
-
upscale_factor = gr.Slider(
|
| 717 |
-
label="Upscale Factor",
|
| 718 |
-
minimum=1,
|
| 719 |
-
maximum=4,
|
| 720 |
-
step=1,
|
| 721 |
-
value=4,
|
| 722 |
-
)
|
| 723 |
-
controlnet_conditioning_scale = gr.Slider(
|
| 724 |
-
label="Controlnet Conditioning Scale",
|
| 725 |
-
minimum=0.1,
|
| 726 |
-
maximum=1.0,
|
| 727 |
-
step=0.05,
|
| 728 |
-
value=0.5, # 기본값을 0.5로 낮춤
|
| 729 |
-
)
|
| 730 |
-
upscale_seed = gr.Slider(
|
| 731 |
-
label="Seed for Upscaling",
|
| 732 |
-
minimum=0,
|
| 733 |
-
maximum=MAX_SEED,
|
| 734 |
-
step=1,
|
| 735 |
-
value=42,
|
| 736 |
-
)
|
| 737 |
-
upscale_randomize_seed = gr.Checkbox(label="Randomize seed for Upscaling", value=True)
|
| 738 |
-
upscale_error = gr.Markdown(visible=False, value="Please provide an input image for upscaling.")
|
| 739 |
-
|
| 740 |
-
with gr.Row():
|
| 741 |
-
upscale_result = gr.Image(label="Upscaled Image", type="pil")
|
| 742 |
-
upscale_seed_output = gr.Number(label="Seed Used", precision=0)
|
| 743 |
-
|
| 744 |
-
|
| 745 |
gallery.select(
|
| 746 |
update_selection,
|
| 747 |
inputs=[selected_indices, loras_state, width, height],
|
| 748 |
-
outputs=[prompt, selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, width, height, lora_image_1, lora_image_2]
|
| 749 |
)
|
|
|
|
| 750 |
remove_button_1.click(
|
| 751 |
remove_lora_1,
|
| 752 |
inputs=[selected_indices, loras_state],
|
| 753 |
-
outputs=[selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
|
| 754 |
)
|
|
|
|
| 755 |
remove_button_2.click(
|
| 756 |
remove_lora_2,
|
| 757 |
inputs=[selected_indices, loras_state],
|
| 758 |
-
outputs=[selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
|
| 759 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 760 |
randomize_button.click(
|
| 761 |
randomize_loras,
|
| 762 |
inputs=[selected_indices, loras_state],
|
| 763 |
-
outputs=[selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2, prompt]
|
| 764 |
)
|
|
|
|
| 765 |
add_custom_lora_button.click(
|
| 766 |
add_custom_lora,
|
| 767 |
inputs=[custom_lora, selected_indices, loras_state],
|
| 768 |
-
outputs=[loras_state, gallery, selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
|
| 769 |
)
|
|
|
|
| 770 |
remove_custom_lora_button.click(
|
| 771 |
remove_custom_lora,
|
| 772 |
inputs=[selected_indices, loras_state],
|
| 773 |
-
outputs=[loras_state, gallery, selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
|
| 774 |
)
|
| 775 |
|
| 776 |
gr.on(
|
| 777 |
triggers=[generate_button.click, prompt.submit],
|
| 778 |
fn=run_lora,
|
| 779 |
-
inputs=[prompt, input_image, image_strength, cfg_scale, steps, selected_indices, lora_scale_1, lora_scale_2, randomize_seed, seed, width, height, loras_state],
|
| 780 |
outputs=[result, seed, progress_bar]
|
| 781 |
).then(
|
| 782 |
fn=lambda x, history: update_history(x, history) if x is not None else history,
|
|
@@ -784,48 +698,6 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, delete_cache=(60, 3600)) as a
|
|
| 784 |
outputs=history_gallery,
|
| 785 |
)
|
| 786 |
|
| 787 |
-
upscale_input.upload(
|
| 788 |
-
lambda x: gr.update(interactive=x is not None),
|
| 789 |
-
inputs=[upscale_input],
|
| 790 |
-
outputs=[upscale_button]
|
| 791 |
-
)
|
| 792 |
-
|
| 793 |
-
upscale_error = gr.Markdown(visible=False, value="")
|
| 794 |
-
|
| 795 |
-
upscale_button.click(
|
| 796 |
-
infer_upscale,
|
| 797 |
-
inputs=[
|
| 798 |
-
upscale_seed,
|
| 799 |
-
upscale_randomize_seed,
|
| 800 |
-
upscale_input,
|
| 801 |
-
upscale_steps,
|
| 802 |
-
upscale_factor,
|
| 803 |
-
controlnet_conditioning_scale,
|
| 804 |
-
],
|
| 805 |
-
outputs=[
|
| 806 |
-
upscale_result,
|
| 807 |
-
upscale_seed_output,
|
| 808 |
-
upscale_steps,
|
| 809 |
-
upscale_factor,
|
| 810 |
-
controlnet_conditioning_scale,
|
| 811 |
-
upscale_randomize_seed,
|
| 812 |
-
upscale_error
|
| 813 |
-
],
|
| 814 |
-
|
| 815 |
-
).then(
|
| 816 |
-
infer_upscale,
|
| 817 |
-
inputs=[
|
| 818 |
-
upscale_seed,
|
| 819 |
-
upscale_randomize_seed,
|
| 820 |
-
upscale_input,
|
| 821 |
-
upscale_steps,
|
| 822 |
-
upscale_factor,
|
| 823 |
-
controlnet_conditioning_scale,
|
| 824 |
-
],
|
| 825 |
-
outputs=[upscale_result, upscale_seed_output]
|
| 826 |
-
)
|
| 827 |
-
|
| 828 |
-
|
| 829 |
if __name__ == "__main__":
|
| 830 |
app.queue(max_size=20)
|
| 831 |
app.launch(debug=True)
|
|
|
|
| 5 |
import torch
|
| 6 |
from PIL import Image
|
| 7 |
import spaces
|
| 8 |
+
from diffusers import DiffusionPipeline, AutoencoderTiny, AutoencoderKL, AutoPipelineForImage2Image
|
|
|
|
| 9 |
from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
|
| 10 |
from diffusers.utils import load_image
|
| 11 |
from huggingface_hub import hf_hub_download, HfFileSystem, ModelCard, snapshot_download
|
|
|
|
| 20 |
import warnings
|
| 21 |
|
| 22 |
|
| 23 |
+
huggingface_token = os.getenv("HF_TOKEN")
|
| 24 |
|
| 25 |
|
| 26 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en", device="cpu")
|
|
|
|
| 60 |
torch_dtype=dtype
|
| 61 |
).to(device)
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
MAX_SEED = 2**32 - 1
|
| 64 |
MAX_PIXEL_BUDGET = 1024 * 1024
|
| 65 |
|
|
|
|
| 100 |
file.write(response.content)
|
| 101 |
|
| 102 |
return filepath
|
| 103 |
+
|
| 104 |
def update_selection(evt: gr.SelectData, selected_indices, loras_state, width, height):
|
| 105 |
selected_index = evt.index
|
| 106 |
selected_indices = selected_indices or []
|
| 107 |
if selected_index in selected_indices:
|
| 108 |
selected_indices.remove(selected_index)
|
| 109 |
else:
|
| 110 |
+
if len(selected_indices) < 3:
|
| 111 |
selected_indices.append(selected_index)
|
| 112 |
else:
|
| 113 |
+
gr.Warning("You can select up to 3 LoRAs, remove one to select a new one.")
|
| 114 |
+
return gr.update(), gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), width, height, gr.update(), gr.update(), gr.update()
|
| 115 |
|
| 116 |
+
selected_info_1 = "Select LoRA 1"
|
| 117 |
+
selected_info_2 = "Select LoRA 2"
|
| 118 |
+
selected_info_3 = "Select LoRA 3"
|
| 119 |
+
|
| 120 |
lora_scale_1 = 1.15
|
| 121 |
lora_scale_2 = 1.15
|
| 122 |
+
lora_scale_3 = 1.15
|
| 123 |
lora_image_1 = None
|
| 124 |
lora_image_2 = None
|
| 125 |
+
lora_image_3 = None
|
| 126 |
+
|
| 127 |
if len(selected_indices) >= 1:
|
| 128 |
lora1 = loras_state[selected_indices[0]]
|
| 129 |
selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}](https://huggingface.co/{lora1['repo']}) ✨"
|
|
|
|
| 132 |
lora2 = loras_state[selected_indices[1]]
|
| 133 |
selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}](https://huggingface.co/{lora2['repo']}) ✨"
|
| 134 |
lora_image_2 = lora2['image']
|
| 135 |
+
if len(selected_indices) >= 3:
|
| 136 |
+
lora3 = loras_state[selected_indices[2]]
|
| 137 |
+
selected_info_3 = f"### LoRA 3 Selected: [{lora3['title']}](https://huggingface.co/{lora3['repo']}) ✨"
|
| 138 |
+
lora_image_3 = lora3['image']
|
| 139 |
+
|
| 140 |
if selected_indices:
|
| 141 |
last_selected_lora = loras_state[selected_indices[-1]]
|
| 142 |
new_placeholder = f"Type a prompt for {last_selected_lora['title']}"
|
| 143 |
else:
|
| 144 |
new_placeholder = "Type a prompt after selecting a LoRA"
|
| 145 |
|
| 146 |
+
return gr.update(placeholder=new_placeholder), selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, width, height, lora_image_1, lora_image_2, lora_image_3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
+
def remove_lora(selected_indices, loras_state, index_to_remove):
|
| 149 |
+
if len(selected_indices) > index_to_remove:
|
| 150 |
+
selected_indices.pop(index_to_remove)
|
| 151 |
+
|
| 152 |
selected_info_1 = "Select LoRA 1"
|
| 153 |
selected_info_2 = "Select LoRA 2"
|
| 154 |
+
selected_info_3 = "Select LoRA 3"
|
| 155 |
lora_scale_1 = 1.15
|
| 156 |
lora_scale_2 = 1.15
|
| 157 |
+
lora_scale_3 = 1.15
|
| 158 |
lora_image_1 = None
|
| 159 |
lora_image_2 = None
|
| 160 |
+
lora_image_3 = None
|
| 161 |
+
|
| 162 |
+
for i, idx in enumerate(selected_indices):
|
| 163 |
+
lora = loras_state[idx]
|
| 164 |
+
if i == 0:
|
| 165 |
+
selected_info_1 = f"### LoRA 1 Selected: [{lora['title']}]({lora['repo']}) ✨"
|
| 166 |
+
lora_image_1 = lora['image']
|
| 167 |
+
elif i == 1:
|
| 168 |
+
selected_info_2 = f"### LoRA 2 Selected: [{lora['title']}]({lora['repo']}) ✨"
|
| 169 |
+
lora_image_2 = lora['image']
|
| 170 |
+
elif i == 2:
|
| 171 |
+
selected_info_3 = f"### LoRA 3 Selected: [{lora['title']}]({lora['repo']}) ✨"
|
| 172 |
+
lora_image_3 = lora['image']
|
| 173 |
+
|
| 174 |
+
return selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, lora_image_1, lora_image_2, lora_image_3
|
| 175 |
+
|
| 176 |
+
def remove_lora_1(selected_indices, loras_state):
|
| 177 |
+
return remove_lora(selected_indices, loras_state, 0)
|
| 178 |
+
|
| 179 |
+
def remove_lora_2(selected_indices, loras_state):
|
| 180 |
+
return remove_lora(selected_indices, loras_state, 1)
|
| 181 |
+
|
| 182 |
+
def remove_lora_3(selected_indices, loras_state):
|
| 183 |
+
return remove_lora(selected_indices, loras_state, 2)
|
| 184 |
|
| 185 |
def randomize_loras(selected_indices, loras_state):
|
| 186 |
try:
|
| 187 |
+
if len(loras_state) < 3:
|
| 188 |
raise gr.Error("Not enough LoRAs to randomize.")
|
| 189 |
+
selected_indices = random.sample(range(len(loras_state)), 3)
|
| 190 |
lora1 = loras_state[selected_indices[0]]
|
| 191 |
lora2 = loras_state[selected_indices[1]]
|
| 192 |
+
lora3 = loras_state[selected_indices[2]]
|
| 193 |
selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}](https://huggingface.co/{lora1['repo']}) ✨"
|
| 194 |
selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}](https://huggingface.co/{lora2['repo']}) ✨"
|
| 195 |
+
selected_info_3 = f"### LoRA 3 Selected: [{lora3['title']}](https://huggingface.co/{lora3['repo']}) ✨"
|
| 196 |
lora_scale_1 = 1.15
|
| 197 |
lora_scale_2 = 1.15
|
| 198 |
+
lora_scale_3 = 1.15
|
| 199 |
+
lora_image_1 = lora1.get('image', 'path/to/default/image.png')
|
| 200 |
+
lora_image_2 = lora2.get('image', 'path/to/default/image.png')
|
| 201 |
+
lora_image_3 = lora3.get('image', 'path/to/default/image.png')
|
| 202 |
random_prompt = random.choice(prompt_values)
|
| 203 |
+
return selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, lora_image_1, lora_image_2, lora_image_3, random_prompt
|
| 204 |
except Exception as e:
|
| 205 |
print(f"Error in randomize_loras: {str(e)}")
|
| 206 |
+
return "Error", "Error", "Error", [], 1.15, 1.15, 1.15, 'path/to/default/image.png', 'path/to/default/image.png', 'path/to/default/image.png', ""
|
| 207 |
|
| 208 |
def add_custom_lora(custom_lora, selected_indices, current_loras):
|
| 209 |
if custom_lora:
|
|
|
|
| 228 |
# Update gallery
|
| 229 |
gallery_items = [(item["image"], item["title"]) for item in current_loras]
|
| 230 |
# Update selected_indices if there's room
|
| 231 |
+
if len(selected_indices) < 3:
|
| 232 |
selected_indices.append(existing_item_index)
|
| 233 |
else:
|
| 234 |
+
gr.Warning("You can select up to 3 LoRAs, remove one to select a new one.")
|
| 235 |
|
| 236 |
# Update selected_info and images
|
| 237 |
selected_info_1 = "Select a LoRA 1"
|
| 238 |
selected_info_2 = "Select a LoRA 2"
|
| 239 |
+
selected_info_3 = "Select a LoRA 3"
|
| 240 |
lora_scale_1 = 1.15
|
| 241 |
lora_scale_2 = 1.15
|
| 242 |
+
lora_scale_3 = 1.15
|
| 243 |
lora_image_1 = None
|
| 244 |
lora_image_2 = None
|
| 245 |
+
lora_image_3 = None
|
| 246 |
if len(selected_indices) >= 1:
|
| 247 |
lora1 = current_loras[selected_indices[0]]
|
| 248 |
selected_info_1 = f"### LoRA 1 Selected: {lora1['title']} ✨"
|
|
|
|
| 251 |
lora2 = current_loras[selected_indices[1]]
|
| 252 |
selected_info_2 = f"### LoRA 2 Selected: {lora2['title']} ✨"
|
| 253 |
lora_image_2 = lora2['image'] if lora2['image'] else None
|
| 254 |
+
if len(selected_indices) >= 3:
|
| 255 |
+
lora3 = current_loras[selected_indices[2]]
|
| 256 |
+
selected_info_3 = f"### LoRA 3 Selected: {lora3['title']} ✨"
|
| 257 |
+
lora_image_3 = lora3['image'] if lora3['image'] else None
|
| 258 |
print("Finished adding custom LoRA")
|
| 259 |
return (
|
| 260 |
current_loras,
|
| 261 |
gr.update(value=gallery_items),
|
| 262 |
selected_info_1,
|
| 263 |
selected_info_2,
|
| 264 |
+
selected_info_3,
|
| 265 |
selected_indices,
|
| 266 |
lora_scale_1,
|
| 267 |
lora_scale_2,
|
| 268 |
+
lora_scale_3,
|
| 269 |
lora_image_1,
|
| 270 |
+
lora_image_2,
|
| 271 |
+
lora_image_3
|
| 272 |
)
|
| 273 |
except Exception as e:
|
| 274 |
print(e)
|
| 275 |
gr.Warning(str(e))
|
| 276 |
+
return current_loras, gr.update(), gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update()
|
| 277 |
else:
|
| 278 |
+
return current_loras, gr.update(), gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update()
|
| 279 |
|
| 280 |
def remove_custom_lora(selected_indices, current_loras):
|
| 281 |
if current_loras:
|
|
|
|
| 291 |
# Update selected_info and images
|
| 292 |
selected_info_1 = "Select a LoRA 1"
|
| 293 |
selected_info_2 = "Select a LoRA 2"
|
| 294 |
+
selected_info_3 = "Select a LoRA 3"
|
| 295 |
lora_scale_1 = 1.15
|
| 296 |
lora_scale_2 = 1.15
|
| 297 |
+
lora_scale_3 = 1.15
|
| 298 |
lora_image_1 = None
|
| 299 |
lora_image_2 = None
|
| 300 |
+
lora_image_3 = None
|
| 301 |
if len(selected_indices) >= 1:
|
| 302 |
lora1 = current_loras[selected_indices[0]]
|
| 303 |
selected_info_1 = f"### LoRA 1 Selected: [{lora1['title']}]({lora1['repo']}) ✨"
|
|
|
|
| 306 |
lora2 = current_loras[selected_indices[1]]
|
| 307 |
selected_info_2 = f"### LoRA 2 Selected: [{lora2['title']}]({lora2['repo']}) ✨"
|
| 308 |
lora_image_2 = lora2['image']
|
| 309 |
+
if len(selected_indices) >= 3:
|
| 310 |
+
lora3 = current_loras[selected_indices[2]]
|
| 311 |
+
selected_info_3 = f"### LoRA 3 Selected: [{lora3['title']}]({lora3['repo']}) ✨"
|
| 312 |
+
lora_image_3 = lora3['image']
|
| 313 |
return (
|
| 314 |
current_loras,
|
| 315 |
gr.update(value=gallery_items),
|
| 316 |
selected_info_1,
|
| 317 |
selected_info_2,
|
| 318 |
+
selected_info_3,
|
| 319 |
selected_indices,
|
| 320 |
lora_scale_1,
|
| 321 |
lora_scale_2,
|
| 322 |
+
lora_scale_3,
|
| 323 |
lora_image_1,
|
| 324 |
+
lora_image_2,
|
| 325 |
+
lora_image_3
|
| 326 |
)
|
| 327 |
|
| 328 |
@spaces.GPU(duration=75)
|
|
|
|
| 364 |
).images[0]
|
| 365 |
return final_image
|
| 366 |
|
| 367 |
+
def run_lora(prompt, image_input, image_strength, cfg_scale, steps, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, randomize_seed, seed, width, height, loras_state, progress=gr.Progress(track_tqdm=True)):
|
| 368 |
try:
|
| 369 |
+
# 한글 감지 및 번역 (이 부분은 그대로 유지)
|
| 370 |
if any('\u3131' <= char <= '\u318E' or '\uAC00' <= char <= '\uD7A3' for char in prompt):
|
| 371 |
translated = translator(prompt, max_length=512)[0]['translation_text']
|
| 372 |
print(f"Original prompt: {prompt}")
|
|
|
|
| 378 |
|
| 379 |
selected_loras = [loras_state[idx] for idx in selected_indices]
|
| 380 |
|
| 381 |
+
# Build the prompt with trigger words (이 부분은 그대로 유지)
|
| 382 |
prepends = []
|
| 383 |
appends = []
|
| 384 |
for lora in selected_loras:
|
|
|
|
| 396 |
pipe.unload_lora_weights()
|
| 397 |
pipe_i2i.unload_lora_weights()
|
| 398 |
|
| 399 |
+
print(f"Active adapters before loading: {pipe.get_active_adapters()}")
|
| 400 |
+
|
| 401 |
# Load LoRA weights with respective scales
|
| 402 |
lora_names = []
|
| 403 |
lora_weights = []
|
| 404 |
with calculateDuration("Loading LoRA weights"):
|
| 405 |
for idx, lora in enumerate(selected_loras):
|
| 406 |
+
try:
|
| 407 |
+
lora_name = f"lora_{idx}"
|
| 408 |
+
lora_path = lora['repo']
|
| 409 |
+
weight_name = lora.get("weights")
|
| 410 |
+
print(f"Loading LoRA {lora_name} from {lora_path}")
|
| 411 |
+
if image_input is not None:
|
| 412 |
+
if weight_name:
|
| 413 |
+
pipe_i2i.load_lora_weights(lora_path, weight_name=weight_name, adapter_name=lora_name)
|
| 414 |
+
else:
|
| 415 |
+
pipe_i2i.load_lora_weights(lora_path, adapter_name=lora_name)
|
| 416 |
else:
|
| 417 |
+
if weight_name:
|
| 418 |
+
pipe.load_lora_weights(lora_path, weight_name=weight_name, adapter_name=lora_name)
|
| 419 |
+
else:
|
| 420 |
+
pipe.load_lora_weights(lora_path, adapter_name=lora_name)
|
| 421 |
+
lora_names.append(lora_name)
|
| 422 |
+
lora_weights.append(lora_scale_1 if idx == 0 else lora_scale_2 if idx == 1 else lora_scale_3)
|
| 423 |
+
except Exception as e:
|
| 424 |
+
print(f"Failed to load LoRA {lora_name}: {str(e)}")
|
| 425 |
+
|
| 426 |
print("Loaded LoRAs:", lora_names)
|
| 427 |
print("Adapter weights:", lora_weights)
|
| 428 |
+
|
| 429 |
+
if lora_names:
|
| 430 |
+
if image_input is not None:
|
| 431 |
+
pipe_i2i.set_adapters(lora_names, adapter_weights=lora_weights)
|
| 432 |
+
else:
|
| 433 |
+
pipe.set_adapters(lora_names, adapter_weights=lora_weights)
|
| 434 |
else:
|
| 435 |
+
print("No LoRAs were successfully loaded.")
|
| 436 |
+
return None, seed, gr.update(visible=False)
|
| 437 |
+
|
| 438 |
+
print(f"Active adapters after loading: {pipe.get_active_adapters()}")
|
| 439 |
+
|
| 440 |
+
# 여기서부터 이미지 생성 로직 (이 부분은 그대로 유지)
|
| 441 |
with calculateDuration("Randomizing seed"):
|
| 442 |
if randomize_seed:
|
| 443 |
seed = random.randint(0, MAX_SEED)
|
| 444 |
|
|
|
|
| 445 |
if image_input is not None:
|
| 446 |
final_image = generate_image_to_image(prompt_mash, image_input, image_strength, steps, cfg_scale, width, height, seed)
|
| 447 |
else:
|
|
|
|
| 453 |
final_image = image
|
| 454 |
progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
|
| 455 |
yield image, seed, gr.update(value=progress_bar, visible=True)
|
|
|
|
|
|
|
| 456 |
|
| 457 |
if final_image is None:
|
| 458 |
raise Exception("Failed to generate image")
|
| 459 |
|
| 460 |
return final_image, seed, gr.update(visible=False)
|
| 461 |
+
|
| 462 |
except Exception as e:
|
| 463 |
print(f"Error in run_lora: {str(e)}")
|
| 464 |
return None, seed, gr.update(visible=False)
|
| 465 |
|
|
|
|
|
|
|
| 466 |
run_lora.zerogpu = True
|
| 467 |
|
| 468 |
def get_huggingface_safetensors(link):
|
|
|
|
| 546 |
footer {visibility: hidden;}
|
| 547 |
'''
|
| 548 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 549 |
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, delete_cache=(60, 3600)) as app:
|
| 550 |
loras_state = gr.State(loras)
|
| 551 |
selected_indices = gr.State([])
|
| 552 |
|
| 553 |
+
gr.Markdown(
|
| 554 |
+
"""
|
| 555 |
+
# MixGen3: 멀티 Lora(이미지 학습) 통합 생성 모델
|
| 556 |
+
|
| 557 |
+
### 사용 안내:
|
| 558 |
+
1) 갤러리에서 원하는 모델을 선택(최대 3개까지)
|
| 559 |
+
2) 프롬프트에 한글 또는 영문으로 원하는 내용을 입력
|
| 560 |
+
3) Generate 버튼 실행
|
| 561 |
+
|
| 562 |
+
### Contacts: [email protected]
|
| 563 |
+
"""
|
| 564 |
+
)
|
| 565 |
+
|
| 566 |
with gr.Row():
|
| 567 |
with gr.Column(scale=3):
|
| 568 |
prompt = gr.Textbox(label="Prompt", lines=1, placeholder="Type a prompt after selecting a LoRA")
|
| 569 |
with gr.Column(scale=1):
|
| 570 |
generate_button = gr.Button("Generate", variant="primary", elem_classes=["button_total"])
|
| 571 |
+
|
| 572 |
+
|
| 573 |
+
|
| 574 |
+
|
| 575 |
with gr.Row(elem_id="loaded_loras"):
|
| 576 |
with gr.Column(scale=1, min_width=25):
|
| 577 |
randomize_button = gr.Button("🎲", variant="secondary", scale=1, elem_id="random_btn")
|
|
|
|
| 585 |
lora_scale_1 = gr.Slider(label="LoRA 1 Scale", minimum=0, maximum=3, step=0.01, value=1.15)
|
| 586 |
with gr.Row():
|
| 587 |
remove_button_1 = gr.Button("Remove", size="sm")
|
| 588 |
+
|
| 589 |
with gr.Column(scale=8):
|
| 590 |
with gr.Row():
|
| 591 |
with gr.Column(scale=0, min_width=50):
|
|
|
|
| 596 |
lora_scale_2 = gr.Slider(label="LoRA 2 Scale", minimum=0, maximum=3, step=0.01, value=1.15)
|
| 597 |
with gr.Row():
|
| 598 |
remove_button_2 = gr.Button("Remove", size="sm")
|
| 599 |
+
|
| 600 |
+
with gr.Column(scale=8):
|
| 601 |
+
with gr.Row():
|
| 602 |
+
with gr.Column(scale=0, min_width=50):
|
| 603 |
+
lora_image_3 = gr.Image(label="LoRA 3 Image", interactive=False, min_width=50, width=50, show_label=False, show_share_button=False, show_download_button=False, show_fullscreen_button=False, height=50)
|
| 604 |
+
with gr.Column(scale=3, min_width=100):
|
| 605 |
+
selected_info_3 = gr.Markdown("Select a LoRA 3")
|
| 606 |
+
with gr.Column(scale=5, min_width=50):
|
| 607 |
+
lora_scale_3 = gr.Slider(label="LoRA 3 Scale", minimum=0, maximum=3, step=0.01, value=1.15)
|
| 608 |
+
with gr.Row():
|
| 609 |
+
remove_button_3 = gr.Button("Remove", size="sm")
|
| 610 |
|
| 611 |
with gr.Row():
|
| 612 |
with gr.Column():
|
|
|
|
| 645 |
randomize_seed = gr.Checkbox(True, label="Randomize seed")
|
| 646 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
|
| 647 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 648 |
gallery.select(
|
| 649 |
update_selection,
|
| 650 |
inputs=[selected_indices, loras_state, width, height],
|
| 651 |
+
outputs=[prompt, selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, width, height, lora_image_1, lora_image_2, lora_image_3]
|
| 652 |
)
|
| 653 |
+
|
| 654 |
remove_button_1.click(
|
| 655 |
remove_lora_1,
|
| 656 |
inputs=[selected_indices, loras_state],
|
| 657 |
+
outputs=[selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, lora_image_1, lora_image_2, lora_image_3]
|
| 658 |
)
|
| 659 |
+
|
| 660 |
remove_button_2.click(
|
| 661 |
remove_lora_2,
|
| 662 |
inputs=[selected_indices, loras_state],
|
| 663 |
+
outputs=[selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, lora_image_1, lora_image_2, lora_image_3]
|
| 664 |
)
|
| 665 |
+
|
| 666 |
+
remove_button_3.click(
|
| 667 |
+
remove_lora_3,
|
| 668 |
+
inputs=[selected_indices, loras_state],
|
| 669 |
+
outputs=[selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, lora_image_1, lora_image_2, lora_image_3]
|
| 670 |
+
)
|
| 671 |
+
|
| 672 |
randomize_button.click(
|
| 673 |
randomize_loras,
|
| 674 |
inputs=[selected_indices, loras_state],
|
| 675 |
+
outputs=[selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, lora_image_1, lora_image_2, lora_image_3, prompt]
|
| 676 |
)
|
| 677 |
+
|
| 678 |
add_custom_lora_button.click(
|
| 679 |
add_custom_lora,
|
| 680 |
inputs=[custom_lora, selected_indices, loras_state],
|
| 681 |
+
outputs=[loras_state, gallery, selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, lora_image_1, lora_image_2, lora_image_3]
|
| 682 |
)
|
| 683 |
+
|
| 684 |
remove_custom_lora_button.click(
|
| 685 |
remove_custom_lora,
|
| 686 |
inputs=[selected_indices, loras_state],
|
| 687 |
+
outputs=[loras_state, gallery, selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, lora_image_1, lora_image_2, lora_image_3]
|
| 688 |
)
|
| 689 |
|
| 690 |
gr.on(
|
| 691 |
triggers=[generate_button.click, prompt.submit],
|
| 692 |
fn=run_lora,
|
| 693 |
+
inputs=[prompt, input_image, image_strength, cfg_scale, steps, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, randomize_seed, seed, width, height, loras_state],
|
| 694 |
outputs=[result, seed, progress_bar]
|
| 695 |
).then(
|
| 696 |
fn=lambda x, history: update_history(x, history) if x is not None else history,
|
|
|
|
| 698 |
outputs=history_gallery,
|
| 699 |
)
|
| 700 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 701 |
if __name__ == "__main__":
|
| 702 |
app.queue(max_size=20)
|
| 703 |
app.launch(debug=True)
|