prithivMLmods commited on
Commit
c5967fe
·
verified ·
1 Parent(s): a94c778

update app

Browse files
Files changed (1) hide show
  1. app.py +0 -311
app.py CHANGED
@@ -309,316 +309,5 @@ with gr.Blocks() as demo:
309
  outputs=[rerun_output, seed]
310
  )
311
 
312
- if __name__ == "__main__":
313
- demo.queue(max_size=30).launch(css=css, theme=deep_sky_blue_theme, mcp_server=True, ssr_mode=False, show_error=True)import os
314
- import gc
315
- import gradio as gr
316
- import numpy as np
317
- import spaces
318
- import torch
319
- import random
320
- import uuid
321
- import tempfile
322
- from PIL import Image
323
- from typing import Iterable
324
- from gradio.themes import Soft
325
- from gradio.themes.utils import colors, fonts, sizes
326
-
327
- # Rerun imports
328
- import rerun as rr
329
- from gradio_rerun import Rerun
330
-
331
- colors.deep_sky_blue = colors.Color(
332
- name="deep_sky_blue",
333
- c50="#E0F7FF",
334
- c100="#B3EAFF",
335
- c200="#80DFFF",
336
- c300="#4DD2FF",
337
- c400="#1AC6FF",
338
- c500="#00BFFF",
339
- c600="#0099CC",
340
- c700="#007399",
341
- c800="#004C66",
342
- c900="#002633",
343
- c950="#00131A",
344
- )
345
-
346
- class DeepSkyBlueTheme(Soft):
347
- def __init__(
348
- self,
349
- *,
350
- primary_hue: colors.Color | str = colors.gray,
351
- secondary_hue: colors.Color | str = colors.deep_sky_blue,
352
- neutral_hue: colors.Color | str = colors.slate,
353
- text_size: sizes.Size | str = sizes.text_lg,
354
- font: fonts.Font | str | Iterable[fonts.Font | str] = (
355
- fonts.GoogleFont("Outfit"), "Arial", "sans-serif",
356
- ),
357
- font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
358
- fonts.GoogleFont("IBM Plex Mono"), "ui-monospace", "monospace",
359
- ),
360
- ):
361
- super().__init__(
362
- primary_hue=primary_hue,
363
- secondary_hue=secondary_hue,
364
- neutral_hue=neutral_hue,
365
- text_size=text_size,
366
- font=font,
367
- font_mono=font_mono,
368
- )
369
- super().set(
370
- background_fill_primary="*primary_50",
371
- background_fill_primary_dark="*primary_900",
372
- body_background_fill="linear-gradient(135deg, *primary_100, #E0F7FF)",
373
- body_background_fill_dark="linear-gradient(135deg, *primary_900, *primary_800)",
374
- button_primary_text_color="white",
375
- button_primary_text_color_hover="white",
376
- button_primary_background_fill="linear-gradient(90deg, *secondary_500, *secondary_600)",
377
- button_primary_background_fill_hover="linear-gradient(90deg, *secondary_600, *secondary_700)",
378
- button_primary_background_fill_dark="linear-gradient(90deg, *secondary_600, *secondary_700)",
379
- button_primary_background_fill_hover_dark="linear-gradient(90deg, *secondary_500, *secondary_600)",
380
- button_secondary_text_color="black",
381
- button_secondary_text_color_hover="white",
382
- button_secondary_background_fill="linear-gradient(90deg, *primary_300, *primary_300)",
383
- button_secondary_background_fill_hover="linear-gradient(90deg, *primary_400, *primary_400)",
384
- button_secondary_background_fill_dark="linear-gradient(90deg, *primary_500, *primary_600)",
385
- button_secondary_background_fill_hover_dark="linear-gradient(90deg, *primary_500, *primary_500)",
386
- slider_color="*secondary_500",
387
- slider_color_dark="*secondary_600",
388
- block_title_text_weight="600",
389
- block_border_width="3px",
390
- block_shadow="*shadow_drop_lg",
391
- button_primary_shadow="*shadow_drop_lg",
392
- button_large_padding="11px",
393
- color_accent_soft="*primary_100",
394
- block_label_background_fill="*primary_200",
395
- )
396
-
397
- deep_sky_blue_theme = DeepSkyBlueTheme()
398
-
399
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
400
-
401
- print("CUDA_VISIBLE_DEVICES=", os.environ.get("CUDA_VISIBLE_DEVICES"))
402
- print("torch.__version__ =", torch.__version__)
403
- print("torch.version.cuda =", torch.version.cuda)
404
- print("Using device:", device)
405
-
406
- from diffusers import FlowMatchEulerDiscreteScheduler
407
- from qwenimage.pipeline_qwenimage_edit_plus import QwenImageEditPlusPipeline
408
- from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel
409
- from qwenimage.qwen_fa3_processor import QwenDoubleStreamAttnProcessorFA3
410
-
411
- dtype = torch.bfloat16
412
-
413
- pipe = QwenImageEditPlusPipeline.from_pretrained(
414
- "Qwen/Qwen-Image-Edit-2511",
415
- transformer=QwenImageTransformer2DModel.from_pretrained(
416
- "linoyts/Qwen-Image-Edit-Rapid-AIO",
417
- subfolder='transformer',
418
- torch_dtype=dtype,
419
- device_map='cuda'
420
- ),
421
- torch_dtype=dtype
422
- ).to(device)
423
-
424
- try:
425
- pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
426
- print("Flash Attention 3 Processor set successfully.")
427
- except Exception as e:
428
- print(f"Warning: Could not set FA3 processor: {e}")
429
-
430
- MAX_SEED = np.iinfo(np.int32).max
431
- TMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp_rerun')
432
- os.makedirs(TMP_DIR, exist_ok=True)
433
-
434
- ADAPTER_SPECS = {
435
- "Multiple-Angles": {
436
- "repo": "dx8152/Qwen-Edit-2509-Multiple-angles",
437
- "weights": "镜头转换.safetensors",
438
- "adapter_name": "multiple-angles"
439
- }
440
- }
441
-
442
- LOADED_ADAPTERS = set()
443
-
444
- def update_dimensions_on_upload(image):
445
- if image is None:
446
- return 1024, 1024
447
-
448
- original_width, original_height = image.size
449
-
450
- if original_width > original_height:
451
- new_width = 1024
452
- aspect_ratio = original_height / original_width
453
- new_height = int(new_width * aspect_ratio)
454
- else:
455
- new_height = 1024
456
- aspect_ratio = original_width / original_height
457
- new_width = int(new_height * aspect_ratio)
458
-
459
- new_width = (new_width // 8) * 8
460
- new_height = (new_height // 8) * 8
461
-
462
- return new_width, new_height
463
-
464
- @spaces.GPU
465
- def infer(
466
- input_image,
467
- prompt,
468
- lora_adapter,
469
- seed,
470
- randomize_seed,
471
- guidance_scale,
472
- steps,
473
- progress=gr.Progress(track_tqdm=True)
474
- ):
475
- gc.collect()
476
- torch.cuda.empty_cache()
477
-
478
- if input_image is None:
479
- raise gr.Error("Please upload an image to edit.")
480
-
481
- spec = ADAPTER_SPECS.get(lora_adapter)
482
- if not spec:
483
- raise gr.Error(f"Configuration not found for: {lora_adapter}")
484
-
485
- adapter_name = spec["adapter_name"]
486
-
487
- if adapter_name not in LOADED_ADAPTERS:
488
- print(f"--- Downloading and Loading Adapter: {lora_adapter} ---")
489
- try:
490
- pipe.load_lora_weights(
491
- spec["repo"],
492
- weight_name=spec["weights"],
493
- adapter_name=adapter_name
494
- )
495
- LOADED_ADAPTERS.add(adapter_name)
496
- except Exception as e:
497
- raise gr.Error(f"Failed to load adapter {lora_adapter}: {e}")
498
- else:
499
- print(f"--- Adapter {lora_adapter} is already loaded. ---")
500
-
501
- pipe.set_adapters([adapter_name], adapter_weights=[1.0])
502
-
503
- if randomize_seed:
504
- seed = random.randint(0, MAX_SEED)
505
-
506
- generator = torch.Generator(device=device).manual_seed(seed)
507
- negative_prompt = "worst quality, low quality, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, jpeg artifacts, signature, watermark, username, blurry"
508
-
509
- original_image = input_image.convert("RGB")
510
- width, height = update_dimensions_on_upload(original_image)
511
-
512
- try:
513
- progress(0.4, desc="Generating Image...")
514
- result_image = pipe(
515
- image=original_image,
516
- prompt=prompt,
517
- negative_prompt=negative_prompt,
518
- height=height,
519
- width=width,
520
- num_inference_steps=steps,
521
- generator=generator,
522
- true_cfg_scale=guidance_scale,
523
- ).images[0]
524
-
525
- # --- Rerun Visualization Logic ---
526
- progress(0.9, desc="Preparing Rerun Visualization...")
527
-
528
- run_id = str(uuid.uuid4())
529
- rec = rr.new_recording(application_id="Qwen-Image-Edit", recording_id=run_id)
530
-
531
- # Log images to Rerun
532
- # We convert PIL images to numpy arrays for Rerun
533
- rec.log("images/original", rr.Image(np.array(original_image)))
534
- rec.log("images/edited", rr.Image(np.array(result_image)))
535
-
536
- # Save RRD
537
- rrd_path = os.path.join(TMP_DIR, f"{run_id}.rrd")
538
- rec.save(rrd_path)
539
-
540
- return rrd_path, seed
541
-
542
- except Exception as e:
543
- raise e
544
- finally:
545
- gc.collect()
546
- torch.cuda.empty_cache()
547
-
548
- @spaces.GPU
549
- def infer_example(input_image, prompt, lora_adapter):
550
- if input_image is None:
551
- return None, 0
552
-
553
- input_pil = input_image.convert("RGB")
554
- guidance_scale = 1.0
555
- steps = 4
556
- # Call main infer but ignore progress for examples if needed
557
- result_rrd, seed = infer(input_pil, prompt, lora_adapter, 0, True, guidance_scale, steps)
558
- return result_rrd, seed
559
-
560
- css="""
561
- #col-container {
562
- margin: 0 auto;
563
- max-width: 960px;
564
- }
565
- #main-title h1 {font-size: 2.1em !important;}
566
- """
567
-
568
- with gr.Blocks() as demo:
569
- with gr.Column(elem_id="col-container"):
570
- gr.Markdown("# **Qwen-Image-Edit-2511-LoRAs-Fast**", elem_id="main-title")
571
- gr.Markdown("Perform diverse image edits using specialized [LoRA](https://huggingface.co/models?other=base_model:adapter:Qwen/Qwen-Image-Edit-2511) adapters for the [Qwen-Image-Edit](https://huggingface.co/Qwen/Qwen-Image-Edit-2511) model.")
572
-
573
- with gr.Row(equal_height=True):
574
- with gr.Column():
575
- input_image = gr.Image(label="Upload Image", type="pil", height=290)
576
-
577
- prompt = gr.Text(
578
- label="Edit Prompt",
579
- show_label=True,
580
- placeholder="e.g., transform into anime..",
581
- )
582
-
583
- run_button = gr.Button("Edit Image", variant="primary")
584
-
585
- with gr.Column():
586
- # Replaced standard Image with Rerun Viewer
587
- rerun_output = Rerun(
588
- label="Rerun Visualization",
589
- height=353
590
- )
591
-
592
- with gr.Row():
593
- lora_adapter = gr.Dropdown(
594
- label="Choose Editing Style",
595
- choices=list(ADAPTER_SPECS.keys()),
596
- value="Multiple-Angles"
597
- )
598
- with gr.Accordion("Advanced Settings", open=False, visible=False):
599
- seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
600
- randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
601
- guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
602
- steps = gr.Slider(label="Inference Steps", minimum=1, maximum=50, step=1, value=4)
603
-
604
- gr.Examples(
605
- examples=[
606
- ["examples/A.jpeg", "Rotate the camera 45 degrees to the right.", "Multiple-Angles"],
607
- ],
608
- inputs=[input_image, prompt, lora_adapter],
609
- outputs=[rerun_output, seed],
610
- fn=infer_example,
611
- cache_examples=False,
612
- label="Examples"
613
- )
614
-
615
- gr.Markdown("[*](https://huggingface.co/spaces/prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast)This is still an experimental Space for Qwen-Image-Edit-2511; you can use [Qwen-Image-Edit-2509-LoRAs-Fast](https://huggingface.co/spaces/prithivMLmods/Qwen-Image-Edit-2509-LoRAs-Fast) instead. This Space will be updated soon.")
616
-
617
- run_button.click(
618
- fn=infer,
619
- inputs=[input_image, prompt, lora_adapter, seed, randomize_seed, guidance_scale, steps],
620
- outputs=[rerun_output, seed]
621
- )
622
-
623
  if __name__ == "__main__":
624
  demo.queue(max_size=30).launch(css=css, theme=deep_sky_blue_theme, mcp_server=True, ssr_mode=False, show_error=True)
 
309
  outputs=[rerun_output, seed]
310
  )
311
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312
  if __name__ == "__main__":
313
  demo.queue(max_size=30).launch(css=css, theme=deep_sky_blue_theme, mcp_server=True, ssr_mode=False, show_error=True)