CommonCanvas / app.py
multimodalart's picture
Update app.py
2ca84f7 verified
raw
history blame
6.67 kB
import gradio as gr
from diffusers import AutoPipelineForText2Image
import numpy as np
import math
import spaces
import torch
import random
theme = gr.themes.Base(
font=[gr.themes.GoogleFont('Libre Franklin'), gr.themes.GoogleFont('Public Sans'), 'system-ui', 'sans-serif'],
)
device="cuda"
pipe_xlc = AutoPipelineForText2Image.from_pretrained(
"temp-org-cc/CommonCanvas-XLC",
custom_pipeline="multimodalart/sdxl_perturbed_attention_guidance",
torch_dtype=torch.float16
).to(device)
pipe_xlnc = AutoPipelineForText2Image.from_pretrained(
"temp-org-cc/CommonCanvas-XLNC",
custom_pipeline="multimodalart/sdxl_perturbed_attention_guidance",
torch_dtype=torch.float16
).to(device)
pipe_sc = AutoPipelineForText2Image.from_pretrained(
"temp-org-cc/CommonCanvas-SC",
custom_pipeline="hyoungwoncho/sd_perturbed_attention_guidance",
torch_dtype=torch.float16
).to(device)
pipe_snc = AutoPipelineForText2Image.from_pretrained(
"temp-org-cc/CommonCanvas-SNC",
custom_pipeline="hyoungwoncho/sd_perturbed_attention_guidance",
torch_dtype=torch.float16
).to(device)
@spaces.GPU
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)):
if(randomize_seed):
seed = random.randint(0, 9007199254740991)
generator = torch.Generator(device="cuda").manual_seed(seed)
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]
return image, seed
@spaces.GPU
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)):
if(randomize_seed):
seed = random.randint(0, 9007199254740991)
generator = torch.Generator(device="cuda").manual_seed(seed)
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]
return image, seed
@spaces.GPU
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)):
if(randomize_seed):
seed = random.randint(0, 9007199254740991)
generator = torch.Generator(device="cuda").manual_seed(seed)
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]
return image, seed
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)):
if(randomize_seed):
seed = random.randint(0, 9007199254740991)
generator = torch.Generator(device="cuda").manual_seed(seed)
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]
return image, seed
css = '''
.gradio-container{
max-width: 768px !important;
margin: 0 auto;
}
'''
with gr.Blocks(css=css, theme=theme) as demo:
gr.Markdown('''# CommonCanvas
Demo for the CommonCanvas suite of models trained on the CommonCatalogue, a dataset with ~70M images dedicated to the Creative Commons
''')
with gr.Group():
with gr.Tab("CommonCanvas XLC"):
with gr.Row():
prompt_xlc = gr.Textbox(show_label=False, scale=4, placeholder="Your prompt")
button_xlc = gr.Button("Generate", min_width=120)
with gr.Tab("CommonCanvas XLNC"):
with gr.Row():
prompt_xlnc = gr.Textbox(show_label=False, scale=4, placeholder="Your prompt")
button_xlnc = gr.Button("Generate", min_width=120)
with gr.Tab("CommonCanvas SC"):
prompt_sc = gr.Textbox(show_label=False, scale=4, placeholder="Your prompt")
button_sc = gr.Button("Generate", min_width=120)
with gr.Tab("CommonCanvas SNC"):
prompt_snc = gr.Textbox(show_label=False, scale=4, placeholder="Your prompt")
button_snc = gr.Button("Generate", min_width=120)
output = gr.Image(label="Your result", interactive=False)
with gr.Accordion("Advanced Settings", open=False):
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)
negative_prompt = gr.Textbox(label="Negative prompt", info="Is only applied for the CFG part, leave blank for unconditional generation")
pag_scale = gr.Number(label="Pag Scale", value=3.0)
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")
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
seed = gr.Slider(minimum=1, maximum=9007199254740991, step=1, randomize=True)
#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)
gr.on(
triggers=[
button_xlc.click,
prompt_xlc.submit
],
fn=run_xlc,
inputs=[prompt_xlc, negative_prompt, guidance_scale, pag_scale, pag_layers, randomize_seed, seed],
outputs=[output, seed],
)
gr.on(
triggers=[
button_xlnc.click,
prompt_xlnc.submit
],
fn=run_xlnc,
inputs=[prompt_xlnc, negative_prompt, guidance_scale, pag_scale, pag_layers, randomize_seed, seed],
outputs=[output, seed],
)
gr.on(
triggers=[
button_sc.click,
prompt_sc.submit
],
fn=run_sc,
inputs=[prompt_sc, negative_prompt, guidance_scale, pag_scale, pag_layers, randomize_seed, seed],
outputs=[output, seed],
)
gr.on(
triggers=[
button_snc.click,
prompt_snc.submit
],
fn=run_sc,
inputs=[prompt_snc, negative_prompt, guidance_scale, pag_scale, pag_layers, randomize_seed, seed],
outputs=[output, seed],
)
if __name__ == "__main__":
demo.launch(share=True)