Spaces:
Runtime error
Runtime error
Update app.py
Browse files2 unmodified lines345678929 unmodified lines3940414243444546474857 unmodified lines1061071081091101117 unmodified lines11912012112212312412513 unmodified lines13914014114214314414514614714814915039 unmodified lines19019119219319419519619719819920093 unmodified lines2942952962972982993002 unmodified linesimport randomimport osimport torchfrom diffusers import StableDiffusionPipeline, ControlNetModel, StableDiffusionControlNetPipelinefrom diffusers.utils import load_imagefrom peft import PeftModel, LoraConfigfrom rembg import remove29 unmodified linesip_adapter_checkbox=False,ip_adapter_scale=0.0,ip_adapter_image=None,del_background=False,progress=gr.Progress(track_tqdm=True),):ckpt_dir='./model_output'unet_sub_dir = os.path.join(ckpt_dir, "unet")text_encoder_sub_dir = os.path.join(ckpt_dir, "text_encoder")57 unmodified linespipe.unet.load_state_dict({k: lora_scale*v for k, v in pipe.unet.state_dict().items()})pipe.text_encoder.load_state_dict({k: lora_scale*v for k, v in pipe.text_encoder.state_dict().items()})if torch_dtype in (torch.float16, torch.bfloat16):pipe.unet.half()7 unmodified linespipe.to(device)if del_background:return remove(pipe(**params).images[0])else:return pipe(**params).images[0]13 unmodified linesgr.Markdown(" # Text-to-Image demo")with gr.Row():model_id = gr.Textbox(label="Model ID",max_lines=1,placeholder="Enter model id",value=model_id_default,)prompt = gr.Textbox(label="Prompt",39 unmodified linesstep=1,value=20, # Replace with defaults that work for your model)with gr.Row():del_background = gr.Checkbox(label="Delete background?",value=False)with gr.Row():controlnet_checkbox = gr.Checkbox(label="ControlNet",93 unmodified linesip_adapter_checkbox,ip_adapter_scale,ip_adapter_image,del_background,],outputs=[result],)2 unmodified lines345678929 unmodified lines394041424344454647484950515253545556575859606162636457 unmodified lines1221231241251261271281291301311321337 unmodified lines14114214314414514614714814915015115215313 unmodified lines16716816917017117217317417517617717817918018139 unmodified lines22122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727893 unmodified lines3723733743753763773783793803813823833843852 unmodified linesimport randomimport osimport torchfrom diffusers import StableDiffusionPipeline, ControlNetModel, StableDiffusionControlNetPipeline, AutoencoderTiny, DDIMSchedulerfrom diffusers.utils import load_imagefrom peft import PeftModel, LoraConfigfrom rembg import remove29 unmodified linesip_adapter_checkbox=False,ip_adapter_scale=0.0,ip_adapter_image=None,tiny_vae=False,ddim=False,del_background=False,alpha_matting=False,alpha_matting_foreground_threshold=240,alpha_matting_background_threshold=10,alpha_matting_erode_size=10,post_process_mask=False,progress=gr.Progress(track_tqdm=True),):if model_id == model_id_default:ckpt_dir='./model_output'elif 'base' in model_id:ckpt_dir='./model_output_distilled_base'else:ckpt_dir='./model_output_distilled_small'unet_sub_dir = os.path.join(ckpt_dir, "unet")text_encoder_sub_dir = os.path.join(ckpt_dir, "text_encoder")57 unmodified linespipe.unet.load_state_dict({k: lora_scale*v for k, v in pipe.unet.state_dict().items()})pipe.text_encoder.load_state_dict({k: lora_scale*v for k, v in pipe.text_encoder.state_dict().items()})if tiny_vae:pipe.vae = AutoencoderTiny.from_pretrained("madebyollin/taesd", torch_dtype=torch_dtype)if ddim:pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)if torch_dtype in (torch.float16, torch.bfloat16):pipe.unet.half()7 unmodified linespipe.to(device)if del_background:return remove(pipe(**params).images[0],alpha_matting=alpha_matting,alpha_matting_foreground_threshold=alpha_matting_foreground_threshold,alpha_matting_background_threshold=alpha_matting_background_threshold,alpha_matting_erode_size=alpha_matting_erode_size,post_process_mask=post_process_mask)else:return pipe(**params).images[0]13 unmodified linesgr.Markdown(" # Text-to-Image demo")with gr.Row():model_id = gr.Dropdown(label="Model ID",choices=[model_id_default,"nota-ai/bk-sdm-v2-base","nota-ai/bk-sdm-v2-small"],value=model_id_default,max_choices=1)prompt = gr.Textbox(label="Prompt",39 unmodified linesstep=1,value=20, # Replace with defaults that work for your model)with gr.Row():tiny_vae = = gr.Checkbox(label="Use AutoencoderTiny?",value=False)ddim = = gr.Checkbox(label="Use DDIMScheduler?",value=False)with gr.Row():del_background = gr.Checkbox(label="Delete background?",value=False)with gr.Column(visible=False) as rembg_params:alpha_matting = gr.Checkbox(label="alpha_matting",value=False)with gr.Column(visible=False) as alpha_params:alpha_matting_foreground_threshold = gr.Slider(label="alpha_matting_foreground_threshold",minimum=0,maximum=255,step=1,value=240,)alpha_matting_background_threshold = gr.Slider(label="alpha_matting_background_threshold",minimum=0,maximum=255,step=1,value=10,)alpha_matting_erode_size = gr.Slider(label="alpha_matting_erode_size",minimum=0,maximum=100,step=1,value=10,)alpha_matting.change(fn=lambda x: gr.Row.update(visible=x),inputs=alpha_matting,outputs=alpha_params)post_process_mask = gr.Checkbox(label="post_process_mask",value=False)with gr.Row():controlnet_checkbox = gr.Checkbox(label="ControlNet",93 unmodified linesip_adapter_checkbox,ip_adapter_scale,ip_adapter_image,tiny_vae,ddim,del_background,alpha_matting,alpha_matting_foreground_threshold,alpha_matting_background_threshold,alpha_matting_erode_size,post_process_mask,],outputs=[result],)
app.py+94-9