import gc import json import webcolors import spaces import gradio as gr import os.path as osp from copy import deepcopy from PIL import Image, ImageDraw, ImageFont import torch from diffusers import UNet2DConditionModel, AutoencoderKL from diffusers.models.attention import BasicTransformerBlock from peft import LoraConfig from peft.utils import set_peft_model_state_dict from transformers import PretrainedConfig from diffusers import DPMSolverMultistepScheduler from glyph_sdxl.utils import ( parse_config, UNET_CKPT_NAME, huggingface_cache_dir, load_byt5_and_byt5_tokenizer, BYT5_MAPPER_CKPT_NAME, INSERTED_ATTN_CKPT_NAME, BYT5_CKPT_NAME, PromptFormat, ) from glyph_sdxl.custom_diffusers import ( StableDiffusionGlyphXLPipeline, CrossAttnInsertBasicTransformerBlock, ) from glyph_sdxl.modules import T5EncoderBlockByT5Mapper byt5_mapper_dict = [T5EncoderBlockByT5Mapper] byt5_mapper_dict = {mapper.__name__: mapper for mapper in byt5_mapper_dict} from demo.constants import MAX_TEXT_BOX html = f"""

Glyph-ByT5: A Customized Text Encoder for Accurate Visual Text Rendering

Project Page | arXiv Paper | Github | Cite our work if our ideas inspire you.

Try some examples at the bottom of the page to get started!

Usage:

1. Select bounding boxes on the canvas on the left by clicking twice.

2. Click "Redo" if you want to cancel last point, "Undo" for clearing the canvas.

3. Click "I've finished my layout!" to start choosing specific prompts, colors and font-types.

4. Enter a design prompt for the background image. Optionally, you can choose to specify the design categories and tags (separated by a comma).

5. For each text box, enter the text prompts in the text box on the left, and select colors and font-types from the drop boxes on the right.

6. Click on "I've finished my texts, colors and styles, generate!" to start generating!.