Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,47 @@
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
-
from diffusers import QwenImageEditPlusPipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# Load pipeline at startup
|
| 8 |
pipeline = QwenImageEditPlusPipeline.from_pretrained(
|
| 9 |
"Qwen/Qwen-Image-Edit-2509",
|
|
|
|
| 10 |
torch_dtype=torch.bfloat16
|
| 11 |
)
|
| 12 |
pipeline.to('cuda')
|
| 13 |
pipeline.set_progress_bar_config(disable=None)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# Create a temporary pipeline for compilation
|
| 22 |
-
compile_pipe = QwenImageEditPlusPipeline.from_pretrained(
|
| 23 |
-
"Qwen/Qwen-Image-Edit-2509",
|
| 24 |
-
torch_dtype=torch.bfloat16
|
| 25 |
-
)
|
| 26 |
-
compile_pipe.to('cuda')
|
| 27 |
-
|
| 28 |
-
# Capture inputs for the transformer
|
| 29 |
-
with spaces.aoti_capture(compile_pipe.transformer) as call:
|
| 30 |
-
# Create dummy inputs that match what Qwen expects
|
| 31 |
-
dummy_image = torch.randn(1, 3, 256, 256, device='cuda', dtype=torch.bfloat16)
|
| 32 |
-
dummy_prompt = "test prompt for compilation"
|
| 33 |
-
compile_pipe(dummy_image, dummy_prompt)
|
| 34 |
-
|
| 35 |
-
# Export the transformer model
|
| 36 |
-
exported = torch.export.export(
|
| 37 |
-
compile_pipe.transformer,
|
| 38 |
-
args=call.args,
|
| 39 |
-
kwargs=call.kwargs,
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
# Compile the exported model
|
| 43 |
-
return spaces.aoti_compile(exported)
|
| 44 |
|
| 45 |
-
# Apply AoT compilation
|
| 46 |
-
try:
|
| 47 |
-
compiled_transformer = compile_qwen_transformer()
|
| 48 |
-
spaces.aoti_apply(compiled_transformer, pipeline.transformer)
|
| 49 |
-
print("✅ Qwen transformer successfully compiled with AoT")
|
| 50 |
-
except Exception as e:
|
| 51 |
-
print(f"⚠️ AoT compilation failed: {e}")
|
| 52 |
|
| 53 |
-
|
|
|
|
| 54 |
def edit_images(image1, image2, prompt, seed, true_cfg_scale, negative_prompt, num_steps, guidance_scale):
|
| 55 |
if image1 is None or image2 is None:
|
| 56 |
gr.Warning("Please upload both images")
|
|
@@ -93,7 +84,7 @@ example_images = [
|
|
| 93 |
with gr.Blocks(css="footer {visibility: hidden}") as demo:
|
| 94 |
gr.Markdown(
|
| 95 |
"""
|
| 96 |
-
# Qwen Image Edit Plus
|
| 97 |
|
| 98 |
Upload two images and describe how you want them combined or edited together.
|
| 99 |
|
|
@@ -150,9 +141,9 @@ with gr.Blocks(css="footer {visibility: hidden}") as demo:
|
|
| 150 |
)
|
| 151 |
num_steps = gr.Slider(
|
| 152 |
label="Number of Inference Steps",
|
| 153 |
-
minimum=
|
| 154 |
maximum=30,
|
| 155 |
-
value=
|
| 156 |
step=1
|
| 157 |
)
|
| 158 |
|
|
@@ -161,7 +152,7 @@ with gr.Blocks(css="footer {visibility: hidden}") as demo:
|
|
| 161 |
label="True CFG Scale",
|
| 162 |
minimum=1.0,
|
| 163 |
maximum=10.0,
|
| 164 |
-
value=
|
| 165 |
step=0.5
|
| 166 |
)
|
| 167 |
guidance_scale = gr.Slider(
|
|
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
+
import math
|
| 5 |
from PIL import Image
|
| 6 |
+
from diffusers import QwenImageEditPlusPipeline, FlowMatchEulerDiscreteScheduler
|
| 7 |
+
|
| 8 |
+
# Load pipeline with optimized scheduler at startup
|
| 9 |
+
scheduler_config = {
|
| 10 |
+
"base_image_seq_len": 256,
|
| 11 |
+
"base_shift": math.log(3),
|
| 12 |
+
"invert_sigmas": False,
|
| 13 |
+
"max_image_seq_len": 8192,
|
| 14 |
+
"max_shift": math.log(3),
|
| 15 |
+
"num_train_timesteps": 1000,
|
| 16 |
+
"shift": 1.0,
|
| 17 |
+
"shift_terminal": None,
|
| 18 |
+
"stochastic_sampling": False,
|
| 19 |
+
"time_shift_type": "exponential",
|
| 20 |
+
"use_beta_sigmas": False,
|
| 21 |
+
"use_dynamic_shifting": True,
|
| 22 |
+
"use_exponential_sigmas": False,
|
| 23 |
+
"use_karras_sigmas": False,
|
| 24 |
+
}
|
| 25 |
+
scheduler = FlowMatchEulerDiscreteScheduler.from_config(scheduler_config)
|
| 26 |
|
|
|
|
| 27 |
pipeline = QwenImageEditPlusPipeline.from_pretrained(
|
| 28 |
"Qwen/Qwen-Image-Edit-2509",
|
| 29 |
+
scheduler=scheduler,
|
| 30 |
torch_dtype=torch.bfloat16
|
| 31 |
)
|
| 32 |
pipeline.to('cuda')
|
| 33 |
pipeline.set_progress_bar_config(disable=None)
|
| 34 |
|
| 35 |
+
# Load LoRA for faster inference
|
| 36 |
+
pipeline.load_lora_weights(
|
| 37 |
+
"lightx2v/Qwen-Image-Lightning",
|
| 38 |
+
weight_name="Qwen-Image-Lightning-8steps-V2.0-bf16.safetensors"
|
| 39 |
+
)
|
| 40 |
+
pipeline.fuse_lora()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
|
| 44 |
+
@spaces.GPU(duration=60)
|
| 45 |
def edit_images(image1, image2, prompt, seed, true_cfg_scale, negative_prompt, num_steps, guidance_scale):
|
| 46 |
if image1 is None or image2 is None:
|
| 47 |
gr.Warning("Please upload both images")
|
|
|
|
| 84 |
with gr.Blocks(css="footer {visibility: hidden}") as demo:
|
| 85 |
gr.Markdown(
|
| 86 |
"""
|
| 87 |
+
# Qwen Image Edit Plus (Optimized)
|
| 88 |
|
| 89 |
Upload two images and describe how you want them combined or edited together.
|
| 90 |
|
|
|
|
| 141 |
)
|
| 142 |
num_steps = gr.Slider(
|
| 143 |
label="Number of Inference Steps",
|
| 144 |
+
minimum=8,
|
| 145 |
maximum=30,
|
| 146 |
+
value=8,
|
| 147 |
step=1
|
| 148 |
)
|
| 149 |
|
|
|
|
| 152 |
label="True CFG Scale",
|
| 153 |
minimum=1.0,
|
| 154 |
maximum=10.0,
|
| 155 |
+
value=1.0,
|
| 156 |
step=0.5
|
| 157 |
)
|
| 158 |
guidance_scale = gr.Slider(
|