Spaces:
Running on Zero
Running on Zero
File size: 5,631 Bytes
a4d53c9 2c67f17 a4d53c9 2c67f17 a4d53c9 2c67f17 a4d53c9 2c67f17 a4d53c9 2c67f17 a4d53c9 2c67f17 a4d53c9 2c67f17 a4d53c9 2c67f17 a4d53c9 2c67f17 a4d53c9 2c67f17 a4d53c9 2c67f17 a4d53c9 2c67f17 a4d53c9 2c67f17 a4d53c9 2c67f17 a4d53c9 2c67f17 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | import gradio as gr
import numpy as np
import random
import torch
import spaces
from typing import Iterable
from gradio.themes import Soft
from gradio.themes.utils import colors, fonts, sizes
from diffusers.models import QwenImageTransformer2DModel
from diffusers import QwenImageEditPlusPipeline
colors.indigo_pink = colors.Color(
name="indigo_pink",
c50="#EEF0FF", c100="#DEE1FF", c200="#BDC3FF", c300="#9CA5FF",
c400="#7B87FF", c500="#4F46E5", c600="#4338CA", c700="#3730A3",
c800="#312E81", c900="#27245F", c950="#1B1940",
)
class IndigoPinkTheme(Soft):
def __init__(
self,
*,
primary_hue=colors.gray,
secondary_hue=colors.indigo_pink,
neutral_hue=colors.slate,
text_size=sizes.text_lg,
font: Iterable = (fonts.GoogleFont("Outfit"), "Arial", "sans-serif"),
font_mono: Iterable = (fonts.GoogleFont("IBM Plex Mono"), "ui-monospace", "monospace"),
):
super().__init__(
primary_hue=primary_hue, secondary_hue=secondary_hue, neutral_hue=neutral_hue,
text_size=text_size, font=font, font_mono=font_mono,
)
super().set(
background_fill_primary="*primary_50",
background_fill_primary_dark="*primary_900",
body_background_fill="linear-gradient(135deg, *primary_200, *primary_100)",
body_background_fill_dark="linear-gradient(135deg, *primary_900, *primary_800)",
button_primary_text_color="white",
button_primary_background_fill="linear-gradient(90deg, *secondary_500, *secondary_600)",
button_primary_background_fill_hover="linear-gradient(90deg, *secondary_600, *secondary_700)",
slider_color="*secondary_500",
block_title_text_weight="600",
block_border_width="3px",
block_shadow="*shadow_drop_lg",
)
theme = IndigoPinkTheme()
AIO_REPO_ID = "prithivMLmods/Qwen-Image-Edit-Rapid-AIO-V23"
MAX_SEED = np.iinfo(np.int32).max
dtype = torch.bfloat16
device = "cuda" if torch.cuda.is_available() else "cpu"
transformer = QwenImageTransformer2DModel.from_pretrained(
AIO_REPO_ID, torch_dtype=dtype, device_map="cuda"
)
pipe = QwenImageEditPlusPipeline.from_pretrained(
"Qwen/Qwen-Image-Edit-2511", transformer=transformer, torch_dtype=dtype
).to(device)
def compute_target_size(image, target_megapixels=1.0, multiple=32):
w, h = image.size
aspect = w / h if h else 1.0
target_area = target_megapixels * 1024 * 1024
new_w = int((target_area * aspect) ** 0.5)
new_h = int((target_area / aspect) ** 0.5)
new_w = max(multiple, (new_w // multiple) * multiple)
new_h = max(multiple, (new_h // multiple) * multiple)
return new_w, new_h
@spaces.GPU(duration=120)
def infer(image, prompt, seed=0, randomize_seed=True, true_guidance_scale=1.0,
num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
if image is None:
raise gr.Error("Please upload an image to edit.")
if not prompt or not prompt.strip():
raise gr.Error("Please describe the edit you want.")
if randomize_seed:
seed = random.randint(0, MAX_SEED)
generator = torch.Generator(device=device).manual_seed(int(seed))
img = image.convert("RGB")
width, height = compute_target_size(img)
result = pipe(
image=[img],
prompt=prompt.strip(),
negative_prompt=" ",
height=height,
width=width,
num_inference_steps=int(num_inference_steps),
generator=generator,
true_cfg_scale=float(true_guidance_scale),
num_images_per_prompt=1,
).images[0]
return result, seed
css = """
#col-container {
margin: 0 auto;
max-width: 960px;
}
#main-title h1 {font-size: 2.1em !important;}
"""
with gr.Blocks(title="Qwen Image Edit -- Rapid AIO v23") as demo:
with gr.Column(elem_id="col-container"):
gr.Markdown("# **Qwen Image Edit -- Rapid AIO v23**", elem_id="main-title")
gr.Markdown(
"Upload an image and describe the edit you want. "
"Powered by Phr00t's Rapid AIO v23 (extracted transformer) on ZeroGPU."
)
with gr.Row(equal_height=True):
with gr.Column():
input_image = gr.Image(label="Input Image", show_label=False, type="pil", height=320)
with gr.Column():
result = gr.Image(label="Result", show_label=False, type="pil", height=320)
with gr.Row():
prompt = gr.Text(
label="Prompt", show_label=False,
placeholder="describe the edit instruction",
container=False, scale=4,
)
run_button = gr.Button("Edit!", variant="primary", scale=1)
with gr.Accordion("Advanced Settings", open=False):
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
with gr.Row():
true_guidance_scale = gr.Slider(label="True guidance scale (CFG)", minimum=1.0, maximum=6.0, step=0.1, value=1.0)
num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=12, step=1, value=4)
gr.on(
triggers=[run_button.click, prompt.submit],
fn=infer,
inputs=[input_image, prompt, seed, randomize_seed, true_guidance_scale, num_inference_steps],
outputs=[result, seed],
)
if __name__ == "__main__":
demo.queue(max_size=20).launch(css=css, theme=theme, ssr_mode=False, show_error=True)
|