Spaces:
Running
on
Zero
Running
on
Zero
Update app_local.py
Browse files- app_local.py +22 -16
app_local.py
CHANGED
|
@@ -170,6 +170,7 @@ def polish_prompt(original_prompt: str) -> str:
|
|
| 170 |
generated_ids[0][model_inputs.input_ids.shape[1]:],
|
| 171 |
skip_special_tokens=True
|
| 172 |
).strip()
|
|
|
|
| 173 |
print(f"Model raw output: {enhanced}") # Debug logging
|
| 174 |
# Try to extract JSON content
|
| 175 |
rewritten_prompt = extract_json_response(enhanced)
|
|
@@ -227,15 +228,20 @@ pipe = QwenImageEditPipeline.from_pretrained(
|
|
| 227 |
# Load LoRA weights for acceleration
|
| 228 |
pipe.load_lora_weights(
|
| 229 |
"lightx2v/Qwen-Image-Lightning",
|
| 230 |
-
weight_name="Qwen-Image-Lightning-8steps-V1.1.safetensors"
|
|
|
|
| 231 |
)
|
| 232 |
pipe.fuse_lora()
|
| 233 |
|
| 234 |
-
if is_xformers_available():
|
| 235 |
-
|
| 236 |
-
else:
|
| 237 |
-
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
# Function to update prompt preview when preset is selected
|
| 240 |
def update_prompt_preview(preset_type, base_prompt):
|
| 241 |
"""Update the prompt preview display based on selected preset and base prompt"""
|
|
@@ -261,7 +267,7 @@ def infer(
|
|
| 261 |
seed=42,
|
| 262 |
randomize_seed=False,
|
| 263 |
true_guidance_scale=4.0,
|
| 264 |
-
num_inference_steps=
|
| 265 |
rewrite_prompt=True,
|
| 266 |
num_images_per_prompt=1,
|
| 267 |
preset_type=None, # New parameter for presets
|
|
@@ -291,7 +297,7 @@ def infer(
|
|
| 291 |
return pil_image # Return original if resize fails
|
| 292 |
|
| 293 |
# Add noise function for batch variation
|
| 294 |
-
def add_noise_to_image(pil_image, noise_level=0.
|
| 295 |
"""Add slight noise to image to create variation in outputs"""
|
| 296 |
try:
|
| 297 |
if pil_image is None:
|
|
@@ -384,7 +390,7 @@ def infer(
|
|
| 384 |
input_image = add_noise_to_image(image, noise_level=0.01 + i*0.003)
|
| 385 |
|
| 386 |
# Slightly vary guidance scale for each image
|
| 387 |
-
varied_guidance = true_guidance_scale + random.uniform(-0.
|
| 388 |
varied_guidance = max(1.0, min(10.0, varied_guidance))
|
| 389 |
|
| 390 |
# Generate single image
|
|
@@ -399,12 +405,12 @@ def infer(
|
|
| 399 |
).images
|
| 400 |
edited_images.extend(result)
|
| 401 |
|
| 402 |
-
print(f"Generated image {i+1}/{len(batch_prompts)} with prompt: {current_prompt[:
|
| 403 |
|
| 404 |
# Clear cache after generation
|
| 405 |
-
if device == "cuda":
|
| 406 |
-
|
| 407 |
-
|
| 408 |
|
| 409 |
return edited_images, base_seed, prompt_info
|
| 410 |
except Exception as e:
|
|
@@ -487,7 +493,7 @@ with gr.Blocks(title="Qwen Image Edit - Fast Lightning Mode w/ Batch") as demo:
|
|
| 487 |
)
|
| 488 |
with gr.Row():
|
| 489 |
true_guidance_scale = gr.Slider(
|
| 490 |
-
label="
|
| 491 |
minimum=1.0,
|
| 492 |
maximum=10.0,
|
| 493 |
step=0.1,
|
|
@@ -495,10 +501,10 @@ with gr.Blocks(title="Qwen Image Edit - Fast Lightning Mode w/ Batch") as demo:
|
|
| 495 |
)
|
| 496 |
num_inference_steps = gr.Slider(
|
| 497 |
label="Inference Steps",
|
| 498 |
-
minimum=
|
| 499 |
maximum=16,
|
| 500 |
step=1,
|
| 501 |
-
value=
|
| 502 |
)
|
| 503 |
num_images_per_prompt = gr.Slider(
|
| 504 |
label="Output Count (Manual)",
|
|
|
|
| 170 |
generated_ids[0][model_inputs.input_ids.shape[1]:],
|
| 171 |
skip_special_tokens=True
|
| 172 |
).strip()
|
| 173 |
+
print(f"Original Prompt: {original_prompt}")
|
| 174 |
print(f"Model raw output: {enhanced}") # Debug logging
|
| 175 |
# Try to extract JSON content
|
| 176 |
rewritten_prompt = extract_json_response(enhanced)
|
|
|
|
| 228 |
# Load LoRA weights for acceleration
|
| 229 |
pipe.load_lora_weights(
|
| 230 |
"lightx2v/Qwen-Image-Lightning",
|
| 231 |
+
# weight_name="Qwen-Image-Lightning-8steps-V1.1.safetensors"
|
| 232 |
+
weight_name="Qwen-Image-Lightning-4steps-V1.0.safetensors"
|
| 233 |
)
|
| 234 |
pipe.fuse_lora()
|
| 235 |
|
| 236 |
+
# if is_xformers_available():
|
| 237 |
+
# pipe.enable_xformers_memory_efficient_attention()
|
| 238 |
+
# else:
|
| 239 |
+
# print("xformers not available")
|
| 240 |
+
try:
|
| 241 |
+
pipe.enable_vae_slicing()
|
| 242 |
+
except Exception as e:
|
| 243 |
+
print(f"VAE Slicing Failed: {e}")
|
| 244 |
+
|
| 245 |
# Function to update prompt preview when preset is selected
|
| 246 |
def update_prompt_preview(preset_type, base_prompt):
|
| 247 |
"""Update the prompt preview display based on selected preset and base prompt"""
|
|
|
|
| 267 |
seed=42,
|
| 268 |
randomize_seed=False,
|
| 269 |
true_guidance_scale=4.0,
|
| 270 |
+
num_inference_steps=4,
|
| 271 |
rewrite_prompt=True,
|
| 272 |
num_images_per_prompt=1,
|
| 273 |
preset_type=None, # New parameter for presets
|
|
|
|
| 297 |
return pil_image # Return original if resize fails
|
| 298 |
|
| 299 |
# Add noise function for batch variation
|
| 300 |
+
def add_noise_to_image(pil_image, noise_level=0.02):
|
| 301 |
"""Add slight noise to image to create variation in outputs"""
|
| 302 |
try:
|
| 303 |
if pil_image is None:
|
|
|
|
| 390 |
input_image = add_noise_to_image(image, noise_level=0.01 + i*0.003)
|
| 391 |
|
| 392 |
# Slightly vary guidance scale for each image
|
| 393 |
+
varied_guidance = true_guidance_scale + random.uniform(-0.1, 0.1)
|
| 394 |
varied_guidance = max(1.0, min(10.0, varied_guidance))
|
| 395 |
|
| 396 |
# Generate single image
|
|
|
|
| 405 |
).images
|
| 406 |
edited_images.extend(result)
|
| 407 |
|
| 408 |
+
print(f"Generated image {i+1}/{len(batch_prompts)} with prompt: {current_prompt[:75]}...")
|
| 409 |
|
| 410 |
# Clear cache after generation
|
| 411 |
+
# if device == "cuda":
|
| 412 |
+
# torch.cuda.empty_cache()
|
| 413 |
+
# gc.collect()
|
| 414 |
|
| 415 |
return edited_images, base_seed, prompt_info
|
| 416 |
except Exception as e:
|
|
|
|
| 493 |
)
|
| 494 |
with gr.Row():
|
| 495 |
true_guidance_scale = gr.Slider(
|
| 496 |
+
label="True CFG Scale",
|
| 497 |
minimum=1.0,
|
| 498 |
maximum=10.0,
|
| 499 |
step=0.1,
|
|
|
|
| 501 |
)
|
| 502 |
num_inference_steps = gr.Slider(
|
| 503 |
label="Inference Steps",
|
| 504 |
+
minimum=2,
|
| 505 |
maximum=16,
|
| 506 |
step=1,
|
| 507 |
+
value=4
|
| 508 |
)
|
| 509 |
num_images_per_prompt = gr.Slider(
|
| 510 |
label="Output Count (Manual)",
|