multimodalart HF Staff commited on
Commit
3b62b34
·
verified ·
1 Parent(s): 1a90431

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +7 -7
  2. app.py +83 -0
  3. requirements.txt +6 -0
README.md CHANGED
@@ -1,13 +1,13 @@
1
  ---
2
- title: Perturbed Attention Guidance Sdxl 2
3
- emoji: 🐢
4
- colorFrom: gray
5
- colorTo: red
6
  sdk: gradio
7
- sdk_version: 6.19.0
8
- python_version: '3.12'
9
  app_file: app.py
10
  pinned: false
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Perturbed-Attention Guidance SDXL
3
+ emoji: 🌀
4
+ colorFrom: pink
5
+ colorTo: yellow
6
  sdk: gradio
7
+ sdk_version: 5.43.1
 
8
  app_file: app.py
9
  pinned: false
10
+ license: mit
11
  ---
12
 
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import spaces
3
+ from diffusers import StableDiffusionXLPipeline
4
+ import numpy as np
5
+ import math
6
+ import torch
7
+ import random
8
+
9
+ from gradio_imageslider import ImageSlider
10
+
11
+ theme = gr.themes.Base(
12
+ font=[gr.themes.GoogleFont('Libre Franklin'), gr.themes.GoogleFont('Public Sans'), 'system-ui', 'sans-serif'],
13
+ )
14
+
15
+ pipe = StableDiffusionXLPipeline.from_pretrained(
16
+ "stabilityai/stable-diffusion-xl-base-1.0",
17
+ custom_pipeline="multimodalart/sdxl_perturbed_attention_guidance",
18
+ torch_dtype=torch.float16
19
+ )
20
+
21
+ device="cuda"
22
+ pipe = pipe.to(device)
23
+
24
+ @spaces.GPU
25
+ def run(prompt, negative_prompt=None, guidance_scale=7.0, pag_scale=3.0, pag_layers=["mid"], randomize_seed=True, seed=42, lora=None, progress=gr.Progress(track_tqdm=True)):
26
+ prompt = prompt.strip()
27
+ negative_prompt = negative_prompt.strip() if negative_prompt and negative_prompt.strip() else None
28
+ print(f"Initial seed for prompt `{prompt}`", seed)
29
+ if(randomize_seed):
30
+ seed = random.randint(0, 9007199254740991)
31
+
32
+ if not prompt and not negative_prompt:
33
+ guidance_scale = 0.0
34
+ pipe.unfuse_lora()
35
+ pipe.unload_lora_weights()
36
+ if lora:
37
+ pipe.load_lora_weights(lora)
38
+ pipe.fuse_lora(lora_scale=0.9)
39
+ print(f"Seed before sending to generator for prompt: `{prompt}`", seed)
40
+ generator = torch.Generator(device="cuda").manual_seed(seed)
41
+ image_pag = pipe(prompt, negative_prompt=negative_prompt, guidance_scale=guidance_scale, pag_scale=pag_scale, pag_applied_layers=pag_layers, generator=generator, num_inference_steps=25).images[0]
42
+
43
+ generator = torch.Generator(device="cuda").manual_seed(seed)
44
+ image_normal = pipe(prompt, negative_prompt=negative_prompt, guidance_scale=guidance_scale, generator=generator, num_inference_steps=25).images[0]
45
+ print(f"Seed at the end of generation for prompt: `{prompt}`", seed)
46
+ return (image_pag, image_normal), seed
47
+
48
+ css = '''
49
+ .gradio-container{
50
+ max-width: 768px !important;
51
+ margin: 0 auto;
52
+ }
53
+ '''
54
+
55
+ with gr.Blocks(css=css, theme=theme) as demo:
56
+ gr.Markdown('''# Perturbed-Attention Guidance SDXL
57
+ SDXL 🧨 [diffusers implementation](https://huggingface.co/multimodalart/sdxl_perturbed_attention_guidance) of [Perturbed-Attenton Guidance](https://ku-cvlab.github.io/Perturbed-Attention-Guidance/)
58
+ ''')
59
+ with gr.Group():
60
+ with gr.Row():
61
+ prompt = gr.Textbox(show_label=False, scale=4, placeholder="Your prompt", info="Leave blank to test unconditional generation")
62
+ button = gr.Button("Generate", min_width=120)
63
+ output = ImageSlider(label="Left: PAG, Right: No PAG", interactive=False)
64
+ with gr.Accordion("Advanced Settings", open=False):
65
+ guidance_scale = gr.Number(label="CFG Guidance Scale", info="The guidance scale for CFG, ignored if no prompt is entered (unconditional generation)", value=7.0)
66
+ negative_prompt = gr.Textbox(label="Negative prompt", info="Is only applied for the CFG part, leave blank for unconditional generation")
67
+ pag_scale = gr.Number(label="Pag Scale", value=3.0)
68
+ pag_layers = gr.Dropdown(label="Model layers to apply Pag to", info="mid is the one used on the paper, up and down blocks seem unstable", choices=["up", "mid", "down"], multiselect=True, value="mid")
69
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
70
+ seed = gr.Slider(minimum=1, maximum=9007199254740991, step=1, randomize=True)
71
+ lora = gr.Textbox(label="Custom LoRA path", info="Load a custom LoRA from Hugging Face to use PAG with")
72
+ gr.Examples(fn=run, examples=[" ", "an insect robot preparing a delicious meal, anime style", "a photo of a group of friends at an amusement park"], inputs=prompt, outputs=[output, seed], cache_examples="lazy")
73
+ gr.on(
74
+ triggers=[
75
+ button.click,
76
+ prompt.submit
77
+ ],
78
+ fn=run,
79
+ inputs=[prompt, negative_prompt, guidance_scale, pag_scale, pag_layers, randomize_seed, seed, lora],
80
+ outputs=[output, seed],
81
+ )
82
+ if __name__ == "__main__":
83
+ demo.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ diffusers
2
+ transformers
3
+ accelerate
4
+ safetensors
5
+ peft
6
+ https://huggingface.co/spaces/multimodalart/face-to-all/resolve/main/gradio_imageslider-0.0.19-py3-none-any.whl?download=true