from cldm.ddim_hacked import DDIMSampler import math from omegaconf import OmegaConf from scripts.rendertext_tool import Render_Text, load_model_from_config, load_model_ckpt import gradio as gr import os import torch import time from PIL import Image from cldm.hack import disable_verbosity, enable_sliced_attention # from pytorch_lightning import seed_everything def process_multi_wrapper(rendered_txt_0, rendered_txt_1, rendered_txt_2, rendered_txt_3, shared_prompt, width_0, width_1, width_2, width_3, ratio_0, ratio_1, ratio_2, ratio_3, top_left_x_0, top_left_x_1, top_left_x_2, top_left_x_3, top_left_y_0, top_left_y_1, top_left_y_2, top_left_y_3, yaw_0, yaw_1, yaw_2, yaw_3, num_rows_0, num_rows_1, num_rows_2, num_rows_3, shared_num_samples, shared_image_resolution, shared_ddim_steps, shared_guess_mode, shared_strength, shared_scale, shared_seed, shared_eta, shared_a_prompt, shared_n_prompt, allow_run_generation = True): if not allow_run_generation: return "Please get the glyph image first by clicking the 'Render Glyph Image' button", None, allow_run_generation rendered_txt_values = [rendered_txt_0, rendered_txt_1, rendered_txt_2, rendered_txt_3] width_values = [width_0, width_1, width_2, width_3] ratio_values = [ratio_0, ratio_1, ratio_2, ratio_3] top_left_x_values = [top_left_x_0, top_left_x_1, top_left_x_2, top_left_x_3] top_left_y_values = [top_left_y_0, top_left_y_1, top_left_y_2, top_left_y_3] yaw_values = [yaw_0, yaw_1, yaw_2, yaw_3] num_rows_values = [num_rows_0, num_rows_1, num_rows_2, num_rows_3] allow_run_generation = False return "The image generation process finished!", render_tool.process_multi(rendered_txt_values, shared_prompt, width_values, ratio_values, top_left_x_values, top_left_y_values, yaw_values, num_rows_values, shared_num_samples, shared_image_resolution, shared_ddim_steps, shared_guess_mode, shared_strength, shared_scale, shared_seed, shared_eta, shared_a_prompt, shared_n_prompt ), allow_run_generation def process_multi_wrapper_only_show_rendered(rendered_txt_0, rendered_txt_1, rendered_txt_2, rendered_txt_3, shared_prompt, width_0, width_1, width_2, width_3, ratio_0, ratio_1, ratio_2, ratio_3, top_left_x_0, top_left_x_1, top_left_x_2, top_left_x_3, top_left_y_0, top_left_y_1, top_left_y_2, top_left_y_3, yaw_0, yaw_1, yaw_2, yaw_3, num_rows_0, num_rows_1, num_rows_2, num_rows_3, shared_num_samples, shared_image_resolution, shared_ddim_steps, shared_guess_mode, shared_strength, shared_scale, shared_seed, shared_eta, shared_a_prompt, shared_n_prompt): rendered_txt_values = [rendered_txt_0, rendered_txt_1, rendered_txt_2, rendered_txt_3] width_values = [width_0, width_1, width_2, width_3] ratio_values = [ratio_0, ratio_1, ratio_2, ratio_3] top_left_x_values = [top_left_x_0, top_left_x_1, top_left_x_2, top_left_x_3] top_left_y_values = [top_left_y_0, top_left_y_1, top_left_y_2, top_left_y_3] yaw_values = [yaw_0, yaw_1, yaw_2, yaw_3] num_rows_values = [num_rows_0, num_rows_1, num_rows_2, num_rows_3] allow_run_generation = True return "The glyph image is generated!", render_tool.process_multi(rendered_txt_values, shared_prompt, width_values, ratio_values, top_left_x_values, top_left_y_values, yaw_values, num_rows_values, shared_num_samples, shared_image_resolution, shared_ddim_steps, shared_guess_mode, shared_strength, shared_scale, shared_seed, shared_eta, shared_a_prompt, shared_n_prompt, only_show_rendered_image=True), allow_run_generation def load_ckpt(model_ckpt = "LAION-Glyph-10M-Epoch-5"): global render_tool, model if torch.cuda.is_available(): for i in range(5): torch.cuda.empty_cache() time.sleep(2) print("empty the cuda cache") # if model_ckpt == "LAION-Glyph-1M": # model = load_model_ckpt(model, "laion1M_model_wo_ema.ckpt") # if model_ckpt == "LAION-Glyph-10M-Epoch-5": # model = load_model_ckpt(model, "laion10M_epoch_5_model_wo_ema.ckpt") if model_ckpt == "LAION-Glyph-10M-Epoch-6": model = load_model_ckpt(model, "checkpoints/laion10M_epoch_6_model_wo_ema.ckpt") elif model_ckpt == "TextCaps-5K-Epoch-10": model = load_model_ckpt(model, "checkpoints/textcaps5K_epoch_10_model_wo_ema.ckpt") elif model_ckpt == "TextCaps-5K-Epoch-20": model = load_model_ckpt(model, "checkpoints/textcaps5K_epoch_20_model_wo_ema.ckpt") elif model_ckpt == "TextCaps-5K-Epoch-40": model = load_model_ckpt(model, "checkpoints/textcaps5K_epoch_40_model_wo_ema.ckpt") render_tool = Render_Text(model, save_memory = SAVE_MEMORY) output_str = f"already change the model checkpoint to {model_ckpt}" print(output_str) if torch.cuda.is_available(): for i in range(5): torch.cuda.empty_cache() time.sleep(2) print("empty the cuda cache") allow_run_generation = False return output_str, None, allow_run_generation SAVE_MEMORY = False disable_verbosity() if SAVE_MEMORY: enable_sliced_attention() cfg = OmegaConf.load("config.yaml") model = load_model_from_config(cfg, "checkpoints/laion10M_epoch_6_model_wo_ema.ckpt", verbose=True) render_tool = Render_Text(model, save_memory = SAVE_MEMORY) description = """ ## Control Stable Diffusion with Glyph Images """ SPACE_ID = os.getenv('SPACE_ID') if SPACE_ID is not None: # description += f'\n
For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings. < a href=" ">< img style="display: inline; margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space" /> a>
' description += f'\nFor faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings.