task_stablepy = { 'txt2img': 'txt2img', 'img2img': 'img2img', 'inpaint': 'inpaint', 'sd_openpose ControlNet': 'openpose', 'sd_canny ControlNet': 'canny', 'sd_mlsd ControlNet': 'mlsd', 'sd_scribble ControlNet': 'scribble', 'sd_softedge ControlNet': 'softedge', 'sd_segmentation ControlNet': 'segmentation', 'sd_depth ControlNet': 'depth', 'sd_normalbae ControlNet': 'normalbae', 'sd_lineart ControlNet': 'lineart', 'sd_lineart_anime ControlNet': 'lineart_anime', 'sd_shuffle ControlNet': 'shuffle', 'sd_ip2p ControlNet': 'ip2p', 'sdxl_canny T2I Adapter': 'sdxl_canny', 'sdxl_sketch T2I Adapter': 'sdxl_sketch', 'sdxl_lineart T2I Adapter': 'sdxl_lineart', 'sdxl_depth-midas T2I Adapter': 'sdxl_depth-midas', 'sdxl_openpose T2I Adapter': 'sdxl_openpose' } task_model_list = list(task_stablepy.keys()) ####################### # UTILS ####################### import spaces import os from stablepy import Model_Diffusers from stablepy.diffusers_vanilla.model import scheduler_names from stablepy.diffusers_vanilla.style_prompt_config import STYLE_NAMES import torch import re preprocessor_controlnet = { "openpose": [ "Openpose", "None", ], "scribble": [ "HED", "Pidinet", "None", ], "softedge": [ "Pidinet", "HED", "HED safe", "Pidinet safe", "None", ], "segmentation": [ "UPerNet", "None", ], "depth": [ "DPT", "Midas", "None", ], "normalbae": [ "NormalBae", "None", ], "lineart": [ "Lineart", "Lineart coarse", "LineartAnime", "None", "None (anime)", ], "shuffle": [ "ContentShuffle", "None", ], "canny": [ "Canny" ], "mlsd": [ "MLSD" ], "ip2p": [ "ip2p" ] } def download_things(directory, url, hf_token="", civitai_api_key=""): url = url.strip() if "drive.google.com" in url: original_dir = os.getcwd() os.chdir(directory) os.system(f"gdown --fuzzy {url}") os.chdir(original_dir) elif "huggingface.co" in url: url = url.replace("?download=true", "") if "/blob/" in url: url = url.replace("/blob/", "/resolve/") user_header = f'"Authorization: Bearer {hf_token}"' if hf_token: os.system(f"aria2c --console-log-level=error --summary-interval=10 --header={user_header} -c -x 16 -k 1M -s 16 {url} -d {directory} -o {url.split('/')[-1]}") else: os.system (f"aria2c --optimize-concurrent-downloads --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 {url} -d {directory} -o {url.split('/')[-1]}") elif "civitai.com" in url: if "?" in url: url = url.split("?")[0] if civitai_api_key: url = url + f"?token={civitai_api_key}" os.system(f"aria2c --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 -d {directory} {url}") else: print("\033[91mYou need an API key to download Civitai models.\033[0m") else: os.system(f"aria2c --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 -d {directory} {url}") def get_model_list(directory_path): model_list = [] valid_extensions = {'.ckpt' , '.pt', '.pth', '.safetensors', '.bin'} for filename in os.listdir(directory_path): if os.path.splitext(filename)[1] in valid_extensions: name_without_extension = os.path.splitext(filename)[0] file_path = os.path.join(directory_path, filename) # model_list.append((name_without_extension, file_path)) model_list.append(file_path) print('\033[34mFILE: ' + file_path + '\033[0m') return model_list def process_string(input_string): parts = input_string.split('/') if len(parts) == 2: first_element = parts[1] complete_string = input_string result = (first_element, complete_string) return result else: return None directory_models = 'models' os.makedirs(directory_models, exist_ok=True) directory_loras = 'loras' os.makedirs(directory_loras, exist_ok=True) directory_vaes = 'vaes' os.makedirs(directory_vaes, exist_ok=True) # - **Download SD 1.5 Models** download_model = "https://huggingface.co/frankjoshua/toonyou_beta6/resolve/main/toonyou_beta6.safetensors" # - **Download VAEs** download_vae = "https://huggingface.co/fp16-guy/anything_kl-f8-anime2_vae-ft-mse-840000-ema-pruned_blessed_clearvae_fp16_cleaned/resolve/main/anything_fp16.safetensors" # - **Download LoRAs** download_lora = "https://civitai.com/api/download/models/97655, https://civitai.com/api/download/models/124358" load_diffusers_format_model = ['runwayml/stable-diffusion-v1-5', 'stabilityai/stable-diffusion-xl-base-1.0'] CIVITAI_API_KEY = "" hf_token = "" # Download stuffs for url in [url.strip() for url in download_model.split(',')]: if not os.path.exists(f"./models/{url.split('/')[-1]}"): download_things(directory_models, url, hf_token, CIVITAI_API_KEY) for url in [url.strip() for url in download_vae.split(',')]: if not os.path.exists(f"./vaes/{url.split('/')[-1]}"): download_things(directory_vaes, url, hf_token, CIVITAI_API_KEY) for url in [url.strip() for url in download_lora.split(',')]: if not os.path.exists(f"./loras/{url.split('/')[-1]}"): download_things(directory_loras, url, hf_token, CIVITAI_API_KEY) # Download Embeddings directory_embeds = 'embedings' os.makedirs(directory_embeds, exist_ok=True) download_embeds = [ 'https://huggingface.co/datasets/Nerfgun3/bad_prompt/resolve/main/bad_prompt.pt', 'https://huggingface.co/datasets/Nerfgun3/bad_prompt/blob/main/bad_prompt_version2.pt', 'https://huggingface.co/embed/EasyNegative/resolve/main/EasyNegative.safetensors', 'https://huggingface.co/embed/negative/resolve/main/EasyNegativeV2.safetensors', 'https://huggingface.co/embed/negative/resolve/main/bad-hands-5.pt', 'https://huggingface.co/embed/negative/resolve/main/bad-artist.pt', 'https://huggingface.co/embed/negative/resolve/main/ng_deepnegative_v1_75t.pt', 'https://huggingface.co/embed/negative/resolve/main/bad-artist-anime.pt', 'https://huggingface.co/embed/negative/resolve/main/bad-image-v2-39000.pt', 'https://huggingface.co/embed/negative/resolve/main/verybadimagenegative_v1.3.pt', ] for url_embed in download_embeds: if not os.path.exists(f"./embedings/{url_embed.split('/')[-1]}"): download_things(directory_embeds, url_embed, hf_token, CIVITAI_API_KEY) # Build list models embed_list = get_model_list(directory_embeds) model_list = get_model_list(directory_models) model_list = model_list + load_diffusers_format_model lora_model_list = get_model_list(directory_loras) lora_model_list.insert(0, "None") vae_model_list = get_model_list(directory_vaes) vae_model_list.insert(0, "None") print('\033[33m🏁 Download and listing of valid models completed.\033[0m') upscaler_dict_gui = { None : None, "Lanczos" : "Lanczos", "Nearest" : "Nearest", "RealESRGAN_x4plus" : "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth", "RealESRNet_x4plus" : "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.1/RealESRNet_x4plus.pth", "RealESRGAN_x4plus_anime_6B": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth", "RealESRGAN_x2plus": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth", "realesr-animevideov3": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-animevideov3.pth", "realesr-general-x4v3": "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth", "realesr-general-wdn-x4v3" : "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-wdn-x4v3.pth", "4x-UltraSharp" : "https://huggingface.co/Shandypur/ESRGAN-4x-UltraSharp/resolve/main/4x-UltraSharp.pth", "4x_foolhardy_Remacri" : "https://huggingface.co/FacehugmanIII/4x_foolhardy_Remacri/resolve/main/4x_foolhardy_Remacri.pth", "Remacri4xExtraSmoother" : "https://huggingface.co/hollowstrawberry/upscalers-backup/resolve/main/ESRGAN/Remacri%204x%20ExtraSmoother.pth", "AnimeSharp4x" : "https://huggingface.co/hollowstrawberry/upscalers-backup/resolve/main/ESRGAN/AnimeSharp%204x.pth", "lollypop" : "https://huggingface.co/hollowstrawberry/upscalers-backup/resolve/main/ESRGAN/lollypop.pth", "RealisticRescaler4x" : "https://huggingface.co/hollowstrawberry/upscalers-backup/resolve/main/ESRGAN/RealisticRescaler%204x.pth", "NickelbackFS4x" : "https://huggingface.co/hollowstrawberry/upscalers-backup/resolve/main/ESRGAN/NickelbackFS%204x.pth" } def extract_parameters(input_string): parameters = {} input_string = input_string.replace("\n", "") if not "Negative prompt:" in input_string: print("Negative prompt not detected") parameters["prompt"] = input_string return parameters parm = input_string.split("Negative prompt:") parameters["prompt"] = parm[0] if not "Steps:" in parm[1]: print("Steps not detected") parameters["neg_prompt"] = parm[1] return parameters parm = parm[1].split("Steps:") parameters["neg_prompt"] = parm[0] input_string = "Steps:" + parm[1] # Extracting Steps steps_match = re.search(r'Steps: (\d+)', input_string) if steps_match: parameters['Steps'] = int(steps_match.group(1)) # Extracting Size size_match = re.search(r'Size: (\d+x\d+)', input_string) if size_match: parameters['Size'] = size_match.group(1) width, height = map(int, parameters['Size'].split('x')) parameters['width'] = width parameters['height'] = height # Extracting other parameters other_parameters = re.findall(r'(\w+): (.*?)(?=, \w+|$)', input_string) for param in other_parameters: parameters[param[0]] = param[1].strip('"') return parameters ####################### # GUI ####################### import spaces import gradio as gr from PIL import Image import IPython.display import time, json from IPython.utils import capture import logging logging.getLogger("diffusers").setLevel(logging.ERROR) import diffusers diffusers.utils.logging.set_verbosity(40) import warnings warnings.filterwarnings(action="ignore", category=FutureWarning, module="diffusers") warnings.filterwarnings(action="ignore", category=UserWarning, module="diffusers") warnings.filterwarnings(action="ignore", category=FutureWarning, module="transformers") from stablepy import logger logger.setLevel(logging.DEBUG) class GuiSD: def __init__(self): self.model = None @spaces.GPU def infer(self, model, pipe_params): images, image_list = model(**pipe_params) return images # @spaces.GPU def generate_pipeline( self, prompt, neg_prompt, num_images, steps, cfg, clip_skip, seed, lora1, lora_scale1, lora2, lora_scale2, lora3, lora_scale3, lora4, lora_scale4, lora5, lora_scale5, sampler, img_height, img_width, model_name, vae_model, task, image_control, preprocessor_name, preprocess_resolution, image_resolution, style_prompt, # list [] style_json_file, image_mask, strength, low_threshold, high_threshold, value_threshold, distance_threshold, controlnet_output_scaling_in_unet, controlnet_start_threshold, controlnet_stop_threshold, textual_inversion, syntax_weights, loop_generation, leave_progress_bar, disable_progress_bar, image_previews, display_images, save_generated_images, image_storage_location, retain_compel_previous_load, retain_detailfix_model_previous_load, retain_hires_model_previous_load, t2i_adapter_preprocessor, t2i_adapter_conditioning_scale, t2i_adapter_conditioning_factor, upscaler_model_path, upscaler_increases_size, esrgan_tile, esrgan_tile_overlap, hires_steps, hires_denoising_strength, hires_sampler, hires_prompt, hires_negative_prompt, hires_before_adetailer, hires_after_adetailer, xformers_memory_efficient_attention, freeu, generator_in_cpu, adetailer_inpaint_only, adetailer_verbose, adetailer_sampler, adetailer_active_a, prompt_ad_a, negative_prompt_ad_a, strength_ad_a, face_detector_ad_a, person_detector_ad_a, hand_detector_ad_a, mask_dilation_a, mask_blur_a, mask_padding_a, adetailer_active_b, prompt_ad_b, negative_prompt_ad_b, strength_ad_b, face_detector_ad_b, person_detector_ad_b, hand_detector_ad_b, mask_dilation_b, mask_blur_b, mask_padding_b, ): task = task_stablepy[task] # First load model_precision = torch.float16 if not self.model: from stablepy import Model_Diffusers print("Loading model...") self.model = Model_Diffusers( base_model_id=model_name, task_name=task, vae_model=vae_model if vae_model != "None" else None, type_model_precision=model_precision ) self.model.load_pipe( model_name, task_name=task, vae_model=vae_model if vae_model != "None" else None, type_model_precision=model_precision ) if task != "txt2img" and not image_control: raise ValueError("No control image found: To use this function, you have to upload an image in 'Image ControlNet/Inpaint/Img2img'") if task == "inpaint" and not image_mask: raise ValueError("No mask image found: Specify one in 'Image Mask'") if upscaler_model_path in [None, "Lanczos", "Nearest"]: upscaler_model = upscaler_model_path else: directory_upscalers = 'upscalers' os.makedirs(directory_upscalers, exist_ok=True) url_upscaler = upscaler_dict_gui[upscaler_model_path] if not os.path.exists(f"./upscalers/{url_upscaler.split('/')[-1]}"): download_things(directory_upscalers, url_upscaler, hf_token) upscaler_model = f"./upscalers/{url_upscaler.split('/')[-1]}" if textual_inversion and self.model.class_name == "StableDiffusionXLPipeline": print("No Textual inversion for SDXL") logging.getLogger("ultralytics").setLevel(logging.INFO if adetailer_verbose else logging.ERROR) adetailer_params_A = { "face_detector_ad" : face_detector_ad_a, "person_detector_ad" : person_detector_ad_a, "hand_detector_ad" : hand_detector_ad_a, "prompt": prompt_ad_a, "negative_prompt" : negative_prompt_ad_a, "strength" : strength_ad_a, # "image_list_task" : None, "mask_dilation" : mask_dilation_a, "mask_blur" : mask_blur_a, "mask_padding" : mask_padding_a, "inpaint_only" : adetailer_inpaint_only, "sampler" : adetailer_sampler, } adetailer_params_B = { "face_detector_ad" : face_detector_ad_b, "person_detector_ad" : person_detector_ad_b, "hand_detector_ad" : hand_detector_ad_b, "prompt": prompt_ad_b, "negative_prompt" : negative_prompt_ad_b, "strength" : strength_ad_b, # "image_list_task" : None, "mask_dilation" : mask_dilation_b, "mask_blur" : mask_blur_b, "mask_padding" : mask_padding_b, } pipe_params = { "prompt": prompt, "negative_prompt": neg_prompt, "img_height": img_height, "img_width": img_width, "num_images": num_images, "num_steps": steps, "guidance_scale": cfg, "clip_skip": clip_skip, "seed": seed, "image": image_control, "preprocessor_name": preprocessor_name, "preprocess_resolution": preprocess_resolution, "image_resolution": image_resolution, "style_prompt": style_prompt if style_prompt else "", "style_json_file": "", "image_mask": image_mask, # only for Inpaint "strength": strength, # only for Inpaint or ... "low_threshold": low_threshold, "high_threshold": high_threshold, "value_threshold": value_threshold, "distance_threshold": distance_threshold, "lora_A": lora1 if lora1 != "None" else None, "lora_scale_A": lora_scale1, "lora_B": lora2 if lora2 != "None" else None, "lora_scale_B": lora_scale2, "lora_C": lora3 if lora3 != "None" else None, "lora_scale_C": lora_scale3, "lora_D": lora4 if lora4 != "None" else None, "lora_scale_D": lora_scale4, "lora_E": lora5 if lora5 != "None" else None, "lora_scale_E": lora_scale5, "textual_inversion": embed_list if textual_inversion and self.model.class_name != "StableDiffusionXLPipeline" else [], "syntax_weights": syntax_weights, # "Classic" "sampler": sampler, "xformers_memory_efficient_attention": xformers_memory_efficient_attention, "gui_active": True, "loop_generation": loop_generation, "controlnet_conditioning_scale": float(controlnet_output_scaling_in_unet), "control_guidance_start": float(controlnet_start_threshold), "control_guidance_end": float(controlnet_stop_threshold), "generator_in_cpu": generator_in_cpu, "FreeU": freeu, "adetailer_A": adetailer_active_a, "adetailer_A_params": adetailer_params_A, "adetailer_B": adetailer_active_b, "adetailer_B_params": adetailer_params_B, "leave_progress_bar": leave_progress_bar, "disable_progress_bar": disable_progress_bar, "image_previews": image_previews, "display_images": display_images, "save_generated_images": save_generated_images, "image_storage_location": image_storage_location, "retain_compel_previous_load": retain_compel_previous_load, "retain_detailfix_model_previous_load": retain_detailfix_model_previous_load, "retain_hires_model_previous_load": retain_hires_model_previous_load, "t2i_adapter_preprocessor": t2i_adapter_preprocessor, "t2i_adapter_conditioning_scale": float(t2i_adapter_conditioning_scale), "t2i_adapter_conditioning_factor": float(t2i_adapter_conditioning_factor), "upscaler_model_path": upscaler_model, "upscaler_increases_size": upscaler_increases_size, "esrgan_tile": esrgan_tile, "esrgan_tile_overlap": esrgan_tile_overlap, "hires_steps": hires_steps, "hires_denoising_strength": hires_denoising_strength, "hires_prompt": hires_prompt, "hires_negative_prompt": hires_negative_prompt, "hires_sampler": hires_sampler, "hires_before_adetailer": hires_before_adetailer, "hires_after_adetailer": hires_after_adetailer } # print(pipe_params) return self.infer(self.model, pipe_params) sd_gen = GuiSD() title_tab_one = "