Spaces:
Paused
Paused
update app [adapter : hf download]
Browse files
app.py
CHANGED
|
@@ -1,10 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
import gc
|
| 3 |
-
|
| 4 |
-
# 1. FIX: Set memory allocation configuration BEFORE importing torch
|
| 5 |
-
# 'expandable_segments:True' prevents the specific CUDACachingAllocator assertion failure
|
| 6 |
-
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
|
| 7 |
-
|
| 8 |
import gradio as gr
|
| 9 |
import numpy as np
|
| 10 |
import spaces
|
|
@@ -15,7 +10,6 @@ from typing import Iterable
|
|
| 15 |
from gradio.themes import Soft
|
| 16 |
from gradio.themes.utils import colors, fonts, sizes
|
| 17 |
|
| 18 |
-
# Define Theme
|
| 19 |
colors.orange_red = colors.Color(
|
| 20 |
name="orange_red",
|
| 21 |
c50="#FFF0E5",
|
|
@@ -104,51 +98,65 @@ pipe = QwenImageEditPlusPipeline.from_pretrained(
|
|
| 104 |
torch_dtype=dtype
|
| 105 |
).to(device)
|
| 106 |
|
| 107 |
-
# 2. FIX: Enable VAE Tiling. This is crucial for decoding large images without OOM.
|
| 108 |
try:
|
| 109 |
pipe.enable_vae_tiling()
|
| 110 |
print("VAE Tiling enabled.")
|
| 111 |
except Exception as e:
|
| 112 |
print(f"Warning: Could not enable VAE tiling: {e}")
|
| 113 |
|
| 114 |
-
print("Loading and Fusing Lightning LoRA...")
|
| 115 |
pipe.load_lora_weights("lightx2v/Qwen-Image-Lightning",
|
| 116 |
weight_name="Qwen-Image-Lightning-4steps-V2.0-bf16.safetensors",
|
| 117 |
adapter_name="lightning")
|
| 118 |
pipe.fuse_lora(adapter_names=["lightning"], lora_scale=1.0)
|
| 119 |
|
| 120 |
-
print("Loading Task Adapters...")
|
| 121 |
-
|
| 122 |
-
pipe.load_lora_weights("tarn59/apply_texture_qwen_image_edit_2509",
|
| 123 |
-
weight_name="apply_texture_v2_qwen_image_edit_2509.safetensors",
|
| 124 |
-
adapter_name="texture")
|
| 125 |
-
|
| 126 |
-
pipe.load_lora_weights("ostris/qwen_image_edit_inpainting",
|
| 127 |
-
weight_name="qwen_image_edit_inpainting.safetensors",
|
| 128 |
-
adapter_name="fusion")
|
| 129 |
-
|
| 130 |
-
pipe.load_lora_weights("ostris/qwen_image_edit_2509_shirt_design",
|
| 131 |
-
weight_name="qwen_image_edit_2509_shirt_design.safetensors",
|
| 132 |
-
adapter_name="shirt_design")
|
| 133 |
-
|
| 134 |
-
pipe.load_lora_weights("dx8152/Qwen-Image-Edit-2509-Fusion",
|
| 135 |
-
weight_name="溶图.safetensors",
|
| 136 |
-
adapter_name="fusion-x")
|
| 137 |
-
|
| 138 |
-
pipe.load_lora_weights("oumoumad/Qwen-Edit-2509-Material-transfer",
|
| 139 |
-
weight_name="material-transfer_000004769.safetensors",
|
| 140 |
-
adapter_name="material-transfer")
|
| 141 |
-
|
| 142 |
-
pipe.load_lora_weights("dx8152/Qwen-Edit-2509-Light-Migration",
|
| 143 |
-
weight_name="参考色调.safetensors",
|
| 144 |
-
adapter_name="light-migration")
|
| 145 |
-
|
| 146 |
try:
|
| 147 |
pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
|
| 148 |
print("Flash Attention 3 Processor set successfully.")
|
| 149 |
except Exception as e:
|
| 150 |
-
print(f"Could not set FA3 processor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
|
|
|
| 152 |
MAX_SEED = np.iinfo(np.int32).max
|
| 153 |
|
| 154 |
def update_dimensions_on_upload(image):
|
|
@@ -172,7 +180,7 @@ def update_dimensions_on_upload(image):
|
|
| 172 |
|
| 173 |
return new_width, new_height
|
| 174 |
|
| 175 |
-
@spaces.GPU
|
| 176 |
def infer(
|
| 177 |
image_1,
|
| 178 |
image_2,
|
|
@@ -184,42 +192,42 @@ def infer(
|
|
| 184 |
steps,
|
| 185 |
progress=gr.Progress(track_tqdm=True)
|
| 186 |
):
|
| 187 |
-
# 3. FIX: Aggressive Garbage Collection before run
|
| 188 |
gc.collect()
|
| 189 |
torch.cuda.empty_cache()
|
| 190 |
|
| 191 |
if image_1 is None or image_2 is None:
|
| 192 |
raise gr.Error("Please upload both images for Fusion/Texture/FaceSwap tasks.")
|
| 193 |
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
prompt = "Apply texture to object."
|
| 199 |
-
elif lora_adapter == "Fuse-Objects":
|
| 200 |
-
prompt = "Fuse object into background."
|
| 201 |
-
elif lora_adapter == "Super-Fusion":
|
| 202 |
-
prompt = "Blend the product into the background, correct its perspective and lighting, and make it naturally integrated with the scene."
|
| 203 |
-
elif lora_adapter == "Material-Transfer":
|
| 204 |
-
prompt = "change materials of image1 to match the reference in image2"
|
| 205 |
-
elif lora_adapter == "Light-Migration":
|
| 206 |
-
prompt = "Refer to the color tone, remove the original lighting from Image 1, and relight Image 1 based on the lighting and color tone of Image 2."
|
| 207 |
-
|
| 208 |
-
adapters_map = {
|
| 209 |
-
"Texture Edit": "texture",
|
| 210 |
-
"Fuse-Objects": "fusion",
|
| 211 |
-
"Cloth-Design-Fuse": "shirt_design",
|
| 212 |
-
"Super-Fusion": "fusion-x",
|
| 213 |
-
"Material-Transfer": "material-transfer",
|
| 214 |
-
"Light-Migration": "light-migration",
|
| 215 |
-
}
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
else:
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
if randomize_seed:
|
| 225 |
seed = random.randint(0, MAX_SEED)
|
|
@@ -233,7 +241,6 @@ def infer(
|
|
| 233 |
width, height = update_dimensions_on_upload(img1_pil)
|
| 234 |
|
| 235 |
try:
|
| 236 |
-
# 3. FIX: Use inference_mode for better memory efficiency
|
| 237 |
with torch.inference_mode():
|
| 238 |
result = pipe(
|
| 239 |
image=[img1_pil, img2_pil],
|
|
@@ -249,11 +256,9 @@ def infer(
|
|
| 249 |
return result, seed
|
| 250 |
|
| 251 |
except Exception as e:
|
| 252 |
-
# Rethrow so Gradio sees the error, but allow finally block to run
|
| 253 |
raise e
|
| 254 |
|
| 255 |
finally:
|
| 256 |
-
# 3. FIX: Cleanup after run regardless of success or failure
|
| 257 |
gc.collect()
|
| 258 |
torch.cuda.empty_cache()
|
| 259 |
|
|
@@ -314,7 +319,7 @@ with gr.Blocks() as demo:
|
|
| 314 |
with gr.Row():
|
| 315 |
lora_adapter = gr.Dropdown(
|
| 316 |
label="Choose Editing Style",
|
| 317 |
-
choices=
|
| 318 |
value="Texture Edit",
|
| 319 |
)
|
| 320 |
|
|
|
|
| 1 |
import os
|
| 2 |
import gc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import numpy as np
|
| 5 |
import spaces
|
|
|
|
| 10 |
from gradio.themes import Soft
|
| 11 |
from gradio.themes.utils import colors, fonts, sizes
|
| 12 |
|
|
|
|
| 13 |
colors.orange_red = colors.Color(
|
| 14 |
name="orange_red",
|
| 15 |
c50="#FFF0E5",
|
|
|
|
| 98 |
torch_dtype=dtype
|
| 99 |
).to(device)
|
| 100 |
|
|
|
|
| 101 |
try:
|
| 102 |
pipe.enable_vae_tiling()
|
| 103 |
print("VAE Tiling enabled.")
|
| 104 |
except Exception as e:
|
| 105 |
print(f"Warning: Could not enable VAE tiling: {e}")
|
| 106 |
|
| 107 |
+
print("Loading and Fusing Lightning LoRA (Base Optimization)...")
|
| 108 |
pipe.load_lora_weights("lightx2v/Qwen-Image-Lightning",
|
| 109 |
weight_name="Qwen-Image-Lightning-4steps-V2.0-bf16.safetensors",
|
| 110 |
adapter_name="lightning")
|
| 111 |
pipe.fuse_lora(adapter_names=["lightning"], lora_scale=1.0)
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
try:
|
| 114 |
pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
|
| 115 |
print("Flash Attention 3 Processor set successfully.")
|
| 116 |
except Exception as e:
|
| 117 |
+
print(f"Could not set FA3 processor: {e}. using default attention.")
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
ADAPTER_SPECS = {
|
| 121 |
+
"Texture Edit": {
|
| 122 |
+
"repo": "tarn59/apply_texture_qwen_image_edit_2509",
|
| 123 |
+
"weights": "apply_texture_v2_qwen_image_edit_2509.safetensors",
|
| 124 |
+
"adapter_name": "texture",
|
| 125 |
+
"default_prompt": "Apply texture to object."
|
| 126 |
+
},
|
| 127 |
+
"Fuse-Objects": {
|
| 128 |
+
"repo": "ostris/qwen_image_edit_inpainting",
|
| 129 |
+
"weights": "qwen_image_edit_inpainting.safetensors",
|
| 130 |
+
"adapter_name": "fusion",
|
| 131 |
+
"default_prompt": "Fuse object into background."
|
| 132 |
+
},
|
| 133 |
+
"Cloth-Design-Fuse": {
|
| 134 |
+
"repo": "ostris/qwen_image_edit_2509_shirt_design",
|
| 135 |
+
"weights": "qwen_image_edit_2509_shirt_design.safetensors",
|
| 136 |
+
"adapter_name": "shirt_design",
|
| 137 |
+
"default_prompt": "Put this design on their shirt."
|
| 138 |
+
},
|
| 139 |
+
"Super-Fusion": {
|
| 140 |
+
"repo": "dx8152/Qwen-Image-Edit-2509-Fusion",
|
| 141 |
+
"weights": "溶图.safetensors",
|
| 142 |
+
"adapter_name": "fusion-x",
|
| 143 |
+
"default_prompt": "Blend the product into the background, correct its perspective and lighting."
|
| 144 |
+
},
|
| 145 |
+
"Material-Transfer": {
|
| 146 |
+
"repo": "oumoumad/Qwen-Edit-2509-Material-transfer",
|
| 147 |
+
"weights": "material-transfer_000004769.safetensors",
|
| 148 |
+
"adapter_name": "material-transfer",
|
| 149 |
+
"default_prompt": "Change materials of image1 to match the reference in image2."
|
| 150 |
+
},
|
| 151 |
+
"Light-Migration": {
|
| 152 |
+
"repo": "dx8152/Qwen-Edit-2509-Light-Migration",
|
| 153 |
+
"weights": "参考色调.safetensors",
|
| 154 |
+
"adapter_name": "light-migration",
|
| 155 |
+
"default_prompt": "Relight Image 1 based on the lighting and color tone of Image 2."
|
| 156 |
+
}
|
| 157 |
+
}
|
| 158 |
|
| 159 |
+
LOADED_ADAPTERS = set()
|
| 160 |
MAX_SEED = np.iinfo(np.int32).max
|
| 161 |
|
| 162 |
def update_dimensions_on_upload(image):
|
|
|
|
| 180 |
|
| 181 |
return new_width, new_height
|
| 182 |
|
| 183 |
+
@spaces.GPU(duration=30)
|
| 184 |
def infer(
|
| 185 |
image_1,
|
| 186 |
image_2,
|
|
|
|
| 192 |
steps,
|
| 193 |
progress=gr.Progress(track_tqdm=True)
|
| 194 |
):
|
|
|
|
| 195 |
gc.collect()
|
| 196 |
torch.cuda.empty_cache()
|
| 197 |
|
| 198 |
if image_1 is None or image_2 is None:
|
| 199 |
raise gr.Error("Please upload both images for Fusion/Texture/FaceSwap tasks.")
|
| 200 |
|
| 201 |
+
# 1. Get Adapter Spec
|
| 202 |
+
spec = ADAPTER_SPECS.get(lora_adapter)
|
| 203 |
+
if not spec:
|
| 204 |
+
raise gr.Error(f"Invalid Adapter Selection: {lora_adapter}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
+
adapter_name = spec["adapter_name"]
|
| 207 |
+
|
| 208 |
+
# 2. Dynamic Loading Logic
|
| 209 |
+
if adapter_name not in LOADED_ADAPTERS:
|
| 210 |
+
print(f"--- Downloading and Loading Adapter: {lora_adapter} ---")
|
| 211 |
+
try:
|
| 212 |
+
pipe.load_lora_weights(
|
| 213 |
+
spec["repo"],
|
| 214 |
+
weight_name=spec["weights"],
|
| 215 |
+
adapter_name=adapter_name
|
| 216 |
+
)
|
| 217 |
+
LOADED_ADAPTERS.add(adapter_name)
|
| 218 |
+
except Exception as e:
|
| 219 |
+
raise gr.Error(f"Failed to load adapter {lora_adapter}: {e}")
|
| 220 |
else:
|
| 221 |
+
print(f"--- Adapter {lora_adapter} already loaded. Activating. ---")
|
| 222 |
+
|
| 223 |
+
# 3. Handle Default Prompts
|
| 224 |
+
if not prompt:
|
| 225 |
+
prompt = spec["default_prompt"]
|
| 226 |
+
|
| 227 |
+
# 4. Activate specific adapter
|
| 228 |
+
# Note: We do not fuse these task adapters, we just activate them.
|
| 229 |
+
# Lightning is already fused.
|
| 230 |
+
pipe.set_adapters([adapter_name], adapter_weights=[1.0])
|
| 231 |
|
| 232 |
if randomize_seed:
|
| 233 |
seed = random.randint(0, MAX_SEED)
|
|
|
|
| 241 |
width, height = update_dimensions_on_upload(img1_pil)
|
| 242 |
|
| 243 |
try:
|
|
|
|
| 244 |
with torch.inference_mode():
|
| 245 |
result = pipe(
|
| 246 |
image=[img1_pil, img2_pil],
|
|
|
|
| 256 |
return result, seed
|
| 257 |
|
| 258 |
except Exception as e:
|
|
|
|
| 259 |
raise e
|
| 260 |
|
| 261 |
finally:
|
|
|
|
| 262 |
gc.collect()
|
| 263 |
torch.cuda.empty_cache()
|
| 264 |
|
|
|
|
| 319 |
with gr.Row():
|
| 320 |
lora_adapter = gr.Dropdown(
|
| 321 |
label="Choose Editing Style",
|
| 322 |
+
choices=list(ADAPTER_SPECS.keys()),
|
| 323 |
value="Texture Edit",
|
| 324 |
)
|
| 325 |
|