Spaces:
Build error
Build error
Commit ·
0f61c4d
1
Parent(s): bf48e0b
update
Browse filesFiles changed (2)
- app.py+424-4
- requirements.txtAdded+7
1234567import gradio as grdef greet(name):return "Hello " + name + "!!"iface = gr.Interface(fn=greet, inputs="text", outputs="text")iface.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427import osimport reimport zipfileimport torchimport gradio as grimport timefrom transformers import CLIPTextModel, CLIPTokenizerfrom diffusers import AutoencoderKL, DDPMScheduler, StableDiffusionPipeline, UNet2DConditionModelfrom tqdm import tqdmfrom PIL import Imagefrom PIL import Image, ImageDraw, ImageFontimport stringalphabet = string.digits + string.ascii_lowercase + string.ascii_uppercase + string.punctuation + ' ' # len(aphabet) = 95'''alphabet0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'''if not os.path.exists('arial.ttf'):os.system('wget https://huggingface.co/datasets/JingyeChen22/TextDiffuser/resolve/main/arial.ttf')if not os.path.exists('architecture.ttf'):os.system('wget https://huggingface.co/JingyeChen22/textdiffuser2-full-ft/blob/main/architecture.jpg')if not os.path.exists('gray256.jpg'):os.system('wget https://huggingface.co/JingyeChen22/textdiffuser2-full-ft/blob/main/gray256.jpg')# #### import m1# from fastchat.model import load_model, get_conversation_template# m1_model_path = '/home/jingyechen/FastChat/1204_final'# m1_model, m1_tokenizer = load_model(# m1_model_path,# 'cuda',# 1,# None,# False,# False,# revision="main",# debug=False,# )#### import diffusion modelstext_encoder = CLIPTextModel.from_pretrained('JingyeChen22/textdiffuser2-full-ft', subfolder="text_encoder", ignore_mismatched_sizes=True).cuda()tokenizer = CLIPTokenizer.from_pretrained('runwayml/stable-diffusion-v1-5', subfolder="tokenizer")#### additional tokens are introduced, including coordinate tokens and character tokensprint('***************')print(len(tokenizer))for i in range(520):tokenizer.add_tokens(['l' + str(i) ]) # lefttokenizer.add_tokens(['t' + str(i) ]) # toptokenizer.add_tokens(['r' + str(i) ]) # widthtokenizer.add_tokens(['b' + str(i) ]) # heightfor c in alphabet:tokenizer.add_tokens([f'[{c}]'])print(len(tokenizer))print('***************')vae = AutoencoderKL.from_pretrained('runwayml/stable-diffusion-v1-5', subfolder="vae").cuda()unet = UNet2DConditionModel.from_pretrained('JingyeChen22/textdiffuser2-full-ft', subfolder="unet").cuda()text_encoder.resize_token_embeddings(len(tokenizer))#### for interactivestack = []state = 0font = ImageFont.truetype("./arial.ttf", 32)def skip_fun(i, t):global statestate = 0def exe_undo(i, t):global stackglobal statestate = 0stack = []image = Image.open('./gray256.jpg')print('stack', stack)return imagedef exe_redo(i, t):global statestate = 0if len(stack) > 0:stack.pop()image = Image.open('./gray256.jpg')draw = ImageDraw.Draw(image)for items in stack:# print('now', items)text_position, t = itemsif len(text_position) == 2:x, y = text_positiontext_color = (255, 0, 0)draw.text((x+2, y), t, font=font, fill=text_color)r = 4leftUpPoint = (x-r, y-r)rightDownPoint = (x+r, y+r)draw.ellipse((leftUpPoint,rightDownPoint), fill='red')elif len(text_position) == 4:x0, y0, x1, y1 = text_positiontext_color = (255, 0, 0)draw.text((x0+2, y0), t, font=font, fill=text_color)r = 4leftUpPoint = (x0-r, y0-r)rightDownPoint = (x0+r, y0+r)draw.ellipse((leftUpPoint,rightDownPoint), fill='red')draw.rectangle((x0,y0,x1,y1), outline=(255, 0, 0) )print('stack', stack)return imagedef get_pixels(i, t, evt: gr.SelectData):global statetext_position = evt.indexif state == 0:stack.append((text_position, t))print(text_position, stack)state = 1else:(_, t) = stack.pop()x, y = _stack.append(((x,y,text_position[0],text_position[1]), t))state = 0image = Image.open('./gray256.jpg')draw = ImageDraw.Draw(image)for items in stack:# print('now', items)text_position, t = itemsif len(text_position) == 2:x, y = text_positiontext_color = (255, 0, 0)draw.text((x+2, y), t, font=font, fill=text_color)r = 4leftUpPoint = (x-r, y-r)rightDownPoint = (x+r, y+r)draw.ellipse((leftUpPoint,rightDownPoint), fill='red')elif len(text_position) == 4:x0, y0, x1, y1 = text_positiontext_color = (255, 0, 0)draw.text((x0+2, y0), t, font=font, fill=text_color)r = 4leftUpPoint = (x0-r, y0-r)rightDownPoint = (x0+r, y0+r)draw.ellipse((leftUpPoint,rightDownPoint), fill='red')draw.rectangle((x0,y0,x1,y1), outline=(255, 0, 0) )print('stack', stack)return imagedef text_to_image(prompt,keywords,slider_step,slider_guidance,slider_batch,slider_temperature):global stackglobal statewith torch.no_grad():time1 = time.time()user_prompt = promptif len(stack) == 0:if len(keywords.strip()) == 0:template = f'Given a prompt that will be used to generate an image, plan the layout of visual text for the image. The size of the image is 128x128. Therefore, all properties of the positions should not exceed 128, including the coordinates of top, left, right, and bottom. All keywords are included in the caption. You dont need to specify the details of font styles. At each line, the format should be keyword left, top, right, bottom. So let us begin. Prompt: {user_prompt}'else:keywords = keywords.split('/')keywords = [i.strip() for i in keywords]template = f'Given a prompt that will be used to generate an image, plan the layout of visual text for the image. The size of the image is 128x128. Therefore, all properties of the positions should not exceed 128, including the coordinates of top, left, right, and bottom. In addition, we also provide all keywords at random order for reference. You dont need to specify the details of font styles. At each line, the format should be keyword left, top, right, bottom. So let us begin. Prompt: {prompt}. Keywords: {str(keywords)}'msg = templateconv = get_conversation_template(m1_model_path)conv.append_message(conv.roles[0], msg)conv.append_message(conv.roles[1], None)prompt = conv.get_prompt()inputs = m1_tokenizer([prompt], return_token_type_ids=False)inputs = {k: torch.tensor(v).to('cuda') for k, v in inputs.items()}output_ids = m1_model.generate(**inputs,do_sample=True,temperature=slider_temperature,repetition_penalty=1.0,max_new_tokens=512,)if m1_model.config.is_encoder_decoder:output_ids = output_ids[0]else:output_ids = output_ids[0][len(inputs["input_ids"][0]) :]outputs = m1_tokenizer.decode(output_ids, skip_special_tokens=True, spaces_between_special_tokens=False)print(f"[{conv.roles[0]}]\n{msg}")print(f"[{conv.roles[1]}]\n{outputs}")ocrs = outputs.split('\n')time2 = time.time()print(time2-time1)# user_prompt = promptcurrent_ocr = ocrsocr_ids = []print('user_prompt', user_prompt)print('current_ocr', current_ocr)for ocr in current_ocr:ocr = ocr.strip()if len(ocr) == 0 or '###' in ocr or '.com' in ocr:continueitems = ocr.split()pred = ' '.join(items[:-1])box = items[-1]l,t,r,b = box.split(',')l,t,r,b = int(l), int(t), int(r), int(b)ocr_ids.extend(['l'+str(l), 't'+str(t), 'r'+str(r), 'b'+str(b)])char_list = list(pred)char_list = [f'[{i}]' for i in char_list]ocr_ids.extend(char_list)ocr_ids.append(tokenizer.eos_token_id)caption_ids = tokenizer(user_prompt, truncation=True, return_tensors="pt").input_ids[0].tolist()try:ocr_ids = tokenizer.encode(ocr_ids)prompt = caption_ids + ocr_idsexcept:prompt = caption_idselse:user_prompt += ' <|endoftext|>'for items in stack:position, text = itemsif len(position) == 2:x, y = positionx = x // 4y = y // 4text_str = ' '.join([f'[{c}]' for c in list(text)])user_prompt += f'<|startoftext|> l{x} t{y} {text_str} <|endoftext|>'elif len(position) == 4:x0, y0, x1, y1 = positionx0 = x0 // 4y0 = y0 // 4x1 = x1 // 4y1 = y1 // 4text_str = ' '.join([f'[{c}]' for c in list(text)])user_prompt += f'<|startoftext|> l{x0} t{y0} r{x1} b{y1} {text_str} <|endoftext|>'prompt = tokenizer.encode(user_prompt)prompt = prompt[:77]while len(prompt) < 77:prompt.append(tokenizer.pad_token_id)prompts_cond = promptprompts_nocond = [tokenizer.pad_token_id]*77prompts_cond = [prompts_cond] * slider_batchprompts_nocond = [prompts_nocond] * slider_batchprompts_cond = torch.Tensor(prompts_cond).long().cuda()prompts_nocond = torch.Tensor(prompts_nocond).long().cuda()scheduler = DDPMScheduler.from_pretrained('runwayml/stable-diffusion-v1-5', subfolder="scheduler")scheduler.set_timesteps(slider_step)noise = torch.randn((slider_batch, 4, 64, 64)).to("cuda")input = noiseencoder_hidden_states_cond = text_encoder(prompts_cond)[0]encoder_hidden_states_nocond = text_encoder(prompts_nocond)[0]for t in tqdm(scheduler.timesteps):with torch.no_grad(): # classifier free guidancenoise_pred_cond = unet(sample=input, timestep=t, encoder_hidden_states=encoder_hidden_states_cond[:slider_batch]).sample # b, 4, 64, 64noise_pred_uncond = unet(sample=input, timestep=t, encoder_hidden_states=encoder_hidden_states_nocond[:slider_batch]).sample # b, 4, 64, 64noisy_residual = noise_pred_uncond + slider_guidance * (noise_pred_cond - noise_pred_uncond) # b, 4, 64, 64prev_noisy_sample = scheduler.step(noisy_residual, t, input).prev_sampleinput = prev_noisy_sample# decodeinput = 1 / vae.config.scaling_factor * inputimages = vae.decode(input, return_dict=False)[0]width, height = 512, 512results = []new_image = Image.new('RGB', (2*width, 2*height))for index, image in enumerate(images.float()):image = (image / 2 + 0.5).clamp(0, 1).unsqueeze(0)image = image.cpu().permute(0, 2, 3, 1).numpy()[0]image = Image.fromarray((image * 255).round().astype("uint8")).convert('RGB')results.append(image)row = index // 2col = index % 2new_image.paste(image, (col*width, row*height))# new_image.save(f'{args.output_dir}/pred_img_{sample_index}_{args.local_rank}.jpg')results.insert(0, new_image)return new_imagewith gr.Blocks() as demo:gr.HTML("""<div style="text-align: center; max-width: 1600px; margin: 20px auto;"><h2 style="font-weight: 900; font-size: 2.7rem; margin: 0rem">TextDiffuser-2: Unleashing the Power of Language Models for Text Rendering</h2><h2 style="font-weight: 480; font-size: 1.4rem; margin: 0rem"><a href="https://jingyechen.github.io/">Jingye Chen</a>, <a href="https://hypjudy.github.io/website/">Yupan Huang</a>, <a href="https://scholar.google.com/citations?user=0LTZGhUAAAAJ&hl=en">Tengchao Lv</a>, <a href="https://www.microsoft.com/en-us/research/people/lecu/">Lei Cui</a>, <a href="https://cqf.io/">Qifeng Chen</a>, <a href="https://thegenerality.com/">Furu Wei</a></h2><h2 style="font-weight: 460; font-size: 1.2rem; margin: 0rem">HKUST, Sun Yat-sen University, Microsoft Research</h2><h3 style="font-weight: 450; font-size: 1rem; margin: 0rem">[<a href="https://arxiv.org/abs/2311.16465" style="color:blue;">arXiv</a>][<a href="https://github.com/microsoft/unilm/tree/master/textdiffuser-2" style="color:blue;">Code</a>]</h3><h2 style="text-align: left; font-weight: 450; font-size: 1rem; margin-top: 0.5rem; margin-bottom: 0.5rem">We propose <b>TextDiffuser-2</b>, aiming at unleashing the power of language models for text rendering. Specifically, we <b>tame a language model into a layout planner</b> to transform user prompt into a layout using the caption-OCR pairs. The language model demonstrates flexibility and automation by inferring keywords from user prompts or incorporating user-specified keywords to determine their positions. Secondly, we <b>leverage the language model in the diffusion model as the layout encoder</b> to represent the position and content of text at the line level. This approach enables diffusion models to generate text images with broader diversity.</h2><h2 style="text-align: left; font-weight: 450; font-size: 1rem; margin-top: 0.5rem; margin-bottom: 0.5rem">👀 <b>Tips for using this demo</b>: <b>(1)</b> Please carefully read the disclaimer in the below. <b>(2)</b> The specification of keywords is optional. If provided, the language model will do its best to plan layouts using the given keywords. <b>(3)</b> If a template is given, the layout planner (M1) is not used. <b>(4)</b> Three operations, including redo, undo, and skip are provided. When using skip, only the left-top point of a keyword will be recorded, resulting in more diversity but sometimes decreasing the accuracy. <b>(5)</b> The layout planner can produce different layouts. You can control the temperature</h2><style>.scaled-image {transform: scale(0.75);}</style><img src="file/architecture.jpg" alt="textdiffuser-2" class="scaled-image"></div>""")with gr.Tab("Text-to-Image"):with gr.Row():with gr.Column(scale=1):prompt = gr.Textbox(label="Input your prompt here.", placeholder="A beautiful city skyline stamp of Shanghai")keywords = gr.Textbox(label="(Optional) Input your keywords here. Keywords should bu seperate by / (e.g., keyword1/keyword2/...)", placeholder="keyword1/keyword2")# 这里加一个会话框with gr.Row():with gr.Column(scale=1):i = gr.Image(label="Template", type='filepath', value='gray256.jpg', height=256, width=256)with gr.Column(scale=3):t = gr.Textbox(label="Template", placeholder='keyword')redo = gr.Button(value='Redo - Cancel the last keyword') # 如何给b绑定事件undo = gr.Button(value='Undo - Clear the canvas') # 如何给b绑定事件skip_button = gr.Button(value='Skip - Operate next keyword') # 如何给b绑定事件i.select(get_pixels,[i,t],[i])redo.click(exe_redo, [i,t],[i])undo.click(exe_undo, [i,t],[i])skip_button.click(skip_fun, [i,t])# radio = gr.Radio(["Stable Diffusion v2.1", "Stable Diffusion v1.5"], label="Pre-trained Model", value="Stable Diffusion v1.5")slider_step = gr.Slider(minimum=1, maximum=50, value=20, step=1, label="Sampling step", info="The sampling step for TextDiffuser.")slider_guidance = gr.Slider(minimum=1, maximum=9, value=7.5, step=0.5, label="Scale of classifier-free guidance", info="The scale of classifier-free guidance and is set to 7.5 in default.")slider_batch = gr.Slider(minimum=1, maximum=4, value=4, step=1, label="Batch size", info="The number of images to be sampled.")slider_temperature = gr.Slider(minimum=0.1, maximum=2, value=0.7, step=0.1, label="Temperature", info="Control the diversity of layout planner. Higher value indicates more diversity.")# slider_seed = gr.Slider(minimum=1, maximum=10000, label="Seed", randomize=True)button = gr.Button("Generate")with gr.Column(scale=1):output = gr.Image(label='Generated image')# with gr.Accordion("Intermediate results", open=False):# gr.Markdown("Layout, segmentation mask, and details of segmentation mask from left to right.")# intermediate_results = gr.Image(label='')# gr.Markdown("## Prompt Examples")button.click(text_to_image, inputs=[prompt,keywords,slider_step,slider_guidance,slider_batch,slider_temperature], outputs=[output])gr.HTML("""<div style="text-align: justify; max-width: 1200px; margin: 20px auto;"><h3 style="font-weight: 450; font-size: 0.8rem; margin: 0rem"><b>Version</b>: 1.0</h3><h3 style="font-weight: 450; font-size: 0.8rem; margin: 0rem"><b>Contact</b>:For help or issues using TextDiffuser-2, please email Jingye Chen <a href="mailto:qwerty.chen@connect.ust.hk">(qwerty.chen@connect.ust.hk)</a>, Yupan Huang <a href="mailto:huangyp28@mail2.sysu.edu.cn">(huangyp28@mail2.sysu.edu.cn)</a> or submit a GitHub issue. For other communications related to TextDiffuser-2, please contact Lei Cui <a href="mailto:lecu@microsoft.com">(lecu@microsoft.com)</a> or Furu Wei <a href="mailto:fuwei@microsoft.com">(fuwei@microsoft.com)</a>.</h3><h3 style="font-weight: 450; font-size: 0.8rem; margin: 0rem"><b>Disclaimer</b>:Please note that the demo is intended for academic and research purposes <b>ONLY</b>. Any use of the demo for generating inappropriate content is strictly prohibited. The responsibility for any misuse or inappropriate use of the demo lies solely with the users who generated such content, and this demo shall not be held liable for any such use.</h3></div>""")demo.launch()
1234567setuptools==66.0.0datasets==2.11.0transformers==4.28.1accelerate==0.22.0 diffusers==0.24.0fschat==0.2.26pillow==10.1.0app.py+424-4
requirements.txtAdded+7