-
Notifications
You must be signed in to change notification settings - Fork 65
/
gradio_app_anime_xl.py
513 lines (486 loc) · 24.6 KB
/
gradio_app_anime_xl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import math
import random
import os
import json
import time
import argparse
import torch
import numpy as np
from torchvision import transforms
from models.region_diffusion_sdxl import RegionDiffusionXL
from utils.attention_utils import get_token_maps
from utils.richtext_utils import seed_everything, parse_json, get_region_diffusion_input,\
get_attention_control_input, get_gradient_guidance_input
import gradio as gr
from PIL import Image, ImageOps
from utils.share_btn import community_icon_html, loading_icon_html, share_js, css
help_text = """
If you are encountering an error or not achieving your desired outcome, here are some potential reasons and recommendations to consider:
1. If you format only a portion of a word rather than the complete word, an error may occur.
2. If you use font color and get completely corrupted results, you may consider decrease the color weight lambda.
3. Consider using a different seed.
"""
canvas_html = """<iframe id='rich-text-root' style='width:100%' height='360px' src='file=utils/rich-text-to-json-iframe.html' frameborder='0' scrolling='no'></iframe>"""
get_js_data = """
async (text_input, negative_prompt, num_segments, segment_threshold, inject_interval, inject_background, seed, color_guidance_weight, rich_text_input, height, width, steps, guidance_weights) => {
const richEl = document.getElementById("rich-text-root");
const data = richEl? richEl.contentDocument.body._data : {};
return [text_input, negative_prompt, num_segments, segment_threshold, inject_interval, inject_background, seed, color_guidance_weight, JSON.stringify(data), height, width, steps, guidance_weights];
}
"""
set_js_data = """
async (text_input) => {
const richEl = document.getElementById("rich-text-root");
const data = text_input ? JSON.parse(text_input) : null;
if (richEl && data) richEl.contentDocument.body.setQuillContents(data);
}
"""
get_window_url_params = """
async (url_params) => {
const params = new URLSearchParams(window.location.search);
url_params = Object.fromEntries(params);
return [url_params];
}
"""
def load_url_params(url_params):
if 'prompt' in url_params:
return gr.update(visible=True), url_params
else:
return gr.update(visible=False), url_params
def main():
model = RegionDiffusionXL("Linaqruf/animagine-xl")
def generate(
text_input: str,
negative_text: str,
num_segments: int,
segment_threshold: float,
inject_interval: float,
inject_background: float,
seed: int,
color_guidance_weight: float,
rich_text_input: str,
height: int,
width: int,
steps: int,
guidance_weight: float,
):
run_dir = 'results/'
os.makedirs(run_dir, exist_ok=True)
# Load region diffusion model.
height = int(height) if height else 1024
width = int(width) if width else 1024
steps = 41 if not steps else steps
guidance_weight = 8.5 if not guidance_weight else guidance_weight
text_input = rich_text_input if rich_text_input != '' and rich_text_input != None else text_input
print('text_input', text_input, width, height, steps, guidance_weight, num_segments, segment_threshold, inject_interval, inject_background, color_guidance_weight, negative_text)
if (text_input == '' or rich_text_input == ''):
raise gr.Error("Please enter some text.")
# parse json to span attributes
base_text_prompt, style_text_prompts, footnote_text_prompts, footnote_target_tokens,\
color_text_prompts, color_names, color_rgbs, size_text_prompts_and_sizes, use_grad_guidance = parse_json(
json.loads(text_input))
# create control input for region diffusion
region_text_prompts, region_target_token_ids, base_tokens = get_region_diffusion_input(
model, base_text_prompt, style_text_prompts, footnote_text_prompts,
footnote_target_tokens, color_text_prompts, color_names)
# create control input for cross attention
text_format_dict = get_attention_control_input(
model, base_tokens, size_text_prompts_and_sizes)
# create control input for region guidance
text_format_dict, color_target_token_ids = get_gradient_guidance_input(
model, base_tokens, color_text_prompts, color_rgbs, text_format_dict, color_guidance_weight=color_guidance_weight)
seed_everything(seed)
# get token maps from plain text to image generation.
begin_time = time.time()
if model.selfattn_maps is None and model.crossattn_maps is None:
model.remove_tokenmap_hooks()
model.register_tokenmap_hooks()
else:
model.remove_tokenmap_hooks()
model.remove_tokenmap_hooks()
plain_img = model.sample([base_text_prompt], negative_prompt=[negative_text],
height=height, width=width, num_inference_steps=steps,
guidance_scale=guidance_weight, run_rich_text=False)
print('time lapses to get attention maps: %.4f' %
(time.time()-begin_time))
seed_everything(seed)
color_obj_masks, segments_vis, token_maps = get_token_maps(model.selfattn_maps, model.crossattn_maps, model.n_maps, run_dir,
1024//8, 1024//8, color_target_token_ids[:-1], seed,
base_tokens, segment_threshold=segment_threshold, num_segments=num_segments,
return_vis=True)
seed_everything(seed)
model.masks, segments_vis, token_maps = get_token_maps(model.selfattn_maps, model.crossattn_maps, model.n_maps, run_dir,
1024//8, 1024//8, region_target_token_ids[:-1], seed,
base_tokens, segment_threshold=segment_threshold, num_segments=num_segments,
return_vis=True)
color_obj_atten_all = torch.zeros_like(color_obj_masks[-1])
for obj_mask in color_obj_masks[:-1]:
color_obj_atten_all += obj_mask
color_obj_masks = [transforms.functional.resize(color_obj_mask, (height, width),
interpolation=transforms.InterpolationMode.BICUBIC,
antialias=True)
for color_obj_mask in color_obj_masks]
text_format_dict['color_obj_atten'] = color_obj_masks
text_format_dict['color_obj_atten_all'] = color_obj_atten_all
model.remove_tokenmap_hooks()
# generate image from rich text
begin_time = time.time()
seed_everything(seed)
rich_img = model.sample(region_text_prompts, negative_prompt=[negative_text],
height=height, width=width, num_inference_steps=steps,
guidance_scale=guidance_weight, use_guidance=use_grad_guidance,
text_format_dict=text_format_dict, inject_selfattn=inject_interval,
inject_background=inject_background, run_rich_text=True)
print('time lapses to generate image from rich text: %.4f' %
(time.time()-begin_time))
return [plain_img.images[0], rich_img.images[0], segments_vis, token_maps]
with gr.Blocks() as demo:
url_params = gr.JSON({}, visible=False, label="URL Params")
gr.HTML("""<h1 style="font-weight: 900; margin-bottom: 7px;">Expressive Text-to-Image Generation with Rich Text</h1>
<p> <a href="https://songweige.github.io/">Songwei Ge</a>, <a href="https://taesung.me/">Taesung Park</a>, <a href="https://www.cs.cmu.edu/~junyanz/">Jun-Yan Zhu</a>, <a href="https://jbhuang0604.github.io/">Jia-Bin Huang</a> <p/>
<p> UMD, Adobe, CMU <p/>
<p> ICCV, 2023 <p/>
<p> <a href="https://huggingface.co/spaces/songweig/rich-text-to-image?duplicate=true"><img src="https://bit.ly/3gLdBN6" style="display:inline;"alt="Duplicate Space"></a> | <a href="https://rich-text-to-image.github.io">[Website]</a> | <a href="https://github.com/SongweiGe/rich-text-to-image">[Code]</a> | <a href="https://arxiv.org/abs/2304.06720">[Paper]</a><p/>
<p> Our method is now using Stable Diffusion XL. For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings.""")
with gr.Row():
with gr.Column():
rich_text_el = gr.HTML(canvas_html, elem_id="canvas_html")
rich_text_input = gr.Textbox(value="", visible=False)
text_input = gr.Textbox(
label='Rich-text JSON Input',
visible=False,
max_lines=1,
placeholder='Example: \'{"ops":[{"insert":"a Gothic "},{"attributes":{"color":"#b26b00"},"insert":"church"},{"insert":" in a the sunset with a beautiful landscape in the background.\n"}]}\'',
elem_id="text_input"
)
negative_prompt = gr.Textbox(
label='Negative Prompt',
max_lines=1,
placeholder='Example: poor quality, blurry, dark, low resolution, low quality, worst quality',
elem_id="negative_prompt"
)
segment_threshold = gr.Slider(label='Token map threshold',
info='(See less area in token maps? Decrease this. See too much area? Increase this.)',
minimum=0,
maximum=1,
step=0.01,
value=0.25)
inject_interval = gr.Slider(label='Detail preservation',
info='(To preserve more structure from plain-text generation, increase this. To see more rich-text attributes, decrease this.)',
minimum=0,
maximum=1,
step=0.01,
value=0.)
inject_background = gr.Slider(label='Unformatted token preservation',
info='(To affect less the tokens without any rich-text attributes, increase this.)',
minimum=0,
maximum=1,
step=0.01,
value=0.3)
color_guidance_weight = gr.Slider(label='Color weight',
info='(To obtain more precise color, increase this, while too large value may cause artifacts.)',
minimum=0,
maximum=2,
step=0.1,
value=0.5)
num_segments = gr.Slider(label='Number of segments',
minimum=2,
maximum=20,
step=1,
value=9)
seed = gr.Slider(label='Seed',
minimum=0,
maximum=100000,
step=1,
value=6,
elem_id="seed"
)
with gr.Accordion('Other Parameters', open=False):
steps = gr.Slider(label='Number of Steps',
minimum=0,
maximum=500,
step=1,
value=41)
guidance_weight = gr.Slider(label='CFG weight',
minimum=0,
maximum=50,
step=0.1,
value=8.5)
width = gr.Dropdown(choices=[1024],
value=1024,
label='Width',
visible=True)
height = gr.Dropdown(choices=[1024],
value=1024,
label='height',
visible=True)
with gr.Row():
with gr.Column(scale=1, min_width=100):
generate_button = gr.Button("Generate")
load_params_button = gr.Button(
"Load from URL Params", visible=True)
with gr.Column():
richtext_result = gr.Image(
label='Rich-text', elem_id="rich-text-image")
richtext_result.style(height=784)
with gr.Row():
plaintext_result = gr.Image(
label='Plain-text', elem_id="plain-text-image")
segments = gr.Image(label='Segmentation')
with gr.Row():
token_map = gr.Image(label='Token Maps')
with gr.Row(visible=False) as share_row:
with gr.Group(elem_id="share-btn-container"):
community_icon = gr.HTML(community_icon_html)
loading_icon = gr.HTML(loading_icon_html)
share_button = gr.Button(
"Share to community", elem_id="share-btn")
share_button.click(None, [], [], _js=share_js)
with gr.Row():
gr.Markdown(help_text)
# with gr.Row():
# footnote_examples = [
# [
# '{"ops":[{"insert":"A close-up 4k dslr photo of a "},{"attributes":{"link":"A cat wearing sunglasses and a bandana around its neck."},"insert":"cat"},{"insert":" riding a scooter. Palm trees in the background."}]}',
# '',
# 9,
# 0.3,
# 0.3,
# 0.5,
# 3,
# 0,
# None,
# ],
# [
# '{"ops":[{"insert":"A cozy "},{"attributes":{"link":"A charming wooden cabin with Christmas decoration, warm light coming out from the windows."},"insert":"cabin"},{"insert":" nestled in a "},{"attributes":{"link":"Towering evergreen trees covered in a thick layer of pristine snow."},"insert":"snowy forest"},{"insert":", and a "},{"attributes":{"link":"A cute snowman wearing a carrot nose, coal eyes, and a colorful scarf, welcoming visitors with a cheerful vibe."},"insert":"snowman"},{"insert":" stands in the yard."}]}',
# '',
# 12,
# 0.4,
# 0.3,
# 0.5,
# 3,
# 0,
# None,
# ],
# [
# '{"ops":[{"insert":"A "},{"attributes":{"link":"Happy Kung fu panda art, elder, asian art, volumetric lighting, dramatic scene, ultra detailed, realism, chinese"},"insert":"panda"},{"insert":" standing on a cliff by a waterfall, wildlife photography, photograph, high quality, wildlife, f 1.8, soft focus, 8k, national geographic, award - winning photograph by nick nichols"}]}',
# '',
# 5,
# 0.3,
# 0,
# 0.1,
# 4,
# 0,
# None,
# ],
# ]
# gr.Examples(examples=footnote_examples,
# label='Footnote examples',
# inputs=[
# text_input,
# negative_prompt,
# num_segments,
# segment_threshold,
# inject_interval,
# inject_background,
# seed,
# color_guidance_weight,
# rich_text_input,
# ],
# outputs=[
# plaintext_result,
# richtext_result,
# segments,
# token_map,
# ],
# fn=generate,
# cache_examples=True,
# examples_per_page=20)
# with gr.Row():
# color_examples = [
# [
# '{"ops":[{"insert":"a beautifule girl with big eye, skin, and long "},{"attributes":{"color":"#04a704"},"insert":"hair"},{"insert":", t-shirt, bursting with vivid color, intricate, elegant, highly detailed, photorealistic, digital painting, artstation, illustration, concept art."}]}',
# 'lowres, had anatomy, bad hands, cropped, worst quality',
# 11,
# 0.5,
# 0.3,
# 0.3,
# 6,
# 0.5,
# None,
# ],
# [
# '{"ops":[{"insert":"a Gothic "},{"attributes":{"color":"#FD6C9E"},"insert":"church"},{"insert":" in a the sunset with a beautiful landscape in the background."}]}',
# '',
# 10,
# 0.5,
# 0.5,
# 0.3,
# 7,
# 0.5,
# None,
# ],
# ]
# gr.Examples(examples=color_examples,
# label='Font color examples',
# inputs=[
# text_input,
# negative_prompt,
# num_segments,
# segment_threshold,
# inject_interval,
# inject_background,
# seed,
# color_guidance_weight,
# rich_text_input,
# ],
# outputs=[
# plaintext_result,
# richtext_result,
# segments,
# token_map,
# ],
# fn=generate,
# cache_examples=True,
# examples_per_page=20)
# with gr.Row():
# style_examples = [
# [
# '{"ops":[{"insert":"a beautiful"},{"attributes":{"font":"mirza"},"insert":" garden"},{"insert":" with a "},{"attributes":{"font":"roboto"},"insert":"snow mountain"},{"insert":" in the background"}]}',
# '',
# 10,
# 0.6,
# 0,
# 0.4,
# 5,
# 0,
# None,
# ],
# [
# '{"ops":[{"insert":"a night"},{"attributes":{"font":"slabo"},"insert":" sky"},{"insert":" filled with stars above a turbulent"},{"attributes":{"font":"roboto"},"insert":" sea"},{"insert":" with giant waves"}]}',
# '',
# 2,
# 0.6,
# 0,
# 0,
# 6,
# 0.5,
# None,
# ],
# ]
# gr.Examples(examples=style_examples,
# label='Font style examples',
# inputs=[
# text_input,
# negative_prompt,
# num_segments,
# segment_threshold,
# inject_interval,
# inject_background,
# seed,
# color_guidance_weight,
# rich_text_input,
# ],
# outputs=[
# plaintext_result,
# richtext_result,
# segments,
# token_map,
# ],
# fn=generate,
# cache_examples=True,
# examples_per_page=20)
# with gr.Row():
# size_examples = [
# [
# '{"ops": [{"insert": "A pizza with "}, {"attributes": {"size": "60px"}, "insert": "pineapple"}, {"insert": ", pepperoni, and mushroom on the top"}]}',
# '',
# 5,
# 0.3,
# 0,
# 0,
# 3,
# 1,
# None,
# ],
# [
# '{"ops": [{"insert": "A pizza with pineapple, "}, {"attributes": {"size": "60px"}, "insert": "pepperoni"}, {"insert": ", and mushroom on the top"}]}',
# '',
# 5,
# 0.3,
# 0,
# 0,
# 3,
# 1,
# None,
# ],
# [
# '{"ops": [{"insert": "A pizza with pineapple, pepperoni, and "}, {"attributes": {"size": "60px"}, "insert": "mushroom"}, {"insert": " on the top"}]}',
# '',
# 5,
# 0.3,
# 0,
# 0,
# 3,
# 1,
# None,
# ],
# ]
# gr.Examples(examples=size_examples,
# label='Font size examples',
# inputs=[
# text_input,
# negative_prompt,
# num_segments,
# segment_threshold,
# inject_interval,
# inject_background,
# seed,
# color_guidance_weight,
# rich_text_input,
# ],
# outputs=[
# plaintext_result,
# richtext_result,
# segments,
# token_map,
# ],
# fn=generate,
# cache_examples=True,
# examples_per_page=20)
generate_button.click(fn=lambda: gr.update(visible=False), inputs=None, outputs=share_row, queue=False).then(
fn=generate,
inputs=[
text_input,
negative_prompt,
num_segments,
segment_threshold,
inject_interval,
inject_background,
seed,
color_guidance_weight,
rich_text_input,
height,
width,
steps,
guidance_weight,
],
outputs=[plaintext_result, richtext_result, segments, token_map],
_js=get_js_data
).then(
fn=lambda: gr.update(visible=True), inputs=None, outputs=share_row, queue=False)
text_input.change(
fn=None, inputs=[text_input], outputs=None, _js=set_js_data, queue=False)
# load url param prompt to textinput
load_params_button.click(fn=lambda x: x['prompt'], inputs=[
url_params], outputs=[text_input], queue=False)
demo.load(
fn=load_url_params,
inputs=[url_params],
outputs=[load_params_button, url_params],
_js=get_window_url_params
)
demo.queue(concurrency_count=1)
demo.launch(share=False)
if __name__ == "__main__":
main()