Spaces:
Build error
Build error
Update app.py
Browse files1 unmodified line23456789101112131415161718192021222324252627282930313233343536373839404142434445464748497 unmodified lines5758596061626364656667688 unmodified lines777879808182839 unmodified lines939495969798991001011021031041051061071081091101111121131141151161171181191 unmodified line1211221231241251261271281291301 unmodified line1321331341351361371381 unmodified line1401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791 unmodified lineimport numpy as npimport random# import spaces #[uncomment to use ZeroGPU]from diffusers import DiffusionPipelineimport torchdevice = "cuda" if torch.cuda.is_available() else "cpu"# default parameter# model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to usemodel_repo_id = "CompVis/stable-diffusion-v1-4"model_dropdown = ['stabilityai/sdxl-turbo', 'CompVis/stable-diffusion-v1-4' ]if torch.cuda.is_available():torch_dtype = torch.float16else:torch_dtype = torch.float32pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)pipe = pipe.to(device)MAX_SEED = np.iinfo(np.int32).maxMAX_IMAGE_SIZE = 1024# @spaces.GPU #[uncomment to use ZeroGPU]def infer(prompt,negative_prompt,randomize_seed,width,height,model_repo_id=model_repo_id,seed=42,guidance_scale=7,num_inference_steps=20,progress=gr.Progress(track_tqdm=True),):if randomize_seed:seed = random.randint(0, MAX_SEED)generator = torch.Generator().manual_seed(seed)pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)pipe = pipe.to(device)image = pipe(prompt=prompt,7 unmodified linesreturn image, seedexamples = ["A young lady in a Russian embroidered kaftan is sitting on a beautiful carved veranda, holding a cup to her mouth and drinking tea from the cup. With her other hand, the girl holds a saucer. The cup and saucer are painted with gzhel. Next to the girl on the table stands a samovar, and steam can be seen above it.","Puss in Boots wearing a sombrero crosses the Grand Canyon on a tightrope with a guitar.","A cat is playing a song called ""About the Cat"" on an accordion by the sea at sunset. The sun is quickly setting behind the horizon, and the light is fading.","A cat walks through the grass on the streets of an abandoned city. The camera view is always focused on the cat's face.","Astronaut in a jungle, cold color palette, muted colors, detailed, 8k","An astronaut riding a green horse","A delicious ceviche cheesecake slice",8 unmodified lineswith gr.Blocks(css=css) as demo:with gr.Column(elem_id="col-container"):gr.Markdown(" # Text-to-Image SemaSci Template")with gr.Row():prompt = gr.Text(9 unmodified linesresult = gr.Image(label="Result", show_label=False)with gr.Accordion("Advanced Settings", open=False):# model_repo_id = gr.Text(# label="Model Id",# max_lines=1,# placeholder="Choose model",# visible=True,# value=model_repo_id,# )model_repo_id = gr.Dropdown(label="Model Id",choices=model_dropdown,info="Choose model",visible=True,allow_custom_value=True,value=model_repo_id,)negative_prompt = gr.Text(label="Negative prompt",max_lines=1,placeholder="Enter a negative prompt",visible=True,)seed = gr.Slider(1 unmodified lineminimum=0,maximum=MAX_SEED,step=1,value=42,)randomize_seed = gr.Checkbox(label="Randomize seed", value=False)with gr.Row():width = gr.Slider(1 unmodified lineminimum=256,maximum=MAX_IMAGE_SIZE,step=32,value=1024, # Replace with defaults that work for your model)height = gr.Slider(1 unmodified lineminimum=256,maximum=MAX_IMAGE_SIZE,step=32,value=1024, # Replace with defaults that work for your model)with gr.Row():guidance_scale = gr.Slider(label="Guidance scale",minimum=0.0,maximum=10.0,step=0.1,value=7.0, # Replace with defaults that work for your model)num_inference_steps = gr.Slider(label="Number of inference steps",minimum=1,maximum=50,step=1,value=20, # Replace with defaults that work for your model)gr.Examples(examples=examples, inputs=[prompt])gr.on(triggers=[run_button.click, prompt.submit],fn=infer,inputs=[prompt,negative_prompt,randomize_seed,width,height,model_repo_id,seed,guidance_scale,num_inference_steps,],outputs=[result, seed],)1 unmodified line2345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394957 unmodified lines1031041051061071081098 unmodified lines1181191201211221231241251261271281291301311329 unmodified lines1421431441451461471481491501511 unmodified line1531541551561571581591601611621 unmodified line1641651661671681691701 unmodified line1721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212221 unmodified lineimport 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,7 unmodified linesreturn image, seedexamples = ["Astronaut in a jungle, cold color palette, muted colors, detailed, 8k","An astronaut riding a green horse","A delicious ceviche cheesecake slice",8 unmodified lineswith 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(9 unmodified linesresult = 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(1 unmodified lineminimum=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(1 unmodified lineminimum=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],)
app.py+93-50