yingzhac-research commited on
Commit ·
4ecea9e
1
Parent(s): 921baab
Expose steps and guidance controls in UI
Browse files
app.py
CHANGED
|
@@ -3,9 +3,10 @@ import numpy as np
|
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
import spaces #
|
| 7 |
from diffusers import QwenImageEditPlusPipeline
|
| 8 |
|
|
|
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
| 11 |
if device == "cuda":
|
|
@@ -22,12 +23,16 @@ pipe.set_progress_bar_config(disable=None)
|
|
| 22 |
MAX_SEED = np.iinfo(np.int32).max
|
| 23 |
|
| 24 |
|
| 25 |
-
@spaces.GPU
|
| 26 |
def edit_images(
|
| 27 |
image1: Image.Image,
|
| 28 |
image2: Image.Image,
|
| 29 |
prompt: str,
|
|
|
|
| 30 |
seed: int,
|
|
|
|
|
|
|
|
|
|
| 31 |
progress=gr.Progress(track_tqdm=True),
|
| 32 |
):
|
| 33 |
# 必须有 prompt,且至少有一张图片
|
|
@@ -44,6 +49,12 @@ def edit_images(
|
|
| 44 |
if len(images) == 0:
|
| 45 |
return None, seed
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
generator = torch.Generator(device=device).manual_seed(seed)
|
| 48 |
|
| 49 |
inputs = {
|
|
@@ -51,10 +62,10 @@ def edit_images(
|
|
| 51 |
"image": images,
|
| 52 |
"prompt": prompt,
|
| 53 |
"generator": generator,
|
| 54 |
-
"true_cfg_scale":
|
| 55 |
-
"negative_prompt":
|
| 56 |
-
"num_inference_steps":
|
| 57 |
-
"guidance_scale":
|
| 58 |
"num_images_per_prompt": 1,
|
| 59 |
}
|
| 60 |
|
|
@@ -91,7 +102,12 @@ with gr.Blocks(css=css) as demo:
|
|
| 91 |
prompt = gr.Textbox(
|
| 92 |
label="Text Prompt",
|
| 93 |
lines=4,
|
| 94 |
-
placeholder="Describe how the
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
)
|
| 96 |
seed = gr.Slider(
|
| 97 |
label="Seed",
|
|
@@ -100,6 +116,27 @@ with gr.Blocks(css=css) as demo:
|
|
| 100 |
step=1,
|
| 101 |
value=0,
|
| 102 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
run_button = gr.Button("Generate", variant="primary")
|
| 104 |
|
| 105 |
result = gr.Image(label="Edited Image", show_label=True)
|
|
@@ -107,9 +144,20 @@ with gr.Blocks(css=css) as demo:
|
|
| 107 |
gr.on(
|
| 108 |
triggers=[run_button.click, prompt.submit],
|
| 109 |
fn=edit_images,
|
| 110 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
outputs=[result, seed],
|
| 112 |
)
|
| 113 |
|
|
|
|
| 114 |
if __name__ == "__main__":
|
| 115 |
demo.launch()
|
|
|
|
|
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
import spaces # use ZeroGPU on Spaces
|
| 7 |
from diffusers import QwenImageEditPlusPipeline
|
| 8 |
|
| 9 |
+
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
|
| 12 |
if device == "cuda":
|
|
|
|
| 23 |
MAX_SEED = np.iinfo(np.int32).max
|
| 24 |
|
| 25 |
|
| 26 |
+
@spaces.GPU
|
| 27 |
def edit_images(
|
| 28 |
image1: Image.Image,
|
| 29 |
image2: Image.Image,
|
| 30 |
prompt: str,
|
| 31 |
+
negative_prompt: str,
|
| 32 |
seed: int,
|
| 33 |
+
num_inference_steps: int,
|
| 34 |
+
guidance_scale: float,
|
| 35 |
+
true_cfg_scale: float,
|
| 36 |
progress=gr.Progress(track_tqdm=True),
|
| 37 |
):
|
| 38 |
# 必须有 prompt,且至少有一张图片
|
|
|
|
| 49 |
if len(images) == 0:
|
| 50 |
return None, seed
|
| 51 |
|
| 52 |
+
# gr.Slider 返回的是 float/int,这里确保为 int
|
| 53 |
+
num_inference_steps = int(num_inference_steps)
|
| 54 |
+
|
| 55 |
+
# 负向提示为空时,用一个空格占位以兼容管线
|
| 56 |
+
negative_prompt_value = negative_prompt if negative_prompt else " "
|
| 57 |
+
|
| 58 |
generator = torch.Generator(device=device).manual_seed(seed)
|
| 59 |
|
| 60 |
inputs = {
|
|
|
|
| 62 |
"image": images,
|
| 63 |
"prompt": prompt,
|
| 64 |
"generator": generator,
|
| 65 |
+
"true_cfg_scale": true_cfg_scale,
|
| 66 |
+
"negative_prompt": negative_prompt_value,
|
| 67 |
+
"num_inference_steps": num_inference_steps,
|
| 68 |
+
"guidance_scale": guidance_scale,
|
| 69 |
"num_images_per_prompt": 1,
|
| 70 |
}
|
| 71 |
|
|
|
|
| 102 |
prompt = gr.Textbox(
|
| 103 |
label="Text Prompt",
|
| 104 |
lines=4,
|
| 105 |
+
placeholder="Describe how the image(s) should be edited...",
|
| 106 |
+
)
|
| 107 |
+
negative_prompt = gr.Textbox(
|
| 108 |
+
label="Negative Prompt (optional)",
|
| 109 |
+
lines=2,
|
| 110 |
+
placeholder="Describe what you want to avoid...",
|
| 111 |
)
|
| 112 |
seed = gr.Slider(
|
| 113 |
label="Seed",
|
|
|
|
| 116 |
step=1,
|
| 117 |
value=0,
|
| 118 |
)
|
| 119 |
+
num_inference_steps = gr.Slider(
|
| 120 |
+
label="Number of inference steps",
|
| 121 |
+
minimum=5,
|
| 122 |
+
maximum=60,
|
| 123 |
+
step=1,
|
| 124 |
+
value=40,
|
| 125 |
+
)
|
| 126 |
+
guidance_scale = gr.Slider(
|
| 127 |
+
label="Guidance scale",
|
| 128 |
+
minimum=0.0,
|
| 129 |
+
maximum=5.0,
|
| 130 |
+
step=0.1,
|
| 131 |
+
value=1.0,
|
| 132 |
+
)
|
| 133 |
+
true_cfg_scale = gr.Slider(
|
| 134 |
+
label="True CFG scale",
|
| 135 |
+
minimum=1.0,
|
| 136 |
+
maximum=8.0,
|
| 137 |
+
step=0.5,
|
| 138 |
+
value=4.0,
|
| 139 |
+
)
|
| 140 |
run_button = gr.Button("Generate", variant="primary")
|
| 141 |
|
| 142 |
result = gr.Image(label="Edited Image", show_label=True)
|
|
|
|
| 144 |
gr.on(
|
| 145 |
triggers=[run_button.click, prompt.submit],
|
| 146 |
fn=edit_images,
|
| 147 |
+
inputs=[
|
| 148 |
+
image1,
|
| 149 |
+
image2,
|
| 150 |
+
prompt,
|
| 151 |
+
negative_prompt,
|
| 152 |
+
seed,
|
| 153 |
+
num_inference_steps,
|
| 154 |
+
guidance_scale,
|
| 155 |
+
true_cfg_scale,
|
| 156 |
+
],
|
| 157 |
outputs=[result, seed],
|
| 158 |
)
|
| 159 |
|
| 160 |
+
|
| 161 |
if __name__ == "__main__":
|
| 162 |
demo.launch()
|
| 163 |
+
|