import spaces
import gradio as gr
from diffusers import AutoPipelineForText2Image
import numpy as np
import math
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

@spaces.GPU
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_snc(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;
}
.tabitem{
padding: 0 !important;
border-radius: 0;
}
.gr-group {
    background: transparent !important;
    border: 0 !important;
}
.styler{
    background: transparent !important
}
'''

with gr.Blocks(css=css, theme=theme) as demo:
    gr.Markdown('''# CommonCanvas Demo
[CommonCanvas suite of models](https://huggingface.co/collections/common-canvas/commoncanvas-66226ef9688b3580a5954653) trained on [CommonCatalogue](https://huggingface.co/collections/common-canvas/commoncatalog-6530907589ffafffe87c31c5), ~70M Creative Commons images.
    ''')
    #with gr.Group():
    with gr.Tab("XL-C model"):
      with gr.Row():
        prompt_xlc = gr.Textbox(show_label=False, scale=4, placeholder="Your prompt for XL-C")
        button_xlc = gr.Button("Generate", min_width=120)
    with gr.Tab("XL-NC model"):
      with gr.Row():
        prompt_xlnc = gr.Textbox(show_label=False, scale=4, placeholder="Your prompt for XL-NC")
        button_xlnc = gr.Button("Generate", min_width=120)
    with gr.Tab("S-C model"):
      with gr.Row():            
        prompt_sc = gr.Textbox(show_label=False, scale=4, placeholder="Your prompt for S-C")
        button_sc = gr.Button("Generate", min_width=120)
    with gr.Tab("S-NC model"):
      with gr.Row():
        prompt_snc = gr.Textbox(show_label=False, scale=4, placeholder="Your prompt for S-NC")
        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)
    
    with gr.Accordion("Use it with 🧨 diffusers, ComfyUI, AUTOMATIC111, Forge, SD.Next, Invoke, etc.", open=False):
        gr.Markdown('''The CommonCanvas S and CommonCanvas XL collections are drop-in replacements of Stable Diffusion 2 and Stable Diffusion XL respectively and can be used as such with `diffusers` or in UIs such as ComfyUI, AUTOMATIC1111, SDNext, InvokeAI, etc.
## Using it with diffusers
```py
from diffusers import AutoPipelineForText2Image
pipe = AutoPipelineForText2Image.from_pretrained(
    "common-canvas/CommonCanvasXL-C", #here you can pick between all models
    custom_pipeline="multimodalart/sdxl_perturbed_attention_guidance",
    torch_dtype=torch.float16
).to(device)

prompt = "a cat"
image = pipe(prompt, num_inference_steps=25).images[0]    
```
## Using it ComfyUI/Automatic1111
- [CommonCanvasS-C.safetensors](https://huggingface.co/common-canvas/CommonCanvas-S-C/resolve/main/commoncanvas_s_c.safetensors?download=true) (SD2 drop-in, commercial)
- [CommonCanvasS-NC.safetensors](https://huggingface.co/common-canvas/CommonCanvas-S-NC/resolve/main/commoncanvas_s_nc.safetensors?download=true) (SD2 drop-in, non-commercial - trained on more data)
- [CommonCanvasXL-C.safetensors](https://huggingface.co/common-canvas/CommonCanvas-XL-NC/blob/main/commoncanvas_xl_nc.safetensors) (SDXL drop-in, commercial)
- [CommonCanvasXL-NC.safetensors](https://huggingface.co/common-canvas/CommonCanvas-XL-NC/resolve/main/commoncanvas_xl_nc.safetensors?download=true) (SDXL drop-in, non-commercial - trained on more data)
''')
    #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)