Spaces:
Build error
Build error
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121132 unmodified lines1161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701 unmodified line172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225import gradio as grimport numpy as npimport randomfrom diffusers import DiffusionPipelinefrom peft import PeftModel, PeftConfigimport torchdevice = "cuda" if torch.cuda.is_available() else "cpu"# Model list including your LoRA modelMODEL_LIST = ["CompVis/stable-diffusion-v1-4","stabilityai/sdxl-turbo","runwayml/stable-diffusion-v1-5","stabilityai/stable-diffusion-2-1","m4r4k0s23/hw5_lora_raccoon",]if torch.cuda.is_available():torch_dtype = torch.float16else:torch_dtype = torch.float32# Cache to avoid re-initializing pipelines repeatedlymodel_cache = {}def load_pipeline(model_id: str):"""Loads or retrieves a cached DiffusionPipeline.If the chosen model is your LoRA adapter, then load the base model(CompVis/stable-diffusion-v1-4) and apply the LoRA weights."""if model_id in model_cache:return model_cache[model_id]if model_id == "m4r4k0s23/hw5_lora_raccoon":# Use the specified base model for your LoRA adapter.base_model = "CompVis/stable-diffusion-v1-4"pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch_dtype)# Load the LoRA weightspipe.unet = PeftModel.from_pretrained(pipe.unet,model_id,subfolder="unet",torch_dtype=torch_dtype)pipe.text_encoder = PeftModel.from_pretrained(pipe.text_encoder,model_id,subfolder="text_encoder",torch_dtype=torch_dtype)else:pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch_dtype)pipe.to(device)model_cache[model_id] = pipereturn pipeMAX_SEED = np.iinfo(np.int32).maxMAX_IMAGE_SIZE = 1024def infer(model_id,prompt,negative_prompt,seed,randomize_seed,width,height,guidance_scale,num_inference_steps,lora_scale, # New parameter for adjusting LoRA scaleprogress=gr.Progress(track_tqdm=True),):# Load the pipeline for the chosen modelpipe = load_pipeline(model_id)if randomize_seed:seed = random.randint(0, MAX_SEED)generator = torch.Generator(device=device).manual_seed(seed)# If using the LoRA model, update the LoRA scale if supported.if model_id == "m4r4k0s23/hw5_lora_raccoon":# This assumes your pipeline's unet has a method to update the LoRA scale.if hasattr(pipe.unet, "set_lora_scale"):pipe.unet.set_lora_scale(lora_scale)else:print("Warning: LoRA scale adjustment method not found on UNet.")image = pipe(prompt=prompt,negative_prompt=negative_prompt,guidance_scale=guidance_scale,num_inference_steps=num_inference_steps,width=width,height=height,generator=generator,).images[0]return image, seedexamples = ["Astronaut in a jungle, cold color palette, muted colors, detailed, 8k","An astronaut riding a green horse","A delicious ceviche cheesecake slice",]css = """#col-container {2 unmodified lines}"""with gr.Blocks(css=css) as demo:with gr.Column(elem_id="col-container"):gr.Markdown(" # Text-to-Image Gradio Template")with gr.Row():# Dropdown to select the model from Hugging Facemodel_id = gr.Dropdown(label="Model",choices=MODEL_LIST,value=MODEL_LIST[0], # Default model)with gr.Row():prompt = gr.Text(label="Prompt",show_label=False,max_lines=1,placeholder="Enter your prompt",container=False,)run_button = gr.Button("Run", scale=0, variant="primary")result = gr.Image(label="Result", show_label=False)with gr.Accordion("Advanced Settings", open=False):negative_prompt = gr.Text(label="Negative prompt",max_lines=1,placeholder="Enter a negative prompt",)seed = gr.Slider(label="Seed",minimum=0,maximum=MAX_SEED,step=1,value=42, # Default seed)randomize_seed = gr.Checkbox(label="Randomize seed", value=True)with gr.Row():width = gr.Slider(label="Width",minimum=256,maximum=MAX_IMAGE_SIZE,step=32,value=1024,)height = gr.Slider(1 unmodified lineminimum=256,maximum=MAX_IMAGE_SIZE,step=32,value=1024,)with gr.Row():guidance_scale = gr.Slider(label="Guidance scale",minimum=0.0,maximum=20.0,step=0.5,value=7.0,)num_inference_steps = gr.Slider(label="Number of inference steps",minimum=1,maximum=100,step=1,value=20,)# New slider for LoRA scale.lora_scale = gr.Slider(label="LoRA Scale",minimum=0.0,maximum=2.0,step=0.1,value=1.0,info="Adjust the influence of the LoRA weights",)gr.Examples(examples=examples, inputs=[prompt])gr.on(triggers=[run_button.click, prompt.submit],fn=infer,inputs=[model_id,prompt,negative_prompt,seed,randomize_seed,width,height,guidance_scale,num_inference_steps,lora_scale, # Pass the new slider value],outputs=[result, seed],)if __name__ == "__main__":demo.launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211222 unmodified lines1251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542551 unmodified line257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291import gradio as grimport numpy as npimport randomimport osimport torchfrom diffusers import StableDiffusionPipeline, ControlNetModel, StableDiffusionControlNetPipelinefrom diffusers.utils import load_imagefrom peft import PeftModel, LoraConfigdevice = "cuda" if torch.cuda.is_available() else "cpu"model_id_default = "stable-diffusion-v1-5/stable-diffusion-v1-5"if torch.cuda.is_available():torch_dtype = torch.float16else:torch_dtype = torch.float32MAX_SEED = np.iinfo(np.int32).maxMAX_IMAGE_SIZE = 1024# @spaces.GPU #[uncomment to use ZeroGPU]def infer(prompt,negative_prompt,width=512,height=512,model_id=model_id_default,seed=42,guidance_scale=7.0,lora_scale=1.0,num_inference_steps=20,controlnet_checkbox=False,controlnet_strength=0.0,controlnet_mode="edge_detection",controlnet_image=None,ip_adapter_checkbox=False,ip_adapter_scale=0.0,ip_adapter_image=None,progress=gr.Progress(track_tqdm=True),):unet_sub_dir = "unet"text_encoder_sub_dir = "text_encoder"if model_id is None:raise ValueError("Please specify the base model name or path")generator = torch.Generator(device).manual_seed(seed)params = {'prompt': prompt,'negative_prompt': negative_prompt,'guidance_scale': guidance_scale,'num_inference_steps': num_inference_steps,'width': width,'height': height,'generator': generator}if controlnet_checkbox:if controlnet_mode == "depth_map":controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-depth",cache_dir="./models_cache",torch_dtype=torch_dtype)elif controlnet_mode == "pose_estimation":controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-openpose",cache_dir="./models_cache",torch_dtype=torch_dtype)elif controlnet_mode == "normal_map":controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-normal",cache_dir="./models_cache",torch_dtype=torch_dtype)elif controlnet_mode == "scribbles":controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-scribble",cache_dir="./models_cache",torch_dtype=torch_dtype)else:controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny",cache_dir="./models_cache",torch_dtype=torch_dtype)pipe = StableDiffusionControlNetPipeline.from_pretrained(model_id,controlnet=controlnet,torch_dtype=torch_dtype,safety_checker=None).to(device)params['image'] = controlnet_imageparams['controlnet_conditioning_scale'] = float(controlnet_strength)else:pipe = StableDiffusionPipeline.from_pretrained(model_id,torch_dtype=torch_dtype,safety_checker=None).to(device)pipe.unet = PeftModel.from_pretrained(pipe.unet, unet_sub_dir)pipe.text_encoder = PeftModel.from_pretrained(pipe.text_encoder, text_encoder_sub_dir)pipe.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()pipe.text_encoder.half()if ip_adapter_checkbox:pipe.load_ip_adapter("h94/IP-Adapter", subfolder="models", weight_name="ip-adapter-plus_sd15.bin")pipe.set_ip_adapter_scale(ip_adapter_scale)params['ip_adapter_image'] = ip_adapter_imagepipe.to(device)return pipe(**params).images[0]css = """#col-container {2 unmodified lines}"""def controlnet_params(show_extra):return gr.update(visible=show_extra)with gr.Blocks(css=css, fill_height=True) as demo:with gr.Column(elem_id="col-container"):gr.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",max_lines=1,placeholder="Enter your prompt",)negative_prompt = gr.Textbox(label="Negative prompt",max_lines=1,placeholder="Enter your negative prompt",)with gr.Row():seed = gr.Number(label="Seed",minimum=0,maximum=MAX_SEED,step=1,value=42,)guidance_scale = gr.Slider(label="Guidance scale",minimum=0.0,maximum=30.0,step=0.1,value=7.0, # Replace with defaults that work for your model)with gr.Row():lora_scale = gr.Slider(label="LoRA scale",minimum=0.0,maximum=1.0,step=0.01,value=1.0,)num_inference_steps = gr.Slider(label="Number of inference steps",minimum=1,maximum=100,step=1,value=20, # Replace with defaults that work for your model)with gr.Row():controlnet_checkbox = gr.Checkbox(label="ControlNet",value=False)with gr.Column(visible=False) as controlnet_params:controlnet_strength = gr.Slider(label="ControlNet conditioning scale",minimum=0.0,maximum=1.0,step=0.01,value=1.0,)controlnet_mode = gr.Dropdown(label="ControlNet mode",choices=["edge_detection","depth_map","pose_estimation","normal_map","scribbles"],value="edge_detection",max_choices=1)controlnet_image = gr.Image(label="ControlNet condition image",type="pil",format="png")controlnet_checkbox.change(fn=lambda x: gr.Row.update(visible=x),inputs=controlnet_checkbox,outputs=controlnet_params)with gr.Row():ip_adapter_checkbox = gr.Checkbox(label="IPAdapter",value=False)with gr.Column(visible=False) as ip_adapter_params:ip_adapter_scale = gr.Slider(label="IPAdapter scale",minimum=0.0,maximum=1.0,step=0.01,value=1.0,)ip_adapter_image = gr.Image(label="IPAdapter condition image",type="pil")ip_adapter_checkbox.change(fn=lambda x: gr.Row.update(visible=x),inputs=ip_adapter_checkbox,outputs=ip_adapter_params)with gr.Accordion("Optional Settings", open=False):with gr.Row():width = gr.Slider(label="Width",minimum=256,maximum=MAX_IMAGE_SIZE,step=32,value=512, # Replace with defaults that work for your model)height = gr.Slider(1 unmodified lineminimum=256,maximum=MAX_IMAGE_SIZE,step=32,value=512, # Replace with defaults that work for your model)run_button = gr.Button("Run", scale=0, variant="primary")result = gr.Image(label="Result", show_label=False)gr.on(triggers=[run_button.click],fn=infer,inputs=[prompt,negative_prompt,width,height,model_id,seed,guidance_scale,lora_scale,num_inference_steps,controlnet_checkbox,controlnet_strength,controlnet_mode,controlnet_image,ip_adapter_checkbox,ip_adapter_scale,ip_adapter_image,],outputs=[result],)if __name__ == "__main__":demo.launch()
app.py+225-159