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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import copy
|
3 |
+
import random
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
import numpy as np
|
7 |
+
import PIL.Image
|
8 |
+
import spaces
|
9 |
+
import torch
|
10 |
+
from diffusers import (
|
11 |
+
AutoPipelineForText2Image,
|
12 |
+
DPMSolverMultistepScheduler,
|
13 |
+
)
|
14 |
+
from huggingface_hub import hf_hub_download
|
15 |
+
from safetensors.torch import load_file
|
16 |
+
|
17 |
+
|
18 |
+
# 1.Description
|
19 |
+
title = r"""
|
20 |
+
<h1 align="center">ResAdapter: Domain Consistent Resolution Adapter for Diffusion Models</h1>
|
21 |
+
"""
|
22 |
+
|
23 |
+
description = r"""
|
24 |
+
<b>Official 🤗 Gradio demo</b> for <a href='https://github.com/bytedance/res-adapter' target='_blank'><b>ResAdapter: Domain Consistent Resolution Adapter for Diffusion Models</b></a>.<br>
|
25 |
+
We propose ResAdapter, a plug-and-play resolution adapter for enabling any diffusion model generate resolution-free images: no additional training, no additional inference and no style transfer.<br>
|
26 |
+
How to use:<br>
|
27 |
+
1. Choose a personalized diffusion model.
|
28 |
+
2. Choose a resadapter weights according to the model type (sd1.5 or sdxl).
|
29 |
+
3. Change generation resolution of images.
|
30 |
+
4. Enter a text prompt, as done in normal text-to-image models.
|
31 |
+
5. Click the <b>Submit</b> button to begin customization.
|
32 |
+
"""
|
33 |
+
|
34 |
+
article = r"""
|
35 |
+
---
|
36 |
+
**Citation**
|
37 |
+
<br>
|
38 |
+
If our work is helpful for your research or applications, please cite us via:
|
39 |
+
```bibtex
|
40 |
+
@article{cheng2024resadapter,
|
41 |
+
title={ResAdapter: Domain Consistent Resolution Adapter for Diffusion Models},
|
42 |
+
author={Cheng, Jiaxiang and Xie, Pan and Xia, Xin and Li, Jiashi and Wu, Jie and Ren, Yuxi and Li, Huixia and Xiao, Xuefeng and Zheng, Min and Fu, Lean},
|
43 |
+
booktitle={arXiv preprint arxiv:2403.02084},
|
44 |
+
year={2024}
|
45 |
+
}
|
46 |
+
```
|
47 |
+
**Contact**
|
48 |
+
<br>
|
49 |
+
For any question, please feel free to contact us via chengjiaxiang@bytedance.com or xiepan.01@bytedance.com.</b>
|
50 |
+
<br>
|
51 |
+
**Acknowledgements**
|
52 |
+
<br>
|
53 |
+
This template is powered from [InstantID](https://huggingface.co/spaces/InstantX/InstantID).
|
54 |
+
"""
|
55 |
+
|
56 |
+
tips = r"""
|
57 |
+
### Usage tips of ResAdapter
|
58 |
+
1. If you are not satisfied with interpolation images, try to increase the alpha of resadapter to 1.0.
|
59 |
+
2. If you are not satisfied with extrapolate images, try to choose the alpha of resadapter in 0.3 ~ 0.7.
|
60 |
+
3. If you find the images with style conflicts, try to decrease the alpha of resadapter.
|
61 |
+
4. If you find resadapter is not compatible with other accelerate lora, try to decrease the alpha of resadapter to 0.5 ~ 0.7.
|
62 |
+
"""
|
63 |
+
|
64 |
+
# 2.Global variable
|
65 |
+
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "2048"))
|
66 |
+
MAX_SEED = np.iinfo(np.int32).max
|
67 |
+
CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "0") == "1"
|
68 |
+
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD") == "1"
|
69 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
70 |
+
|
71 |
+
# 3.Default model name
|
72 |
+
|
73 |
+
default_model_name = "dreamlike-art/dreamlike-diffusion-1.0"
|
74 |
+
default_pipe = AutoPipelineForText2Image.from_pretrained(default_model_name, torch_dtype=torch.float16)
|
75 |
+
default_pipe.scheduler = DPMSolverMultistepScheduler.from_config(default_pipe.scheduler.config, use_karras_sigmas=True, algorithm_type="sde-dpmsolver++")
|
76 |
+
default_pipe = default_pipe.to(device)
|
77 |
+
|
78 |
+
# 4. Prepare examples
|
79 |
+
examples = [
|
80 |
+
[
|
81 |
+
"dreamlike-art/dreamlike-diffusion-1.0",
|
82 |
+
"resadapter_v2_sd1.5",
|
83 |
+
0.7,
|
84 |
+
"Award-winning photo of a mystical fox girl fox in a serene forest clearing, sunlight filtering through the trees,ethereal,enchanting,vibrant orange fur,piercing amber eyes,delicate floral crown, flowing gown,surrounded by a gentle breeze, whispering leaves,magical atmosphere,captured by renowned photographer Emily Thompson using a Nikon D850,creating a dreamlike and captivating image",
|
85 |
+
"NSFW, poor bad amateur assignment cut out ugly",
|
86 |
+
1024,
|
87 |
+
1024,
|
88 |
+
],
|
89 |
+
[
|
90 |
+
"dreamlike-art/dreamlike-diffusion-1.0",
|
91 |
+
"resadapter_v2_sd1.5",
|
92 |
+
0.7,
|
93 |
+
"Pictures of you, beautiful face, youthful appearance, ultra focus, face iluminated, face detailed, ultra focus, dreamlike images, pixel perfect precision, ultra realistic, vibrant, ultra focus, face ilumined, face detailed, 8k resolution, watercolor, detailed colors, ultra focus, 8k resolution, watercolor, razumov style. art by Carne Griffiths, Frank Frazetta, sf, intricate artwork masterpiece, ominous, golden ratio, in the oil painting style reminiscent of Konstantin Razumov's work, yet interspersed with the layered paper illusion effect characteristic of Eiko Ojala, Reimagined splashes of ink in the digital art style, evoking at once impressions of Alberto Seveso's signature pieces, model standing confidently at the center, trending on cgsociety, intricate, epic, trending on artstation, by artgerm, h. r. giger and beksinski, highly detailed, vibrant, production cinematic character render, ultra high quality model, sf, intricate artwork masterpiece, ominous, matte painting movie poster, golden ratio, trending on cgsociety, intricate, epic, trending on artstation, by artgerm, h. r. giger and beksinski, highly detailed, vibrant",
|
94 |
+
"NSFW, poor bad amateur assignment cut out ugly",
|
95 |
+
1024,
|
96 |
+
1024,
|
97 |
+
],
|
98 |
+
[
|
99 |
+
"Lykon/dreamshaper-xl-1-0",
|
100 |
+
"resadapter_v2_sdxl",
|
101 |
+
1.0,
|
102 |
+
"(masterpiece), (extremely intricate), (realistic), portrait of a girl, the most beautiful in the world, (medieval armor), metal reflections, upper body, outdoors, intense sunlight, far away castle, professional photograph of a stunning woman detailed, sharp focus, dramatic, award winning, cinematic lighting, octane render unreal engine, volumetrics dtx, (film grain, blurry background, blurry foreground, bokeh, depth of field, sunset, motion blur), chainmail",
|
103 |
+
"ugly, deformed, noisy, blurry, low contrast, text, BadDream, 3d, cgi, render, fake, anime, open mouth, big forehead, long neck",
|
104 |
+
384,
|
105 |
+
768,
|
106 |
+
],
|
107 |
+
[
|
108 |
+
"Lykon/dreamshaper-xl-1-0",
|
109 |
+
"resadapter_v2_sdxl",
|
110 |
+
1.0,
|
111 |
+
"masterpiece, best quality, 1girl, sci-fi armor with black and red colors, glowing elements, redhair",
|
112 |
+
"ugly, deformed, noisy, blurry, low contrast, text, BadDream, 3d, cgi, render, fake, anime, open mouth, big forehead, long neck",
|
113 |
+
384,
|
114 |
+
768,
|
115 |
+
]
|
116 |
+
]
|
117 |
+
|
118 |
+
# 5. Themes
|
119 |
+
# theme = gr.themes.Base(
|
120 |
+
# font=[
|
121 |
+
# gr.themes.GoogleFont("Libre Franklin"),
|
122 |
+
# gr.themes.GoogleFont("Public Sans"),
|
123 |
+
# "system-ui",
|
124 |
+
# "sans-serif",
|
125 |
+
# ],
|
126 |
+
# )
|
127 |
+
css = """
|
128 |
+
.gradio-container {width: 85% !important}
|
129 |
+
"""
|
130 |
+
|
131 |
+
def run_for_examples(model_name, resadapter_model_name, resadapter_alpha, prompt, negative_prompt, width, height):
|
132 |
+
return generate(
|
133 |
+
model_name,
|
134 |
+
resadapter_model_name,
|
135 |
+
resadapter_alpha,
|
136 |
+
prompt,
|
137 |
+
negative_prompt,
|
138 |
+
width,
|
139 |
+
height,
|
140 |
+
guidance_scale = 7.5,
|
141 |
+
num_inference_steps = 25,
|
142 |
+
seed = 44,
|
143 |
+
)
|
144 |
+
|
145 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
146 |
+
# random seed
|
147 |
+
if randomize_seed:
|
148 |
+
seed = random.randint(0, MAX_SEED)
|
149 |
+
return seed
|
150 |
+
|
151 |
+
|
152 |
+
def load_resadapter_for_pipe(pipe, resadapter_model_name, resadapter_alpha):
|
153 |
+
# load lora
|
154 |
+
pipe.load_lora_weights(
|
155 |
+
hf_hub_download(repo_id="jiaxiangc/res-adapter", subfolder=resadapter_model_name, filename="pytorch_lora_weights.safetensors"),
|
156 |
+
adapter_name="res_adapter",
|
157 |
+
)
|
158 |
+
pipe.set_adapters(["res_adapter"], adapter_weights=[resadapter_alpha])
|
159 |
+
# load normalization
|
160 |
+
pipe.unet.load_state_dict(
|
161 |
+
load_file(hf_hub_download(repo_id="jiaxiangc/res-adapter", subfolder=resadapter_model_name, filename="diffusion_pytorch_model.safetensors")),
|
162 |
+
strict=False,
|
163 |
+
)
|
164 |
+
|
165 |
+
return pipe
|
166 |
+
|
167 |
+
|
168 |
+
@spaces.GPU(enable_queue=True)
|
169 |
+
def generate(
|
170 |
+
model_name: str,
|
171 |
+
resadapter_model_name: str,
|
172 |
+
resadapter_alpha: float,
|
173 |
+
prompt: str,
|
174 |
+
negative_prompt: str = "",
|
175 |
+
width: int = 1024,
|
176 |
+
height: int = 1024,
|
177 |
+
guidance_scale: float = 7.5,
|
178 |
+
num_inference_steps: int = 25,
|
179 |
+
seed: int = 0,
|
180 |
+
) -> PIL.Image.Image:
|
181 |
+
global default_model_name, default_pipe, device
|
182 |
+
|
183 |
+
print(f'Generating image from: {prompt}')
|
184 |
+
generator = torch.Generator().manual_seed(seed)
|
185 |
+
|
186 |
+
if model_name == default_model_name:
|
187 |
+
pipe = copy.deepcopy(default_pipe)
|
188 |
+
pipe = pipe.to(device)
|
189 |
+
|
190 |
+
else:
|
191 |
+
pipe = AutoPipelineForText2Image.from_pretrained(model_name, torch_dtype=torch.float16)
|
192 |
+
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config, use_karras_sigmas=True, algorithm_type="sde-dpmsolver++")
|
193 |
+
pipe = pipe.to(device)
|
194 |
+
default_pipe = copy.deepcopy(pipe)
|
195 |
+
default_model_name = model_name
|
196 |
+
|
197 |
+
# inference baseline
|
198 |
+
base_image = pipe(
|
199 |
+
prompt=prompt,
|
200 |
+
width=width,
|
201 |
+
height=height,
|
202 |
+
negative_prompt=negative_prompt,
|
203 |
+
guidance_scale=guidance_scale,
|
204 |
+
num_inference_steps=num_inference_steps,
|
205 |
+
output_type="pil",
|
206 |
+
generator=generator,
|
207 |
+
).images[0]
|
208 |
+
|
209 |
+
# inference resadapter
|
210 |
+
pipe = load_resadapter_for_pipe(pipe, resadapter_model_name, resadapter_alpha)
|
211 |
+
resadapter_image = pipe(
|
212 |
+
prompt=prompt,
|
213 |
+
width=width,
|
214 |
+
height=height,
|
215 |
+
negative_prompt=negative_prompt,
|
216 |
+
guidance_scale=guidance_scale,
|
217 |
+
num_inference_steps=num_inference_steps,
|
218 |
+
output_type="pil",
|
219 |
+
generator=generator,
|
220 |
+
).images[0]
|
221 |
+
|
222 |
+
return [resadapter_image, base_image]
|
223 |
+
|
224 |
+
|
225 |
+
# 6. UI
|
226 |
+
with gr.Blocks(css=css) as demo:
|
227 |
+
gr.Markdown(title)
|
228 |
+
gr.Markdown(description)
|
229 |
+
gr.DuplicateButton(
|
230 |
+
value="Duplicate Space for private use",
|
231 |
+
elem_id="duplicate-button",
|
232 |
+
visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
|
233 |
+
)
|
234 |
+
|
235 |
+
with gr.Row():
|
236 |
+
with gr.Column():
|
237 |
+
with gr.Row():
|
238 |
+
model_name_choices = [
|
239 |
+
"dreamlike-art/dreamlike-diffusion-1.0",
|
240 |
+
"Lykon/dreamshaper-xl-1-0",
|
241 |
+
]
|
242 |
+
model_name = gr.Dropdown(
|
243 |
+
label="Model name",
|
244 |
+
choices=model_name_choices,
|
245 |
+
value="dreamlike-art/dreamlike-diffusion-1.0",
|
246 |
+
)
|
247 |
+
resadapter_model_name_choices = ["resadapter_v2_sd1.5", "resadapter_v2_sdxl"]
|
248 |
+
resadapter_model_name = gr.Dropdown(
|
249 |
+
label="ResaAapter model name",
|
250 |
+
choices=resadapter_model_name_choices,
|
251 |
+
value="resadapter_v2_sd1.5",
|
252 |
+
)
|
253 |
+
resadapter_alpha = gr.Slider(
|
254 |
+
label="Resadapter alpha",
|
255 |
+
minimum=0,
|
256 |
+
maximum=1.0,
|
257 |
+
step=0.01,
|
258 |
+
value=0.7,
|
259 |
+
)
|
260 |
+
with gr.Column():
|
261 |
+
prompt = gr.Text(
|
262 |
+
label="Prompt",
|
263 |
+
max_lines=1,
|
264 |
+
placeholder="Enter your prompt",
|
265 |
+
visible=True,
|
266 |
+
)
|
267 |
+
negative_prompt = gr.Text(
|
268 |
+
label="Negative prompt",
|
269 |
+
max_lines=1,
|
270 |
+
placeholder="NSFW, poor bad amateur assignment cut out ugly",
|
271 |
+
visible=True,
|
272 |
+
)
|
273 |
+
run_button = gr.Button("Submmit")
|
274 |
+
width = gr.Slider(
|
275 |
+
label="Width",
|
276 |
+
minimum=128,
|
277 |
+
maximum=MAX_IMAGE_SIZE,
|
278 |
+
step=32,
|
279 |
+
value=1024,
|
280 |
+
)
|
281 |
+
height = gr.Slider(
|
282 |
+
label="Height",
|
283 |
+
minimum=128,
|
284 |
+
maximum=MAX_IMAGE_SIZE,
|
285 |
+
step=32,
|
286 |
+
value=1024,
|
287 |
+
)
|
288 |
+
guidance_scale = gr.Slider(
|
289 |
+
label="CFG Scale",
|
290 |
+
minimum=0,
|
291 |
+
maximum=20,
|
292 |
+
step=0.5,
|
293 |
+
value=7.5,
|
294 |
+
)
|
295 |
+
num_inference_steps = gr.Slider(
|
296 |
+
label="Sampling steps",
|
297 |
+
minimum=1,
|
298 |
+
maximum=50,
|
299 |
+
step=1,
|
300 |
+
value=25,
|
301 |
+
)
|
302 |
+
seed = gr.Slider(
|
303 |
+
label="Seed",
|
304 |
+
minimum=0,
|
305 |
+
maximum=MAX_SEED,
|
306 |
+
step=1,
|
307 |
+
value=0,
|
308 |
+
)
|
309 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
310 |
+
with gr.Row():
|
311 |
+
resadapter_output = gr.Image(label="Resadapter images")
|
312 |
+
baseline_output = gr.Image(label="Baseline images")
|
313 |
+
|
314 |
+
gr.on(
|
315 |
+
triggers=[
|
316 |
+
prompt.submit,
|
317 |
+
negative_prompt.submit,
|
318 |
+
run_button.click,
|
319 |
+
],
|
320 |
+
fn=randomize_seed_fn,
|
321 |
+
inputs=[seed, randomize_seed],
|
322 |
+
outputs=seed,
|
323 |
+
queue=False,
|
324 |
+
api_name=False,
|
325 |
+
).then(
|
326 |
+
fn=generate,
|
327 |
+
inputs=[
|
328 |
+
model_name,
|
329 |
+
resadapter_model_name,
|
330 |
+
resadapter_alpha,
|
331 |
+
prompt,
|
332 |
+
negative_prompt,
|
333 |
+
width,
|
334 |
+
height,
|
335 |
+
guidance_scale,
|
336 |
+
num_inference_steps,
|
337 |
+
seed,
|
338 |
+
],
|
339 |
+
outputs=[resadapter_output, baseline_output],
|
340 |
+
api_name="run",
|
341 |
+
)
|
342 |
+
|
343 |
+
gr.Examples(
|
344 |
+
examples=examples,
|
345 |
+
inputs=[model_name, resadapter_model_name, resadapter_alpha, prompt, negative_prompt, width, height],
|
346 |
+
outputs=[resadapter_output, baseline_output],
|
347 |
+
fn=run_for_examples,
|
348 |
+
cache_examples="lazy",
|
349 |
+
)
|
350 |
+
gr.Markdown(tips)
|
351 |
+
gr.Markdown(article)
|
352 |
+
|
353 |
+
if __name__ == "__main__":
|
354 |
+
demo.queue(max_size=20, api_open=False).launch(show_api=False)
|