Update app.py
Browse files9 unmodified lines1011121314151617181920212223241314151617181920213031222324253 unmodified lines29303141423233343536483738394041424344454647484950512 unmodified lines5455565758596061606263646566676869707172737475767778798063646566676869707172737475768178828083848526 unmodified lines1121131141131151161151161171181191201211221231171181191201211221231241251261271281291301311321301331341351361371381371381391401411421431441451441451461471481491501511521531521541551541561571581591601611621631641651661 unmodified line16816917016217117217317418 unmodified lines1931941951961971981991902002012022031942049 unmodified linesimport torchfrom diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
def save_image(img): unique_name = str(uuid.uuid4()) + ".png" img.save(unique_name) return unique_name
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int: if randomize_seed: seed = random.randint(0, MAX_SEED) return seed
MAX_SEED = np.iinfo(np.int32).max
if 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"
USE_TORCH_COMPILE = 0ENABLE_CPU_OFFLOAD = 0device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if torch.cuda.is_available(): pipe = StableDiffusionXLPipeline.from_pretrained(3 unmodified lines add_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 save_image(img): unique_name = str(uuid.uuid4()) + ".png" img.save(unique_name) return unique_name
def 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 lines width: 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).images
if not use_negative_prompt: negative_prompt = "" # type: ignore
images = 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", ).images image_paths = [save_image(img) for img in images] print(image_paths) return image_paths, seed
examples = [26 unmodified lines container=False, ) run_button = gr.Button("Run", scale=0) result = gr.Gallery(label="Result", columns=1, preview=True, show_label=False) result = gr.Gallery(label="Result", columns=1) 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, ) 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, 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, maximum=MAX_IMAGE_SIZE, step=64, value=1024, ) height = gr.Slider( label="Height", minimum=512, maximum=2048, step=8, maximum=MAX_IMAGE_SIZE, step=64, value=1024, ) with gr.Row(): guidance_scale = gr.Slider( label="Guidance Scale", minimum=0.1, maximum=20.0, maximum=6, step=0.1, value=6, 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 line inputs=prompt, outputs=[result, seed], fn=generate, cache_examples=False, cache_examples=CACHE_EXAMPLES, )
use_negative_prompt.change(18 unmodified lines width, height, guidance_scale, num_inference_steps, randomize_seed, ], outputs=[result, seed], api_name="run" api_name="run", )
if __name__ == "__main__": demo.queue(max_size=20).launch(show_api=False, debug=False) demo.queue(max_size=20).launch()app.py+64-54