Update app.py
Browse files9 unmodified lines101112131415161718192021222324252627282930313233343 unmodified lines38394041424344454647484950512 unmodified lines54555657585960616263646566676869707172737475767778798081828326 unmodified lines1101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571 unmodified line15916016116216316416518 unmodified lines1841851861871881891901911921931949 unmodified linesimport torchfrom diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteSchedulerdef save_image(img):unique_name = str(uuid.uuid4()) + ".png"img.save(unique_name)return unique_namedef randomize_seed_fn(seed: int, randomize_seed: bool) -> int:if randomize_seed:seed = random.randint(0, MAX_SEED)return seedMAX_SEED = np.iinfo(np.int32).maxif not torch.cuda.is_available():DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"MAX_SEED = np.iinfo(np.int32).maxUSE_TORCH_COMPILE = 0ENABLE_CPU_OFFLOAD = 0if torch.cuda.is_available():pipe = StableDiffusionXLPipeline.from_pretrained(3 unmodified linesadd_watermarker=False)pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)pipe.load_lora_weights("ehristoforu/dalle-3-xl-v2", weight_name="dalle-3-xl-lora-v2.safetensors", adapter_name="dalle")pipe.set_adapters("dalle")pipe.to("cuda")@spaces.GPU(enable_queue=True)def generate(prompt: str,negative_prompt: str = "",2 unmodified lineswidth: int = 1024,height: int = 1024,guidance_scale: float = 3,randomize_seed: bool = False,progress=gr.Progress(track_tqdm=True),):seed = int(randomize_seed_fn(seed, randomize_seed))if not use_negative_prompt:negative_prompt = "" # type: ignoreimages = pipe(prompt=prompt,negative_prompt=negative_prompt,width=width,height=height,guidance_scale=guidance_scale,num_inference_steps=25,num_images_per_prompt=1,cross_attention_kwargs={"scale": 0.65},output_type="pil",).imagesimage_paths = [save_image(img) for img in images]print(image_paths)return image_paths, seedexamples = [26 unmodified linescontainer=False,)run_button = gr.Button("Run", scale=0)result = gr.Gallery(label="Result", columns=1, preview=True, show_label=False)with gr.Accordion("Advanced options", open=False):use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)negative_prompt = gr.Text(label="Negative prompt",lines=4,max_lines=6,value="""(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, (NSFW:1.25)""",placeholder="Enter a negative prompt",visible=True,)seed = gr.Slider(label="Seed",minimum=0,maximum=MAX_SEED,step=1,value=0,visible=True)randomize_seed = gr.Checkbox(label="Randomize seed", value=True)with gr.Row(visible=True):width = gr.Slider(label="Width",minimum=512,maximum=2048,step=8,value=1024,)height = gr.Slider(label="Height",minimum=512,maximum=2048,step=8,value=1024,)with gr.Row():guidance_scale = gr.Slider(label="Guidance Scale",minimum=0.1,maximum=20.0,step=0.1,value=6,)gr.Examples(1 unmodified lineinputs=prompt,outputs=[result, seed],fn=generate,cache_examples=False,)use_negative_prompt.change(18 unmodified lineswidth,height,guidance_scale,randomize_seed,],outputs=[result, seed],api_name="run")if __name__ == "__main__":demo.queue(max_size=20).launch(show_api=False, debug=False)9 unmodified lines101112131415161718192021222324253 unmodified lines29303132333435363738394041424344454647484950512 unmodified lines545556575859606162636465666768697071727374757677787980818283848526 unmodified lines1121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661 unmodified line16816917017117217317418 unmodified lines1931941951961971981992002012022032049 unmodified linesimport torchfrom diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteSchedulerif not torch.cuda.is_available():DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"MAX_SEED = np.iinfo(np.int32).maxCACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "1") == "1"MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")if torch.cuda.is_available():pipe = StableDiffusionXLPipeline.from_pretrained(3 unmodified linesadd_watermarker=False)pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)pipe.load_lora_weights("ehristoforu/dalle-3-xl-v2", weight_name="dalle-3-xl-lora-v2.safetensors", adapter_name="dalle")pipe.set_adapters("dalle")pipe.to("cuda")def save_image(img):unique_name = str(uuid.uuid4()) + ".png"img.save(unique_name)return unique_namedef randomize_seed_fn(seed: int, randomize_seed: bool) -> int:if randomize_seed:seed = random.randint(0, MAX_SEED)return seed@spaces.GPU(duration=30, queue=False)def generate(prompt: str,negative_prompt: str = "",2 unmodified lineswidth: int = 1024,height: int = 1024,guidance_scale: float = 3,num_inference_steps: int = 10,randomize_seed: bool = False,use_resolution_binning: bool = True,progress=gr.Progress(track_tqdm=True),):pipe.to(device)seed = int(randomize_seed_fn(seed, randomize_seed))generator = torch.Generator().manual_seed(seed)options = {"prompt":prompt,"negative_prompt":negative_prompt,"width":width,"height":height,"guidance_scale":guidance_scale,"num_inference_steps":num_inference_steps,"generator":generator,"use_resolution_binning":use_resolution_binning,"output_type":"pil",}images = pipe(**options).imagesimage_paths = [save_image(img) for img in images]return image_paths, seedexamples = [26 unmodified linescontainer=False,)run_button = gr.Button("Run", scale=0)result = gr.Gallery(label="Result", columns=1)with gr.Accordion("Advanced options", open=False):with gr.Row():use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)negative_prompt = gr.Text(label="Negative prompt",max_lines=5,lines=4,placeholder="Enter a negative prompt",value="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, NSFW",visible=True,)seed = gr.Slider(label="Seed",minimum=0,maximum=MAX_SEED,step=1,value=0,)randomize_seed = gr.Checkbox(label="Randomize seed", value=True)with gr.Row(visible=True):width = gr.Slider(label="Width",minimum=512,maximum=MAX_IMAGE_SIZE,step=64,value=1024,)height = gr.Slider(label="Height",minimum=512,maximum=MAX_IMAGE_SIZE,step=64,value=1024,)with gr.Row():guidance_scale = gr.Slider(label="Guidance Scale",minimum=0.1,maximum=6,step=0.1,value=3.0,)num_inference_steps = gr.Slider(label="Number of inference steps",minimum=1,maximum=15,step=1,value=8,)gr.Examples(1 unmodified lineinputs=prompt,outputs=[result, seed],fn=generate,cache_examples=CACHE_EXAMPLES,)use_negative_prompt.change(18 unmodified lineswidth,height,guidance_scale,num_inference_steps,randomize_seed,],outputs=[result, seed],api_name="run",)if __name__ == "__main__":demo.queue(max_size=20).launch()
app.py+64-54