Spaces:
Runtime error
Runtime error
File size: 18,446 Bytes
4c022fe 3c42b4c 4c022fe 0a499ee ba87c8b 0a499ee 4c022fe baa687a d019488 baa687a d019488 4c022fe 51be712 4c022fe ed9d237 d019488 4c022fe f5cbba2 4c022fe d019488 4c022fe d0745b6 4c022fe ed9d237 4c022fe ab7db7f 4c022fe ab7db7f 4c022fe b624d65 4c022fe 0a499ee b624d65 4c022fe baa687a d019488 4c022fe f5cbba2 4c022fe 69e92de 4c022fe baa687a b624d65 8f96b94 baa687a 4c022fe ba87c8b 4c022fe f5cbba2 4c022fe bc4d8bb 4c022fe ed9d237 d019488 4c022fe f5cbba2 4c022fe bc4d8bb 4c022fe ae61c93 ed9d237 d019488 4c022fe f5cbba2 5db5f99 4c022fe 5db5f99 4c022fe ed9d237 d019488 4c022fe fb8cc1d 5db5f99 f5cbba2 5db5f99 ae61c93 d019488 ae61c93 5db5f99 fb8cc1d 5db5f99 f5cbba2 5db5f99 f5cbba2 5db5f99 f5cbba2 5db5f99 f5cbba2 5db5f99 f5cbba2 5db5f99 f5cbba2 5db5f99 ed9d237 d019488 fb8cc1d f5cbba2 5db5f99 f5cbba2 5db5f99 f5cbba2 5db5f99 f5cbba2 5db5f99 fb8cc1d 5db5f99 fb8cc1d 5db5f99 ed9d237 d019488 fb8cc1d 4c022fe 5db5f99 4c022fe ed9d237 d019488 4c022fe b624d65 4c022fe ed9d237 d019488 4c022fe b624d65 d019488 4c022fe 7b1dafa 3c42b4c 4c022fe |
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 |
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
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. The token map may not always accurately capture the region of the formatted tokens. If you're experiencing this problem, experiment with selecting more or fewer tokens to expand or reduce the area covered by the token maps.
3. If you use font color and get completely corrupted results, you may consider decrease the color weight lambda.
4. 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, guidance_weight, color_guidance_weight, rich_text_input) => {
const richEl = document.getElementById("rich-text-root");
const data = richEl? richEl.contentDocument.body._data : {};
return [text_input, negative_prompt, height, width, seed, steps, guidance_weight, color_guidance_weight, JSON.stringify(data)];
}
"""
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,
guidance_weight: float,
color_guidance_weight: float,
rich_text_input: str
):
run_dir = 'results/'
# 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 = text_input if text_input != '' else rich_text_input
print('text_input', text_input)
# 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), device)
# 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.attention_maps is None:
model.register_evaluation_hooks()
else:
model.reset_attention_maps()
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))
color_obj_masks, _ = get_token_maps(
model.attention_maps, run_dir, width//8, height//8, color_target_token_ids, seed)
model.masks, token_maps = get_token_maps(
model.attention_maps, run_dir, width//8, height//8, region_target_token_ids, seed, base_tokens)
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_evaluation_hooks()
# generate image from rich text
begin_time = time.time()
seed_everything(seed)
rich_img = model.prompt_to_img(region_text_prompts, [negative_text],
height=height, width=width, num_inference_steps=steps,
guidance_scale=guidance_weight, use_grad_guidance=use_grad_guidance,
text_format_dict=text_format_dict)
print('time lapses to generate image from rich text: %.4f' %
(time.time()-begin_time))
return [plain_img[0], rich_img[0], token_maps]
with gr.Blocks() as demo:
gr.HTML("""<h1 style="font-weight: 900; margin-bottom: 7px;">Expressive Text-to-Image Generation with Rich Text</h1>
<p> Visit our <a href="https://rich-text-to-image.github.io/rich-text-to-json.html">rich-text-to-json interface</a> to generate rich-text JSON input.<p/>
<p> <a href="https://rich-text-to-image.github.io">[Website]</a> | <a href="https://github.com/SongweiGe/rich-text-to-image">[Code]</a> <p/> """)
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',
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"}]}\'')
negative_prompt = gr.Textbox(
label='Negative Prompt',
max_lines=1,
placeholder='Example: poor quality, blurry, dark, low resolution, low quality, worst quality')
seed = gr.Slider(label='Seed',
minimum=0,
maximum=100000,
step=1,
value=6)
color_guidance_weight = gr.Slider(label='Color weight lambda',
minimum=0,
maximum=2,
step=0.1,
value=0.5)
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, 768, 896],
value=512,
label='Width',
visible=True)
height = gr.Dropdown(choices=[512, 768, 896],
value=512,
label='height',
visible=True)
with gr.Row():
with gr.Column(scale=1, min_width=100):
generate_button = gr.Button("Generate")
with gr.Column():
richtext_result = gr.Image(label='Rich-text')
with gr.Row():
plaintext_result = gr.Image(label='Plain-text')
token_map = gr.Image(label='Token Maps')
with gr.Row():
gr.Markdown(help_text)
with gr.Row():
style_examples = [
[
'{"ops":[{"insert":"a "},{"attributes":{"font":"slabo"},"insert":"night sky filled with stars"},{"insert":" above a "},{"attributes":{"font":"roboto"},"insert":"turbulent sea with giant waves"}]}',
'',
512,
512,
6,
1,
None
],
[
'{"ops":[{"insert":"a "},{"attributes":{"font":"mirza"},"insert":"beautiful garden"},{"insert":" with a "},{"attributes":{"font":"roboto"},"insert":"snow mountain in the background"},{"insert":""}]}',
'',
512,
512,
3,
1,
None
],
[
'{"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"}]}',
'',
512,
512,
9,
1,
None
],
]
gr.Examples(examples=style_examples,
label='Font style examples',
inputs=[
text_input,
negative_prompt,
height,
width,
seed,
color_guidance_weight,
rich_text_input,
],
outputs=[
plaintext_result,
richtext_result,
token_map,
],
fn=generate,
# cache_examples=True,
examples_per_page=20)
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."}]}',
'',
512,
512,
6,
1,
None
],
[
'{"ops":[{"insert":"A "},{"attributes":{"link":"kitchen island with a built-in oven and a stove with gas burners "},"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"}]}',
'',
512,
512,
6,
1,
None
],
[
'{"ops":[{"insert":"A "},{"attributes":{"link":"Art inspired by kung fu panda, 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"}]}',
'',
512,
512,
6,
1,
None
],
]
gr.Examples(examples=footnote_examples,
label='Footnote examples',
inputs=[
text_input,
negative_prompt,
height,
width,
seed,
color_guidance_weight,
rich_text_input,
],
outputs=[
plaintext_result,
richtext_result,
token_map,
],
fn=generate,
# cache_examples=True,
examples_per_page=20)
with gr.Row():
color_examples = [
[
'{"ops":[{"insert":"a Gothic "},{"attributes":{"color":"#b26b00"},"insert":"church"},{"insert":" in a the sunset with a beautiful landscape in the background."}]}',
'',
512,
512,
6,
1,
None
],
[
'{"ops":[{"insert":"A mesmerizing sight that captures the beauty of a "},{"attributes":{"color":"#4775fc"},"insert":"rose"},{"insert":" blooming, close up"}]}',
'',
512,
512,
9,
1,
None
],
[
'{"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 "}]}',
'',
512,
512,
5,
0.6,
None
],
]
gr.Examples(examples=color_examples,
label='Font color examples',
inputs=[
text_input,
negative_prompt,
height,
width,
seed,
color_guidance_weight,
rich_text_input,
],
outputs=[
plaintext_result,
richtext_result,
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',
512,
512,
13,
1,
None
],
[
'{"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',
512,
512,
13,
1,
None
],
[
'{"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',
512,
512,
13,
1,
None
],
]
gr.Examples(examples=size_examples,
label='Font size examples',
inputs=[
text_input,
negative_prompt,
height,
width,
seed,
color_guidance_weight,
rich_text_input,
],
outputs=[
plaintext_result,
richtext_result,
token_map,
],
fn=generate,
# cache_examples=True,
examples_per_page=20)
generate_button.click(
fn=generate,
inputs=[
text_input,
negative_prompt,
height,
width,
seed,
steps,
guidance_weight,
color_guidance_weight,
rich_text_input
],
outputs=[plaintext_result, richtext_result, token_map],
_js=get_js_data
)
demo.queue(concurrency_count=1)
demo.launch(share=False)
if __name__ == "__main__":
main() |