rinong commited on
Commit
1edbcae
1 Parent(s): 96b8eee

Changed blocks format to tabs

Browse files
Files changed (1) hide show
  1. app.py +62 -60
app.py CHANGED
@@ -128,18 +128,7 @@ class ImageEditor(object):
128
 
129
  return style_list
130
 
131
- def predict(
132
- self,
133
- input, # Input image path
134
- output_styles, # Which output style do you want to use?
135
- generate_video, # Generate a video instead of an output image
136
- with_editing, # Apply latent space editing to the generated video
137
- video_format # Choose gif to display in browser, mp4 for higher-quality downloadable video
138
- ):
139
-
140
- styles = output_styles
141
-
142
- # @title Align image
143
  input_image = self.run_alignment(str(input))
144
 
145
  input_image = input_image.resize(self.resize_dims)
@@ -152,10 +141,32 @@ class ImageEditor(object):
152
  result_image, latent = images[0], latents[0]
153
 
154
  inverted_latent = latent.unsqueeze(0).unsqueeze(1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  out_dir = Path(tempfile.mkdtemp())
156
  out_path = out_dir / "out.jpg"
157
 
158
- generators = [self.generators[style] for style in styles]
 
159
 
160
  if not generate_video:
161
  with torch.no_grad():
@@ -232,54 +243,22 @@ with blocks:
232
  "For more information about the paper and code for training your own models (with examples OR text), see below."
233
  )
234
 
235
- with gr.Row():
236
-
237
- with gr.Column():
238
- input_img = gr.inputs.Image(type="filepath", label="Input image")
239
- style_choice = gr.inputs.CheckboxGroup(choices=editor.get_style_list(), type="value", label="Choose your styles!")
240
-
241
- video_choice = gr.inputs.Checkbox(default=False, label="Generate Video?", optional=False)
242
-
243
- video_options_group = gr.Group()
244
-
245
- with video_options_group:
246
- edit_choice = gr.inputs.Checkbox(default=False, label="With Editing?", optional=False)
247
- vid_format_choice = gr.inputs.Radio(choices=["gif", "mp4"], type="value", default='mp4', label="Video Format")
248
-
249
- # img_button = gr.Button("Edit Image")
250
- # vid_button = gr.Button("Generate Video")
251
- img_button = gr.Button("Edit Image")
252
- vid_button = gr.Button("Generate Video")
253
-
254
- with gr.Column():
255
- img_output = gr.outputs.Image(type="file")
256
- vid_output = gr.outputs.Video()
257
-
258
- # visibility_fn = change_component_visibility(component_types=[gr.Checkbox, gr.Radio, gr.Video, gr.Button, gr.Image, gr.Button],
259
- # invert_choices=[False, False, False, False, True, True])
260
-
261
- # video_choice.change(fn=visibility_fn, inputs=video_choice, outputs=[edit_choice, vid_format_choice, vid_output, vid_button, img_output, img_button])
262
- video_choice.change(fn=group_visibility, inputs=video_choice, outputs=video_options_group)
263
- img_button.click(fn=editor.predict, inputs=[input_img, style_choice, video_choice, edit_choice, vid_format_choice], outputs=img_output)
264
- vid_button.click(fn=editor.predict, inputs=[input_img, style_choice, video_choice, edit_choice, vid_format_choice], outputs=vid_output)
265
-
266
- # input_img = gr.inputs.Image(type="filepath", label="Input image")
267
- # style_choice = gr.inputs.CheckboxGroup(choices=editor.get_style_list(), type="value", label="Choose your styles!")
268
-
269
-
270
 
271
- # with gr.Tabs():
272
- # with gr.TabItem("Edit Images"):
273
- # with gr.Row():
274
- # with gr.Column():
275
-
276
-
277
 
278
  # video_choice = gr.inputs.Checkbox(default=False, label="Generate Video?", optional=False)
279
 
280
- # edit_choice = gr.inputs.Checkbox(default=False, label="With Editing?", optional=False)
281
- # vid_format_choice = gr.inputs.Radio(choices=["gif", "mp4"], type="value", default='mp4', label="Video Format")
 
 
 
282
 
 
 
283
  # img_button = gr.Button("Edit Image")
284
  # vid_button = gr.Button("Generate Video")
285
 
@@ -287,13 +266,36 @@ with blocks:
287
  # img_output = gr.outputs.Image(type="file")
288
  # vid_output = gr.outputs.Video()
289
 
290
- # visibility_fn = change_component_visibility(component_types=[gr.Checkbox, gr.Radio, gr.Video, gr.Button, gr.Image, gr.Button],
291
- # invert_choices=[False, False, False, False, True, True])
292
-
293
- # video_choice.change(fn=visibility_fn, inputs=video_choice, outputs=[edit_choice, vid_format_choice, vid_output, vid_button, img_output, img_button])
294
 
 
 
295
  # img_button.click(fn=editor.predict, inputs=[input_img, style_choice, video_choice, edit_choice, vid_format_choice], outputs=img_output)
296
  # vid_button.click(fn=editor.predict, inputs=[input_img, style_choice, video_choice, edit_choice, vid_format_choice], outputs=vid_output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
 
298
  article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2108.00946' target='_blank'>StyleGAN-NADA: CLIP-Guided Domain Adaptation of Image Generators</a> | <a href='https://stylegan-nada.github.io/' target='_blank'>Project Page</a> | <a href='https://github.com/rinongal/StyleGAN-nada' target='_blank'>Code</a></p> <center><img src='https://visitor-badge.glitch.me/badge?page_id=rinong_sgnada' alt='visitor badge'></center>"
299
  gr.Markdown(article)
 
128
 
129
  return style_list
130
 
131
+ def invert_image(self, input_image):
 
 
 
 
 
 
 
 
 
 
 
132
  input_image = self.run_alignment(str(input))
133
 
134
  input_image = input_image.resize(self.resize_dims)
 
141
  result_image, latent = images[0], latents[0]
142
 
143
  inverted_latent = latent.unsqueeze(0).unsqueeze(1)
144
+
145
+ return inverted_latent
146
+
147
+ def get_generators_for_styles(self, output_styles):
148
+ return [self.generators[style] for style in output_styles]
149
+
150
+ def edit_image(self, input, output_styles):
151
+ return self.predict(input, output_styles, False, False, None)
152
+
153
+ def edit_video(self, input, output_styles, with_editing, video_format):
154
+ return self.predict(input, output_styles, True, with_editing, video_format)
155
+
156
+ def predict(
157
+ self,
158
+ input, # Input image path
159
+ output_styles, # Which output style do you want to use?
160
+ generate_video, # Generate a video instead of an output image
161
+ with_editing, # Apply latent space editing to the generated video
162
+ video_format # Choose gif to display in browser, mp4 for higher-quality downloadable video
163
+ ):
164
+ # @title Align image
165
  out_dir = Path(tempfile.mkdtemp())
166
  out_path = out_dir / "out.jpg"
167
 
168
+ inverted_latent = self.invert_image(input)
169
+ generators = self.get_generators_for_styles(output_styles)
170
 
171
  if not generate_video:
172
  with torch.no_grad():
 
243
  "For more information about the paper and code for training your own models (with examples OR text), see below."
244
  )
245
 
246
+ # with gr.Row():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
 
248
+ # with gr.Column():
249
+ # input_img = gr.inputs.Image(type="filepath", label="Input image")
250
+ # style_choice = gr.inputs.CheckboxGroup(choices=editor.get_style_list(), type="value", label="Choose your styles!")
 
 
 
251
 
252
  # video_choice = gr.inputs.Checkbox(default=False, label="Generate Video?", optional=False)
253
 
254
+ # video_options_group = gr.Group()
255
+
256
+ # with video_options_group:
257
+ # edit_choice = gr.inputs.Checkbox(default=False, label="With Editing?", optional=False)
258
+ # vid_format_choice = gr.inputs.Radio(choices=["gif", "mp4"], type="value", default='mp4', label="Video Format")
259
 
260
+ # # img_button = gr.Button("Edit Image")
261
+ # # vid_button = gr.Button("Generate Video")
262
  # img_button = gr.Button("Edit Image")
263
  # vid_button = gr.Button("Generate Video")
264
 
 
266
  # img_output = gr.outputs.Image(type="file")
267
  # vid_output = gr.outputs.Video()
268
 
269
+ # # visibility_fn = change_component_visibility(component_types=[gr.Checkbox, gr.Radio, gr.Video, gr.Button, gr.Image, gr.Button],
270
+ # # invert_choices=[False, False, False, False, True, True])
 
 
271
 
272
+ # # video_choice.change(fn=visibility_fn, inputs=video_choice, outputs=[edit_choice, vid_format_choice, vid_output, vid_button, img_output, img_button])
273
+ # video_choice.change(fn=group_visibility, inputs=video_choice, outputs=video_options_group)
274
  # img_button.click(fn=editor.predict, inputs=[input_img, style_choice, video_choice, edit_choice, vid_format_choice], outputs=img_output)
275
  # vid_button.click(fn=editor.predict, inputs=[input_img, style_choice, video_choice, edit_choice, vid_format_choice], outputs=vid_output)
276
+
277
+ input_img = gr.inputs.Image(type="filepath", label="Input image")
278
+
279
+ with gr.Row():
280
+ style_choice = gr.inputs.CheckboxGroup(choices=editor.get_style_list(), type="value", label="Choose your styles!")
281
+
282
+ with gr.Tabs():
283
+ with gr.TabItem("Edit Images"):
284
+ with gr.Row():
285
+ img_button = gr.Button("Edit Image")
286
+ img_output = gr.outputs.Image(type="file")
287
+
288
+ with gr.TabItem("Create Video"):
289
+ with gr.Row():
290
+ edit_choice = gr.inputs.Checkbox(default=False, label="With latent space editing?", optional=False)
291
+ vid_format_choice = gr.inputs.Radio(choices=["gif", "mp4"], type="value", default='mp4', label="Video Format")
292
+
293
+ with gr.Row():
294
+ vid_button = gr.Button("Generate Video")
295
+ vid_output = gr.outputs.Video()
296
+
297
+ img_button.click(fn=editor.edit_image, inputs=[input_img, style_choice], outputs=img_output)
298
+ vid_button.click(fn=editor.edit_video, inputs=[input_img, style_choice, edit_choice, vid_format_choice], outputs=vid_output)
299
 
300
  article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2108.00946' target='_blank'>StyleGAN-NADA: CLIP-Guided Domain Adaptation of Image Generators</a> | <a href='https://stylegan-nada.github.io/' target='_blank'>Project Page</a> | <a href='https://github.com/rinongal/StyleGAN-nada' target='_blank'>Code</a></p> <center><img src='https://visitor-badge.glitch.me/badge?page_id=rinong_sgnada' alt='visitor badge'></center>"
301
  gr.Markdown(article)