Skip to content

Commit cf43fc2

Browse files
committed
update gradio demo with example
1 parent 92c1bae commit cf43fc2

File tree

1 file changed

+60
-41
lines changed

1 file changed

+60
-41
lines changed

scripts/gradio_demo.py

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,40 @@
3535
pipe.load_lora_weights(args.lora_path)
3636

3737
if args.enable_model_cpu_offload:
38-
pipe.enable_model_cpu_offload()
38+
pipe.enable_model_cpu_offload()
3939
else:
4040
pipe = pipe.to("cuda")
4141

42+
4243
@spaces.GPU
43-
def infer(edit_images,
44-
prompt,
45-
seed=666,
46-
randomize_seed=False,
47-
width=1024,
48-
height=1024,
49-
guidance_scale=50,
50-
num_inference_steps=28,
51-
progress=gr.Progress(track_tqdm=True)
52-
):
53-
44+
def infer(edit_images,
45+
prompt,
46+
seed=666,
47+
randomize_seed=False,
48+
width=1024,
49+
height=1024,
50+
guidance_scale=50,
51+
num_inference_steps=28,
52+
progress=gr.Progress(track_tqdm=True)):
53+
5454
image = edit_images
55-
55+
5656
if image.size[0] != 512:
5757
print("\033[93m[WARNING] We can only deal with the case where the image's width is 512.\033[0m")
5858
new_width = 512
5959
scale = new_width / image.size[0]
6060
new_height = int(image.size[1] * scale)
61-
new_height = (new_height // 8) * 8
61+
new_height = (new_height // 8) * 8
6262
image = image.resize((new_width, new_height))
6363
print(f"\033[93m[WARNING] Resizing the image to {new_width} x {new_height}\033[0m")
64-
64+
6565
image = image.convert("RGB")
6666
width, height = image.size
6767
image = image.resize((512, int(512 * height / width)))
6868
combined_image = Image.new("RGB", (width * 2, height))
69-
combined_image.paste(image, (0, 0))
69+
combined_image.paste(image, (0, 0))
7070
mask_array = np.zeros((height, width * 2), dtype=np.uint8)
71-
mask_array[:, width:] = 255
71+
mask_array[:, width:] = 255
7272
mask = Image.fromarray(mask_array)
7373
instruction = f'A diptych with two side-by-side images of the same scene. On the right, the scene is exactly the same as on the left but {prompt}'
7474

@@ -80,41 +80,51 @@ def infer(edit_images,
8080
image=combined_image,
8181
mask_image=mask,
8282
height=height,
83-
width=width*2,
83+
width=width * 2,
8484
guidance_scale=guidance_scale,
8585
num_inference_steps=num_inference_steps,
86-
generator=torch.Generator("cpu").manual_seed(seed)
86+
generator=torch.Generator().manual_seed(seed),
8787
).images[0]
8888

89-
w,h = image.size
90-
image = image.crop((w//2, 0, w, h))
89+
w, h = image.size
90+
image = image.crop((w // 2, 0, w, h))
9191

9292
os.makedirs(args.output_dir, exist_ok=True)
93-
93+
9494
index = len(os.listdir(args.output_dir))
9595
image.save(f"{args.output_dir}/result_{index}.png")
96-
96+
9797
return image, seed
98-
99-
examples = [
98+
99+
100+
# 原有的示例
101+
original_examples = [
100102
"a tiny astronaut hatching from an egg on the moon",
101103
"a cat holding a sign that says hello world",
102104
"an anime illustration of a wiener schnitzel",
103105
]
104106

105-
css="""
107+
# 新增的示例,将元组转换为列表
108+
new_examples = [
109+
['assets/girl.png', 'Make her hair dark green and her clothes checked.', 42],
110+
['assets/boy.png', 'Change the sunglasses to a Christmas hat.', 27440001],
111+
['assets/kaori.jpg', 'Make it a sketch.', 329918865]
112+
]
113+
114+
css = """
106115
#col-container {
107116
margin: 0 auto;
108117
max-width: 1000px;
109118
}
110119
"""
111120

112121
with gr.Blocks(css=css) as demo:
113-
122+
114123
with gr.Column(elem_id="col-container"):
115124
gr.Markdown(f"""# IC-Edit
116125
A demo for [IC-Edit](https://arxiv.org/pdf/2504.20690).
117126
More **open-source**, with **lower costs**, **faster speed** (it takes about 9 seconds to process one image), and **powerful performance**.
127+
For more details, check out our [Github Repository](https://github.com/River-Zhang/ICEdit) and [website](https://river-zhang.github.io/ICEdit-gh-pages/). If our project resonates with you or proves useful, we'd be truly grateful if you could spare a moment to give it a star.
118128
""")
119129
with gr.Row():
120130
with gr.Column():
@@ -133,23 +143,23 @@ def infer(edit_images,
133143
container=False,
134144
)
135145
run_button = gr.Button("Run")
136-
146+
137147
result = gr.Image(label="Result", show_label=False)
138-
139-
with gr.Accordion("Advanced Settings", open=False):
140-
148+
149+
with gr.Accordion("Advanced Settings", open=True):
150+
141151
seed = gr.Slider(
142152
label="Seed",
143153
minimum=0,
144154
maximum=MAX_SEED,
145155
step=1,
146156
value=0,
147157
)
148-
158+
149159
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
150-
160+
151161
with gr.Row():
152-
162+
153163
width = gr.Slider(
154164
label="Width",
155165
minimum=512,
@@ -158,7 +168,7 @@ def infer(edit_images,
158168
value=1024,
159169
visible=False
160170
)
161-
171+
162172
height = gr.Slider(
163173
label="Height",
164174
minimum=512,
@@ -167,17 +177,17 @@ def infer(edit_images,
167177
value=1024,
168178
visible=False
169179
)
170-
180+
171181
with gr.Row():
172182

173183
guidance_scale = gr.Slider(
174184
label="Guidance Scale",
175185
minimum=1,
176-
maximum=50,
186+
maximum=100,
177187
step=0.5,
178188
value=50,
179189
)
180-
190+
181191
num_inference_steps = gr.Slider(
182192
label="Number of inference steps",
183193
minimum=1,
@@ -186,11 +196,20 @@ def infer(edit_images,
186196
value=28,
187197
)
188198

199+
# 添加 Gradio 示例组件
200+
gr.Examples(
201+
examples=new_examples,
202+
inputs=[edit_image, prompt, seed],
203+
outputs=[result, seed],
204+
fn=infer,
205+
cache_examples=False
206+
)
207+
189208
gr.on(
190209
triggers=[run_button.click, prompt.submit],
191-
fn = infer,
192-
inputs = [edit_image, prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
193-
outputs = [result, seed]
210+
fn=infer,
211+
inputs=[edit_image, prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
212+
outputs=[result, seed]
194213
)
195214

196215
demo.launch(server_port=args.port)

0 commit comments

Comments
 (0)