Spaces:
Sleeping
Sleeping
update app
Browse files
app.py
CHANGED
|
@@ -5,16 +5,11 @@ import numpy as np
|
|
| 5 |
import spaces
|
| 6 |
import torch
|
| 7 |
import random
|
| 8 |
-
import uuid
|
| 9 |
-
import tempfile
|
| 10 |
from PIL import Image
|
| 11 |
from typing import Iterable
|
| 12 |
from gradio.themes import Soft
|
| 13 |
from gradio.themes.utils import colors, fonts, sizes
|
| 14 |
|
| 15 |
-
import rerun as rr
|
| 16 |
-
from gradio_rerun import Rerun
|
| 17 |
-
|
| 18 |
colors.orange_red = colors.Color(
|
| 19 |
name="orange_red",
|
| 20 |
c50="#FFF0E5",
|
|
@@ -114,8 +109,6 @@ except Exception as e:
|
|
| 114 |
print(f"Warning: Could not set FA3 processor: {e}")
|
| 115 |
|
| 116 |
MAX_SEED = np.iinfo(np.int32).max
|
| 117 |
-
TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp_rerun')
|
| 118 |
-
os.makedirs(TMP_DIR, exist_ok=True)
|
| 119 |
|
| 120 |
ADAPTER_SPECS = {
|
| 121 |
"Multiple-Angles": {
|
|
@@ -179,7 +172,6 @@ def infer(
|
|
| 179 |
if not images:
|
| 180 |
raise gr.Error("Please upload at least one image to edit.")
|
| 181 |
|
| 182 |
-
# --- Process Gallery Input ---
|
| 183 |
pil_images = []
|
| 184 |
if images is not None:
|
| 185 |
for item in images:
|
|
@@ -202,7 +194,6 @@ def infer(
|
|
| 202 |
if not pil_images:
|
| 203 |
raise gr.Error("Could not process uploaded images.")
|
| 204 |
|
| 205 |
-
# --- Load Adapter ---
|
| 206 |
spec = ADAPTER_SPECS.get(lora_adapter)
|
| 207 |
if not spec:
|
| 208 |
raise gr.Error(f"Configuration not found for: {lora_adapter}")
|
|
@@ -225,7 +216,6 @@ def infer(
|
|
| 225 |
|
| 226 |
pipe.set_adapters([adapter_name], adapter_weights=[1.0])
|
| 227 |
|
| 228 |
-
# --- Setup Generation ---
|
| 229 |
if randomize_seed:
|
| 230 |
seed = random.randint(0, MAX_SEED)
|
| 231 |
|
|
@@ -235,8 +225,6 @@ def infer(
|
|
| 235 |
width, height = update_dimensions_on_upload(pil_images[0])
|
| 236 |
|
| 237 |
try:
|
| 238 |
-
progress(0.4, desc="Generating Image...")
|
| 239 |
-
|
| 240 |
result_image = pipe(
|
| 241 |
image=pil_images,
|
| 242 |
prompt=prompt,
|
|
@@ -248,36 +236,7 @@ def infer(
|
|
| 248 |
true_cfg_scale=guidance_scale,
|
| 249 |
).images[0]
|
| 250 |
|
| 251 |
-
|
| 252 |
-
run_id = str(uuid.uuid4())
|
| 253 |
-
output_image_path = os.path.join(TMP_DIR, f"{run_id}_output.png")
|
| 254 |
-
result_image.save(output_image_path)
|
| 255 |
-
|
| 256 |
-
# --- Rerun Visualization Logic ---
|
| 257 |
-
progress(0.9, desc="Preparing Rerun Visualization...")
|
| 258 |
-
|
| 259 |
-
# Handle different Rerun SDK versions
|
| 260 |
-
rec = None
|
| 261 |
-
if hasattr(rr, "new_recording"):
|
| 262 |
-
rec = rr.new_recording(application_id="Qwen-Image-Edit", recording_id=run_id)
|
| 263 |
-
elif hasattr(rr, "RecordingStream"):
|
| 264 |
-
rec = rr.RecordingStream(application_id="Qwen-Image-Edit", recording_id=run_id)
|
| 265 |
-
else:
|
| 266 |
-
rr.init("Qwen-Image-Edit", recording_id=run_id, spawn=False)
|
| 267 |
-
rec = rr
|
| 268 |
-
|
| 269 |
-
# Log inputs
|
| 270 |
-
for i, img in enumerate(pil_images):
|
| 271 |
-
rec.log(f"images/input_{i}", rr.Image(np.array(img)))
|
| 272 |
-
|
| 273 |
-
# Log result
|
| 274 |
-
rec.log("images/edited_result", rr.Image(np.array(result_image)))
|
| 275 |
-
|
| 276 |
-
# Save RRD
|
| 277 |
-
rrd_path = os.path.join(TMP_DIR, f"{run_id}.rrd")
|
| 278 |
-
rec.save(rrd_path)
|
| 279 |
-
|
| 280 |
-
return rrd_path, seed, gr.update(value=output_image_path, visible=True)
|
| 281 |
|
| 282 |
except Exception as e:
|
| 283 |
raise e
|
|
@@ -288,13 +247,15 @@ def infer(
|
|
| 288 |
@spaces.GPU
|
| 289 |
def infer_example(images, prompt, lora_adapter):
|
| 290 |
if not images:
|
| 291 |
-
return None, 0
|
| 292 |
|
| 293 |
if isinstance(images, str):
|
| 294 |
-
|
|
|
|
|
|
|
| 295 |
|
| 296 |
-
|
| 297 |
-
images=
|
| 298 |
prompt=prompt,
|
| 299 |
lora_adapter=lora_adapter,
|
| 300 |
seed=0,
|
|
@@ -302,7 +263,7 @@ def infer_example(images, prompt, lora_adapter):
|
|
| 302 |
guidance_scale=1.0,
|
| 303 |
steps=4
|
| 304 |
)
|
| 305 |
-
return
|
| 306 |
|
| 307 |
css="""
|
| 308 |
#col-container {
|
|
@@ -315,7 +276,7 @@ css="""
|
|
| 315 |
with gr.Blocks() as demo:
|
| 316 |
with gr.Column(elem_id="col-container"):
|
| 317 |
gr.Markdown("# **Qwen-Image-Edit-2511-LoRAs-Fast**", elem_id="main-title")
|
| 318 |
-
gr.Markdown("Perform diverse image edits using specialized [LoRA](https://huggingface.co/models?other=base_model:adapter:Qwen/Qwen-Image-Edit-2511) adapters. Upload one or more images.")
|
| 319 |
|
| 320 |
with gr.Row(equal_height=True):
|
| 321 |
with gr.Column():
|
|
@@ -337,10 +298,7 @@ with gr.Blocks() as demo:
|
|
| 337 |
run_button = gr.Button("Edit Image", variant="primary")
|
| 338 |
|
| 339 |
with gr.Column():
|
| 340 |
-
|
| 341 |
-
label="Rerun Visualization",
|
| 342 |
-
height=354
|
| 343 |
-
)
|
| 344 |
|
| 345 |
with gr.Row():
|
| 346 |
lora_adapter = gr.Dropdown(
|
|
@@ -354,34 +312,27 @@ with gr.Blocks() as demo:
|
|
| 354 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 355 |
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
|
| 356 |
steps = gr.Slider(label="Inference Steps", minimum=1, maximum=50, step=1, value=4)
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
with gr.Accordion("Run Edit Image and Download Result 📂", open=False, visible=True):
|
| 360 |
-
download_button = gr.DownloadButton(
|
| 361 |
-
label="Download Image",
|
| 362 |
-
visible=False,
|
| 363 |
-
)
|
| 364 |
|
| 365 |
gr.Examples(
|
| 366 |
examples=[
|
| 367 |
[["examples/B.jpg"], "Transform into anime.", "Photo-to-Anime"],
|
| 368 |
[["examples/A.jpeg"], "Rotate the camera 45 degrees to the right.", "Multiple-Angles"],
|
| 369 |
[["examples/L1.jpg", "examples/L2.jpg"], "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.", "Light-Migration"],
|
| 370 |
-
[["examples/P1.jpg", "examples/P2.jpg"], "Make the person in image 1 do the exact same pose of the person in image 2. Changing the style and background of the image of the person in image 1 is undesirable, so don't do it.
|
| 371 |
],
|
| 372 |
inputs=[images, prompt, lora_adapter],
|
| 373 |
-
outputs=[
|
| 374 |
fn=infer_example,
|
| 375 |
cache_examples=False,
|
| 376 |
label="Examples"
|
| 377 |
)
|
| 378 |
|
| 379 |
-
gr.Markdown("
|
| 380 |
|
| 381 |
run_button.click(
|
| 382 |
fn=infer,
|
| 383 |
inputs=[images, prompt, lora_adapter, seed, randomize_seed, guidance_scale, steps],
|
| 384 |
-
outputs=[
|
| 385 |
)
|
| 386 |
|
| 387 |
if __name__ == "__main__":
|
|
|
|
| 5 |
import spaces
|
| 6 |
import torch
|
| 7 |
import random
|
|
|
|
|
|
|
| 8 |
from PIL import Image
|
| 9 |
from typing import Iterable
|
| 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",
|
|
|
|
| 109 |
print(f"Warning: Could not set FA3 processor: {e}")
|
| 110 |
|
| 111 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
|
|
|
|
| 112 |
|
| 113 |
ADAPTER_SPECS = {
|
| 114 |
"Multiple-Angles": {
|
|
|
|
| 172 |
if not images:
|
| 173 |
raise gr.Error("Please upload at least one image to edit.")
|
| 174 |
|
|
|
|
| 175 |
pil_images = []
|
| 176 |
if images is not None:
|
| 177 |
for item in images:
|
|
|
|
| 194 |
if not pil_images:
|
| 195 |
raise gr.Error("Could not process uploaded images.")
|
| 196 |
|
|
|
|
| 197 |
spec = ADAPTER_SPECS.get(lora_adapter)
|
| 198 |
if not spec:
|
| 199 |
raise gr.Error(f"Configuration not found for: {lora_adapter}")
|
|
|
|
| 216 |
|
| 217 |
pipe.set_adapters([adapter_name], adapter_weights=[1.0])
|
| 218 |
|
|
|
|
| 219 |
if randomize_seed:
|
| 220 |
seed = random.randint(0, MAX_SEED)
|
| 221 |
|
|
|
|
| 225 |
width, height = update_dimensions_on_upload(pil_images[0])
|
| 226 |
|
| 227 |
try:
|
|
|
|
|
|
|
| 228 |
result_image = pipe(
|
| 229 |
image=pil_images,
|
| 230 |
prompt=prompt,
|
|
|
|
| 236 |
true_cfg_scale=guidance_scale,
|
| 237 |
).images[0]
|
| 238 |
|
| 239 |
+
return result_image, seed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
except Exception as e:
|
| 242 |
raise e
|
|
|
|
| 247 |
@spaces.GPU
|
| 248 |
def infer_example(images, prompt, lora_adapter):
|
| 249 |
if not images:
|
| 250 |
+
return None, 0
|
| 251 |
|
| 252 |
if isinstance(images, str):
|
| 253 |
+
images_list = [images]
|
| 254 |
+
else:
|
| 255 |
+
images_list = images
|
| 256 |
|
| 257 |
+
result, seed = infer(
|
| 258 |
+
images=images_list,
|
| 259 |
prompt=prompt,
|
| 260 |
lora_adapter=lora_adapter,
|
| 261 |
seed=0,
|
|
|
|
| 263 |
guidance_scale=1.0,
|
| 264 |
steps=4
|
| 265 |
)
|
| 266 |
+
return result, seed
|
| 267 |
|
| 268 |
css="""
|
| 269 |
#col-container {
|
|
|
|
| 276 |
with gr.Blocks() as demo:
|
| 277 |
with gr.Column(elem_id="col-container"):
|
| 278 |
gr.Markdown("# **Qwen-Image-Edit-2511-LoRAs-Fast**", elem_id="main-title")
|
| 279 |
+
gr.Markdown("Perform diverse image edits using specialized [LoRA](https://huggingface.co/models?other=base_model:adapter:Qwen/Qwen-Image-Edit-2511) adapters. Upload one or more images (required for tasks like Any-Pose or Light-Migration).")
|
| 280 |
|
| 281 |
with gr.Row(equal_height=True):
|
| 282 |
with gr.Column():
|
|
|
|
| 298 |
run_button = gr.Button("Edit Image", variant="primary")
|
| 299 |
|
| 300 |
with gr.Column():
|
| 301 |
+
output_image = gr.Image(label="Output Image", interactive=False, format="png", height=353)
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
with gr.Row():
|
| 304 |
lora_adapter = gr.Dropdown(
|
|
|
|
| 312 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 313 |
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
|
| 314 |
steps = gr.Slider(label="Inference Steps", minimum=1, maximum=50, step=1, value=4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
gr.Examples(
|
| 317 |
examples=[
|
| 318 |
[["examples/B.jpg"], "Transform into anime.", "Photo-to-Anime"],
|
| 319 |
[["examples/A.jpeg"], "Rotate the camera 45 degrees to the right.", "Multiple-Angles"],
|
| 320 |
[["examples/L1.jpg", "examples/L2.jpg"], "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.", "Light-Migration"],
|
| 321 |
+
[["examples/P1.jpg", "examples/P2.jpg"], "Make the person in image 1 do the exact same pose of the person in image 2. Changing the style and background of the image of the person in image 1 is undesirable, so don't do it.", "Any-Pose"],
|
| 322 |
],
|
| 323 |
inputs=[images, prompt, lora_adapter],
|
| 324 |
+
outputs=[output_image, seed],
|
| 325 |
fn=infer_example,
|
| 326 |
cache_examples=False,
|
| 327 |
label="Examples"
|
| 328 |
)
|
| 329 |
|
| 330 |
+
gr.Markdown("Note: Some adapters (like Any-Pose and Light-Migration) require uploading multiple images to the gallery.")
|
| 331 |
|
| 332 |
run_button.click(
|
| 333 |
fn=infer,
|
| 334 |
inputs=[images, prompt, lora_adapter, seed, randomize_seed, guidance_scale, steps],
|
| 335 |
+
outputs=[output_image, seed]
|
| 336 |
)
|
| 337 |
|
| 338 |
if __name__ == "__main__":
|