Spaces:
Runtime error
Runtime error
File size: 26,777 Bytes
99e3c03 |
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 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 |
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 import RegionDiffusion
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 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=rich-text-to-json-iframe.html' frameborder='0' scrolling='no'></iframe>"""
get_js_data = """
async (text_input, negative_prompt, height, width, seed, steps, num_segments, segment_threshold, inject_interval, guidance_weight, color_guidance_weight, rich_text_input, background_aug) => {
const richEl = document.getElementById("rich-text-root");
const data = richEl? richEl.contentDocument.body._data : {};
return [text_input, negative_prompt, height, width, seed, steps, num_segments, segment_threshold, inject_interval, guidance_weight, color_guidance_weight, JSON.stringify(data), background_aug];
}
"""
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():
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = RegionDiffusion(device)
def generate(
text_input: str,
negative_text: str,
height: int,
width: int,
seed: int,
steps: int,
num_segments: int,
segment_threshold: float,
inject_interval: float,
guidance_weight: float,
color_guidance_weight: float,
rich_text_input: str,
background_aug: bool,
):
run_dir = 'results/'
os.makedirs(run_dir, exist_ok=True)
# Load region diffusion model.
height = int(height)
width = int(width)
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 != '' else text_input
print('text_input', text_input)
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.reset_attention_maps()
model.remove_tokenmap_hooks()
plain_img = model.produce_attn_maps([base_text_prompt], [negative_text],
height=height, width=width, num_inference_steps=steps,
guidance_scale=guidance_weight)
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,
512//8, 512//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,
512//8, 512//8, region_target_token_ids[:-1], seed,
base_tokens, segment_threshold=segment_threshold, num_segments=num_segments,
return_vis=True)
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
model.remove_tokenmap_hooks()
# generate image from rich text
begin_time = time.time()
seed_everything(seed)
if background_aug:
bg_aug_end = 500
else:
bg_aug_end = 1000
rich_img = model.prompt_to_img(region_text_prompts, [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,
bg_aug_end=bg_aug_end)
print('time lapses to generate image from rich text: %.4f' %
(time.time()-begin_time))
return [plain_img[0], rich_img[0], segments_vis, token_maps]
with gr.Blocks(css=css) 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> <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> 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.)
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"
)
background_aug = gr.Checkbox(
label='Precise region alignment',
info='(For strict region alignment, select this option, but beware of potential artifacts when using with style.)',
value=True)
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=[512],
value=512,
label='Width',
visible=True)
height = gr.Dropdown(choices=[512],
value=512,
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=512)
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."}]}',
'',
5,
0.3,
0,
6,
1,
None,
True
],
[
'{"ops":[{"insert":"A "},{"attributes":{"link":"kitchen island with a stove with gas burners and a built-in oven "},"insert":"kitchen island"},{"insert":" next to a "},{"attributes":{"link":"an open refrigerator stocked with fresh produce, dairy products, and beverages. "},"insert":"refrigerator"},{"insert":", by James McDonald and Joarc Architects, home, interior, octane render, deviantart, cinematic, key art, hyperrealism, sun light, sunrays, canon eos c 300, ƒ 1.8, 35 mm, 8k, medium - format print"}]}',
'',
6,
0.5,
0,
6,
1,
None,
True
],
[
'{"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"}]}',
'',
4,
0.3,
0,
4,
1,
None,
True
],
]
gr.Examples(examples=footnote_examples,
label='Footnote examples',
inputs=[
text_input,
negative_prompt,
num_segments,
segment_threshold,
inject_interval,
seed,
color_guidance_weight,
rich_text_input,
background_aug,
],
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":"#00ffff"},"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',
9,
0.25,
0.3,
6,
0.5,
None,
True
],
[
'{"ops":[{"insert":"a beautifule girl with big eye, skin, and long "},{"attributes":{"color":"#eeeeee"},"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',
9,
0.25,
0.3,
6,
0.1,
None,
True
],
[
'{"ops":[{"insert":"a Gothic "},{"attributes":{"color":"#FD6C9E"},"insert":"church"},{"insert":" in a the sunset with a beautiful landscape in the background."}]}',
'',
5,
0.3,
0.5,
6,
0.5,
None,
False
],
[
'{"ops":[{"insert":"A mesmerizing sight that captures the beauty of a "},{"attributes":{"color":"#4775fc"},"insert":"rose"},{"insert":" blooming, close up"}]}',
'',
3,
0.3,
0,
9,
1,
None,
False
],
[
'{"ops":[{"insert":"A "},{"attributes":{"color":"#FFD700"},"insert":"marble statue of a wolf\'s head and shoulder"},{"insert":", surrounded by colorful flowers michelangelo, detailed, intricate, full of color, led lighting, trending on artstation, 4 k, hyperrealistic, 3 5 mm, focused, extreme details, unreal engine 5, masterpiece "}]}',
'',
5,
0.3,
0,
5,
0.6,
None,
False
],
]
gr.Examples(examples=color_examples,
label='Font color examples',
inputs=[
text_input,
negative_prompt,
num_segments,
segment_threshold,
inject_interval,
seed,
color_guidance_weight,
rich_text_input,
background_aug,
],
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 "},{"attributes":{"font":"mirza"},"insert":"beautiful garden"},{"insert":" with a "},{"attributes":{"font":"roboto"},"insert":"snow mountain in the background"},{"insert":""}]}',
'',
10,
0.45,
0,
0.2,
3,
0.5,
None,
False
],
[
'{"ops":[{"attributes":{"link":"the awe-inspiring sky and ocean in the style of J.M.W. Turner"},"insert":"the awe-inspiring sky and sea"},{"insert":" by "},{"attributes":{"font":"mirza"},"insert":"a coast with flowers and grasses in spring"}]}',
'worst quality, dark, poor quality',
2,
0.45,
0,
9,
0.5,
None,
False
],
[
'{"ops":[{"insert":"a "},{"attributes":{"font":"slabo"},"insert":"night sky filled with stars"},{"insert":" above a "},{"attributes":{"font":"roboto"},"insert":"turbulent sea with giant waves"}]}',
'',
2,
0.45,
0,
0,
6,
0.5,
None,
False
],
]
gr.Examples(examples=style_examples,
label='Font style examples',
inputs=[
text_input,
negative_prompt,
num_segments,
segment_threshold,
inject_interval,
seed,
color_guidance_weight,
rich_text_input,
background_aug,
],
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, 4k, photorealistic"}]}',
'blurry, art, painting, rendering, drawing, sketch, ugly, duplicate, morbid, mutilated, mutated, deformed, disfigured low quality, worst quality',
5,
0.3,
0,
13,
1,
None,
False
],
[
'{"ops": [{"insert": "A pizza with pineapple, "}, {"attributes": {"size": "20px"}, "insert": "pepperoni"}, {"insert": ", and mushroom on the top, 4k, photorealistic"}]}',
'blurry, art, painting, rendering, drawing, sketch, ugly, duplicate, morbid, mutilated, mutated, deformed, disfigured low quality, worst quality',
5,
0.3,
0,
13,
1,
None,
False
],
[
'{"ops": [{"insert": "A pizza with pineapple, pepperoni, and "}, {"attributes": {"size": "70px"}, "insert": "mushroom"}, {"insert": " on the top, 4k, photorealistic"}]}',
'blurry, art, painting, rendering, drawing, sketch, ugly, duplicate, morbid, mutilated, mutated, deformed, disfigured low quality, worst quality',
5,
0.3,
0,
13,
1,
None,
False
],
]
gr.Examples(examples=size_examples,
label='Font size examples',
inputs=[
text_input,
negative_prompt,
num_segments,
segment_threshold,
inject_interval,
seed,
color_guidance_weight,
rich_text_input,
background_aug,
],
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,
height,
width,
seed,
steps,
num_segments,
segment_threshold,
inject_interval,
guidance_weight,
color_guidance_weight,
rich_text_input,
background_aug
],
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()
|