caohy666 commited on
Commit
ad5712e
·
1 Parent(s): e7e90f8

<feat> optimize output format and add examples

Browse files
Files changed (3) hide show
  1. .gitignore +3 -1
  2. app.py +187 -12
  3. assets/README.md +1 -0
.gitignore CHANGED
@@ -1 +1,3 @@
1
- __pycache__/
 
 
 
1
+ __pycache__/
2
+ *.jpg
3
+ *.png
app.py CHANGED
@@ -9,6 +9,8 @@ import copy
9
  import cv2
10
  import spaces
11
  import gc
 
 
12
  import gradio as gr
13
  import numpy as np
14
 
@@ -285,8 +287,167 @@ def process_image_and_text(condition_image, target_prompt, condition_image_promp
285
  out = (out * 255).astype(np.uint8)
286
  out = Image.fromarray(out)
287
  output_images.append(out)
288
-
289
- return output_images[1:] + [output_images[0]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
290
 
291
  def create_app():
292
  with gr.Blocks() as app:
@@ -315,7 +476,8 @@ def create_app():
315
  )
316
  gr.Markdown(notice, elem_id="notice")
317
  target_prompt = gr.Textbox(lines=2, label="Target Prompt", elem_id="tp")
318
- condition_image_prompt = gr.Textbox(lines=2, label="Condition Image Prompt (Only required by Subject-driven Image Generation and Style Transfer tasks)", elem_id="cp")
 
319
  random_seed = gr.Number(label="Random Seed", precision=0, value=0, elem_id="seed")
320
  num_steps = gr.Number(label="Diffusion Inference Steps", precision=0, value=50, elem_id="steps")
321
  inpainting = gr.Checkbox(label="Inpainting", value=False, elem_id="inpainting")
@@ -327,20 +489,33 @@ def create_app():
327
 
328
  with gr.Column(variant="panel", elem_classes="outputPanel"):
329
  # output_image = gr.Image(type="pil", elem_id="output")
330
- output_images = gr.Gallery(
331
- label="Output Images",
332
- show_label=True,
333
- elem_id="output_gallery",
334
- columns=1,
335
- rows=10,
336
- object_fit="contain",
337
- height="auto",
 
 
 
 
 
 
338
  )
339
 
 
 
 
 
 
 
 
340
  submit_btn.click(
341
  fn=process_image_and_text,
342
  inputs=[condition_image, target_prompt, condition_image_prompt, task, random_seed, num_steps, inpainting, fill_x1, fill_x2, fill_y1, fill_y2],
343
- outputs=output_images,
344
  )
345
 
346
  return app
 
9
  import cv2
10
  import spaces
11
  import gc
12
+ import tempfile
13
+ import imageio
14
  import gradio as gr
15
  import numpy as np
16
 
 
287
  out = (out * 255).astype(np.uint8)
288
  out = Image.fromarray(out)
289
  output_images.append(out)
290
+
291
+ # video = [np.array(img.convert('RGB')) for img in output_images[1:] + [output_images[0]]]
292
+ # video = np.stack(video, axis=0)
293
+
294
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as f:
295
+ video_path = f.name
296
+ imageio.mimsave(video_path, output_images[1:]+[output_images[0]], fps=5)
297
+
298
+ return output_images[0], video_path
299
+
300
+ def get_samples():
301
+ sample_list = [
302
+ {
303
+ "task": "subject_driven",
304
+ "input": "assets/subject_driven_image_generation_dreambench_input.jpg",
305
+ "target_prompt": "a cat in a chef outfit",
306
+ "condition_image_prompt": "a cat",
307
+ "output": "assets/subject_driven_image_generation_dreambench_output.png",
308
+ "inpainting": False,
309
+ "fill_x1": None,
310
+ "fill_x2": None,
311
+ "fill_y1": None,
312
+ "fill_y2": None,
313
+ },
314
+ {
315
+ "task": "subject_driven",
316
+ "input": "assets/subject_driven_image_generation_input.jpg",
317
+ "target_prompt": "The woman stands in a snowy forest, captured in a half-portrait",
318
+ "condition_image_prompt": "Woman in cream knit sweater sits calmly by a crackling fireplace, surrounded by warm candlelight and rustic wooden shelves",
319
+ "output": "assets/subject_driven_image_generation_output.png",
320
+ "inpainting": False,
321
+ "fill_x1": None,
322
+ "fill_x2": None,
323
+ "fill_y1": None,
324
+ "fill_y2": None,
325
+ },
326
+ {
327
+ "task": "canny",
328
+ "input": "assets/canny_to_image_input.jpg",
329
+ "target_prompt": "Mosquito frozen in clear ice cube on sand, glowing sunset casting golden light with misty halo around ice",
330
+ "condition_image_prompt": "",
331
+ "output": "assets/canny_to_image_output.png",
332
+ "inpainting": False,
333
+ "fill_x1": None,
334
+ "fill_x2": None,
335
+ "fill_y1": None,
336
+ "fill_y2": None,
337
+ },
338
+ {
339
+ "task": "coloring",
340
+ "input": "assets/colorization_input.jpg",
341
+ "target_prompt": "A vibrant young woman with rainbow glasses, yellow eyes, and colorful feather accessory against a bright yellow background",
342
+ "condition_image_prompt": "",
343
+ "output": "assets/colorization_output.png",
344
+ "inpainting": False,
345
+ "fill_x1": None,
346
+ "fill_x2": None,
347
+ "fill_y1": None,
348
+ "fill_y2": None,
349
+ },
350
+ {
351
+ "task": "deblurring",
352
+ "input": "assets/deblurring_input.jpg",
353
+ "target_prompt": "Vibrant rainbow ball creates dramatic splash in clear water, bubbles swirling against crisp white background",
354
+ "condition_image_prompt": "",
355
+ "output": "assets/deblurring_output.png",
356
+ "inpainting": False,
357
+ "fill_x1": None,
358
+ "fill_x2": None,
359
+ "fill_y1": None,
360
+ "fill_y2": None,
361
+ },
362
+ {
363
+ "task": "depth",
364
+ "input": "assets/depth_to_image_input.jpg",
365
+ "target_prompt": "Golden-brown cat-shaped bread loaf with closed eyes rests on wooden table, soft kitchen blur in background",
366
+ "condition_image_prompt": "",
367
+ "output": "assets/depth_to_image_output.png",
368
+ "inpainting": False,
369
+ "fill_x1": None,
370
+ "fill_x2": None,
371
+ "fill_y1": None,
372
+ "fill_y2": None,
373
+ },
374
+ {
375
+ "task": "depth_pred",
376
+ "input": "assets/depth_prediction_input.jpg",
377
+ "target_prompt": "Steaming bowl of ramen with pork slices, soft-boiled egg, greens, and scallions in rich broth on wooden table",
378
+ "condition_image_prompt": "",
379
+ "output": "assets/depth_prediction_output.png",
380
+ "inpainting": False,
381
+ "fill_x1": None,
382
+ "fill_x2": None,
383
+ "fill_y1": None,
384
+ "fill_y2": None,
385
+ },
386
+ {
387
+ "task": "fill",
388
+ "input": "assets/inpainting_input.jpg",
389
+ "target_prompt": "Mona Lisa dons a medical mask, her enigmatic smile now concealed beneath crisp white fabric",
390
+ "condition_image_prompt": "",
391
+ "output": "assets/inpainting_output.png",
392
+ "inpainting": True,
393
+ "fill_x1": 170,
394
+ "fill_x2": 300,
395
+ "fill_y1": 190,
396
+ "fill_y2": 290,
397
+ },
398
+ {
399
+ "task": "fill",
400
+ "input": "assets/outpainting_input.jpg",
401
+ "target_prompt": "Her left hand emerges at the frame's lower right, delicately cradling a vibrant red flower against the black void",
402
+ "condition_image_prompt": "",
403
+ "output": "assets/outpainting_output.png",
404
+ "inpainting": False,
405
+ "fill_x1": 155,
406
+ "fill_x2": 512,
407
+ "fill_y1": 0,
408
+ "fill_y2": 330,
409
+ },
410
+ {
411
+ "task": "sr",
412
+ "input": "assets/super_resolution_input.jpg",
413
+ "target_prompt": "Crispy buffalo wings and golden fries rest on a red-and-white checkered paper lining a gleaming metal tray, with creamy dip",
414
+ "condition_image_prompt": "",
415
+ "output": "assets/super_resolution_output.png",
416
+ "inpainting": False,
417
+ "fill_x1": None,
418
+ "fill_x2": None,
419
+ "fill_y1": None,
420
+ "fill_y2": None,
421
+ },
422
+ {
423
+ "task": "style_transfer",
424
+ "input": "assets/style_transfer_input.png",
425
+ "target_prompt": "bitmoji style. An orange cat sits quietly on the stone slab. Beside it are the green grasses. With its ears perked up, it looks to one side.",
426
+ "condition_image_prompt": "An orange cat sits quietly on the stone slab. Beside it are the green grasses. With its ears perked up, it looks to one side.",
427
+ "output": "assets/style_transfer_output.png",
428
+ "inpainting": False,
429
+ "fill_x1": None,
430
+ "fill_x2": None,
431
+ "fill_y1": None,
432
+ "fill_y2": None,
433
+ },
434
+ ]
435
+
436
+ return [
437
+ [
438
+ sample['task'],
439
+ Image.open(sample['input']),
440
+ sample['target_prompt'],
441
+ sample['condition_image_prompt'],
442
+ Image.open(sample['output']),
443
+ sample['inpainting'],
444
+ sample['fill_x1'],
445
+ sample['fill_x2'],
446
+ sample['fill_y1'],
447
+ sample['fill_y2'],
448
+ ]
449
+ for sample in sample_list
450
+ ]
451
 
452
  def create_app():
453
  with gr.Blocks() as app:
 
476
  )
477
  gr.Markdown(notice, elem_id="notice")
478
  target_prompt = gr.Textbox(lines=2, label="Target Prompt", elem_id="tp")
479
+ gr.Markdown("**Condition Image Prompt** _(Only required by Subject-driven Image Generation and Style Transfer tasks)_")
480
+ condition_image_prompt = gr.Textbox(lines=2, label="Condition Image Prompt", elem_id="cp")
481
  random_seed = gr.Number(label="Random Seed", precision=0, value=0, elem_id="seed")
482
  num_steps = gr.Number(label="Diffusion Inference Steps", precision=0, value=50, elem_id="steps")
483
  inpainting = gr.Checkbox(label="Inpainting", value=False, elem_id="inpainting")
 
489
 
490
  with gr.Column(variant="panel", elem_classes="outputPanel"):
491
  # output_image = gr.Image(type="pil", elem_id="output")
492
+ # output_images = gr.Gallery(
493
+ # label="Output Images",
494
+ # show_label=True,
495
+ # elem_id="output_gallery",
496
+ # columns=1,
497
+ # rows=10,
498
+ # object_fit="contain",
499
+ # height="auto",
500
+ # )
501
+ output_image = gr.Image(
502
+ type="pil", label="Output Image", elem_id="output_image"
503
+ )
504
+ output_video = gr.Video(
505
+ label="Output Video", elem_id="output_video"
506
  )
507
 
508
+ with gr.Row():
509
+ examples = gr.Examples(
510
+ examples=get_samples(),
511
+ inputs=[task, condition_image, target_prompt, condition_image_prompt, output_image, inpainting, fill_x1, fill_x2, fill_y1, fill_y2],
512
+ label="Examples",
513
+ )
514
+
515
  submit_btn.click(
516
  fn=process_image_and_text,
517
  inputs=[condition_image, target_prompt, condition_image_prompt, task, random_seed, num_steps, inpainting, fill_x1, fill_x2, fill_y1, fill_y2],
518
+ outputs=[output_image, output_video],
519
  )
520
 
521
  return app
assets/README.md ADDED
@@ -0,0 +1 @@
 
 
1
+ Here are some examples.