Spaces:
Sleeping
Sleeping
[Admin maintenance] Migrate grant to ZeroGPU
#2
by multimodalart HF Staff - opened
- app.py +26 -120
- requirements.txt +1 -4
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
import torch
|
| 4 |
-
import spaces
|
| 5 |
from src.euler_scheduler import MyEulerAncestralDiscreteScheduler
|
| 6 |
from diffusers.pipelines.auto_pipeline import AutoPipelineForImage2Image
|
| 7 |
from src.sdxl_inversion_pipeline import SDXLDDIMPipeline
|
|
@@ -10,98 +10,53 @@ from src.editor import ImageEditorDemo
|
|
| 10 |
|
| 11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
|
| 13 |
-
# if torch.cuda.is_available():
|
| 14 |
-
# torch.cuda.max_memory_allocated(device=device)
|
| 15 |
-
# pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
|
| 16 |
-
# pipe.enable_xformers_memory_efficient_attention()
|
| 17 |
-
# pipe = pipe.to(device)
|
| 18 |
-
# else:
|
| 19 |
-
# pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
|
| 20 |
-
# pipe = pipe.to(device)
|
| 21 |
-
|
| 22 |
-
# css = """
|
| 23 |
-
# #col-container-1 {
|
| 24 |
-
# margin: 0 auto;
|
| 25 |
-
# max-width: 520px;
|
| 26 |
-
# }
|
| 27 |
-
# #col-container-2 {
|
| 28 |
-
# margin: 0 auto;
|
| 29 |
-
# max-width: 520px;
|
| 30 |
-
# }
|
| 31 |
-
# """
|
| 32 |
-
|
| 33 |
if device == "cuda":
|
| 34 |
torch.cuda.max_memory_allocated(device=device)
|
| 35 |
|
| 36 |
scheduler_class = MyEulerAncestralDiscreteScheduler
|
| 37 |
|
| 38 |
-
pipe_inversion = SDXLDDIMPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
|
| 39 |
-
pipe_inference = AutoPipelineForImage2Image.from_pretrained("stabilityai/sdxl-turbo",
|
| 40 |
-
use_safetensors=True).to(device)
|
| 41 |
pipe_inference.scheduler = scheduler_class.from_config(pipe_inference.scheduler.config)
|
| 42 |
pipe_inversion.scheduler = scheduler_class.from_config(pipe_inversion.scheduler.config)
|
| 43 |
pipe_inversion.scheduler_inference = scheduler_class.from_config(pipe_inference.scheduler.config)
|
| 44 |
|
| 45 |
-
if device == "cuda":
|
| 46 |
-
pipe_inference.enable_xformers_memory_efficient_attention()
|
| 47 |
-
pipe_inversion.enable_xformers_memory_efficient_attention()
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
-
# with gr.Blocks(css=css) as demo:
|
| 52 |
-
# with gr.Blocks(css="style.css") as demo:
|
| 53 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 54 |
gr.Markdown(f""" # Real Time Editing with GNRI Inversion 🍎⚡️
|
| 55 |
-
This is a demo for our [paper](https://arxiv.org/abs/2312.12540) **GNRI: Lightning-fast Image Inversion and Editing
|
| 56 |
for Text-to-Image Diffusion Models. Accepted to ICLR 2025**.
|
| 57 |
More details can be found in the [project page](https://barakmam.github.io/rnri.github.io/).
|
| 58 |
-
|
| 59 |
The demo is based on SDXL. Improved results can be achieved with our FLUX version (examples in project page).
|
| 60 |
""")
|
| 61 |
-
inv_state = gr.State()
|
| 62 |
-
|
| 63 |
|
| 64 |
@spaces.GPU
|
| 65 |
-
def
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
|
| 79 |
config = RunConfig(num_inference_steps=num_inference_steps,
|
| 80 |
-
num_inversion_steps=
|
| 81 |
edit_guidance_scale=edit_guidance_scale,
|
| 82 |
inversion_max_step=inversion_max_step)
|
| 83 |
-
if device == 'cuda':
|
| 84 |
-
pipe_inference.to('cpu')
|
| 85 |
-
torch.cuda.empty_cache()
|
| 86 |
|
| 87 |
-
inversion_state = ImageEditorDemo.invert(pipe_inversion
|
| 88 |
[rnri_iterations, rnri_alpha, rnri_lr], device)
|
| 89 |
-
if device == 'cuda':
|
| 90 |
-
pipe_inversion.to('cpu')
|
| 91 |
-
torch.cuda.empty_cache()
|
| 92 |
-
pipe_inference.to(device)
|
| 93 |
-
|
| 94 |
-
gr.Info('Input has set!')
|
| 95 |
-
return inversion_state, "Input has set!"
|
| 96 |
-
|
| 97 |
-
@spaces.GPU
|
| 98 |
-
def edit(inversion_state, target_prompt):
|
| 99 |
-
if inversion_state is None:
|
| 100 |
-
raise gr.Error("Set inputs before editing. Progress indication below")
|
| 101 |
|
| 102 |
-
image = ImageEditorDemo.edit(pipe_inference, target_prompt,
|
|
|
|
| 103 |
inversion_state['cfg'], inversion_state['cfg'].edit_guidance_scale)
|
| 104 |
-
|
| 105 |
return image
|
| 106 |
|
| 107 |
|
|
@@ -167,9 +122,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 167 |
value=0.2,
|
| 168 |
)
|
| 169 |
|
| 170 |
-
with gr.Row():
|
| 171 |
-
is_set_text = gr.Text("", show_label=False)
|
| 172 |
-
|
| 173 |
with gr.Column(elem_id="col-container-2"):
|
| 174 |
result = gr.Image(label="Result")
|
| 175 |
|
|
@@ -198,57 +150,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 198 |
Errors may appear due to resource availability by HF.
|
| 199 |
Performance may be inferior to the reported in the paper due to hardware limitation.""")
|
| 200 |
|
| 201 |
-
input_image.change(set_pipe, inputs=[input_image, description_prompt, edit_guidance_scale, num_inference_steps,
|
| 202 |
-
num_inference_steps, inversion_max_step, rnri_iterations, rnri_alpha, rnri_lr],
|
| 203 |
-
outputs=[inv_state, is_set_text], trigger_mode='once')
|
| 204 |
-
|
| 205 |
-
description_prompt.change(set_pipe, inputs=[input_image, description_prompt, edit_guidance_scale,
|
| 206 |
-
num_inference_steps,
|
| 207 |
-
num_inference_steps, inversion_max_step, rnri_iterations, rnri_alpha,
|
| 208 |
-
rnri_lr],
|
| 209 |
-
outputs=[inv_state, is_set_text], trigger_mode='once')
|
| 210 |
-
|
| 211 |
-
edit_guidance_scale.change(set_pipe, inputs=[input_image, description_prompt, edit_guidance_scale,
|
| 212 |
-
num_inference_steps,
|
| 213 |
-
num_inference_steps, inversion_max_step, rnri_iterations, rnri_alpha,
|
| 214 |
-
rnri_lr],
|
| 215 |
-
outputs=[inv_state, is_set_text], trigger_mode='once')
|
| 216 |
-
num_inference_steps.change(set_pipe, inputs=[input_image, description_prompt, edit_guidance_scale,
|
| 217 |
-
num_inference_steps,
|
| 218 |
-
num_inference_steps, inversion_max_step, rnri_iterations, rnri_alpha,
|
| 219 |
-
rnri_lr],
|
| 220 |
-
outputs=[inv_state, is_set_text], trigger_mode='once')
|
| 221 |
-
inversion_max_step.change(set_pipe, inputs=[input_image, description_prompt, edit_guidance_scale,
|
| 222 |
-
num_inference_steps,
|
| 223 |
-
num_inference_steps, inversion_max_step, rnri_iterations, rnri_alpha,
|
| 224 |
-
rnri_lr],
|
| 225 |
-
outputs=[inv_state, is_set_text], trigger_mode='once')
|
| 226 |
-
rnri_iterations.change(set_pipe, inputs=[input_image, description_prompt, edit_guidance_scale,
|
| 227 |
-
num_inference_steps,
|
| 228 |
-
num_inference_steps, inversion_max_step, rnri_iterations, rnri_alpha,
|
| 229 |
-
rnri_lr],
|
| 230 |
-
outputs=[inv_state, is_set_text], trigger_mode='once')
|
| 231 |
-
rnri_alpha.change(set_pipe, inputs=[input_image, description_prompt, edit_guidance_scale,
|
| 232 |
-
num_inference_steps,
|
| 233 |
-
num_inference_steps, inversion_max_step, rnri_iterations, rnri_alpha,
|
| 234 |
-
rnri_lr],
|
| 235 |
-
outputs=[inv_state, is_set_text], trigger_mode='once')
|
| 236 |
-
rnri_lr.change(set_pipe, inputs=[input_image, description_prompt, edit_guidance_scale,
|
| 237 |
-
num_inference_steps,
|
| 238 |
-
num_inference_steps, inversion_max_step, rnri_iterations, rnri_alpha,
|
| 239 |
-
rnri_lr],
|
| 240 |
-
outputs=[inv_state, is_set_text], trigger_mode='once')
|
| 241 |
-
|
| 242 |
-
# set_button.click(
|
| 243 |
-
# fn=set_pipe,
|
| 244 |
-
# inputs=[inv_state, input_image, description_prompt, edit_guidance_scale, num_inference_steps,
|
| 245 |
-
# num_inference_steps, inversion_max_step, rnri_iterations, rnri_alpha, rnri_lr],
|
| 246 |
-
# outputs=[inv_state, is_set_text],
|
| 247 |
-
# )
|
| 248 |
-
|
| 249 |
run_button.click(
|
| 250 |
-
fn=
|
| 251 |
-
inputs=[
|
|
|
|
|
|
|
| 252 |
outputs=[result]
|
| 253 |
)
|
| 254 |
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
import torch
|
|
|
|
| 5 |
from src.euler_scheduler import MyEulerAncestralDiscreteScheduler
|
| 6 |
from diffusers.pipelines.auto_pipeline import AutoPipelineForImage2Image
|
| 7 |
from src.sdxl_inversion_pipeline import SDXLDDIMPipeline
|
|
|
|
| 10 |
|
| 11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
if device == "cuda":
|
| 14 |
torch.cuda.max_memory_allocated(device=device)
|
| 15 |
|
| 16 |
scheduler_class = MyEulerAncestralDiscreteScheduler
|
| 17 |
|
| 18 |
+
pipe_inversion = SDXLDDIMPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True).to(device)
|
| 19 |
+
pipe_inference = AutoPipelineForImage2Image.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True).to(device)
|
|
|
|
| 20 |
pipe_inference.scheduler = scheduler_class.from_config(pipe_inference.scheduler.config)
|
| 21 |
pipe_inversion.scheduler = scheduler_class.from_config(pipe_inversion.scheduler.config)
|
| 22 |
pipe_inversion.scheduler_inference = scheduler_class.from_config(pipe_inference.scheduler.config)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 26 |
gr.Markdown(f""" # Real Time Editing with GNRI Inversion 🍎⚡️
|
| 27 |
+
This is a demo for our [paper](https://arxiv.org/abs/2312.12540) **GNRI: Lightning-fast Image Inversion and Editing
|
| 28 |
for Text-to-Image Diffusion Models. Accepted to ICLR 2025**.
|
| 29 |
More details can be found in the [project page](https://barakmam.github.io/rnri.github.io/).
|
| 30 |
+
|
| 31 |
The demo is based on SDXL. Improved results can be achieved with our FLUX version (examples in project page).
|
| 32 |
""")
|
|
|
|
|
|
|
| 33 |
|
| 34 |
@spaces.GPU
|
| 35 |
+
def invert_and_edit(input_image, description_prompt, target_prompt,
|
| 36 |
+
edit_guidance_scale, num_inference_steps=4,
|
| 37 |
+
inversion_max_step=0.6, rnri_iterations=2, rnri_alpha=0.1, rnri_lr=0.2,
|
| 38 |
+
progress=gr.Progress(track_tqdm=True)):
|
| 39 |
+
if input_image is None or not description_prompt or not target_prompt:
|
| 40 |
+
raise gr.Error("Please provide an input image, description, and edit prompt.")
|
| 41 |
+
|
| 42 |
+
num_inference_steps = int(num_inference_steps)
|
| 43 |
+
edit_guidance_scale = float(edit_guidance_scale)
|
| 44 |
+
inversion_max_step = float(inversion_max_step)
|
| 45 |
+
rnri_iterations = int(rnri_iterations)
|
| 46 |
+
rnri_alpha = float(rnri_alpha)
|
| 47 |
+
rnri_lr = float(rnri_lr)
|
| 48 |
|
| 49 |
config = RunConfig(num_inference_steps=num_inference_steps,
|
| 50 |
+
num_inversion_steps=num_inference_steps,
|
| 51 |
edit_guidance_scale=edit_guidance_scale,
|
| 52 |
inversion_max_step=inversion_max_step)
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
inversion_state = ImageEditorDemo.invert(pipe_inversion, input_image, description_prompt, config,
|
| 55 |
[rnri_iterations, rnri_alpha, rnri_lr], device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
image = ImageEditorDemo.edit(pipe_inference, target_prompt,
|
| 58 |
+
inversion_state['latent'], inversion_state['noise'],
|
| 59 |
inversion_state['cfg'], inversion_state['cfg'].edit_guidance_scale)
|
|
|
|
| 60 |
return image
|
| 61 |
|
| 62 |
|
|
|
|
| 122 |
value=0.2,
|
| 123 |
)
|
| 124 |
|
|
|
|
|
|
|
|
|
|
| 125 |
with gr.Column(elem_id="col-container-2"):
|
| 126 |
result = gr.Image(label="Result")
|
| 127 |
|
|
|
|
| 150 |
Errors may appear due to resource availability by HF.
|
| 151 |
Performance may be inferior to the reported in the paper due to hardware limitation.""")
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
run_button.click(
|
| 154 |
+
fn=invert_and_edit,
|
| 155 |
+
inputs=[input_image, description_prompt, target_prompt,
|
| 156 |
+
edit_guidance_scale, num_inference_steps,
|
| 157 |
+
inversion_max_step, rnri_iterations, rnri_alpha, rnri_lr],
|
| 158 |
outputs=[result]
|
| 159 |
)
|
| 160 |
|
requirements.txt
CHANGED
|
@@ -1,8 +1,5 @@
|
|
| 1 |
accelerate==0.25.0
|
| 2 |
diffusers==0.29.0
|
| 3 |
invisible_watermark
|
| 4 |
-
torch==2.8.0
|
| 5 |
transformers==4.32.1
|
| 6 |
-
|
| 7 |
-
torchvision==0.23.0
|
| 8 |
-
pyrallis==0.3.1
|
|
|
|
| 1 |
accelerate==0.25.0
|
| 2 |
diffusers==0.29.0
|
| 3 |
invisible_watermark
|
|
|
|
| 4 |
transformers==4.32.1
|
| 5 |
+
pyrallis==0.3.1
|
|
|
|
|
|