Spaces:
Running on Zero
Running on Zero
Download app.py from pakdeveloper/QIE_RAPID_AIO_V23: direct link, hf CLI and curl.
- Browser
- Download file 5.63 kB
-
https://huggingface.co/spaces/pakdeveloper/QIE_RAPID_AIO_V23/resolve/main/app.py
- Command line
-
hf download hf://spaces/pakdeveloper/QIE_RAPID_AIO_V23/app.py
-
curl -L -o app.py https://huggingface.co/spaces/pakdeveloper/QIE_RAPID_AIO_V23/resolve/main/app.py
5.63 kB
| 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 | |
| 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) | |