Spaces:
Runtime error
Runtime error
File size: 7,490 Bytes
5978fbd b6d3fe5 5978fbd bff370f dc77641 3104476 5978fbd bff370f 5978fbd b6d3fe5 5978fbd b6d3fe5 3104476 cd4f796 b6d3fe5 cd4f796 bff370f 6711456 c842a65 2b6ccee 8f7b417 947bcf8 8f7b417 947bcf8 8f7b417 947bcf8 2b6ccee 947bcf8 2b6ccee c842a65 2b6ccee 8f7b417 947bcf8 2b6ccee c842a65 8f7b417 62508ec 8756fd9 c842a65 8f7b417 436ce8f b6d3fe5 4dc9242 b6d3fe5 5978fbd b6d3fe5 8f7b417 b6d3fe5 3104476 b6d3fe5 5978fbd b6d3fe5 6711456 b6d3fe5 bff370f 004b80f 6711456 b6d3fe5 6711456 b6d3fe5 6711456 8f7b417 6711456 8f7b417 5978fbd 505b4e4 6711456 505b4e4 5978fbd 505b4e4 b6d3fe5 5978fbd b6d3fe5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
import gradio as gr
import torch
from src.euler_scheduler import MyEulerAncestralDiscreteScheduler
from diffusers.pipelines.auto_pipeline import AutoPipelineForImage2Image
from src.sdxl_inversion_pipeline import SDXLDDIMPipeline
from src.config import RunConfig
from src.editor import ImageEditorDemo
import spaces
device = "cuda" if torch.cuda.is_available() else "cpu"
# if torch.cuda.is_available():
# torch.cuda.max_memory_allocated(device=device)
# pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
# pipe.enable_xformers_memory_efficient_attention()
# pipe = pipe.to(device)
# else:
# pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
# pipe = pipe.to(device)
# css = """
# #col-container-1 {
# margin: 0 auto;
# max-width: 520px;
# }
# #col-container-2 {
# margin: 0 auto;
# max-width: 520px;
# }
# """
if torch.cuda.is_available():
power_device = "GPU"
else:
power_device = "CPU"
# with gr.Blocks(css=css) as demo:
with gr.Blocks(css="style.css") as demo:
gr.Markdown(f""" # Real Time Editing with RNRI Inversion 🍎⚡️
This is a demo for our [paper](https://arxiv.org/abs/2312.12540) **RNRI: Regularized Newton Raphson Inversion for Text-to-Image Diffusion Models**.
Image editing using our RNRI for inversion demonstrates significant speed-up and improved quality compared to previous state-of-the-art methods.
Take a look at our [project page](https://barakmam.github.io/rnri.github.io/).
""")
editor_state = gr.State()
@spaces.GPU
def set_pipe(input_image, description_prompt, edit_guidance_scale, num_inference_steps=4,
num_inversion_steps=4, inversion_max_step=0.6, rnri_iterations=2, rnri_alpha=0.1, rnri_lr=0.2):
scheduler_class = MyEulerAncestralDiscreteScheduler
print('\n################## 1')
pipe_inversion = SDXLDDIMPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True) # .to('cpu')
print('\n################## 2')
pipe_inference = AutoPipelineForImage2Image.from_pretrained("stabilityai/sdxl-turbo",
use_safetensors=True) # .to('cpu')
print('\n################## 3')
pipe_inference.scheduler = scheduler_class.from_config(pipe_inference.scheduler.config)
pipe_inversion.scheduler = scheduler_class.from_config(pipe_inversion.scheduler.config)
pipe_inversion.scheduler_inference = scheduler_class.from_config(pipe_inference.scheduler.config)
print('\n################## 4')
config = RunConfig(num_inference_steps=num_inference_steps,
num_inversion_steps=num_inversion_steps,
edit_guidance_scale=edit_guidance_scale,
inversion_max_step=inversion_max_step)
image_editor = ImageEditorDemo(pipe_inversion, pipe_inference, input_image,
description_prompt, config, device,
[rnri_iterations, rnri_alpha, rnri_lr])
print('\n################## 5')
return image_editor, "Input has set!"
@spaces.GPU
def edit(editor, target_prompt):
if editor is None:
raise gr.Error("Set inputs before editing.")
# if device == "cuda":
# image = editor.to(device).edit(target_prompt)
# else:
image = editor.edit(target_prompt)
return image
gr.Markdown(f"""running on {power_device}""")
with gr.Row():
with gr.Column(elem_id="col-container-1"):
with gr.Row():
input_image = gr.Image(label="Input image", sources=['upload', 'webcam'], type="pil")
with gr.Row():
description_prompt = gr.Text(
label="Image description",
info="Enter your image description ",
show_label=False,
max_lines=1,
placeholder="a cake on a table",
container=False,
)
with gr.Accordion("Advanced Settings", open=False):
with gr.Row():
edit_guidance_scale = gr.Slider(
label="Guidance scale",
minimum=0.0,
maximum=10.0,
step=0.1,
value=1.2,
)
num_inference_steps = gr.Slider(
label="Inference steps",
minimum=1,
maximum=12,
step=1,
value=4,
)
inversion_max_step = gr.Slider(
label="Inversion strength",
minimum=0.0,
maximum=1.0,
step=0.01,
value=0.6,
)
rnri_iterations = gr.Slider(
label="RNRI iterations",
minimum=0,
maximum=5,
step=1,
value=2,
)
rnri_alpha = gr.Slider(
label="RNRI alpha",
minimum=0.0,
maximum=1.0,
step=0.05,
value=0.1,
)
rnri_lr = gr.Slider(
label="RNRI learning rate",
minimum=0.0,
maximum=1.0,
step=0.05,
value=0.2,
)
with gr.Row():
set_button = gr.Button("Set input image & description & settings", scale=1)
is_set_text = gr.Text("", show_label=False)
with gr.Column(elem_id="col-container-2"):
result = gr.Image(label="Result")
with gr.Row():
target_prompt = gr.Text(
label="Edit prompt",
info="Enter your edit prompt",
show_label=False,
max_lines=1,
placeholder="an oreo cake on a table",
container=False,
)
with gr.Row():
run_button = gr.Button("Edit", scale=1)
with gr.Row():
gr.Examples(
examples='examples',
inputs=[input_image, description_prompt, target_prompt, edit_guidance_scale, num_inference_steps,
inversion_max_step, rnri_iterations, rnri_alpha, rnri_lr],
)
set_button.click(
fn=set_pipe,
inputs=[input_image, description_prompt, edit_guidance_scale, num_inference_steps,
num_inference_steps, inversion_max_step, rnri_iterations, rnri_alpha, rnri_lr],
outputs=[editor_state, is_set_text],
)
run_button.click(
fn=edit,
inputs=[editor_state, target_prompt],
outputs=[result]
)
demo.queue().launch()
# im = infer(input_image, description_prompt, target_prompt, edit_guidance_scale, num_inference_steps=4, num_inversion_steps=4,
# inversion_max_step=0.6)
|