Spaces:
LAOS-Y
/
Running on Zero

tennant commited on
Commit
6129036
1 Parent(s): 2b5b6fe

update demo

Browse files
Files changed (4) hide show
  1. README.md +22 -2
  2. edit_app.py +194 -0
  3. imgs/example.jpg +0 -0
  4. requirements.txt +6 -0
README.md CHANGED
@@ -5,8 +5,28 @@ colorFrom: green
5
  colorTo: green
6
  sdk: gradio
7
  sdk_version: 4.25.0
8
- app_file: app.py
9
- pinned: false
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  colorTo: green
6
  sdk: gradio
7
  sdk_version: 4.25.0
8
+ app_file: edit_app.py
9
+ pinned: true
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
13
+
14
+
15
+ ## BibTeX
16
+
17
+ ```
18
+ @article{hqedit2024,
19
+ title={HQ-Edit: A High-Quality and High-Coverage Dataset for General Image Editing},
20
+ author={Mude Hui and Siwei Yang and Bingchen Zhao and Yichun Shi and Heng Wang and Peng Wang and Cihang Xie and Yuyin Zhou},
21
+ journal={arXiv preprint arXiv:xxxx.xxxxx},
22
+ year={2024}
23
+ }
24
+
25
+ @article{brooks2022instructpix2pix,
26
+ title={InstructPix2Pix: Learning to Follow Image Editing Instructions},
27
+ author={Brooks, Tim and Holynski, Aleksander and Efros, Alexei A},
28
+ journal={arXiv preprint arXiv:2211.09800},
29
+ year={2022}
30
+ }
31
+ ```
32
+
edit_app.py ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import math
4
+ import random
5
+
6
+ import gradio as gr
7
+ import torch
8
+ from PIL import Image, ImageOps
9
+ from diffusers import StableDiffusionInstructPix2PixPipeline, EulerAncestralDiscreteScheduler
10
+
11
+
12
+ help_text = """
13
+ If you're not getting what you want, there may be a few reasons:
14
+ 1. Is the image not changing enough? Your Image CFG weight may be too high. This value dictates how similar the output should be to the input. It's possible your edit requires larger changes from the original image, and your Image CFG weight isn't allowing that. Alternatively, your Text CFG weight may be too low. This value dictates how much to listen to the text instruction. The default Image CFG of 1.5 and Text CFG of 7.5 are a good starting point, but aren't necessarily optimal for each edit. Try:
15
+ * Decreasing the Image CFG weight, or
16
+ * Increasing the Text CFG weight, or
17
+ 2. Conversely, is the image changing too much, such that the details in the original image aren't preserved? Try:
18
+ * Increasing the Image CFG weight, or
19
+ * Decreasing the Text CFG weight
20
+ 3. Try generating results with different random seeds by setting "Randomize Seed" and running generation multiple times. You can also try setting "Randomize CFG" to sample new Text CFG and Image CFG values each time.
21
+ 4. Rephrasing the instruction sometimes improves results (e.g., "turn him into a dog" vs. "make him a dog" vs. "as a dog").
22
+ 5. Increasing the number of steps sometimes improves results.
23
+ 6. Do faces look weird? The Stable Diffusion autoencoder has a hard time with faces that are small in the image. Try:
24
+ * Cropping the image so the face takes up a larger portion of the frame.
25
+ """
26
+
27
+
28
+ example_instructions = [
29
+ "Make it a picasso painting",
30
+ "as if it were by modigliani",
31
+ "convert to a bronze statue",
32
+ "Turn it into an anime.",
33
+ "have it look like a graphic novel",
34
+ "make him gain weight",
35
+ "what would he look like bald?",
36
+ "Have him smile",
37
+ "Put him in a cocktail party.",
38
+ "move him at the beach.",
39
+ "add dramatic lighting",
40
+ "Convert to black and white",
41
+ "What if it were snowing?",
42
+ "Give him a leather jacket",
43
+ "Turn him into a cyborg!",
44
+ "make him wear a beanie",
45
+ ]
46
+
47
+ # model_id = "timbrooks/instruct-pix2pix"
48
+ model_id = "MudeHui/ip2p-warp-gpt4v"
49
+
50
+ def main():
51
+ pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float16, safety_checker=None).to("cuda")
52
+ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
53
+ example_image = Image.open("imgs/example.jpg").convert("RGB")
54
+
55
+ def load_example(
56
+ steps: int,
57
+ randomize_seed: bool,
58
+ seed: int,
59
+ randomize_cfg: bool,
60
+ text_cfg_scale: float,
61
+ image_cfg_scale: float,
62
+ ):
63
+ example_instruction = random.choice(example_instructions)
64
+ return [example_image, example_instruction] + generate(
65
+ example_image,
66
+ example_instruction,
67
+ steps,
68
+ randomize_seed,
69
+ seed,
70
+ randomize_cfg,
71
+ text_cfg_scale,
72
+ image_cfg_scale,
73
+ )
74
+
75
+ def generate(
76
+ input_image: Image.Image,
77
+ instruction: str,
78
+ steps: int,
79
+ randomize_seed: bool,
80
+ seed: int,
81
+ randomize_cfg: bool,
82
+ text_cfg_scale: float,
83
+ image_cfg_scale: float,
84
+ ):
85
+ seed = random.randint(0, 100000) if randomize_seed else seed
86
+ text_cfg_scale = round(random.uniform(6.0, 9.0), ndigits=2) if randomize_cfg else text_cfg_scale
87
+ image_cfg_scale = round(random.uniform(1.2, 1.8), ndigits=2) if randomize_cfg else image_cfg_scale
88
+
89
+ width, height = input_image.size
90
+ factor = 512 / max(width, height)
91
+ factor = math.ceil(min(width, height) * factor / 64) * 64 / min(width, height)
92
+ width = int((width * factor) // 64) * 64
93
+ height = int((height * factor) // 64) * 64
94
+ input_image = ImageOps.fit(input_image, (width, height), method=Image.Resampling.LANCZOS)
95
+
96
+ if instruction == "":
97
+ return [input_image, seed]
98
+
99
+ generator = torch.manual_seed(seed)
100
+ edited_image = pipe(
101
+ instruction, image=input_image,
102
+ guidance_scale=text_cfg_scale, image_guidance_scale=image_cfg_scale,
103
+ num_inference_steps=steps, generator=generator,
104
+ ).images[0]
105
+ return [seed, text_cfg_scale, image_cfg_scale, edited_image]
106
+
107
+ def reset():
108
+ return [0, "Randomize Seed", 1371, "Fix CFG", 7.5, 1.5, None]
109
+
110
+ with gr.Blocks() as demo:
111
+ gr.HTML("""<h1 style="font-weight: 900; margin-bottom: 7px;">
112
+ InstructPix2Pix: Learning to Follow Image Editing Instructions
113
+ </h1>
114
+ <p>For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings.
115
+ <br/>
116
+ <a href="https://huggingface.co/spaces/timbrooks/instruct-pix2pix?duplicate=true">
117
+ <img style="margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
118
+ <p/>""")
119
+ with gr.Row():
120
+ with gr.Column(scale=1, min_width=100):
121
+ generate_button = gr.Button("Generate")
122
+ with gr.Column(scale=1, min_width=100):
123
+ load_button = gr.Button("Load Example")
124
+ with gr.Column(scale=1, min_width=100):
125
+ reset_button = gr.Button("Reset")
126
+ with gr.Column(scale=3):
127
+ instruction = gr.Textbox(lines=1, label="Edit Instruction", interactive=True)
128
+
129
+ with gr.Row():
130
+ input_image = gr.Image(label="Input Image", type="pil", interactive=True, height=512, width=512)
131
+ edited_image = gr.Image(label=f"Edited Image", type="pil", interactive=False, height=512, width=512)
132
+ # input_image.style(height=512, width=512)
133
+ # edited_image.style(height=512, width=512)
134
+
135
+ with gr.Row():
136
+ steps = gr.Number(value=20, precision=0, label="Steps", interactive=True)
137
+ randomize_seed = gr.Radio(
138
+ ["Fix Seed", "Randomize Seed"],
139
+ value="Randomize Seed",
140
+ type="index",
141
+ show_label=False,
142
+ interactive=True,
143
+ )
144
+ seed = gr.Number(value=1371, precision=0, label="Seed", interactive=True)
145
+ randomize_cfg = gr.Radio(
146
+ ["Fix CFG", "Randomize CFG"],
147
+ value="Fix CFG",
148
+ type="index",
149
+ show_label=False,
150
+ interactive=True,
151
+ )
152
+ text_cfg_scale = gr.Number(value=7.0, label=f"Text CFG", interactive=True)
153
+ image_cfg_scale = gr.Number(value=1.5, label=f"Image CFG", interactive=True)
154
+
155
+ gr.Markdown(help_text)
156
+
157
+ load_button.click(
158
+ fn=load_example,
159
+ inputs=[
160
+ steps,
161
+ randomize_seed,
162
+ seed,
163
+ randomize_cfg,
164
+ text_cfg_scale,
165
+ image_cfg_scale,
166
+ ],
167
+ outputs=[input_image, instruction, seed, text_cfg_scale, image_cfg_scale, edited_image],
168
+ )
169
+ generate_button.click(
170
+ fn=generate,
171
+ inputs=[
172
+ input_image,
173
+ instruction,
174
+ steps,
175
+ randomize_seed,
176
+ seed,
177
+ randomize_cfg,
178
+ text_cfg_scale,
179
+ image_cfg_scale,
180
+ ],
181
+ outputs=[seed, text_cfg_scale, image_cfg_scale, edited_image],
182
+ )
183
+ reset_button.click(
184
+ fn=reset,
185
+ inputs=[],
186
+ outputs=[steps, randomize_seed, seed, randomize_cfg, text_cfg_scale, image_cfg_scale, edited_image],
187
+ )
188
+
189
+ demo.queue()
190
+ demo.launch(share=True, max_threads=1)
191
+
192
+
193
+ if __name__ == "__main__":
194
+ main()
imgs/example.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ -f --extra-index-url https://download.pytorch.org/whl/cu116
2
+ torch
3
+ torchvision
4
+ numpy
5
+ transformers
6
+ git+https://github.com/huggingface/diffusers