Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,363 +1,145 @@
|
|
| 1 |
import os
|
| 2 |
-
import subprocess
|
| 3 |
-
import sys
|
| 4 |
-
import io
|
| 5 |
import gradio as gr
|
| 6 |
import numpy as np
|
| 7 |
import random
|
| 8 |
import spaces
|
| 9 |
import torch
|
| 10 |
-
from diffusers import
|
| 11 |
-
import
|
| 12 |
-
|
| 13 |
-
import json
|
| 14 |
import base64
|
| 15 |
-
from huggingface_hub import InferenceClient
|
| 16 |
|
|
|
|
|
|
|
| 17 |
dtype = torch.bfloat16
|
| 18 |
-
device = "
|
| 19 |
-
|
| 20 |
-
MAX_SEED = np.iinfo(np.int32).max
|
| 21 |
-
MAX_IMAGE_SIZE = 1024
|
| 22 |
-
|
| 23 |
-
hf_client = InferenceClient(
|
| 24 |
-
api_key=os.environ.get("HF_TOKEN"),
|
| 25 |
-
)
|
| 26 |
-
VLM_MODEL = "baidu/ERNIE-4.5-VL-424B-A47B-Base-PT"
|
| 27 |
-
|
| 28 |
-
SYSTEM_PROMPT_TEXT_ONLY = """You are an expert prompt engineer for FLUX.2 by Black Forest Labs. Rewrite user prompts to be more descriptive while strictly preserving their core subject and intent.
|
| 29 |
-
|
| 30 |
-
Guidelines:
|
| 31 |
-
1. Structure: Keep structured inputs structured (enhance within fields). Convert natural language to detailed paragraphs.
|
| 32 |
-
2. Details: Add concrete visual specifics - form, scale, textures, materials, lighting (quality, direction, color), shadows, spatial relationships, and environmental context.
|
| 33 |
-
3. Text in Images: Put ALL text in quotation marks, matching the prompt's language. Always provide explicit quoted text for objects that would contain text in reality (signs, labels, screens, etc.) - without it, the model generates gibberish.
|
| 34 |
-
|
| 35 |
-
Output only the revised prompt and nothing else."""
|
| 36 |
-
|
| 37 |
-
SYSTEM_PROMPT_WITH_IMAGES = """You are FLUX.2 by Black Forest Labs, an image-editing expert. You convert editing requests into one concise instruction (50-80 words, ~30 for brief requests).
|
| 38 |
-
|
| 39 |
-
Rules:
|
| 40 |
-
- Single instruction only, no commentary
|
| 41 |
-
- Use clear, analytical language (avoid "whimsical," "cascading," etc.)
|
| 42 |
-
- Specify what changes AND what stays the same (face, lighting, composition)
|
| 43 |
-
- Reference actual image elements
|
| 44 |
-
- Turn negatives into positives ("don't change X" → "keep X")
|
| 45 |
-
- Make abstractions concrete ("futuristic" → "glowing cyan neon, metallic panels")
|
| 46 |
-
- Keep content PG-13
|
| 47 |
-
|
| 48 |
-
Output only the final instruction in plain text and nothing else."""
|
| 49 |
-
|
| 50 |
-
# Model repository IDs for 4B
|
| 51 |
-
REPO_ID_REGULAR = "black-forest-labs/FLUX.2-klein-base-4B"
|
| 52 |
-
REPO_ID_DISTILLED = "black-forest-labs/FLUX.2-klein-4B"
|
| 53 |
-
|
| 54 |
-
# Load both 4B models
|
| 55 |
-
print("Loading 4B Regular model...")
|
| 56 |
-
pipe_regular = Flux2KleinPipeline.from_pretrained(REPO_ID_REGULAR, torch_dtype=dtype)
|
| 57 |
-
pipe_regular.to("cuda")
|
| 58 |
-
|
| 59 |
-
print("Loading 4B Distilled model...")
|
| 60 |
-
pipe_distilled = Flux2KleinPipeline.from_pretrained(REPO_ID_DISTILLED, torch_dtype=dtype)
|
| 61 |
-
pipe_distilled.to("cuda")
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
"Base (50 steps)": pipe_regular,
|
| 67 |
-
}
|
| 68 |
|
| 69 |
-
|
| 70 |
-
DEFAULT_STEPS = {
|
| 71 |
-
"Distilled (4 steps)": 4,
|
| 72 |
-
"Base (50 steps)": 50,
|
| 73 |
-
}
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
def image_to_data_uri(img):
|
| 82 |
-
buffered = io.BytesIO()
|
| 83 |
-
img.save(buffered, format="PNG")
|
| 84 |
-
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 85 |
-
return f"data:image/png;base64,{img_str}"
|
| 86 |
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
try:
|
| 90 |
-
if image_list and len(image_list) > 0:
|
| 91 |
-
# Image + Text Editing Mode
|
| 92 |
-
system_content = SYSTEM_PROMPT_WITH_IMAGES
|
| 93 |
-
|
| 94 |
-
# Construct user message with text and images
|
| 95 |
-
user_content = [{"type": "text", "text": prompt}]
|
| 96 |
-
|
| 97 |
-
for img in image_list:
|
| 98 |
-
data_uri = image_to_data_uri(img)
|
| 99 |
-
user_content.append({
|
| 100 |
-
"type": "image_url",
|
| 101 |
-
"image_url": {"url": data_uri}
|
| 102 |
-
})
|
| 103 |
-
|
| 104 |
-
messages = [
|
| 105 |
-
{"role": "system", "content": system_content},
|
| 106 |
-
{"role": "user", "content": user_content}
|
| 107 |
-
]
|
| 108 |
-
else:
|
| 109 |
-
# Text Only Mode
|
| 110 |
-
system_content = SYSTEM_PROMPT_TEXT_ONLY
|
| 111 |
-
messages = [
|
| 112 |
-
{"role": "system", "content": system_content},
|
| 113 |
-
{"role": "user", "content": prompt}
|
| 114 |
-
]
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
if image_list is None or len(image_list) == 0:
|
| 132 |
-
return 1024, 1024 # Default dimensions
|
| 133 |
-
|
| 134 |
-
# Get the first image to determine dimensions
|
| 135 |
-
img = image_list[0][0] # Gallery returns list of tuples (image, caption)
|
| 136 |
-
img_width, img_height = img.size
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
if aspect_ratio >= 1: # Landscape or square
|
| 141 |
-
new_width = 1024
|
| 142 |
-
new_height = int(1024 / aspect_ratio)
|
| 143 |
-
else: # Portrait
|
| 144 |
-
new_height = 1024
|
| 145 |
-
new_width = int(1024 * aspect_ratio)
|
| 146 |
-
|
| 147 |
-
# Round to nearest multiple of 8
|
| 148 |
-
new_width = round(new_width / 8) * 8
|
| 149 |
-
new_height = round(new_height / 8) * 8
|
| 150 |
-
|
| 151 |
-
# Ensure within valid range (minimum 256, maximum 1024)
|
| 152 |
-
new_width = max(256, min(1024, new_width))
|
| 153 |
-
new_height = max(256, min(1024, new_height))
|
| 154 |
-
|
| 155 |
-
return new_width, new_height
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
def update_steps_from_mode(mode_choice):
|
| 159 |
-
"""Update the number of inference steps based on the selected mode."""
|
| 160 |
-
return DEFAULT_STEPS[mode_choice], DEFAULT_CFG[mode_choice]
|
| 161 |
-
|
| 162 |
|
| 163 |
-
@spaces.GPU(duration=85)
|
| 164 |
-
def infer(prompt, input_images=None, mode_choice="Distilled (4 steps)", seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, guidance_scale=4.0, prompt_upsampling=False, progress=gr.Progress(track_tqdm=True)):
|
| 165 |
-
|
| 166 |
if randomize_seed:
|
| 167 |
seed = random.randint(0, MAX_SEED)
|
| 168 |
-
|
| 169 |
-
#
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
#
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
# Add images if provided
|
| 202 |
-
if image_list is not None:
|
| 203 |
-
pipe_kwargs["image"] = image_list
|
| 204 |
-
|
| 205 |
-
image = pipe(**pipe_kwargs).images[0]
|
| 206 |
-
|
| 207 |
-
return image, seed
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
examples = [
|
| 211 |
-
["Create a vase on a table in living room, the color of the vase is a gradient of color, starting with #02eb3c color and finishing with #edfa3c. The flowers inside the vase have the color #ff0088"],
|
| 212 |
-
["Photorealistic infographic showing the complete Berlin TV Tower (Fernsehturm) from ground base to antenna tip, full vertical view with entire structure visible including concrete shaft, metallic sphere, and antenna spire. Slight upward perspective angle looking up toward the iconic sphere, perfectly centered on clean white background. Left side labels with thin horizontal connector lines: the text '368m' in extra large bold dark grey numerals (#2D3748) positioned at exactly the antenna tip with 'TOTAL HEIGHT' in small caps below. The text '207m' in extra large bold with 'TELECAFÉ' in small caps below, with connector line touching the sphere precisely at the window level. Right side label with horizontal connector line touching the sphere's equator: the text '32m' in extra large bold dark grey numerals with 'SPHERE DIAMETER' in small caps below. Bottom section arranged in three balanced columns: Left - Large text '986' in extra bold dark grey with 'STEPS' in caps below. Center - 'BERLIN TV TOWER' in bold caps with 'FERNSEHTURM' in lighter weight below. Right - 'INAUGURATED' in bold caps with 'OCTOBER 3, 1969' below. All typography in modern sans-serif font (such as Inter or Helvetica), color #2D3748, clean minimal technical diagram style. Horizontal connector lines are thin, precise, and clearly visible, touching the tower structure at exact corresponding measurement points. Professional architectural elevation drawing aesthetic with dynamic low angle perspective creating sense of height and grandeur, poster-ready infographic design with perfect visual hierarchy."],
|
| 213 |
-
["Soaking wet capybara taking shelter under a banana leaf in the rainy jungle, close up photo"],
|
| 214 |
-
["A kawaii die-cut sticker of a chubby orange cat, featuring big sparkly eyes and a happy smile with paws raised in greeting and a heart-shaped pink nose. The design should have smooth rounded lines with black outlines and soft gradient shading with pink cheeks."],
|
| 215 |
-
]
|
| 216 |
-
|
| 217 |
-
examples_images = [
|
| 218 |
-
["The person from image 1 is petting the cat from image 2, the bird from image 3 is next to them", ["woman1.webp", "cat_window.webp", "bird.webp"]]
|
| 219 |
-
]
|
| 220 |
-
|
| 221 |
css = """
|
| 222 |
-
#col-container {
|
| 223 |
-
margin: 0 auto;
|
| 224 |
-
max-width: 1200px;
|
| 225 |
-
}
|
| 226 |
-
.gallery-container img{
|
| 227 |
-
object-fit: contain;
|
| 228 |
-
}
|
| 229 |
"""
|
| 230 |
|
| 231 |
with gr.Blocks(css=css) as demo:
|
| 232 |
-
|
| 233 |
with gr.Column(elem_id="col-container"):
|
| 234 |
-
gr.Markdown(
|
| 235 |
-
|
| 236 |
-
|
| 237 |
with gr.Row():
|
| 238 |
with gr.Column():
|
|
|
|
|
|
|
| 239 |
with gr.Row():
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
max_lines=2,
|
| 244 |
-
placeholder="Enter your prompt",
|
| 245 |
-
container=False,
|
| 246 |
-
scale=3
|
| 247 |
-
)
|
| 248 |
-
|
| 249 |
-
run_button = gr.Button("Run", scale=1)
|
| 250 |
-
|
| 251 |
-
with gr.Accordion("Input image(s) (optional)", open=False):
|
| 252 |
-
input_images = gr.Gallery(
|
| 253 |
-
label="Input Image(s)",
|
| 254 |
-
type="pil",
|
| 255 |
-
columns=3,
|
| 256 |
-
rows=1,
|
| 257 |
-
)
|
| 258 |
-
|
| 259 |
-
mode_choice = gr.Radio(
|
| 260 |
-
label="Mode",
|
| 261 |
-
choices=["Distilled (4 steps)", "Base (50 steps)"],
|
| 262 |
-
value="Distilled (4 steps)",
|
| 263 |
-
)
|
| 264 |
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
info="Automatically enhance the prompt using a VLM"
|
| 271 |
-
)
|
| 272 |
-
|
| 273 |
-
seed = gr.Slider(
|
| 274 |
-
label="Seed",
|
| 275 |
-
minimum=0,
|
| 276 |
-
maximum=MAX_SEED,
|
| 277 |
-
step=1,
|
| 278 |
-
value=0,
|
| 279 |
-
)
|
| 280 |
-
|
| 281 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 282 |
-
|
| 283 |
-
with gr.Row():
|
| 284 |
-
|
| 285 |
-
width = gr.Slider(
|
| 286 |
-
label="Width",
|
| 287 |
-
minimum=256,
|
| 288 |
-
maximum=MAX_IMAGE_SIZE,
|
| 289 |
-
step=8,
|
| 290 |
-
value=1024,
|
| 291 |
-
)
|
| 292 |
-
|
| 293 |
-
height = gr.Slider(
|
| 294 |
-
label="Height",
|
| 295 |
-
minimum=256,
|
| 296 |
-
maximum=MAX_IMAGE_SIZE,
|
| 297 |
-
step=8,
|
| 298 |
-
value=1024,
|
| 299 |
-
)
|
| 300 |
-
|
| 301 |
-
with gr.Row():
|
| 302 |
-
|
| 303 |
-
num_inference_steps = gr.Slider(
|
| 304 |
-
label="Number of inference steps",
|
| 305 |
-
minimum=1,
|
| 306 |
-
maximum=100,
|
| 307 |
-
step=1,
|
| 308 |
-
value=4,
|
| 309 |
-
)
|
| 310 |
-
|
| 311 |
-
guidance_scale = gr.Slider(
|
| 312 |
-
label="Guidance scale",
|
| 313 |
-
minimum=0.0,
|
| 314 |
-
maximum=10.0,
|
| 315 |
-
step=0.1,
|
| 316 |
-
value=1.0,
|
| 317 |
-
)
|
| 318 |
|
|
|
|
| 319 |
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
inputs=[prompt],
|
| 328 |
-
outputs=[result, seed],
|
| 329 |
-
cache_examples=True,
|
| 330 |
-
cache_mode="lazy"
|
| 331 |
-
)
|
| 332 |
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
inputs=[prompt, input_images],
|
| 337 |
-
outputs=[result, seed],
|
| 338 |
-
cache_examples=True,
|
| 339 |
-
cache_mode="lazy"
|
| 340 |
-
)
|
| 341 |
-
|
| 342 |
-
# Auto-update dimensions when images are uploaded
|
| 343 |
-
input_images.upload(
|
| 344 |
-
fn=update_dimensions_from_image,
|
| 345 |
-
inputs=[input_images],
|
| 346 |
-
outputs=[width, height]
|
| 347 |
-
)
|
| 348 |
-
|
| 349 |
-
# Auto-update steps when mode changes
|
| 350 |
-
mode_choice.change(
|
| 351 |
-
fn=update_steps_from_mode,
|
| 352 |
-
inputs=[mode_choice],
|
| 353 |
-
outputs=[num_inference_steps, guidance_scale]
|
| 354 |
-
)
|
| 355 |
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
outputs=[result, seed]
|
| 361 |
)
|
| 362 |
|
| 363 |
demo.launch()
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
import random
|
| 5 |
import spaces
|
| 6 |
import torch
|
| 7 |
+
from diffusers import FluxImg2ImgPipeline
|
| 8 |
+
from PIL import Image, ImageOps
|
| 9 |
+
import io
|
|
|
|
| 10 |
import base64
|
|
|
|
| 11 |
|
| 12 |
+
# --- CONFIGURATION ---
|
| 13 |
+
# We use bfloat16 for speed/memory on CPU
|
| 14 |
dtype = torch.bfloat16
|
| 15 |
+
device = "cpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# We use Schnell because it is 10x faster on CPU than Dev or 'Klein'
|
| 18 |
+
# If you explicitly have access to Flux 2, change this ID.
|
| 19 |
+
MODEL_ID = "black-forest-labs/FLUX.1-schnell"
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
print(f"Loading Model: {MODEL_ID} on {device}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Load Pipeline
|
| 24 |
+
pipe = FluxImg2ImgPipeline.from_pretrained(
|
| 25 |
+
MODEL_ID,
|
| 26 |
+
torch_dtype=dtype
|
| 27 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# CRITICAL CPU OPTIMIZATION
|
| 30 |
+
# This replaces .to("cuda"). It loads the model in pieces so RAM doesn't crash.
|
| 31 |
+
pipe.enable_model_cpu_offload()
|
| 32 |
|
| 33 |
+
MAX_SEED = np.iinfo(np.int32).max
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# --- THE "INJECTION" LOGIC ---
|
| 36 |
+
# This acts as a pre-processor injection. It forces the pixel data
|
| 37 |
+
# to be symmetrical before the UNet even sees it.
|
| 38 |
+
# This guarantees the "Face Lock".
|
| 39 |
+
def inject_symmetry(image, side="Left"):
|
| 40 |
+
if image is None:
|
| 41 |
+
return None
|
| 42 |
|
| 43 |
+
img = image.convert("RGB")
|
| 44 |
+
w, h = img.size
|
| 45 |
+
mid = w // 2
|
| 46 |
+
arr = np.array(img)
|
| 47 |
+
|
| 48 |
+
# Mathematical locking of geometry
|
| 49 |
+
if side == "Left":
|
| 50 |
+
target_half = arr[:, :mid, :] # Get Left
|
| 51 |
+
mirrored = np.fliplr(target_half) # Mirror it
|
| 52 |
+
locked_face = np.concatenate((target_half, mirrored), axis=1)
|
| 53 |
+
else:
|
| 54 |
+
target_half = arr[:, mid:, :] # Get Right
|
| 55 |
+
mirrored = np.fliplr(target_half) # Mirror it
|
| 56 |
+
locked_face = np.concatenate((mirrored, target_half), axis=1)
|
| 57 |
+
|
| 58 |
+
return Image.fromarray(locked_face)
|
| 59 |
|
| 60 |
+
# --- INFERENCE FUNCTION ---
|
| 61 |
+
@spaces.GPU(duration=120) # Request GPU if available, falls back to CPU logic if not
|
| 62 |
+
def infer(prompt, input_image, side_choice, strength, seed, randomize_seed, width, height, steps, guidance):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
if input_image is None:
|
| 65 |
+
raise gr.Error("Please upload an image for face symmetry.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
|
|
|
|
|
|
|
|
|
| 67 |
if randomize_seed:
|
| 68 |
seed = random.randint(0, MAX_SEED)
|
| 69 |
+
|
| 70 |
+
# 1. INJECT SYMMETRY
|
| 71 |
+
# We process the image *before* the model touches it.
|
| 72 |
+
print("Injecting symmetry constraints...")
|
| 73 |
+
processed_image = inject_symmetry(input_image, side_choice)
|
| 74 |
+
|
| 75 |
+
# Resize to be compatible with Flux (multiples of 16)
|
| 76 |
+
w, h = processed_image.size
|
| 77 |
+
w = (w // 16) * 16
|
| 78 |
+
h = (h // 16) * 16
|
| 79 |
+
processed_image = processed_image.resize((w, h))
|
| 80 |
+
|
| 81 |
+
print("Running Flux to smooth seams...")
|
| 82 |
+
generator = torch.Generator(device="cpu").manual_seed(seed)
|
| 83 |
+
|
| 84 |
+
# 2. RUN DIFFUSION
|
| 85 |
+
# We use the processed image as the base.
|
| 86 |
+
# Strength is CRITICAL:
|
| 87 |
+
# 0.1 - 0.30 = Locks Identity (Only fixes the seam)
|
| 88 |
+
# 0.35+ = Starts changing the face
|
| 89 |
+
result = pipe(
|
| 90 |
+
prompt=prompt,
|
| 91 |
+
image=processed_image,
|
| 92 |
+
strength=strength,
|
| 93 |
+
num_inference_steps=steps,
|
| 94 |
+
guidance_scale=guidance,
|
| 95 |
+
generator=generator
|
| 96 |
+
).images[0]
|
| 97 |
+
|
| 98 |
+
return result, seed
|
| 99 |
+
|
| 100 |
+
# --- UI SETUP ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
css = """
|
| 102 |
+
#col-container { max-width: 1000px; margin: 0 auto; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
"""
|
| 104 |
|
| 105 |
with gr.Blocks(css=css) as demo:
|
|
|
|
| 106 |
with gr.Column(elem_id="col-container"):
|
| 107 |
+
gr.Markdown("# Flux Face Symmetry (Identity Lock)")
|
| 108 |
+
gr.Markdown("CPU-Optimized Mode. Uses Pixel Injection to lock face geometry.")
|
| 109 |
+
|
| 110 |
with gr.Row():
|
| 111 |
with gr.Column():
|
| 112 |
+
input_img = gr.Image(label="Upload Face", type="pil")
|
| 113 |
+
|
| 114 |
with gr.Row():
|
| 115 |
+
side = gr.Radio(["Left", "Right"], label="Keep Side", value="Left")
|
| 116 |
+
# Strength default is 0.25 -> This ensures ID is locked
|
| 117 |
+
strength = gr.Slider(0.1, 0.6, value=0.25, step=0.01, label="Denoise Strength (Keep <0.30 to lock ID)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
prompt = gr.Text(
|
| 120 |
+
label="Prompt (Optional - usually leave empty or describe lighting)",
|
| 121 |
+
value="high quality, realistic, smooth skin, 8k",
|
| 122 |
+
lines=2
|
| 123 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
run_btn = gr.Button("Generate Symmetrical Face", variant="primary")
|
| 126 |
|
| 127 |
+
with gr.Accordion("Advanced", open=False):
|
| 128 |
+
steps = gr.Slider(1, 50, value=4, step=1, label="Steps (Keep low for CPU)")
|
| 129 |
+
guidance = gr.Slider(0, 10, value=1.0, step=0.1, label="Guidance")
|
| 130 |
+
width = gr.Slider(256, 1024, value=1024, step=16, label="Width")
|
| 131 |
+
height = gr.Slider(256, 1024, value=1024, step=16, label="Height")
|
| 132 |
+
seed = gr.Slider(0, MAX_SEED, value=0, label="Seed")
|
| 133 |
+
randomize_seed = gr.Checkbox(True, label="Randomize Seed")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
+
with gr.Column():
|
| 136 |
+
output_img = gr.Image(label="Result")
|
| 137 |
+
seed_output = gr.Number(label="Used Seed")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
+
run_btn.click(
|
| 140 |
+
infer,
|
| 141 |
+
inputs=[prompt, input_img, side, strength, seed, randomize_seed, width, height, steps, guidance],
|
| 142 |
+
outputs=[output_img, seed_output]
|
|
|
|
| 143 |
)
|
| 144 |
|
| 145 |
demo.launch()
|