Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from diffusers import StableDiffusionXLPipeline
|
3 |
+
import numpy as np
|
4 |
+
import math
|
5 |
+
import spaces
|
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="nyanko7/sdxl_smoothed_energy_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, seg_scale=3.0, seg_layers=["mid"], randomize_seed=True, seed=42, 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 |
+
|
35 |
+
print(f"Seed before sending to generator for prompt: `{prompt}`", seed)
|
36 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
37 |
+
image = pipe(prompt, negative_prompt=negative_prompt, guidance_scale=guidance_scale, seg_scale=seg_scale, seg_applied_layers=seg_layers, generator=generator, num_inference_steps=25).images[0]
|
38 |
+
|
39 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
40 |
+
image_normal = pipe(prompt, negative_prompt=negative_prompt, guidance_scale=guidance_scale, seg_scale=0.0, generator=generator, num_inference_steps=25).images[0]
|
41 |
+
print(f"Seed at the end of generation for prompt: `{prompt}`", seed)
|
42 |
+
return (image, image_normal), seed
|
43 |
+
|
44 |
+
css = '''
|
45 |
+
.gradio-container{
|
46 |
+
max-width: 768px !important;
|
47 |
+
margin: 0 auto;
|
48 |
+
}
|
49 |
+
'''
|
50 |
+
|
51 |
+
with gr.Blocks(css=css, theme=theme) as demo:
|
52 |
+
gr.Markdown('''# Smoothed Energy Guidance SDXL
|
53 |
+
SDXL [diffusers implementation](https://huggingface.co/nyanko7/sdxl_smoothed_energy_guidance) of [Smoothed Energy Guidance](https://arxiv.org/abs/2408.00760)
|
54 |
+
''')
|
55 |
+
with gr.Group():
|
56 |
+
with gr.Row():
|
57 |
+
prompt = gr.Textbox(show_label=False, scale=4, placeholder="Your prompt", info="Leave blank to test unconditional generation")
|
58 |
+
button = gr.Button("Generate", min_width=120)
|
59 |
+
output = ImageSlider(label="Left: SEG, Right: No SEG", interactive=False)
|
60 |
+
with gr.Accordion("Advanced Settings", open=False):
|
61 |
+
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)
|
62 |
+
negative_prompt = gr.Textbox(label="Negative prompt", info="Is only applied for the CFG part, leave blank for unconditional generation")
|
63 |
+
seg_scale = gr.Number(label="Seg Scale", value=3.0)
|
64 |
+
seg_layers = gr.Dropdown(label="Model layers to apply Seg to", info="mid is the one used on the paper, up and down blocks seem unstable", choices=["up", "mid", "down"], multiselect=True, value="mid")
|
65 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
66 |
+
seed = gr.Slider(minimum=1, maximum=9007199254740991, step=1, randomize=True)
|
67 |
+
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")
|
68 |
+
gr.on(
|
69 |
+
triggers=[
|
70 |
+
button.click,
|
71 |
+
prompt.submit
|
72 |
+
],
|
73 |
+
fn=run,
|
74 |
+
inputs=[prompt, negative_prompt, guidance_scale, seg_scale, seg_layers, randomize_seed, seed],
|
75 |
+
outputs=[output, seed],
|
76 |
+
)
|
77 |
+
if __name__ == "__main__":
|
78 |
+
demo.launch(share=True)
|