Spaces:
Running
on
Zero
Running
on
Zero
Commit
•
8226b60
1
Parent(s):
08500d8
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from diffusers import AutoPipelineForText2Image
|
3 |
+
import numpy as np
|
4 |
+
import math
|
5 |
+
import spaces
|
6 |
+
import torch
|
7 |
+
import random
|
8 |
+
|
9 |
+
theme = gr.themes.Base(
|
10 |
+
font=[gr.themes.GoogleFont('Libre Franklin'), gr.themes.GoogleFont('Public Sans'), 'system-ui', 'sans-serif'],
|
11 |
+
)
|
12 |
+
|
13 |
+
pipe_xlc = AutoPipelineForText2Image.from_pretrained(
|
14 |
+
"temp-org-cc/CommonCanvas-XLC",
|
15 |
+
custom_pipeline="multimodalart/sdxl_perturbed_attention_guidance",
|
16 |
+
torch_dtype=torch.float16
|
17 |
+
)
|
18 |
+
pipe_xlnc = AutoPipelineForText2Image.from_pretrained(
|
19 |
+
"temp-org-cc/CommonCanvas-XLNC",
|
20 |
+
custom_pipeline="multimodalart/sdxl_perturbed_attention_guidance",
|
21 |
+
torch_dtype=torch.float16
|
22 |
+
)
|
23 |
+
pipe_sc = AutoPipelineForText2Image.from_pretrained(
|
24 |
+
"temp-org-cc/CommonCanvas-SC",
|
25 |
+
custom_pipeline="hyoungwoncho/sd_perturbed_attention_guidance",
|
26 |
+
torch_dtype=torch.float16
|
27 |
+
)
|
28 |
+
pipe_snc = AutoPipelineForText2Image.from_pretrained(
|
29 |
+
"temp-org-cc/CommonCanvas-SNC",
|
30 |
+
custom_pipeline="hyoungwoncho/sd_perturbed_attention_guidance",
|
31 |
+
torch_dtype=torch.float16
|
32 |
+
)
|
33 |
+
|
34 |
+
device="cuda"
|
35 |
+
pipe = pipe.to(device)
|
36 |
+
|
37 |
+
@spaces.GPU
|
38 |
+
def run_xlc(prompt, negative_prompt=None, guidance_scale=7.0, pag_scale=3.0, pag_layers=["mid"], randomize_seed=True, seed=42, progress=gr.Progress(track_tqdm=True)):
|
39 |
+
if(randomize_seed):
|
40 |
+
seed = random.randint(0, 9007199254740991)
|
41 |
+
|
42 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
43 |
+
image = pipe_xlc(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]
|
44 |
+
|
45 |
+
return image, seed
|
46 |
+
|
47 |
+
@spaces.GPU
|
48 |
+
def run_xlnc(prompt, negative_prompt=None, guidance_scale=7.0, pag_scale=3.0, pag_layers=["mid"], randomize_seed=True, seed=42, progress=gr.Progress(track_tqdm=True)):
|
49 |
+
if(randomize_seed):
|
50 |
+
seed = random.randint(0, 9007199254740991)
|
51 |
+
|
52 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
53 |
+
image = pipe_xlnc(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]
|
54 |
+
|
55 |
+
return image, seed
|
56 |
+
|
57 |
+
@spaces.GPU
|
58 |
+
def run_sc(prompt, negative_prompt=None, guidance_scale=7.0, pag_scale=3.0, pag_layers=["mid"], randomize_seed=True, seed=42, progress=gr.Progress(track_tqdm=True)):
|
59 |
+
if(randomize_seed):
|
60 |
+
seed = random.randint(0, 9007199254740991)
|
61 |
+
|
62 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
63 |
+
image = pipe_sc(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]
|
64 |
+
|
65 |
+
return image, seed
|
66 |
+
|
67 |
+
def run_snc(prompt, negative_prompt=None, guidance_scale=7.0, pag_scale=3.0, pag_layers=["mid"], randomize_seed=True, seed=42, progress=gr.Progress(track_tqdm=True)):
|
68 |
+
if(randomize_seed):
|
69 |
+
seed = random.randint(0, 9007199254740991)
|
70 |
+
|
71 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
72 |
+
image = pipe_sc(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]
|
73 |
+
|
74 |
+
return image, seed
|
75 |
+
|
76 |
+
css = '''
|
77 |
+
.gradio-container{
|
78 |
+
max-width: 768px !important;
|
79 |
+
margin: 0 auto;
|
80 |
+
}
|
81 |
+
'''
|
82 |
+
|
83 |
+
with gr.Blocks(css=css, theme=theme) as demo:
|
84 |
+
gr.Markdown('''# CommonCanvas
|
85 |
+
Demo for the CommonCanvas suite of models trained on the CommonCatalogue, a dataset with ~70M images dedicated to the Creative Commons
|
86 |
+
''')
|
87 |
+
with gr.Group():
|
88 |
+
with gr.Tab("CommonCanvas XLC"):
|
89 |
+
with gr.Row():
|
90 |
+
prompt_xlc = gr.Textbox(show_label=False, scale=4, placeholder="Your prompt")
|
91 |
+
button_xlc = gr.Button("Generate", min_width=120)
|
92 |
+
with gr.Tab("CommonCanvas XLNC"):
|
93 |
+
with gr.Row():
|
94 |
+
prompt_xlnc = gr.Textbox(show_label=False, scale=4, placeholder="Your prompt")
|
95 |
+
button_xlnc = gr.Button("Generate", min_width=120)
|
96 |
+
with gr.Tab("CommonCanvas SC"):
|
97 |
+
prompt_sc = gr.Textbox(show_label=False, scale=4, placeholder="Your prompt")
|
98 |
+
button_sc = gr.Button("Generate", min_width=120)
|
99 |
+
with gr.Tab("CommonCanvas SNC"):
|
100 |
+
prompt_snc = gr.Textbox(show_label=False, scale=4, placeholder="Your prompt")
|
101 |
+
button_snc = gr.Button("Generate", min_width=120)
|
102 |
+
output = gr.Image(label="Your result", interactive=False)
|
103 |
+
with gr.Accordion("Advanced Settings", open=False):
|
104 |
+
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)
|
105 |
+
negative_prompt = gr.Textbox(label="Negative prompt", info="Is only applied for the CFG part, leave blank for unconditional generation")
|
106 |
+
pag_scale = gr.Number(label="Pag Scale", value=3.0)
|
107 |
+
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")
|
108 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
109 |
+
seed = gr.Slider(minimum=1, maximum=9007199254740991, step=1, randomize=True)
|
110 |
+
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=True)
|
111 |
+
gr.on(
|
112 |
+
triggers=[
|
113 |
+
button_xlc.click,
|
114 |
+
prompt_xlc.submit
|
115 |
+
],
|
116 |
+
fn=run_xlc,
|
117 |
+
inputs=[prompt_xlc, negative_prompt, guidance_scale, pag_scale, pag_layers, randomize_seed, seed],
|
118 |
+
outputs=[output, seed],
|
119 |
+
)
|
120 |
+
gr.on(
|
121 |
+
triggers=[
|
122 |
+
button_xlnc.click,
|
123 |
+
prompt_xlnc.submit
|
124 |
+
],
|
125 |
+
fn=run_xlnc,
|
126 |
+
inputs=[prompt_xlnc, negative_prompt, guidance_scale, pag_scale, pag_layers, randomize_seed, seed],
|
127 |
+
outputs=[output, seed],
|
128 |
+
)
|
129 |
+
gr.on(
|
130 |
+
triggers=[
|
131 |
+
button_sc.click,
|
132 |
+
prompt_sc.submit
|
133 |
+
],
|
134 |
+
fn=run_sc,
|
135 |
+
inputs=[prompt_sc, negative_prompt, guidance_scale, pag_scale, pag_layers, randomize_seed, seed],
|
136 |
+
outputs=[output, seed],
|
137 |
+
)
|
138 |
+
gr.on(
|
139 |
+
triggers=[
|
140 |
+
button_snc.click,
|
141 |
+
prompt_snc.submit
|
142 |
+
],
|
143 |
+
fn=run_sc,
|
144 |
+
inputs=[prompt_snc, negative_prompt, guidance_scale, pag_scale, pag_layers, randomize_seed, seed],
|
145 |
+
outputs=[output, seed],
|
146 |
+
)
|
147 |
+
if __name__ == "__main__":
|
148 |
+
demo.launch(share=True)
|