Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files12345644 unmodified lines51525354555638 unmodified lines959697989910052 unmodified lines15315415515615715815966 unmodified lines22622722822923023133 unmodified lines26526626726826927063 unmodified lines33433533633733833934034134234334434534634782 unmodified lines43043143243343443511 unmodified lines44744844945045145226 unmodified lines47948048148248348448515 unmodified lines50150250350450550650754 unmodified lines562563564565566567120 unmodified lines68868969069169269369412 unmodified lines7077087097107117127137147157167177187197202 unmodified lines7237247257267277287296 unmodified lines73673773873974074174222 unmodified lines76576676776876977045 unmodified lines81681781881982082182282382426 unmodified lines85185285385485585685792 unmodified lines950951952953954955956145 unmodified lines11021103110411051106110733 unmodified lines11411142114311441145114654 unmodified lines1201120212031204import spacesimport osfrom stablepy import Model_Diffusersfrom constants import (DIRECTORY_MODELS,DIRECTORY_LORAS,44 unmodified linesdownload_diffuser_repo,progress_step_bar,html_template_message,)from datetime import datetimeimport gradio as gr38 unmodified lineslora_model_list.insert(0, "None")lora_model_list = lora_model_list + DIFFUSERS_FORMAT_LORASvae_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')52 unmodified linesyield f"Loading model: {model_name}"if vae_model:vae_type = "SDXL" if "sdxl" in vae_model.lower() else "SD 1.5"if model_type != vae_type:gr.Warning(WARNING_MSG_VAE)66 unmodified lineslora5,lora_scale5,sampler,img_height,img_width,model_name,33 unmodified linesimage_previews,display_images,save_generated_images,image_storage_location,retain_compel_previous_load,retain_detailfix_model_previous_load,63 unmodified lines(image_ip2, mask_ip2, model_ip2, mode_ip2, scale_ip2),]for imgip, mskip, modelip, modeip, scaleip in all_adapters:if imgip:params_ip_img.append(imgip)if mskip:params_ip_msk.append(mskip)params_ip_model.append(modelip)params_ip_mode.append(modeip)params_ip_scale.append(scaleip)concurrency = 5self.model.stream_config(concurrency=concurrency, latent_resize_by=1, vae_decoding=False)82 unmodified lines"textual_inversion": embed_list if textual_inversion else [],"syntax_weights": syntax_weights, # "Classic""sampler": sampler,"xformers_memory_efficient_attention": xformers_memory_efficient_attention,"gui_active": True,"loop_generation": loop_generation,11 unmodified lines"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,26 unmodified linesactual_progress = 0info_images = gr.update()for img, seed, image_path, metadata in self.model(**pipe_params):info_state = progress_step_bar(actual_progress, steps)actual_progress += concurrencyif image_path:15 unmodified linesif msg_lora:info_images += msg_lorainfo_images = info_images + "<br>" + "GENERATION DATA:<br>" + metadata[0].replace("\n", "<br>") + "<br>-------<br>"download_links = "<br>".join([54 unmodified lines)print(lora_status)if verbose_arg:for status, lora in zip(lora_status, lora_list):if status:120 unmodified lineswith gr.Column(scale=1):steps_gui = gr.Slider(minimum=1, maximum=100, step=1, value=30, label="Steps")cfg_gui = gr.Slider(minimum=0, maximum=30, step=0.5, value=7., label="CFG")sampler_gui = gr.Dropdown(label="Sampler", choices=scheduler_names, value="Euler a")img_width_gui = gr.Slider(minimum=64, maximum=4096, step=8, value=1024, label="Img Width")img_height_gui = gr.Slider(minimum=64, maximum=4096, step=8, value=1024, label="Img Height")seed_gui = gr.Number(minimum=-1, maximum=9999999999, value=-1, label="Seed")12 unmodified lines"width": gr.update(value=1024),"height": gr.update(value=1024),"Seed": gr.update(value=-1),"Sampler": gr.update(value="Euler a"),"scale": gr.update(value=7.), # cfg"skip": gr.update(value=True),"Model": gr.update(value=name_model),}valid_keys = list(valid_receptors.keys())parameters = extract_parameters(base_prompt)for key, val in parameters.items():# print(val)2 unmodified linesif key == "Sampler":if val not in scheduler_names:continueelif key == "skip":if "," in str(val):val = val.replace(",", "")if int(val) >= 2:6 unmodified linesval = re.sub(r'\s+', ' ', re.sub(r',+', ',', val)).strip()if key in ["Steps", "width", "height", "Seed"]:val = int(val)if key == "scale":val = float(val)if key == "Model":filtered_models = [m for m in model_list if val in m]22 unmodified linescfg_gui,clip_skip_gui,model_name_gui,],)45 unmodified lineslora_scale_5_gui = lora_scale_slider("Lora Scale 5")with gr.Accordion("From URL", open=False, visible=True):text_lora = gr.Textbox(label="LoRA URL", placeholder="https://civitai.com/api/download/models/28907", lines=1)romanize_text = gr.Checkbox(value=False, label="Transliterate name")button_lora = gr.Button("Obtain and refresh the LoRAs lists")new_lora_status = gr.HTML()button_lora.click(get_my_lora,26 unmodified linesminimum=0.01, maximum=1.0, step=0.01, value=0.55, label="Strength",info="This option adjusts the level of changes for img2img and inpainting.")image_resolution_gui = gr.Slider(minimum=64, maximum=2048, step=64, value=1024, label="Image Resolution")preprocessor_name_gui = gr.Dropdown(label="Preprocessor Name", choices=PREPROCESSOR_CONTROLNET["canny"])def change_preprocessor_choices(task):92 unmodified linesmask_padding_b_gui = gr.Number(label="Mask padding:", value=32, minimum=1)with gr.Accordion("Other settings", open=False, visible=True):save_generated_images_gui = gr.Checkbox(value=True, label="Create a download link for the images")hires_before_adetailer_gui = gr.Checkbox(value=False, label="Hires Before Adetailer")hires_after_adetailer_gui = gr.Checkbox(value=True, label="Hires After Adetailer")generator_in_cpu_gui = gr.Checkbox(value=False, label="Generator in CPU")145 unmodified lineslora5_gui,lora_scale_5_gui,sampler_gui,img_height_gui,img_width_gui,model_name_gui,33 unmodified linesimage_previews_gui,display_images_gui,save_generated_images_gui,image_storage_location_gui,retain_compel_previous_load_gui,retain_detailfix_model_previous_load_gui,54 unmodified linesshow_error=True,debug=True,allowed_paths=["./images/"],)123456789101144 unmodified lines5657585960616238 unmodified lines10110210310410510610752 unmodified lines16016116216316416516616716816917017166 unmodified lines23823924024124224324424533 unmodified lines27928028128228328428563 unmodified lines34935035135235335435535635735835936036136236382 unmodified lines44644744844945045145245311 unmodified lines46546646746846947047126 unmodified lines49849950050150250350415 unmodified lines52052152252352452552654 unmodified lines581582583584585586587588589590591592593594120 unmodified lines71571671771871972072172212 unmodified lines7357367377387397407417427437447457467477487497507517527537547557567577587597602 unmodified lines7637647657667677687697707717726 unmodified lines77978078178278378478578678722 unmodified lines81081181281381481581681781845 unmodified lines86486586686786886987087187287387487587687726 unmodified lines90490590690790890991091191291392 unmodified lines100610071008100910101011101210131014145 unmodified lines1160116111621163116411651166116733 unmodified lines120112021203120412051206120754 unmodified lines1262126312641265import spacesimport osfrom stablepy import (Model_Diffusers,SCHEDULE_TYPE_OPTIONS,SCHEDULE_PREDICTION_TYPE_OPTIONS,check_scheduler_compatibility,)from constants import (DIRECTORY_MODELS,DIRECTORY_LORAS,44 unmodified linesdownload_diffuser_repo,progress_step_bar,html_template_message,escape_html,)from datetime import datetimeimport gradio as gr38 unmodified lineslora_model_list.insert(0, "None")lora_model_list = lora_model_list + DIFFUSERS_FORMAT_LORASvae_model_list = get_model_list(DIRECTORY_VAES)vae_model_list.insert(0, "BakedVAE")vae_model_list.insert(0, "None")print('\033[33m🏁 Download and listing of valid models completed.\033[0m')52 unmodified linesyield f"Loading model: {model_name}"if vae_model == "BakedVAE":if not os.path.exists(model_name):vae_model = model_nameelse:vae_model = Noneelif vae_model:vae_type = "SDXL" if "sdxl" in vae_model.lower() else "SD 1.5"if model_type != vae_type:gr.Warning(WARNING_MSG_VAE)66 unmodified lineslora5,lora_scale5,sampler,schedule_type,schedule_prediction_type,img_height,img_width,model_name,33 unmodified linesimage_previews,display_images,save_generated_images,filename_pattern,image_storage_location,retain_compel_previous_load,retain_detailfix_model_previous_load,63 unmodified lines(image_ip2, mask_ip2, model_ip2, mode_ip2, scale_ip2),]if not hasattr(self.model.pipe, "transformer"):for imgip, mskip, modelip, modeip, scaleip in all_adapters:if imgip:params_ip_img.append(imgip)if mskip:params_ip_msk.append(mskip)params_ip_model.append(modelip)params_ip_mode.append(modeip)params_ip_scale.append(scaleip)concurrency = 5self.model.stream_config(concurrency=concurrency, latent_resize_by=1, vae_decoding=False)82 unmodified lines"textual_inversion": embed_list if textual_inversion else [],"syntax_weights": syntax_weights, # "Classic""sampler": sampler,"schedule_type": schedule_type,"schedule_prediction_type": schedule_prediction_type,"xformers_memory_efficient_attention": xformers_memory_efficient_attention,"gui_active": True,"loop_generation": loop_generation,11 unmodified lines"image_previews": image_previews,"display_images": display_images,"save_generated_images": save_generated_images,"filename_pattern": filename_pattern,"image_storage_location": image_storage_location,"retain_compel_previous_load": retain_compel_previous_load,"retain_detailfix_model_previous_load": retain_detailfix_model_previous_load,26 unmodified linesactual_progress = 0info_images = gr.update()for img, [seed, image_path, metadata] in self.model(**pipe_params):info_state = progress_step_bar(actual_progress, steps)actual_progress += concurrencyif image_path:15 unmodified linesif msg_lora:info_images += msg_lorainfo_images = info_images + "<br>" + "GENERATION DATA:<br>" + escape_html(metadata[0]) + "<br>-------<br>"download_links = "<br>".join([54 unmodified lines)print(lora_status)sampler_name = args[17]schedule_type_name = args[18]_, _, msg_sampler = check_scheduler_compatibility(sd_gen.model.class_name, sampler_name, schedule_type_name)if msg_sampler:gr.Warning(msg_sampler)if verbose_arg:for status, lora in zip(lora_status, lora_list):if status:120 unmodified lineswith gr.Column(scale=1):steps_gui = gr.Slider(minimum=1, maximum=100, step=1, value=30, label="Steps")cfg_gui = gr.Slider(minimum=0, maximum=30, step=0.5, value=7., label="CFG")sampler_gui = gr.Dropdown(label="Sampler", choices=scheduler_names, value="Euler")schedule_type_gui = gr.Dropdown(label="Schedule type", choices=SCHEDULE_TYPE_OPTIONS, value=SCHEDULE_TYPE_OPTIONS[0])img_width_gui = gr.Slider(minimum=64, maximum=4096, step=8, value=1024, label="Img Width")img_height_gui = gr.Slider(minimum=64, maximum=4096, step=8, value=1024, label="Img Height")seed_gui = gr.Number(minimum=-1, maximum=9999999999, value=-1, label="Seed")12 unmodified lines"width": gr.update(value=1024),"height": gr.update(value=1024),"Seed": gr.update(value=-1),"Sampler": gr.update(value="Euler"),"CFG scale": gr.update(value=7.), # cfg"Clip skip": gr.update(value=True),"Model": gr.update(value=name_model),"Schedule type": gr.update(value="Automatic"),"PAG": gr.update(value=.0),"FreeU": gr.update(value=False),}valid_keys = list(valid_receptors.keys())parameters = extract_parameters(base_prompt)# print(parameters)if "Sampler" in parameters:value_sampler = parameters["Sampler"]for s_type in SCHEDULE_TYPE_OPTIONS:if s_type in value_sampler:value_sampler = value_sampler.replace(s_type, "").strip()parameters["Sampler"] = value_samplerparameters["Schedule type"] = s_typefor key, val in parameters.items():# print(val)2 unmodified linesif key == "Sampler":if val not in scheduler_names:continueif key == "Schedule type":if val not in SCHEDULE_TYPE_OPTIONS:val = "Automatic"elif key == "Clip skip":if "," in str(val):val = val.replace(",", "")if int(val) >= 2:6 unmodified linesval = re.sub(r'\s+', ' ', re.sub(r',+', ',', val)).strip()if key in ["Steps", "width", "height", "Seed"]:val = int(val)if key == "FreeU":val = Trueif key in ["CFG scale", "PAG"]:val = float(val)if key == "Model":filtered_models = [m for m in model_list if val in m]22 unmodified linescfg_gui,clip_skip_gui,model_name_gui,schedule_type_gui,pag_scale_gui,free_u_gui,],)45 unmodified lineslora_scale_5_gui = lora_scale_slider("Lora Scale 5")with gr.Accordion("From URL", open=False, visible=True):text_lora = gr.Textbox(label="LoRA's download URL",placeholder="https://civitai.com/api/download/models/28907",lines=1,info="It has to be .safetensors files, and you can also download them from Hugging Face.",)romanize_text = gr.Checkbox(value=False, label="Transliterate name")button_lora = gr.Button("Get and Refresh the LoRA Lists")new_lora_status = gr.HTML()button_lora.click(get_my_lora,26 unmodified linesminimum=0.01, maximum=1.0, step=0.01, value=0.55, label="Strength",info="This option adjusts the level of changes for img2img and inpainting.")image_resolution_gui = gr.Slider(minimum=64, maximum=2048, step=64, value=1024, label="Image Resolution",info="The maximum proportional size of the generated image based on the uploaded image.")preprocessor_name_gui = gr.Dropdown(label="Preprocessor Name", choices=PREPROCESSOR_CONTROLNET["canny"])def change_preprocessor_choices(task):92 unmodified linesmask_padding_b_gui = gr.Number(label="Mask padding:", value=32, minimum=1)with gr.Accordion("Other settings", open=False, visible=True):schedule_prediction_type_gui = gr.Dropdown(label="Discrete Sampling Type", choices=SCHEDULE_PREDICTION_TYPE_OPTIONS, value=SCHEDULE_PREDICTION_TYPE_OPTIONS[0])save_generated_images_gui = gr.Checkbox(value=True, label="Create a download link for the images")filename_pattern_gui = gr.Textbox(label="Filename pattern", value="model,seed", placeholder="model,seed,sampler,schedule_type,img_width,img_height,guidance_scale,num_steps,vae,prompt_section,neg_prompt_section", lines=1)hires_before_adetailer_gui = gr.Checkbox(value=False, label="Hires Before Adetailer")hires_after_adetailer_gui = gr.Checkbox(value=True, label="Hires After Adetailer")generator_in_cpu_gui = gr.Checkbox(value=False, label="Generator in CPU")145 unmodified lineslora5_gui,lora_scale_5_gui,sampler_gui,schedule_type_gui,schedule_prediction_type_gui,img_height_gui,img_width_gui,model_name_gui,33 unmodified linesimage_previews_gui,display_images_gui,save_generated_images_gui,filename_pattern_gui,image_storage_location_gui,retain_compel_previous_load_gui,retain_detailfix_model_previous_load_gui,54 unmodified linesshow_error=True,debug=True,allowed_paths=["./images/"],)
app.py+83-22