Spaces:
Running
on
Zero
Running
on
Zero
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,224 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import numpy as np
|
| 4 |
-
import spaces
|
| 5 |
-
import torch
|
| 6 |
-
import random
|
| 7 |
-
from PIL import Image
|
| 8 |
-
from typing import Iterable
|
| 9 |
-
from gradio.themes import Soft
|
| 10 |
-
from gradio.themes.utils import colors, fonts, sizes
|
| 11 |
-
|
| 12 |
-
colors.steel_blue = colors.Color(
|
| 13 |
-
name="steel_blue",
|
| 14 |
-
c50="#EBF3F8",
|
| 15 |
-
c100="#D3E5F0",
|
| 16 |
-
c200="#A8CCE1",
|
| 17 |
-
c300="#7DB3D2",
|
| 18 |
-
c400="#529AC3",
|
| 19 |
-
c500="#4682B4",
|
| 20 |
-
c600="#3E72A0",
|
| 21 |
-
c700="#36638C",
|
| 22 |
-
c800="#2E5378",
|
| 23 |
-
c900="#264364",
|
| 24 |
-
c950="#1E3450",
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
class SteelBlueTheme(Soft):
|
| 28 |
-
def __init__(
|
| 29 |
-
self,
|
| 30 |
-
*,
|
| 31 |
-
primary_hue: colors.Color | str = colors.gray,
|
| 32 |
-
secondary_hue: colors.Color | str = colors.steel_blue,
|
| 33 |
-
neutral_hue: colors.Color | str = colors.slate,
|
| 34 |
-
text_size: sizes.Size | str = sizes.text_lg,
|
| 35 |
-
font: fonts.Font | str | Iterable[fonts.Font | str] = (
|
| 36 |
-
fonts.GoogleFont("Outfit"), "Arial", "sans-serif",
|
| 37 |
-
),
|
| 38 |
-
font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
|
| 39 |
-
fonts.GoogleFont("IBM Plex Mono"), "ui-monospace", "monospace",
|
| 40 |
-
),
|
| 41 |
-
):
|
| 42 |
-
super().__init__(
|
| 43 |
-
primary_hue=primary_hue,
|
| 44 |
-
secondary_hue=secondary_hue,
|
| 45 |
-
neutral_hue=neutral_hue,
|
| 46 |
-
text_size=text_size,
|
| 47 |
-
font=font,
|
| 48 |
-
font_mono=font_mono,
|
| 49 |
-
)
|
| 50 |
-
super().set(
|
| 51 |
-
background_fill_primary="*primary_50",
|
| 52 |
-
background_fill_primary_dark="*primary_900",
|
| 53 |
-
body_background_fill="linear-gradient(135deg, *primary_200, *primary_100)",
|
| 54 |
-
body_background_fill_dark="linear-gradient(135deg, *primary_900, *primary_800)",
|
| 55 |
-
button_primary_text_color="white",
|
| 56 |
-
button_primary_text_color_hover="white",
|
| 57 |
-
button_primary_background_fill="linear-gradient(90deg, *secondary_500, *secondary_600)",
|
| 58 |
-
button_primary_background_fill_hover="linear-gradient(90deg, *secondary_600, *secondary_700)",
|
| 59 |
-
slider_color="*secondary_500",
|
| 60 |
-
slider_color_dark="*secondary_600",
|
| 61 |
-
block_title_text_weight="600",
|
| 62 |
-
block_border_width="3px",
|
| 63 |
-
block_shadow="*shadow_drop_lg",
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
steel_blue_theme = SteelBlueTheme()
|
| 67 |
-
|
| 68 |
-
from diffusers import FlowMatchEulerDiscreteScheduler
|
| 69 |
-
# from optimization import optimize_pipeline_ # Assuming this is a custom file, if not available, comment out the call
|
| 70 |
-
from qwenimage.pipeline_qwenimage_edit_plus import QwenImageEditPlusPipeline
|
| 71 |
-
from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel
|
| 72 |
-
from qwenimage.qwen_fa3_processor import QwenDoubleStreamAttnProcessorFA3
|
| 73 |
-
|
| 74 |
-
dtype = torch.bfloat16
|
| 75 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 76 |
-
|
| 77 |
-
pipe = QwenImageEditPlusPipeline.from_pretrained(
|
| 78 |
-
"Qwen/Qwen-Image-Edit-2509",
|
| 79 |
-
transformer=QwenImageTransformer2DModel.from_pretrained(
|
| 80 |
-
"linoyts/Qwen-Image-Edit-Rapid-AIO",
|
| 81 |
-
subfolder='transformer',
|
| 82 |
-
torch_dtype=dtype,
|
| 83 |
-
device_map='cuda'
|
| 84 |
-
),
|
| 85 |
-
torch_dtype=dtype
|
| 86 |
-
).to(device)
|
| 87 |
-
|
| 88 |
-
pipe.load_lora_weights("autoweeb/Qwen-Image-Edit-2509-Photo-to-Anime",
|
| 89 |
-
weight_name="Qwen-Image-Edit-2509-Photo-to-Anime_000001000.safetensors",
|
| 90 |
-
adapter_name="anime")
|
| 91 |
-
pipe.load_lora_weights("dx8152/Qwen-Edit-2509-Multiple-angles",
|
| 92 |
-
weight_name="镜头转换.safetensors",
|
| 93 |
-
adapter_name="multiple-angles")
|
| 94 |
-
pipe.load_lora_weights("dx8152/Qwen-Image-Edit-2509-Light_restoration",
|
| 95 |
-
weight_name="移除光影.safetensors",
|
| 96 |
-
adapter_name="light-restoration")
|
| 97 |
-
pipe.load_lora_weights("dx8152/Qwen-Image-Edit-2509-Relight",
|
| 98 |
-
weight_name="Qwen-Edit-Relight.safetensors",
|
| 99 |
-
adapter_name="relight")
|
| 100 |
-
|
| 101 |
-
pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
|
| 102 |
-
MAX_SEED = np.iinfo(np.int32).max
|
| 103 |
-
|
| 104 |
-
@spaces.GPU
|
| 105 |
-
def infer(
|
| 106 |
-
input_image,
|
| 107 |
-
prompt,
|
| 108 |
-
lora_adapter,
|
| 109 |
-
seed,
|
| 110 |
-
randomize_seed,
|
| 111 |
-
guidance_scale,
|
| 112 |
-
steps,
|
| 113 |
-
progress=gr.Progress(track_tqdm=True)
|
| 114 |
-
):
|
| 115 |
-
if input_image is None:
|
| 116 |
-
raise gr.Error("Please upload an image to edit.")
|
| 117 |
-
|
| 118 |
-
if lora_adapter == "Photo-to-Anime":
|
| 119 |
-
pipe.set_adapters(["anime"], adapter_weights=[1.0])
|
| 120 |
-
elif lora_adapter == "Multiple-Angles":
|
| 121 |
-
pipe.set_adapters(["multiple-angles"], adapter_weights=[1.0])
|
| 122 |
-
elif lora_adapter == "Light-Restoration":
|
| 123 |
-
pipe.set_adapters(["light-restoration"], adapter_weights=[1.0])
|
| 124 |
-
elif lora_adapter == "Relight":
|
| 125 |
-
pipe.set_adapters(["relight"], adapter_weights=[1.0])
|
| 126 |
-
|
| 127 |
-
if randomize_seed:
|
| 128 |
-
seed = random.randint(0, MAX_SEED)
|
| 129 |
-
|
| 130 |
-
generator = torch.Generator(device=device).manual_seed(seed)
|
| 131 |
-
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"
|
| 132 |
-
|
| 133 |
-
original_image = input_image.convert("RGB")
|
| 134 |
-
width, height = original_image.size
|
| 135 |
-
|
| 136 |
-
result = pipe(
|
| 137 |
-
image=original_image,
|
| 138 |
-
prompt=prompt,
|
| 139 |
-
negative_prompt=negative_prompt,
|
| 140 |
-
height=height,
|
| 141 |
-
width=width,
|
| 142 |
-
num_inference_steps=steps,
|
| 143 |
-
generator=generator,
|
| 144 |
-
true_cfg_scale=guidance_scale,
|
| 145 |
-
).images[0]
|
| 146 |
-
|
| 147 |
-
return result, seed
|
| 148 |
-
|
| 149 |
-
@spaces.GPU
|
| 150 |
-
def infer_example(input_image, prompt, lora_adapter):
|
| 151 |
-
input_pil = input_image.convert("RGB")
|
| 152 |
-
guidance_scale = 1.0
|
| 153 |
-
steps = 4
|
| 154 |
-
result, seed = infer(input_pil, prompt, lora_adapter, 0, True, guidance_scale, steps)
|
| 155 |
-
return result, seed
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
css="""
|
| 159 |
-
#col-container {
|
| 160 |
-
margin: 0 auto;
|
| 161 |
-
max-width: 960px;
|
| 162 |
-
}
|
| 163 |
-
#main-title h1 {font-size: 2.1em !important;}
|
| 164 |
-
"""
|
| 165 |
-
|
| 166 |
-
with gr.Blocks(css=css, theme=steel_blue_theme) as demo:
|
| 167 |
-
with gr.Column(elem_id="col-container"):
|
| 168 |
-
gr.Markdown("# **Qwen-Image-Edit-2509-LoRAs-Fast**", elem_id="main-title")
|
| 169 |
-
gr.Markdown("Perform diverse image edits using specialized LoRA adapters for the Qwen-Image-Edit model.")
|
| 170 |
-
|
| 171 |
-
with gr.Row(equal_height=True):
|
| 172 |
-
with gr.Column():
|
| 173 |
-
input_image = gr.Image(label="Upload Image", type="pil")
|
| 174 |
-
|
| 175 |
-
prompt = gr.Text(
|
| 176 |
-
label="Edit Prompt",
|
| 177 |
-
show_label=True,
|
| 178 |
-
placeholder="e.g., transform into anime",
|
| 179 |
-
)
|
| 180 |
-
|
| 181 |
-
run_button = gr.Button("Run", variant="primary")
|
| 182 |
-
|
| 183 |
-
with gr.Column():
|
| 184 |
-
output_image = gr.Image(label="Output Image", interactive=False, format="png", height=290)
|
| 185 |
-
|
| 186 |
-
with gr.Row():
|
| 187 |
-
lora_adapter = gr.Dropdown(
|
| 188 |
-
label="Choose Editing Style",
|
| 189 |
-
choices=["Photo-to-Anime", "Multiple-Angles", "Light-Restoration", "Relight"],
|
| 190 |
-
value="Photo-to-Anime"
|
| 191 |
-
)
|
| 192 |
-
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 193 |
-
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 194 |
-
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 195 |
-
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
|
| 196 |
-
steps = gr.Slider(label="Inference Steps", minimum=1, maximum=50, step=1, value=4)
|
| 197 |
-
|
| 198 |
-
gr.Examples(
|
| 199 |
-
examples=[
|
| 200 |
-
["examples/1.jpg", "Transform into anime.", "Photo-to-Anime"],
|
| 201 |
-
["examples/5.jpg", "Remove shadows and relight the image using soft lighting.", "Light-Restoration"],
|
| 202 |
-
["examples/4.jpg", "Relight the image using soft, diffused lighting that simulates sunlight filtering.", "Relight"],
|
| 203 |
-
["examples/2.jpeg", "Move the camera left.", "Multiple-Angles"],
|
| 204 |
-
["examples/2.jpeg", "Move the camera right.", "Multiple-Angles"],
|
| 205 |
-
["examples/2.jpeg", "Rotate the camera 45 degrees to the left.", "Multiple-Angles"],
|
| 206 |
-
["examples/3.jpg", "Rotate the camera 45 degrees to the right.", "Multiple-Angles"],
|
| 207 |
-
["examples/3.jpg", "Switch the camera to a top-down view.", "Multiple-Angles"],
|
| 208 |
-
["examples/3.jpg", "Switch the camera to a wide-angle lens.", "Multiple-Angles"],
|
| 209 |
-
["examples/3.jpg", "Switch the camera to a close-up lens.", "Multiple-Angles"],
|
| 210 |
-
],
|
| 211 |
-
inputs=[input_image, prompt, lora_adapter],
|
| 212 |
-
outputs=[output_image, seed],
|
| 213 |
-
fn=infer_example,
|
| 214 |
-
cache_examples=False,
|
| 215 |
-
label="Examples"
|
| 216 |
-
)
|
| 217 |
-
|
| 218 |
-
run_button.click(
|
| 219 |
-
fn=infer,
|
| 220 |
-
inputs=[input_image, prompt, lora_adapter, seed, randomize_seed, guidance_scale, steps],
|
| 221 |
-
outputs=[output_image, seed]
|
| 222 |
-
)
|
| 223 |
-
|
| 224 |
-
demo.launch(mcp_server=True, ssr_mode=False, show_error=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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