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 = """
Visit our rich-text-to-json interface to generate rich-text JSON input.
""") 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(): with gr.Row(): plaintext_result = gr.Image(label='Plain-text') richtext_result = gr.Image(label='Rich-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(): 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, 8, 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) 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 ], ] 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) 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()