from diffusers import CycleDiffusionPipeline, DDIMScheduler
import gradio as gr
import torch
from PIL import Image
import utils
import ptp_utils
import seq_aligner
import torch.nn.functional as nnf
from typing import Optional, Union, Tuple, List, Callable, Dict
import abc
LOW_RESOURCE = False
MAX_NUM_WORDS = 77
is_colab = utils.is_google_colab()
colab_instruction = """
CycleDiffusion with Stable Diffusion
Demo for CycleDiffusion with Stable Diffusion.
CycleDiffusion (๐ Paper link | ๐งจ Pipeline doc) is an image-to-image translation method that supports stochastic samplers for diffusion models.
We also support the combination of CycleDiffusion and Cross Attention Control (CAC | ๐ Paper link). CAC is a technique to transfer the attention map from the source prompt to the target prompt.
Quick start:
1. Click one row of Examples at the end of this page. It will fill all inputs needed.
2. Click the "Edit" button.
How to use:
1. Upload an image.
2. Enter the source and target prompts.
3. Select the source guidance scale (for "encoding") and the target guidance scale (for "decoding").
4. Select the strength (smaller strength means better content preservation).
5 (optional). Configurate Cross Attention Control options (e.g., CAC type, cross replace steps, self replace steps).
6 (optional). Configurate other options (e.g., image size, inference steps, random seed).
7. Click the "Edit" button.
Notes:
1. CycleDiffusion is likely to fail when drastic changes are intended (e.g., changing a large black car to red).
2. The value of strength can be set larger when CAC is used.
3. If CAC type is "Replace", the source and target prompts should differ in only one token; otherwise, an error will be raised. This is why we deliberately make some grammar mistakes in Examples.
4. If CAC type is "Refine", the source prompt be a subsequence of the target prompt; otherwise, an error will be raised.
Runtimes: 30s on A10G small, 90s on T4 small.
{colab_instruction}
Running on
{device_print}{(" in a
Google Colab." if is_colab else "")}
"""
)
with gr.Row():
with gr.Column(scale=55):
with gr.Group():
img = gr.Image(label="Input image", height=512, tool="editor", type="pil")
image_out = gr.Image(label="Output image", height=512)
# gallery = gr.Gallery(
# label="Generated images", show_label=False, elem_id="gallery"
# ).style(grid=[1], height="auto")
with gr.Column(scale=45):
with gr.Tab("Edit options"):
with gr.Group():
with gr.Row():
source_prompt = gr.Textbox(label="Source prompt", placeholder="Source prompt describes the input image")
source_guidance_scale = gr.Slider(label="Source guidance scale", value=1, minimum=1, maximum=10)
with gr.Row():
target_prompt = gr.Textbox(label="Target prompt", placeholder="Target prompt describes the output image")
guidance_scale = gr.Slider(label="Target guidance scale", value=5, minimum=1, maximum=10)
with gr.Row():
strength = gr.Slider(label="Strength", value=0.7, minimum=0.5, maximum=1, step=0.01)
with gr.Row():
generate1 = gr.Button(value="Edit")
with gr.Tab("CAC options"):
with gr.Group():
with gr.Row():
cross_attention_control = gr.Radio(label="CAC type", choices=["None", "Replace", "Refine"], value="None")
with gr.Row():
# If not "None", the following two parameters will be used.
cross_replace_steps = gr.Slider(label="Cross replace steps", value=0.8, minimum=0.0, maximum=1, step=0.01)
self_replace_steps = gr.Slider(label="Self replace steps", value=0.4, minimum=0.0, maximum=1, step=0.01)
with gr.Row():
generate2 = gr.Button(value="Edit")
with gr.Tab("Other options"):
with gr.Group():
with gr.Row():
num_inference_steps = gr.Slider(label="Number of inference steps", value=100, minimum=25, maximum=500, step=1)
width = gr.Slider(label="Width", value=512, minimum=64, maximum=1024, step=8)
height = gr.Slider(label="Height", value=512, minimum=64, maximum=1024, step=8)
with gr.Row():
seed = gr.Slider(0, 2147483647, label='Seed', value=0, step=1)
with gr.Row():
generate3 = gr.Button(value="Edit")
inputs = [source_prompt, target_prompt, source_guidance_scale, guidance_scale, num_inference_steps,
width, height, seed, img, strength,
cross_attention_control, cross_replace_steps, self_replace_steps]
generate1.click(inference, inputs=inputs, outputs=image_out)
generate2.click(inference, inputs=inputs, outputs=image_out)
generate3.click(inference, inputs=inputs, outputs=image_out)
ex = gr.Examples(
[
["An astronaut riding a horse", "An astronaut riding an elephant", 1, 2, 100, "images/astronaut_horse.png", 0.8, "None", 0, 0],
["An astronaut riding a horse", "An astronaut riding a elephant", 1, 2, 100, "images/astronaut_horse.png", 0.9, "Replace", 0.15, 0.10],
["A black colored car.", "A blue colored car.", 1, 3, 100, "images/black_car.png", 0.85, "None", 0, 0],
["A black colored car.", "A blue colored car.", 1, 5, 100, "images/black_car.png", 0.95, "Replace", 0.8, 0.4],
["A black colored car.", "A red colored car.", 1, 5, 100, "images/black_car.png", 1, "Replace", 0.8, 0.4],
["An aerial view of autumn scene.", "An aerial view of winter scene.", 1, 5, 100, "images/mausoleum.png", 0.9, "None", 0, 0],
["An aerial view of autumn scene.", "An aerial view of winter scene.", 1, 5, 100, "images/mausoleum.png", 1, "Replace", 0.8, 0.4],
["A green apple and a black backpack on the floor.", "A red apple and a black backpack on the floor.", 1, 7, 100, "images/apple_bag.png", 0.9, "None", 0, 0],
["A green apple and a black backpack on the floor.", "A red apple and a black backpack on the floor.", 1, 7, 100, "images/apple_bag.png", 0.9, "Replace", 0.8, 0.4],
["A hotel room with red flowers on the bed.", "A hotel room with a cat sitting on the bed.", 1, 4, 100, "images/flower_hotel.png", 0.8, "None", 0, 0],
["A hotel room with red flowers on the bed.", "A hotel room with blue flowers on the bed.", 1, 5, 100, "images/flower_hotel.png", 0.95, "None", 0, 0],
["A green apple and a black backpack on the floor.", "Two green apples and a black backpack on the floor.", 1, 5, 100, "images/apple_bag.png", 0.89, "None", 0, 0],
],
[source_prompt, target_prompt, source_guidance_scale, guidance_scale, num_inference_steps,
img, strength,
cross_attention_control, cross_replace_steps, self_replace_steps],
image_out, inference, cache_examples=False)
gr.Markdown('''
Space built with Diffusers ๐งจ by HuggingFace ๐ค.
[![Twitter Follow](https://img.shields.io/twitter/follow/ChenHenryWu?style=social)](https://twitter.com/ChenHenryWu)
![visitors](https://visitor-badge.glitch.me/badge?page_id=ChenWu98.CycleDiffusion)
''')
if not is_colab:
demo.queue(concurrency_count=1)
demo.launch(debug=is_colab, share=is_colab)