Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
import math
|
3 |
+
import random
|
4 |
+
import gradio as gr
|
5 |
+
import torch
|
6 |
+
from PIL import Image, ImageOps
|
7 |
+
from diffusers import StableDiffusionInstructPix2PixPipeline
|
8 |
+
|
9 |
+
example_instructions = [
|
10 |
+
"move the lemon to the right of the table"
|
11 |
+
]
|
12 |
+
|
13 |
+
def main():
|
14 |
+
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained("McGill-NLP/AURORA", safety_checker=None).to("cuda")
|
15 |
+
example_image = Image.open("example.jpg").convert("RGB")
|
16 |
+
|
17 |
+
def load_example(
|
18 |
+
steps: int,
|
19 |
+
seed: int,
|
20 |
+
text_cfg_scale: float,
|
21 |
+
image_cfg_scale: float,
|
22 |
+
):
|
23 |
+
example_instruction = random.choice(example_instructions)
|
24 |
+
return [example_image, example_instruction] + generate(
|
25 |
+
example_image,
|
26 |
+
example_instruction,
|
27 |
+
steps,
|
28 |
+
seed,
|
29 |
+
text_cfg_scale,
|
30 |
+
image_cfg_scale,
|
31 |
+
)
|
32 |
+
|
33 |
+
def generate(
|
34 |
+
input_image: Image.Image,
|
35 |
+
instruction: str,
|
36 |
+
steps: int,
|
37 |
+
seed: int,
|
38 |
+
text_cfg_scale: float,
|
39 |
+
image_cfg_scale: float,
|
40 |
+
):
|
41 |
+
width, height = input_image.size
|
42 |
+
factor = 512 / max(width, height)
|
43 |
+
factor = math.ceil(min(width, height) * factor / 64) * 64 / min(width, height)
|
44 |
+
width = int((width * factor) // 64) * 64
|
45 |
+
height = int((height * factor) // 64) * 64
|
46 |
+
input_image = ImageOps.fit(input_image, (width, height), method=Image.Resampling.LANCZOS)
|
47 |
+
|
48 |
+
if instruction == "":
|
49 |
+
return [input_image, seed]
|
50 |
+
|
51 |
+
generator = torch.manual_seed(seed)
|
52 |
+
edited_image = pipe(
|
53 |
+
instruction, image=input_image,
|
54 |
+
guidance_scale=text_cfg_scale, image_guidance_scale=image_cfg_scale,
|
55 |
+
num_inference_steps=steps, generator=generator,
|
56 |
+
).images[0]
|
57 |
+
return [seed, text_cfg_scale, image_cfg_scale, edited_image]
|
58 |
+
|
59 |
+
def reset():
|
60 |
+
return [50, 42, 7.5, 1.5, None]
|
61 |
+
|
62 |
+
with gr.Blocks() as demo:
|
63 |
+
gr.HTML("""<h1 style="font-weight: 900; margin-bottom: 10px;">
|
64 |
+
AURORA: Learning Action and Reasoning-Centric Image Editing from Videos and Simulations
|
65 |
+
</h1>
|
66 |
+
<p>
|
67 |
+
AURORA (Action Reasoning Object Attribute) enables training an instruction-guided image editing model that can perform action and reasoning-centric edits, in addition to "simpler" established object, attribute or global edits. <b> To illustrate this, please click "Load example" </b>.
|
68 |
+
</p>""")
|
69 |
+
|
70 |
+
with gr.Row():
|
71 |
+
with gr.Column(scale=3):
|
72 |
+
instruction = gr.Textbox(lines=1, label="Edit instruction", interactive=True)
|
73 |
+
with gr.Column(scale=1, min_width=100):
|
74 |
+
generate_button = gr.Button("Generate", variant="primary")
|
75 |
+
with gr.Column(scale=1, min_width=100):
|
76 |
+
reset_button = gr.Button("Reset", variant="stop")
|
77 |
+
with gr.Column(scale=1, min_width=100):
|
78 |
+
load_button = gr.Button("Load example")
|
79 |
+
|
80 |
+
with gr.Row():
|
81 |
+
input_image = gr.Image(label="Input image", type="pil", interactive=True)
|
82 |
+
edited_image = gr.Image(label=f"Edited image", type="pil", interactive=False)
|
83 |
+
|
84 |
+
with gr.Row():
|
85 |
+
steps = gr.Number(value=50, precision=0, label="Steps", interactive=True)
|
86 |
+
seed = gr.Number(value=42, precision=0, label="Seed", interactive=True)
|
87 |
+
text_cfg_scale = gr.Number(value=7.5, label=f"Text CFG", interactive=True)
|
88 |
+
image_cfg_scale = gr.Number(value=1.5, label=f"Image CFG", interactive=True)
|
89 |
+
|
90 |
+
load_button.click(
|
91 |
+
fn=load_example,
|
92 |
+
inputs=[
|
93 |
+
steps,
|
94 |
+
seed,
|
95 |
+
text_cfg_scale,
|
96 |
+
image_cfg_scale,
|
97 |
+
],
|
98 |
+
outputs=[input_image, instruction, seed, text_cfg_scale, image_cfg_scale, edited_image],
|
99 |
+
)
|
100 |
+
generate_button.click(
|
101 |
+
fn=generate,
|
102 |
+
inputs=[
|
103 |
+
input_image,
|
104 |
+
instruction,
|
105 |
+
steps,
|
106 |
+
seed,
|
107 |
+
text_cfg_scale,
|
108 |
+
image_cfg_scale,
|
109 |
+
],
|
110 |
+
outputs=[seed, text_cfg_scale, image_cfg_scale, edited_image],
|
111 |
+
)
|
112 |
+
reset_button.click(
|
113 |
+
fn=reset,
|
114 |
+
inputs=[],
|
115 |
+
outputs=[steps, seed, text_cfg_scale, image_cfg_scale, edited_image],
|
116 |
+
)
|
117 |
+
|
118 |
+
demo.queue()
|
119 |
+
demo.launch()
|
120 |
+
# demo.launch(share=True)
|
121 |
+
|
122 |
+
if __name__ == "__main__":
|
123 |
+
main()
|