Linoy Tsaban commited on
Commit
bd0e99e
·
1 Parent(s): 4dfe276

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -24
app.py CHANGED
@@ -24,7 +24,11 @@ import gc
24
  import random
25
  from time import sleep
26
 
27
- with open("sdxl_loras.json", "r") as file:
 
 
 
 
28
  data = json.load(file)
29
  sdxl_loras_raw = [
30
  {
@@ -147,16 +151,24 @@ def shuffle_lora(sdxl_loras, selected_lora=None):
147
 
148
  def check_if_removed(input_image):
149
  if(input_image is None):
150
- return gr.Row(visible=False), gr.Column(elem_classes="output_column"), gr.Image(value=None), gr.State(value=None), gr.Button(visible=False)
151
  else:
152
- return gr.Row(), gr.Column(), gr.Image(), None, gr.Button()
153
 
154
  def block_if_removed(input_image):
155
  if(input_image is None):
156
  raise gr.Warning("Photo removed. Upload a new one!")
157
 
158
- def select_lora(selected_state: gr.SelectData):
159
- return selected_state.index
 
 
 
 
 
 
 
 
160
 
161
 
162
  def edit(sdxl_loras,
@@ -186,7 +198,7 @@ def edit(sdxl_loras,
186
  source_prompt= src_prompt,
187
  # source_prompt_2 = None,
188
  source_guidance_scale = src_cfg_scale,
189
- negative_prompt = "blurry, ugly, bad quality",
190
  # negative_prompt_2 = None,
191
  num_inversion_steps = steps,
192
  skip_steps = skip,
@@ -226,7 +238,7 @@ def edit(sdxl_loras,
226
  #lora_desc = f"### LoRA Used To Edit this Image: {lora_repo}' }"
227
  #lora_image = sdxl_loras[random_lora_index]["image"]
228
 
229
- return sega_out.images[0], wts, zs, do_inversion, gr.update(height=512), gr.Column(elem_classes="output_column_reverse"), gr.Button(visible=True)
230
 
231
 
232
 
@@ -257,7 +269,16 @@ def crop_image(image):
257
  return image
258
 
259
 
260
-
 
 
 
 
 
 
 
 
 
261
 
262
  ########r
263
  # demo #
@@ -288,24 +309,25 @@ with gr.Blocks(css="style.css") as demo:
288
  lora_image = gr.Image(interactive=False, height=128, width=128, elem_id="lora_image", show_label=False, show_download_button=False)
289
  lora_desc = gr.Markdown()
290
  sega_edited_image = gr.Image(label=f"LEDITS Edited Image", interactive=False, elem_id="output_image", height=512, width=512)
 
 
 
 
291
 
292
 
293
-
294
-
295
- with gr.Row():
296
- run_button = gr.Button("Rerun with the same picture", elem_id="run_again", visible=False)
297
- with gr.Accordion("Tired of randomizing? Pick your LoRA", open=False, elem_id="pick"):
298
- choose_gallery = gr.Gallery(
299
- value=[(item["image"], item["title"]) for item in sdxl_loras_raw],
300
- allow_preview=False,
301
- columns=6,
302
- elem_id="gallery",
303
- show_share_button=False
304
- )
305
 
306
  choose_gallery.select(
307
  fn=select_lora,
308
- outputs=[gr_picked_lora],
 
309
  queue=False
310
  )
311
 
@@ -322,12 +344,17 @@ with gr.Blocks(css="style.css") as demo:
322
  do_inversion,
323
  gr_lora_index
324
  ],
325
- outputs=[sega_edited_image, wts, zs, do_inversion, sega_edited_image, output_column, run_button])
 
 
 
 
 
326
 
327
  input_image.change(
328
  fn = check_if_removed,
329
  inputs = [input_image],
330
- outputs = [loaded_lora, output_column, sega_edited_image, gr_picked_lora, run_button],
331
  queue=False,
332
  show_progress=False
333
  ).then(
@@ -351,7 +378,11 @@ with gr.Blocks(css="style.css") as demo:
351
  do_inversion,
352
  gr_lora_index
353
  ],
354
- outputs=[sega_edited_image, wts, zs, do_inversion, sega_edited_image, output_column, run_button]
 
 
 
 
355
  )
356
  gr.HTML('''
357
  <h1>Hugging Face at</h1>
 
24
  import random
25
  from time import sleep
26
 
27
+ IMAGE_DATASET_DIR = Path("image_dataset") / f"train-{uuid4()}"
28
+ IMAGE_DATASET_DIR.mkdir(parents=True, exist_ok=True)
29
+ IMAGE_JSONL_PATH = IMAGE_DATASET_DIR / "metadata.jsonl"
30
+
31
+ with open("sdxl_loras-Copy1.json", "r") as file:
32
  data = json.load(file)
33
  sdxl_loras_raw = [
34
  {
 
151
 
152
  def check_if_removed(input_image):
153
  if(input_image is None):
154
+ return gr.Row(visible=False), gr.Column(elem_classes="output_column"), gr.Image(value=None), gr.State(value=None), gr.Column(visible=False)
155
  else:
156
+ return gr.Row(), gr.Column(), gr.Image(), None, gr.Column()
157
 
158
  def block_if_removed(input_image):
159
  if(input_image is None):
160
  raise gr.Warning("Photo removed. Upload a new one!")
161
 
162
+ def select_lora(selected_state: gr.SelectData, sdxl_loras):
163
+ random_lora_index = selected_state.index
164
+ lora_repo = sdxl_loras[random_lora_index]["repo"]
165
+ lora_title = sdxl_loras[random_lora_index]["title"]
166
+ lora_desc = f"""#### LoRA used to edit this image:
167
+ ## {lora_title}
168
+ by `{lora_repo.split('/')[0]}`
169
+ """
170
+ lora_image = sdxl_loras[random_lora_index]["image"]
171
+ return random_lora_index, lora_image, lora_desc
172
 
173
 
174
  def edit(sdxl_loras,
 
198
  source_prompt= src_prompt,
199
  # source_prompt_2 = None,
200
  source_guidance_scale = src_cfg_scale,
201
+ negative_prompt = "blurry, bad quality",
202
  # negative_prompt_2 = None,
203
  num_inversion_steps = steps,
204
  skip_steps = skip,
 
238
  #lora_desc = f"### LoRA Used To Edit this Image: {lora_repo}' }"
239
  #lora_image = sdxl_loras[random_lora_index]["image"]
240
 
241
+ return sega_out.images[0], wts, zs, do_inversion, gr.update(height=512), gr.Column(elem_classes="output_column_reverse"), gr.Row(visible=True)
242
 
243
 
244
 
 
269
  return image
270
 
271
 
272
+ def save_preferences(sdxl_loras, selected_lora, input_image, result_image):
273
+ lora_id = sdxl_loras[selected_lora]["repo"]
274
+ uuid = uuid4()
275
+ input_image_path = IMAGE_DATASET_DIR / f"{uuid}-input.png"
276
+ output_image_path = IMAGE_DATASET_DIR / f"{uuid}-output.png"
277
+ Image.fromarray(input_image).save(input_image_path)
278
+ Image.fromarray(result_image).save(output_image_path)
279
+ with IMAGE_JSONL_PATH.open("a") as f:
280
+ json.dump({"selected_lora": lora_id, "input_image":input_image_path.name, "result_image":output_image_path.name}, f)
281
+ f.write("\n")
282
 
283
  ########r
284
  # demo #
 
309
  lora_image = gr.Image(interactive=False, height=128, width=128, elem_id="lora_image", show_label=False, show_download_button=False)
310
  lora_desc = gr.Markdown()
311
  sega_edited_image = gr.Image(label=f"LEDITS Edited Image", interactive=False, elem_id="output_image", height=512, width=512)
312
+ with gr.Column(visible=False) as buttons_area:
313
+ with gr.Row(elem_id="buttons_area"):
314
+ print_button = gr.HTML('<button onclick="window.print()" class="lg secondary svelte-cmf5ev" id="print" style="width: 100%;margin-bottom:0">Print 🖨️</button>')
315
+ run_button = gr.Button("Regenerate with the same picture 🖼️🎲", elem_id="run_again")
316
 
317
 
318
+ with gr.Accordion("Tired of randomizing? Pick your LoRA", open=False, elem_id="pick", ):
319
+ choose_gallery = gr.Gallery(
320
+ value=[(item["image"], item["title"]) for item in sdxl_loras_raw],
321
+ allow_preview=False,
322
+ columns=6,
323
+ elem_id="gallery",
324
+ show_share_button=False
325
+ )
 
 
 
 
326
 
327
  choose_gallery.select(
328
  fn=select_lora,
329
+ inputs=gr_sdxl_loras,
330
+ outputs=[gr_picked_lora, lora_image, lora_desc],
331
  queue=False
332
  )
333
 
 
344
  do_inversion,
345
  gr_lora_index
346
  ],
347
+ outputs=[sega_edited_image, wts, zs, do_inversion, sega_edited_image, output_column, buttons_area]
348
+ ).then(
349
+ fn=save_preferences,
350
+ inputs=[gr_sdxl_loras, gr_lora_index, input_image, sega_edited_image],
351
+ queue=False
352
+ )
353
 
354
  input_image.change(
355
  fn = check_if_removed,
356
  inputs = [input_image],
357
+ outputs = [loaded_lora, output_column, sega_edited_image, gr_picked_lora, buttons_area],
358
  queue=False,
359
  show_progress=False
360
  ).then(
 
378
  do_inversion,
379
  gr_lora_index
380
  ],
381
+ outputs=[sega_edited_image, wts, zs, do_inversion, sega_edited_image, output_column, buttons_area]
382
+ ).then(
383
+ fn=save_preferences,
384
+ inputs=[gr_sdxl_loras, gr_lora_index, input_image, sega_edited_image],
385
+ queue=False
386
  )
387
  gr.HTML('''
388
  <h1>Hugging Face at</h1>