hysts HF staff commited on
Commit
39f6ea0
1 Parent(s): d8b3099
Files changed (1) hide show
  1. edit_app.py +52 -26
edit_app.py CHANGED
@@ -53,42 +53,28 @@ pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(
53
  example_image = Image.open("imgs/example.jpg").convert("RGB")
54
 
55
 
56
- def load_example(
57
- steps: int,
58
  randomize_seed: bool,
59
  seed: int,
60
  randomize_cfg: bool,
61
  text_cfg_scale: float,
62
  image_cfg_scale: float,
63
- ):
64
- example_instruction = random.choice(example_instructions)
65
- return [example_image, example_instruction] + generate(
66
- example_image,
67
- example_instruction,
68
- steps,
69
- randomize_seed,
70
- seed,
71
- randomize_cfg,
72
- text_cfg_scale,
73
- image_cfg_scale,
74
- )
75
 
76
 
77
  def generate(
78
  input_image: Image.Image,
79
  instruction: str,
80
  steps: int,
81
- randomize_seed: bool,
82
  seed: int,
83
- randomize_cfg: bool,
84
  text_cfg_scale: float,
85
  image_cfg_scale: float,
86
  progress=gr.Progress(track_tqdm=True),
87
- ):
88
- seed = random.randint(0, 100000) if randomize_seed else seed
89
- text_cfg_scale = round(random.uniform(6.0, 9.0), ndigits=2) if randomize_cfg else text_cfg_scale
90
- image_cfg_scale = round(random.uniform(1.2, 1.8), ndigits=2) if randomize_cfg else image_cfg_scale
91
-
92
  width, height = input_image.size
93
  factor = 512 / max(width, height)
94
  factor = math.ceil(min(width, height) * factor / 64) * 64 / min(width, height)
@@ -108,7 +94,37 @@ def generate(
108
  num_inference_steps=steps,
109
  generator=generator,
110
  ).images[0]
111
- return [seed, text_cfg_scale, image_cfg_scale, edited_image]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
 
114
  def reset():
@@ -116,7 +132,7 @@ def reset():
116
 
117
 
118
  def process_example(input_image: Image.Image, instruction: str, seed: int) -> Image.Image:
119
- return generate(input_image, instruction, 50, False, seed, False, 7.5, 1.5)[-1]
120
 
121
 
122
  with gr.Blocks() as demo:
@@ -214,18 +230,28 @@ InstructPix2Pix: Learning to Follow Image Editing Instructions
214
  text_cfg_scale.submit,
215
  image_cfg_scale.submit,
216
  ],
 
 
 
 
 
 
 
 
 
 
 
 
217
  fn=generate,
218
  inputs=[
219
  input_image,
220
  instruction,
221
  steps,
222
- randomize_seed,
223
  seed,
224
- randomize_cfg,
225
  text_cfg_scale,
226
  image_cfg_scale,
227
  ],
228
- outputs=[seed, text_cfg_scale, image_cfg_scale, edited_image],
229
  api_name="run",
230
  )
231
 
 
53
  example_image = Image.open("imgs/example.jpg").convert("RGB")
54
 
55
 
56
+ def randomize(
 
57
  randomize_seed: bool,
58
  seed: int,
59
  randomize_cfg: bool,
60
  text_cfg_scale: float,
61
  image_cfg_scale: float,
62
+ ) -> tuple[int, float, float]:
63
+ seed = random.randint(0, 100000) if randomize_seed else seed
64
+ text_cfg_scale = round(random.uniform(6.0, 9.0), ndigits=2) if randomize_cfg else text_cfg_scale
65
+ image_cfg_scale = round(random.uniform(1.2, 1.8), ndigits=2) if randomize_cfg else image_cfg_scale
66
+ return seed, text_cfg_scale, image_cfg_scale
 
 
 
 
 
 
 
67
 
68
 
69
  def generate(
70
  input_image: Image.Image,
71
  instruction: str,
72
  steps: int,
 
73
  seed: int,
 
74
  text_cfg_scale: float,
75
  image_cfg_scale: float,
76
  progress=gr.Progress(track_tqdm=True),
77
+ ) -> Image.Image:
 
 
 
 
78
  width, height = input_image.size
79
  factor = 512 / max(width, height)
80
  factor = math.ceil(min(width, height) * factor / 64) * 64 / min(width, height)
 
94
  num_inference_steps=steps,
95
  generator=generator,
96
  ).images[0]
97
+ return edited_image
98
+
99
+
100
+ def load_example(
101
+ steps: int,
102
+ randomize_seed: bool,
103
+ seed: int,
104
+ randomize_cfg: bool,
105
+ text_cfg_scale: float,
106
+ image_cfg_scale: float,
107
+ progress=gr.Progress(track_tqdm=True),
108
+ ):
109
+ example_instruction = random.choice(example_instructions)
110
+ seed, text_cfg_scale, image_cfg_scale = randomize(
111
+ randomize_seed, seed, randomize_cfg, text_cfg_scale, image_cfg_scale
112
+ )
113
+ return [
114
+ example_image,
115
+ example_instruction,
116
+ seed,
117
+ text_cfg_scale,
118
+ image_cfg_scale,
119
+ generate(
120
+ example_image,
121
+ example_instruction,
122
+ steps,
123
+ seed,
124
+ text_cfg_scale,
125
+ image_cfg_scale,
126
+ ),
127
+ ]
128
 
129
 
130
  def reset():
 
132
 
133
 
134
  def process_example(input_image: Image.Image, instruction: str, seed: int) -> Image.Image:
135
+ return generate(input_image, instruction, 50, seed, 7.5, 1.5)
136
 
137
 
138
  with gr.Blocks() as demo:
 
230
  text_cfg_scale.submit,
231
  image_cfg_scale.submit,
232
  ],
233
+ fn=randomize,
234
+ inputs=[
235
+ randomize_seed,
236
+ seed,
237
+ randomize_cfg,
238
+ text_cfg_scale,
239
+ image_cfg_scale,
240
+ ],
241
+ outputs=[seed, text_cfg_scale, image_cfg_scale],
242
+ queue=False,
243
+ api_name=False,
244
+ ).then(
245
  fn=generate,
246
  inputs=[
247
  input_image,
248
  instruction,
249
  steps,
 
250
  seed,
 
251
  text_cfg_scale,
252
  image_cfg_scale,
253
  ],
254
+ outputs=edited_image,
255
  api_name="run",
256
  )
257