Spaces:
Build error
Build error
Commit ·
08f7d1b
1
Parent(s): 5d34271
update
Browse files81 unmodified lines82838485868799 unmodified lines1871881891901911921935 unmodified lines19920020120220320497 unmodified lines3023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553562 unmodified lines35936036136236336411 unmodified lines37637737837938038138220 unmodified lines40340440540640740840941014 unmodified lines42542642742842943043143243343443543681 unmodified linestext_encoder.resize_token_embeddings(len(tokenizer))#### for interactivestack = []state = 099 unmodified linesdef text_to_image(prompt,keywords,slider_step,slider_guidance,slider_batch,slider_temperature,slider_natural):global stackglobal state5 unmodified linesif slider_natural:user_prompt = f'<|startoftext|> {user_prompt} <|endoftext|>'composed_prompt = user_promptelse:if len(stack) == 0:97 unmodified linescomposed_prompt = user_promptprompt = 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_imageos.system('nvidia-smi')return tuple(results), composed_promptwith gr.Blocks() as demo:gr.HTML(2 unmodified lines<h2 style="font-weight: 900; font-size: 2.5rem; margin: 0rem">TextDiffuser-2: Unleashing the Power of Language Models for Text Rendering</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>]11 unmodified lines}</style><img src="https://i.ibb.co/vmrXRb5/architecture.jpg" alt="textdiffuser-2" class="scaled-image"></div>""")20 unmodified linesundo.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.")14 unmodified lines# gr.Markdown("## Prompt Examples")button.click(text_to_image, inputs=[prompt,keywords,slider_step,slider_guidance,slider_batch,slider_temperature,slider_natural], outputs=[output, composed_prompt])gr.Markdown("## Prompt Examples")gr.Examples([["A beautiful city skyline stamp of Shanghai", ""],["A book cover named summer vibe", ""],],prompt,81 unmodified lines82838485868788899091929394959699 unmodified lines1961971981992002012025 unmodified lines20820921021121221321497 unmodified lines3123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773782 unmodified lines38138238338438538638738838939039139211 unmodified lines40440540640740840941020 unmodified lines43143243343443543643743814 unmodified lines45345445545645745845946046146246346446581 unmodified linestext_encoder.resize_token_embeddings(len(tokenizer))#### load lcm componentsmodel_id = "lambdalabs/sd-pokemon-diffusers"lcm_lora_id = "latent-consistency/lcm-lora-sdv1-5"pipe = DiffusionPipeline.from_pretrained(model_id, unet=unet, tokenizer=tokenizer, text_encoder=text_encoder)pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)pipe.load_lora_weights(lcm_lora_id)pipe.to(device="cuda")#### for interactivestack = []state = 099 unmodified linesdef text_to_image(prompt,keywords,radio,slider_step,slider_guidance,slider_batch,slider_temperature,slider_natural):global stackglobal state5 unmodified linesif slider_natural:user_prompt = f'<|startoftext|> {user_prompt} <|endoftext|>'composed_prompt = user_promptprompt = tokenizer.encode(user_prompt)else:if len(stack) == 0:97 unmodified linescomposed_prompt = user_promptprompt = tokenizer.encode(user_prompt)if radio == 'TextDiffuser-2':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_imageos.system('nvidia-smi')return tuple(results), composed_promptelif radio == 'TextDiffuser-2-LCM':generator = torch.Generator(device=pipe.device).manual_seed(random.randint(0,1000))image = pipe(prompt=user_prompt,generator=generator,# negative_prompt=negative_prompt,num_inference_steps=slider_step,guidance_scale=1,).images[0]return tuple([image]), composed_promptwith gr.Blocks() as demo:gr.HTML(2 unmodified lines<h2 style="font-weight: 900; font-size: 2.5rem; margin: 0rem">TextDiffuser-2: Unleashing the Power of Language Models for Text Rendering</h2><h2 style="font-weight: 460; font-size: 1.1rem; 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.1rem; 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>]11 unmodified lines}</style><img src="https://i.ibb.co/56JVg5j/architecture.jpg" alt="textdiffuser-2" class="scaled-image"></div>""")20 unmodified linesundo.click(exe_undo, [i,t],[i])skip_button.click(skip_fun, [i,t])radio = gr.Radio(["TextDiffuser-2", "TextDiffuser-2-LCM"], label="Choices of models", value="TextDiffuser-2")slider_step = gr.Slider(minimum=1, maximum=50, value=20, step=1, label="Sampling step", info="The sampling step for TextDiffuser-2.")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.")14 unmodified lines# gr.Markdown("## Prompt Examples")button.click(text_to_image, inputs=[prompt,keywords,radio,slider_step,slider_guidance,slider_batch,slider_temperature,slider_natural], outputs=[output, composed_prompt])gr.Markdown("## Prompt Examples")gr.Examples([["A beautiful city skyline stamp of Shanghai", ""],["A stamp of U.S.A.", ""],["A book cover named summer vibe", ""],],prompt,
app.py+83-54