Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,14 +7,7 @@ import torch
|
|
| 7 |
import random
|
| 8 |
from PIL import Image
|
| 9 |
from typing import Iterable
|
| 10 |
-
|
| 11 |
-
from diffusers import DiffusionPipeline
|
| 12 |
from diffusers import Flux2KleinPipeline
|
| 13 |
-
from diffusers.utils import load_image
|
| 14 |
-
from huggingface_hub import hf_hub_download
|
| 15 |
-
|
| 16 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
-
|
| 18 |
from gradio.themes import Soft
|
| 19 |
from gradio.themes.utils import colors, fonts, sizes
|
| 20 |
|
|
@@ -60,9 +53,10 @@ class OrangeRedTheme(Soft):
|
|
| 60 |
)
|
| 61 |
|
| 62 |
orange_red_theme = OrangeRedTheme()
|
|
|
|
| 63 |
MAX_SEED = np.iinfo(np.int32).max
|
| 64 |
|
| 65 |
-
# Face Swap Prompt Template
|
| 66 |
FACE_SWAP_PROMPT = """head_swap: start with Picture 1 as the base image, keeping its lighting, environment, and background. Remove the head from Picture 1 completely and replace it with the head from Picture 2.
|
| 67 |
FROM PICTURE 1 (strictly preserve):
|
| 68 |
- Scene: lighting conditions, shadows, highlights, color temperature, environment, background
|
|
@@ -121,33 +115,26 @@ LORA_STYLES = [
|
|
| 121 |
LOADED_ADAPTERS = set()
|
| 122 |
|
| 123 |
print("Loading FLUX.2 Klein 9B model base...")
|
| 124 |
-
pipe =
|
| 125 |
"black-forest-labs/FLUX.2-klein-9B",
|
| 126 |
torch_dtype=torch.bfloat16,
|
| 127 |
-
).to(device)
|
| 128 |
print("Base Model loaded successfully.")
|
| 129 |
|
| 130 |
def update_dimensions_on_upload(image):
|
| 131 |
-
"""Resizes image to be divisible by 16 to avoid tensor mismatch errors in FLUX."""
|
| 132 |
if image is None:
|
| 133 |
return 1024, 1024
|
| 134 |
-
|
| 135 |
original_width, original_height = image.size
|
| 136 |
-
|
| 137 |
scale = min(1024 / original_width, 1024 / original_height)
|
| 138 |
new_width = int(original_width * scale)
|
| 139 |
new_height = int(original_height * scale)
|
| 140 |
-
|
| 141 |
new_width = (new_width // 16) * 16
|
| 142 |
new_height = (new_height // 16) * 16
|
| 143 |
-
|
| 144 |
return new_width, new_height
|
| 145 |
|
| 146 |
def process_gallery_images(images):
|
| 147 |
-
"""Process images from gallery input and return list of PIL images."""
|
| 148 |
if not images:
|
| 149 |
return []
|
| 150 |
-
|
| 151 |
pil_images = []
|
| 152 |
for item in images:
|
| 153 |
try:
|
|
@@ -155,7 +142,6 @@ def process_gallery_images(images):
|
|
| 155 |
path_or_img = item[0]
|
| 156 |
else:
|
| 157 |
path_or_img = item
|
| 158 |
-
|
| 159 |
if isinstance(path_or_img, str):
|
| 160 |
pil_images.append(Image.open(path_or_img).convert("RGB"))
|
| 161 |
elif isinstance(path_or_img, Image.Image):
|
|
@@ -165,49 +151,31 @@ def process_gallery_images(images):
|
|
| 165 |
except Exception as e:
|
| 166 |
print(f"Skipping invalid image item: {e}")
|
| 167 |
continue
|
| 168 |
-
|
| 169 |
return pil_images
|
| 170 |
|
| 171 |
def get_style_by_name(name):
|
| 172 |
-
"""Retrieve the style dictionary by its title."""
|
| 173 |
for style in LORA_STYLES:
|
| 174 |
if style["title"] == name:
|
| 175 |
return style
|
| 176 |
-
return LORA_STYLES[0]
|
| 177 |
|
| 178 |
def update_style_selection(evt: gr.SelectData):
|
| 179 |
-
"""Update selected style based on gallery click."""
|
| 180 |
selected_style = LORA_STYLES[evt.index]
|
| 181 |
default_prompt = selected_style.get("default_prompt", None)
|
| 182 |
-
# Return the title string and optional prompt update
|
| 183 |
return selected_style["title"], default_prompt if default_prompt else gr.update()
|
| 184 |
|
| 185 |
def update_style_info(style_name):
|
| 186 |
-
"""Update the info text based on the selected style name."""
|
| 187 |
return f"### Selected: {style_name} ✅"
|
| 188 |
|
| 189 |
-
def get_image_count_info(images):
|
| 190 |
-
"""Return info about uploaded images"""
|
| 191 |
-
if not images:
|
| 192 |
-
return "📷 No images uploaded"
|
| 193 |
-
|
| 194 |
-
count = len(images)
|
| 195 |
-
if count == 1:
|
| 196 |
-
return "📷 1 image uploaded (Picture 1 - Base)"
|
| 197 |
-
elif count == 2:
|
| 198 |
-
return "📷 2 images uploaded (Picture 1 - Base, Picture 2 - Face Source)"
|
| 199 |
-
else:
|
| 200 |
-
return f"📷 {count} images uploaded"
|
| 201 |
-
|
| 202 |
@spaces.GPU
|
| 203 |
def infer(
|
| 204 |
-
input_images,
|
| 205 |
-
prompt,
|
| 206 |
style_name,
|
| 207 |
-
seed=42,
|
| 208 |
-
randomize_seed=True,
|
| 209 |
-
guidance_scale=1.0,
|
| 210 |
-
steps=4,
|
| 211 |
progress=gr.Progress(track_tqdm=True)
|
| 212 |
):
|
| 213 |
gc.collect()
|
|
@@ -216,23 +184,19 @@ def infer(
|
|
| 216 |
if not input_images:
|
| 217 |
raise gr.Error("Please upload at least one image to apply a style to.")
|
| 218 |
|
| 219 |
-
# Process gallery images
|
| 220 |
pil_images = process_gallery_images(input_images)
|
| 221 |
-
|
| 222 |
if not pil_images:
|
| 223 |
raise gr.Error("Could not process uploaded images.")
|
| 224 |
|
| 225 |
-
# Find the selected style configuration
|
| 226 |
selected_style = get_style_by_name(style_name)
|
| 227 |
-
|
| 228 |
-
# Check if Face Swap is selected and validate image count
|
| 229 |
if selected_style["adapter_name"] == "face-swap":
|
| 230 |
if len(pil_images) < 2:
|
| 231 |
-
raise gr.Error("Face Swap requires exactly 2 images: Picture 1 (base/body) and Picture 2 (face source).
|
| 232 |
elif len(pil_images) > 2:
|
| 233 |
-
gr.Warning("Face Swap uses only the first 2 images.
|
| 234 |
pil_images = pil_images[:2]
|
| 235 |
-
|
| 236 |
if selected_style["adapter_name"] is None:
|
| 237 |
print("Selection is None. Disabling LoRA adapters.")
|
| 238 |
pipe.disable_lora()
|
|
@@ -242,48 +206,46 @@ def infer(
|
|
| 242 |
if adapter_name not in LOADED_ADAPTERS:
|
| 243 |
print(f"--- Downloading and Loading Adapter: {selected_style['title']} ---")
|
| 244 |
try:
|
|
|
|
|
|
|
| 245 |
pipe.load_lora_weights(
|
| 246 |
-
selected_style["repo"],
|
| 247 |
-
weight_name=selected_style["weights"],
|
| 248 |
-
adapter_name=adapter_name
|
|
|
|
| 249 |
)
|
| 250 |
LOADED_ADAPTERS.add(adapter_name)
|
| 251 |
except Exception as e:
|
| 252 |
raise gr.Error(f"Failed to load adapter {selected_style['title']}: {e}")
|
| 253 |
else:
|
| 254 |
print(f"--- Adapter {selected_style['title']} is already loaded. ---")
|
| 255 |
-
|
| 256 |
print(f"Activating LoRA: {adapter_name}")
|
| 257 |
pipe.set_adapters([adapter_name], adapter_weights=[1.0])
|
| 258 |
|
| 259 |
if randomize_seed:
|
| 260 |
seed = random.randint(0, MAX_SEED)
|
| 261 |
-
|
| 262 |
-
# Get dimensions from first image
|
| 263 |
width, height = update_dimensions_on_upload(pil_images[0])
|
| 264 |
-
|
| 265 |
-
# Process all images to the same dimensions
|
| 266 |
processed_images = [
|
| 267 |
-
img.resize((width, height), Image.LANCZOS).convert("RGB")
|
| 268 |
for img in pil_images
|
| 269 |
]
|
| 270 |
-
|
| 271 |
try:
|
| 272 |
-
# Pass single image or list based on count
|
| 273 |
image_input = processed_images if len(processed_images) > 1 else processed_images[0]
|
| 274 |
-
|
| 275 |
image = pipe(
|
| 276 |
-
image=image_input,
|
| 277 |
prompt=prompt,
|
| 278 |
guidance_scale=guidance_scale,
|
| 279 |
width=width,
|
| 280 |
height=height,
|
| 281 |
num_inference_steps=steps,
|
| 282 |
-
generator=torch.Generator(device=device).manual_seed(seed),
|
| 283 |
).images[0]
|
| 284 |
-
|
| 285 |
return image, seed
|
| 286 |
-
|
| 287 |
except Exception as e:
|
| 288 |
raise gr.Error(f"Inference failed: {e}")
|
| 289 |
finally:
|
|
@@ -292,18 +254,15 @@ def infer(
|
|
| 292 |
|
| 293 |
@spaces.GPU
|
| 294 |
def infer_example(input_images, prompt, style_name):
|
| 295 |
-
if not input_images:
|
| 296 |
return None, 0
|
| 297 |
-
|
| 298 |
-
# Handle examples where inputs might be paths
|
| 299 |
if isinstance(input_images, str):
|
| 300 |
input_images = [input_images]
|
| 301 |
-
|
| 302 |
image, seed = infer(
|
| 303 |
-
input_images=input_images,
|
| 304 |
-
prompt=prompt,
|
| 305 |
-
style_name=style_name,
|
| 306 |
-
seed=0,
|
| 307 |
randomize_seed=True,
|
| 308 |
guidance_scale=1.0,
|
| 309 |
steps=4
|
|
@@ -317,47 +276,44 @@ css = """
|
|
| 317 |
#input_gallery .grid-wrap { min-height: 200px }
|
| 318 |
"""
|
| 319 |
|
| 320 |
-
with gr.Blocks() as demo:
|
| 321 |
with gr.Column(elem_id="col-container"):
|
| 322 |
gr.Markdown("# **FLUX.2-Klein-LoRA-Studio**", elem_id="main-title")
|
| 323 |
-
gr.Markdown("Perform diverse image edits using specialized
|
| 324 |
-
|
| 325 |
selected_style_name = gr.Textbox(value="None", visible=False, label="Selected Style Name")
|
| 326 |
-
|
| 327 |
with gr.Row(equal_height=True):
|
| 328 |
with gr.Column():
|
| 329 |
input_images = gr.Gallery(
|
| 330 |
-
label="Upload Images",
|
| 331 |
-
type="filepath",
|
| 332 |
-
columns=2,
|
| 333 |
-
rows=1,
|
| 334 |
height=290,
|
| 335 |
allow_preview=True,
|
| 336 |
elem_id="input_gallery"
|
| 337 |
)
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
)
|
| 346 |
-
|
| 347 |
run_button = gr.Button("Apply Style", variant="primary")
|
| 348 |
-
|
| 349 |
with gr.Accordion("Advanced Settings", open=False, visible=False):
|
| 350 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 351 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 352 |
-
guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.0, maximum=10.0, step=0.1, value=1.0)
|
| 353 |
steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=4, step=1)
|
| 354 |
-
|
| 355 |
with gr.Column():
|
| 356 |
output_image = gr.Image(label="Output Image", interactive=False, format="png", height=358)
|
| 357 |
used_seed = gr.Textbox(label="Used Seed", interactive=False, visible=False)
|
| 358 |
|
| 359 |
selected_style_info = gr.Markdown("### Selected: None (FLUX.2-klein-9B) ✅")
|
| 360 |
-
|
| 361 |
style_gallery = gr.Gallery(
|
| 362 |
[(item["image"], item["title"]) for item in LORA_STYLES],
|
| 363 |
label="Edit Style Gallery",
|
|
@@ -365,27 +321,27 @@ with gr.Blocks() as demo:
|
|
| 365 |
columns=3,
|
| 366 |
elem_id="style_gallery",
|
| 367 |
)
|
| 368 |
-
|
| 369 |
gr.Examples(
|
| 370 |
examples=[
|
| 371 |
[
|
| 372 |
-
["examples/2.jpg"],
|
| 373 |
-
"Relight the image to remove all existing lighting conditions and replace them with neutral, uniform illumination.
|
| 374 |
"Klein-Delight-Style"
|
| 375 |
],
|
| 376 |
[
|
| 377 |
-
["examples/1.jpg", "examples/2.jpg"],
|
| 378 |
-
FACE_SWAP_PROMPT,
|
| 379 |
"Best-Face-Swap"
|
| 380 |
],
|
| 381 |
[
|
| 382 |
-
["examples/1.jpg"],
|
| 383 |
-
"cinematic polaroid with soft grain subtle vignette gentle lighting white frame handwritten photographed by prithivMLmods preserving realistic texture and details",
|
| 384 |
"None"
|
| 385 |
],
|
| 386 |
[
|
| 387 |
-
["examples/cloth.jpg"],
|
| 388 |
-
"3Dghostmannequin",
|
| 389 |
"Ghost-Mannequin"
|
| 390 |
],
|
| 391 |
],
|
|
@@ -395,25 +351,23 @@ with gr.Blocks() as demo:
|
|
| 395 |
cache_examples=False,
|
| 396 |
label="Examples"
|
| 397 |
)
|
| 398 |
-
|
| 399 |
-
gr.Markdown("[*]
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
style_gallery.select(
|
| 407 |
fn=update_style_selection,
|
| 408 |
outputs=[selected_style_name, prompt]
|
| 409 |
)
|
| 410 |
-
|
| 411 |
selected_style_name.change(
|
| 412 |
fn=update_style_info,
|
| 413 |
inputs=[selected_style_name],
|
| 414 |
outputs=[selected_style_info]
|
| 415 |
)
|
| 416 |
-
|
| 417 |
run_button.click(
|
| 418 |
fn=infer,
|
| 419 |
inputs=[input_images, prompt, selected_style_name, seed, randomize_seed, guidance_scale, steps],
|
|
@@ -421,4 +375,10 @@ with gr.Blocks() as demo:
|
|
| 421 |
)
|
| 422 |
|
| 423 |
if __name__ == "__main__":
|
| 424 |
-
demo.queue().launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import random
|
| 8 |
from PIL import Image
|
| 9 |
from typing import Iterable
|
|
|
|
|
|
|
| 10 |
from diffusers import Flux2KleinPipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
from gradio.themes import Soft
|
| 12 |
from gradio.themes.utils import colors, fonts, sizes
|
| 13 |
|
|
|
|
| 53 |
)
|
| 54 |
|
| 55 |
orange_red_theme = OrangeRedTheme()
|
| 56 |
+
|
| 57 |
MAX_SEED = np.iinfo(np.int32).max
|
| 58 |
|
| 59 |
+
# Face Swap Prompt Template (unchanged)
|
| 60 |
FACE_SWAP_PROMPT = """head_swap: start with Picture 1 as the base image, keeping its lighting, environment, and background. Remove the head from Picture 1 completely and replace it with the head from Picture 2.
|
| 61 |
FROM PICTURE 1 (strictly preserve):
|
| 62 |
- Scene: lighting conditions, shadows, highlights, color temperature, environment, background
|
|
|
|
| 115 |
LOADED_ADAPTERS = set()
|
| 116 |
|
| 117 |
print("Loading FLUX.2 Klein 9B model base...")
|
| 118 |
+
pipe = Flux2KleinPipeline.from_pretrained(
|
| 119 |
"black-forest-labs/FLUX.2-klein-9B",
|
| 120 |
torch_dtype=torch.bfloat16,
|
| 121 |
+
).to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))
|
| 122 |
print("Base Model loaded successfully.")
|
| 123 |
|
| 124 |
def update_dimensions_on_upload(image):
|
|
|
|
| 125 |
if image is None:
|
| 126 |
return 1024, 1024
|
|
|
|
| 127 |
original_width, original_height = image.size
|
|
|
|
| 128 |
scale = min(1024 / original_width, 1024 / original_height)
|
| 129 |
new_width = int(original_width * scale)
|
| 130 |
new_height = int(original_height * scale)
|
|
|
|
| 131 |
new_width = (new_width // 16) * 16
|
| 132 |
new_height = (new_height // 16) * 16
|
|
|
|
| 133 |
return new_width, new_height
|
| 134 |
|
| 135 |
def process_gallery_images(images):
|
|
|
|
| 136 |
if not images:
|
| 137 |
return []
|
|
|
|
| 138 |
pil_images = []
|
| 139 |
for item in images:
|
| 140 |
try:
|
|
|
|
| 142 |
path_or_img = item[0]
|
| 143 |
else:
|
| 144 |
path_or_img = item
|
|
|
|
| 145 |
if isinstance(path_or_img, str):
|
| 146 |
pil_images.append(Image.open(path_or_img).convert("RGB"))
|
| 147 |
elif isinstance(path_or_img, Image.Image):
|
|
|
|
| 151 |
except Exception as e:
|
| 152 |
print(f"Skipping invalid image item: {e}")
|
| 153 |
continue
|
|
|
|
| 154 |
return pil_images
|
| 155 |
|
| 156 |
def get_style_by_name(name):
|
|
|
|
| 157 |
for style in LORA_STYLES:
|
| 158 |
if style["title"] == name:
|
| 159 |
return style
|
| 160 |
+
return LORA_STYLES[0]
|
| 161 |
|
| 162 |
def update_style_selection(evt: gr.SelectData):
|
|
|
|
| 163 |
selected_style = LORA_STYLES[evt.index]
|
| 164 |
default_prompt = selected_style.get("default_prompt", None)
|
|
|
|
| 165 |
return selected_style["title"], default_prompt if default_prompt else gr.update()
|
| 166 |
|
| 167 |
def update_style_info(style_name):
|
|
|
|
| 168 |
return f"### Selected: {style_name} ✅"
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
@spaces.GPU
|
| 171 |
def infer(
|
| 172 |
+
input_images,
|
| 173 |
+
prompt,
|
| 174 |
style_name,
|
| 175 |
+
seed=42,
|
| 176 |
+
randomize_seed=True,
|
| 177 |
+
guidance_scale=1.0,
|
| 178 |
+
steps=4,
|
| 179 |
progress=gr.Progress(track_tqdm=True)
|
| 180 |
):
|
| 181 |
gc.collect()
|
|
|
|
| 184 |
if not input_images:
|
| 185 |
raise gr.Error("Please upload at least one image to apply a style to.")
|
| 186 |
|
|
|
|
| 187 |
pil_images = process_gallery_images(input_images)
|
|
|
|
| 188 |
if not pil_images:
|
| 189 |
raise gr.Error("Could not process uploaded images.")
|
| 190 |
|
|
|
|
| 191 |
selected_style = get_style_by_name(style_name)
|
| 192 |
+
|
|
|
|
| 193 |
if selected_style["adapter_name"] == "face-swap":
|
| 194 |
if len(pil_images) < 2:
|
| 195 |
+
raise gr.Error("Face Swap requires exactly 2 images: Picture 1 (base/body) and Picture 2 (face source).")
|
| 196 |
elif len(pil_images) > 2:
|
| 197 |
+
gr.Warning("Face Swap uses only the first 2 images.")
|
| 198 |
pil_images = pil_images[:2]
|
| 199 |
+
|
| 200 |
if selected_style["adapter_name"] is None:
|
| 201 |
print("Selection is None. Disabling LoRA adapters.")
|
| 202 |
pipe.disable_lora()
|
|
|
|
| 206 |
if adapter_name not in LOADED_ADAPTERS:
|
| 207 |
print(f"--- Downloading and Loading Adapter: {selected_style['title']} ---")
|
| 208 |
try:
|
| 209 |
+
# ==================== FIX 1 ====================
|
| 210 |
+
# prefix=None fixes Object-Remover (and many other community FLUX.2-Klein LoRAs)
|
| 211 |
pipe.load_lora_weights(
|
| 212 |
+
selected_style["repo"],
|
| 213 |
+
weight_name=selected_style["weights"],
|
| 214 |
+
adapter_name=adapter_name,
|
| 215 |
+
prefix=None
|
| 216 |
)
|
| 217 |
LOADED_ADAPTERS.add(adapter_name)
|
| 218 |
except Exception as e:
|
| 219 |
raise gr.Error(f"Failed to load adapter {selected_style['title']}: {e}")
|
| 220 |
else:
|
| 221 |
print(f"--- Adapter {selected_style['title']} is already loaded. ---")
|
| 222 |
+
|
| 223 |
print(f"Activating LoRA: {adapter_name}")
|
| 224 |
pipe.set_adapters([adapter_name], adapter_weights=[1.0])
|
| 225 |
|
| 226 |
if randomize_seed:
|
| 227 |
seed = random.randint(0, MAX_SEED)
|
| 228 |
+
|
|
|
|
| 229 |
width, height = update_dimensions_on_upload(pil_images[0])
|
|
|
|
|
|
|
| 230 |
processed_images = [
|
| 231 |
+
img.resize((width, height), Image.LANCZOS).convert("RGB")
|
| 232 |
for img in pil_images
|
| 233 |
]
|
| 234 |
+
|
| 235 |
try:
|
|
|
|
| 236 |
image_input = processed_images if len(processed_images) > 1 else processed_images[0]
|
| 237 |
+
|
| 238 |
image = pipe(
|
| 239 |
+
image=image_input,
|
| 240 |
prompt=prompt,
|
| 241 |
guidance_scale=guidance_scale,
|
| 242 |
width=width,
|
| 243 |
height=height,
|
| 244 |
num_inference_steps=steps,
|
| 245 |
+
generator=torch.Generator(device=pipe.device).manual_seed(seed),
|
| 246 |
).images[0]
|
| 247 |
+
|
| 248 |
return image, seed
|
|
|
|
| 249 |
except Exception as e:
|
| 250 |
raise gr.Error(f"Inference failed: {e}")
|
| 251 |
finally:
|
|
|
|
| 254 |
|
| 255 |
@spaces.GPU
|
| 256 |
def infer_example(input_images, prompt, style_name):
|
| 257 |
+
if not input_images:
|
| 258 |
return None, 0
|
|
|
|
|
|
|
| 259 |
if isinstance(input_images, str):
|
| 260 |
input_images = [input_images]
|
|
|
|
| 261 |
image, seed = infer(
|
| 262 |
+
input_images=input_images,
|
| 263 |
+
prompt=prompt,
|
| 264 |
+
style_name=style_name,
|
| 265 |
+
seed=0,
|
| 266 |
randomize_seed=True,
|
| 267 |
guidance_scale=1.0,
|
| 268 |
steps=4
|
|
|
|
| 276 |
#input_gallery .grid-wrap { min-height: 200px }
|
| 277 |
"""
|
| 278 |
|
| 279 |
+
with gr.Blocks(theme=orange_red_theme, css=css) as demo:
|
| 280 |
with gr.Column(elem_id="col-container"):
|
| 281 |
gr.Markdown("# **FLUX.2-Klein-LoRA-Studio**", elem_id="main-title")
|
| 282 |
+
gr.Markdown("Perform diverse image edits using specialized LoRAs for the FLUX.2-Klein-9B model.")
|
| 283 |
+
|
| 284 |
selected_style_name = gr.Textbox(value="None", visible=False, label="Selected Style Name")
|
| 285 |
+
|
| 286 |
with gr.Row(equal_height=True):
|
| 287 |
with gr.Column():
|
| 288 |
input_images = gr.Gallery(
|
| 289 |
+
label="Upload Images",
|
| 290 |
+
type="filepath",
|
| 291 |
+
columns=2,
|
| 292 |
+
rows=1,
|
| 293 |
height=290,
|
| 294 |
allow_preview=True,
|
| 295 |
elem_id="input_gallery"
|
| 296 |
)
|
| 297 |
+
|
| 298 |
+
prompt = gr.Text(
|
| 299 |
+
label="Edit Prompt",
|
| 300 |
+
max_lines=2,
|
| 301 |
+
placeholder="e.g., a man with a red superhero mask"
|
| 302 |
+
)
|
| 303 |
+
|
|
|
|
|
|
|
| 304 |
run_button = gr.Button("Apply Style", variant="primary")
|
|
|
|
| 305 |
with gr.Accordion("Advanced Settings", open=False, visible=False):
|
| 306 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 307 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 308 |
+
guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.0, maximum=10.0, step=0.1, value=1.0)
|
| 309 |
steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=4, step=1)
|
| 310 |
+
|
| 311 |
with gr.Column():
|
| 312 |
output_image = gr.Image(label="Output Image", interactive=False, format="png", height=358)
|
| 313 |
used_seed = gr.Textbox(label="Used Seed", interactive=False, visible=False)
|
| 314 |
|
| 315 |
selected_style_info = gr.Markdown("### Selected: None (FLUX.2-klein-9B) ✅")
|
| 316 |
+
|
| 317 |
style_gallery = gr.Gallery(
|
| 318 |
[(item["image"], item["title"]) for item in LORA_STYLES],
|
| 319 |
label="Edit Style Gallery",
|
|
|
|
| 321 |
columns=3,
|
| 322 |
elem_id="style_gallery",
|
| 323 |
)
|
| 324 |
+
|
| 325 |
gr.Examples(
|
| 326 |
examples=[
|
| 327 |
[
|
| 328 |
+
["examples/2.jpg"],
|
| 329 |
+
"Relight the image to remove all existing lighting conditions and replace them with neutral, uniform illumination...",
|
| 330 |
"Klein-Delight-Style"
|
| 331 |
],
|
| 332 |
[
|
| 333 |
+
["examples/1.jpg", "examples/2.jpg"],
|
| 334 |
+
FACE_SWAP_PROMPT,
|
| 335 |
"Best-Face-Swap"
|
| 336 |
],
|
| 337 |
[
|
| 338 |
+
["examples/1.jpg"],
|
| 339 |
+
"cinematic polaroid with soft grain subtle vignette gentle lighting white frame handwritten photographed by prithivMLmods preserving realistic texture and details",
|
| 340 |
"None"
|
| 341 |
],
|
| 342 |
[
|
| 343 |
+
["examples/cloth.jpg"],
|
| 344 |
+
"3Dghostmannequin",
|
| 345 |
"Ghost-Mannequin"
|
| 346 |
],
|
| 347 |
],
|
|
|
|
| 351 |
cache_examples=False,
|
| 352 |
label="Examples"
|
| 353 |
)
|
| 354 |
+
|
| 355 |
+
gr.Markdown("[*] Experimental Space for FLUX.2-Klein-9B. More adapters coming soon.")
|
| 356 |
+
|
| 357 |
+
# ==================== FIX 2 ====================
|
| 358 |
+
# Removed the old input_images.change() that caused the Gradio warning
|
| 359 |
+
# (the function returned a value but had no outputs=)
|
| 360 |
+
|
|
|
|
| 361 |
style_gallery.select(
|
| 362 |
fn=update_style_selection,
|
| 363 |
outputs=[selected_style_name, prompt]
|
| 364 |
)
|
|
|
|
| 365 |
selected_style_name.change(
|
| 366 |
fn=update_style_info,
|
| 367 |
inputs=[selected_style_name],
|
| 368 |
outputs=[selected_style_info]
|
| 369 |
)
|
| 370 |
+
|
| 371 |
run_button.click(
|
| 372 |
fn=infer,
|
| 373 |
inputs=[input_images, prompt, selected_style_name, seed, randomize_seed, guidance_scale, steps],
|
|
|
|
| 375 |
)
|
| 376 |
|
| 377 |
if __name__ == "__main__":
|
| 378 |
+
demo.queue().launch(
|
| 379 |
+
css=css,
|
| 380 |
+
theme=orange_red_theme,
|
| 381 |
+
mcp_server=True,
|
| 382 |
+
ssr_mode=False,
|
| 383 |
+
show_error=True
|
| 384 |
+
)
|