yizhangliu commited on
Commit
e56f195
1 Parent(s): 80ad95f

update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -17
app.py CHANGED
@@ -129,10 +129,7 @@ ram_model = None
129
  kosmos_model = None
130
  kosmos_processor = None
131
 
132
- brush_color = "#00FFFF"
133
-
134
  MAX_SEED = np.iinfo(np.int32).max
135
- MAX_IMAGE_SIZE = 1024
136
 
137
  def load_model_hf(model_config_path, repo_id, filename, device='cpu'):
138
  args = SLConfig.fromfile(model_config_path)
@@ -419,7 +416,7 @@ def lama_cleaner_process(image, mask, cleaner_size_limit=1080):
419
 
420
  logger.info(f'_______lama_cleaner_process_______9____')
421
  if config.sd_seed == -1:
422
- config.sd_seed = random.randint(1, 999999999)
423
 
424
  # logger.info(f"Origin image shape_0_: {original_shape} / {size_limit}")
425
  logger.info(f'_______lama_cleaner_process_______10____')
@@ -618,29 +615,53 @@ def get_time_cost(run_task_time, time_cost_str):
618
  def load_kolors_inpainting(inpaint_prompt, input_image, mask_image):
619
  from gradio_client import Client, handle_file
620
  import tempfile
 
621
  try:
 
 
 
622
  job_image = {}
623
  if 'background' in input_image.keys():
 
 
 
 
 
 
 
 
 
 
 
 
624
  _, temp_file_path = tempfile.mkstemp(suffix='.png')
625
- img = input_image['background'].convert("RGB")
626
  img.save(temp_file_path)
 
627
  job_image["background"] = handle_file(temp_file_path)
628
  if 'layers' in input_image.keys() and len(input_image['layers']) > 0:
629
  _, temp_file_path = tempfile.mkstemp(suffix='.png')
630
- img = input_image['layers'][0].convert("RGB")
631
  img.save(temp_file_path)
 
632
  job_image["layers"] = [handle_file(temp_file_path)]
 
633
  if 'composite' in input_image.keys():
634
  _, temp_file_path = tempfile.mkstemp(suffix='.png')
635
- img = input_image['composite'].convert("RGB")
636
  img.save(temp_file_path)
 
637
  job_image["composite"] = handle_file(temp_file_path)
638
 
639
  _, temp_file_path = tempfile.mkstemp(suffix='.png')
640
- img = mask_image.convert("RGB")
641
  img.save(temp_file_path)
 
642
  job_mask_image = handle_file(temp_file_path)
643
 
 
 
 
644
  client = Client("Kwai-Kolors/Kolors-Inpainting")
645
 
646
  job = client.submit(
@@ -648,7 +669,7 @@ def load_kolors_inpainting(inpaint_prompt, input_image, mask_image):
648
  image=job_image,
649
  mask_image=job_mask_image,
650
  negative_prompt="broken fingers, deformed fingers, deformed hands, stumps, blurriness, low quality",
651
- seed=0,
652
  randomize_seed=True,
653
  guidance_scale=6,
654
  num_inference_steps=25,
@@ -657,7 +678,12 @@ def load_kolors_inpainting(inpaint_prompt, input_image, mask_image):
657
  while not job.done():
658
  time.sleep(0.1)
659
 
660
- result = job.outputs()[0]
 
 
 
 
 
661
  im = Image.open(result)
662
  if im.mode == "RGBA":
663
  im.load()
@@ -839,8 +865,8 @@ def run_anything_task(input_image, text_prompt, task_type, inpaint_prompt, box_t
839
 
840
  if task_type in ['inpainting', 'outpainting']:
841
  # inpainting pipeline
842
- image_source_for_inpaint = image_pil.resize((512, 512))
843
- image_mask_for_inpaint = mask_pil.resize((512, 512))
844
  if task_type in ['outpainting']:
845
  # reverse mask
846
  img_arr = np.array(image_mask_for_inpaint)
@@ -973,23 +999,26 @@ def main_gradio(args):
973
  task_types.append("segment")
974
  if inpainting_enable:
975
  task_types.append("inpainting")
976
- task_types.append("outpainting")
977
  if lama_cleaner_enable:
978
  task_types.append("remove")
979
  if ram_enable:
980
  task_types.append("relate anything")
981
  if kosmos_enable:
982
  task_types.append("Kosmos-2")
983
-
984
- input_image = gr.ImageMask(sources='upload', elem_id="image_upload", type='pil', label="Upload",
985
- brush=gr.Brush(colors=[brush_color], color_mode="fixed"))
 
 
 
986
  task_type = gr.Radio(task_types, value="detection",
987
  label='Task type', visible=True)
988
  mask_source_radio = gr.Radio([mask_source_draw, mask_source_segment],
989
  value=mask_source_segment, label="Mask from",
990
  visible=False)
991
  text_prompt = gr.Textbox(label="Detection Prompt[To detect multiple objects, seperating each with '.', like this: cat . dog . chair ]", placeholder="Cannot be empty")
992
- inpaint_prompt = gr.Textbox(label="Inpaint/Outpaint Prompt (if this is empty, then remove)", visible=False)
993
  num_relation = gr.Slider(label="How many relations do you want to see", minimum=1, maximum=20, value=5, step=1, visible=False)
994
 
995
  kosmos_input = gr.Radio(["Brief", "Detailed"], label="Kosmos Description Type", value="Brief", visible=False)
 
129
  kosmos_model = None
130
  kosmos_processor = None
131
 
 
 
132
  MAX_SEED = np.iinfo(np.int32).max
 
133
 
134
  def load_model_hf(model_config_path, repo_id, filename, device='cpu'):
135
  args = SLConfig.fromfile(model_config_path)
 
416
 
417
  logger.info(f'_______lama_cleaner_process_______9____')
418
  if config.sd_seed == -1:
419
+ config.sd_seed = random.randint(1, MAX_SEED)
420
 
421
  # logger.info(f"Origin image shape_0_: {original_shape} / {size_limit}")
422
  logger.info(f'_______lama_cleaner_process_______10____')
 
615
  def load_kolors_inpainting(inpaint_prompt, input_image, mask_image):
616
  from gradio_client import Client, handle_file
617
  import tempfile
618
+ MAX_IMAGE_SIZE = 1024
619
  try:
620
+ # logger.info(f'load_kolors_inpainting_input_image={inpaint_prompt} // {input_image}')
621
+ # logger.info(f'load_kolors_inpainting_mask_image={mask_image}')
622
+
623
  job_image = {}
624
  if 'background' in input_image.keys():
625
+ width, height = input_image['background'].size
626
+ if max(width, height) > MAX_IMAGE_SIZE:
627
+ if width > height:
628
+ resize_width = MAX_IMAGE_SIZE
629
+ resize_height = int(height * MAX_IMAGE_SIZE / width)
630
+ else:
631
+ resize_height = MAX_IMAGE_SIZE
632
+ resize_width = int(width * MAX_IMAGE_SIZE / height)
633
+ else:
634
+ resize_width, resize_height = width, height
635
+
636
+ logger.info(f"resize____{width}, {height}==>{resize_width}, {resize_height}")
637
  _, temp_file_path = tempfile.mkstemp(suffix='.png')
638
+ img = input_image['background'].convert("RGB").resize((resize_width, resize_height))
639
  img.save(temp_file_path)
640
+ # logger.info(f'load_kolors_inpainting_temp_file_background_={temp_file_path}')
641
  job_image["background"] = handle_file(temp_file_path)
642
  if 'layers' in input_image.keys() and len(input_image['layers']) > 0:
643
  _, temp_file_path = tempfile.mkstemp(suffix='.png')
644
+ img = input_image['layers'][0].convert("RGB").resize((resize_width, resize_height))
645
  img.save(temp_file_path)
646
+ # logger.info(f'load_kolors_inpainting_temp_file_layers_={temp_file_path}')
647
  job_image["layers"] = [handle_file(temp_file_path)]
648
+
649
  if 'composite' in input_image.keys():
650
  _, temp_file_path = tempfile.mkstemp(suffix='.png')
651
+ img = input_image['composite'].convert("RGB").resize((resize_width, resize_height))
652
  img.save(temp_file_path)
653
+ # logger.info(f'load_kolors_inpainting_temp_file_composite_={temp_file_path}')
654
  job_image["composite"] = handle_file(temp_file_path)
655
 
656
  _, temp_file_path = tempfile.mkstemp(suffix='.png')
657
+ img = mask_image.convert("RGB").resize((resize_width, resize_height))
658
  img.save(temp_file_path)
659
+ # logger.info(f'load_kolors_inpainting_temp_file_mask_={temp_file_path}')
660
  job_mask_image = handle_file(temp_file_path)
661
 
662
+ # logger.info(f'load_kolors_inpainting_job_image={job_image}')
663
+ # logger.info(f'load_kolors_inpainting_job_mask_image={job_mask_image}')
664
+
665
  client = Client("Kwai-Kolors/Kolors-Inpainting")
666
 
667
  job = client.submit(
 
669
  image=job_image,
670
  mask_image=job_mask_image,
671
  negative_prompt="broken fingers, deformed fingers, deformed hands, stumps, blurriness, low quality",
672
+ seed=random.randint(1, MAX_SEED), #0,
673
  randomize_seed=True,
674
  guidance_scale=6,
675
  num_inference_steps=25,
 
678
  while not job.done():
679
  time.sleep(0.1)
680
 
681
+ result = job.outputs()
682
+ logger.info(f'load_kolors_inpainting_result={result}')
683
+ if len(result) <= 0:
684
+ return None
685
+
686
+ result = result[0]
687
  im = Image.open(result)
688
  if im.mode == "RGBA":
689
  im.load()
 
865
 
866
  if task_type in ['inpainting', 'outpainting']:
867
  # inpainting pipeline
868
+ image_source_for_inpaint = image_pil #.resize((512, 512))
869
+ image_mask_for_inpaint = mask_pil #.resize((512, 512))
870
  if task_type in ['outpainting']:
871
  # reverse mask
872
  img_arr = np.array(image_mask_for_inpaint)
 
999
  task_types.append("segment")
1000
  if inpainting_enable:
1001
  task_types.append("inpainting")
1002
+ # task_types.append("outpainting")
1003
  if lama_cleaner_enable:
1004
  task_types.append("remove")
1005
  if ram_enable:
1006
  task_types.append("relate anything")
1007
  if kosmos_enable:
1008
  task_types.append("Kosmos-2")
1009
+
1010
+ brush_color = "#00FF00"
1011
+ color_mode = "fixed"
1012
+ input_image = gr.ImageMask(sources=["upload", "webcam"], image_mode='RGB', elem_id="image_upload", type='pil', label="Upload",
1013
+ brush=gr.Brush(colors=[brush_color], color_mode=color_mode))
1014
+
1015
  task_type = gr.Radio(task_types, value="detection",
1016
  label='Task type', visible=True)
1017
  mask_source_radio = gr.Radio([mask_source_draw, mask_source_segment],
1018
  value=mask_source_segment, label="Mask from",
1019
  visible=False)
1020
  text_prompt = gr.Textbox(label="Detection Prompt[To detect multiple objects, seperating each with '.', like this: cat . dog . chair ]", placeholder="Cannot be empty")
1021
+ inpaint_prompt = gr.Textbox(label="Inpaint Prompt (if this is empty, then remove)", visible=False)
1022
  num_relation = gr.Slider(label="How many relations do you want to see", minimum=1, maximum=20, value=5, step=1, visible=False)
1023
 
1024
  kosmos_input = gr.Radio(["Brief", "Detailed"], label="Kosmos Description Type", value="Brief", visible=False)