yizhangliu
commited on
Commit
•
80ad95f
1
Parent(s):
9369662
update app.py
Browse files
app.py
CHANGED
@@ -58,7 +58,7 @@ kosmos_enable = False
|
|
58 |
if os.environ.get('IS_MY_DEBUG') is not None:
|
59 |
sam_enable = False
|
60 |
ram_enable = False
|
61 |
-
inpainting_enable = False
|
62 |
kosmos_enable = False
|
63 |
|
64 |
if lama_cleaner_enable:
|
@@ -79,8 +79,6 @@ from io import BytesIO
|
|
79 |
from diffusers import StableDiffusionInpaintPipeline
|
80 |
from huggingface_hub import hf_hub_download
|
81 |
|
82 |
-
from gradio_client import Client, handle_file
|
83 |
-
|
84 |
# from huggingface_hub import snapshot_download
|
85 |
# from kolors.pipelines.pipeline_stable_diffusion_xl_chatglm_256_inpainting import StableDiffusionXLInpaintPipeline
|
86 |
# from kolors.models.modeling_chatglm import ChatGLMModel
|
@@ -617,32 +615,61 @@ def get_time_cost(run_task_time, time_cost_str):
|
|
617 |
run_task_time = now_time
|
618 |
return run_task_time, time_cost_str
|
619 |
|
620 |
-
def load_kolors_inpainting(inpaint_prompt,
|
621 |
-
|
622 |
-
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
-
|
627 |
-
|
628 |
-
|
629 |
-
|
630 |
-
|
631 |
-
|
632 |
-
|
633 |
-
|
634 |
-
|
635 |
-
|
636 |
-
|
637 |
-
|
638 |
-
|
639 |
-
|
640 |
-
|
641 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
642 |
|
643 |
def run_anything_task(input_image, text_prompt, task_type, inpaint_prompt, box_threshold, text_threshold,
|
644 |
iou_threshold, inpaint_mode, mask_source_radio, remove_mode, remove_mask_extend, num_relation, kosmos_input, cleaner_size_limit=1080):
|
645 |
-
|
646 |
text_prompt = getTextTrans(text_prompt, source='zh', target='en')
|
647 |
inpaint_prompt = getTextTrans(inpaint_prompt, source='zh', target='en')
|
648 |
|
@@ -824,7 +851,10 @@ def run_anything_task(input_image, text_prompt, task_type, inpaint_prompt, box_t
|
|
824 |
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
|
825 |
|
826 |
# image_inpainting = sd_model(prompt=inpaint_prompt, image=image_source_for_inpaint, mask_image=image_mask_for_inpaint).images[0]
|
827 |
-
image_inpainting = load_kolors_inpainting(
|
|
|
|
|
|
|
828 |
else:
|
829 |
# remove from mask
|
830 |
if mask_source_radio == mask_source_segment:
|
@@ -1034,6 +1064,8 @@ def main_gradio(args):
|
|
1034 |
DESCRIPTION += f'Kosmos-2 from [Kosmos-2](https://github.com/microsoft/unilm/tree/master/kosmos-2). <br>'
|
1035 |
if ram_enable:
|
1036 |
DESCRIPTION += f'RAM from [RelateAnything](https://github.com/Luodian/RelateAnything). <br>'
|
|
|
|
|
1037 |
DESCRIPTION += f'Thanks for their excellent work.'
|
1038 |
DESCRIPTION += f'<p>For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings. \
|
1039 |
<a href="https://huggingface.co/spaces/yizhangliu/Grounded-Segment-Anything?duplicate=true"><img style="display: inline; margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space" /></a></p>'
|
|
|
58 |
if os.environ.get('IS_MY_DEBUG') is not None:
|
59 |
sam_enable = False
|
60 |
ram_enable = False
|
61 |
+
# inpainting_enable = False
|
62 |
kosmos_enable = False
|
63 |
|
64 |
if lama_cleaner_enable:
|
|
|
79 |
from diffusers import StableDiffusionInpaintPipeline
|
80 |
from huggingface_hub import hf_hub_download
|
81 |
|
|
|
|
|
82 |
# from huggingface_hub import snapshot_download
|
83 |
# from kolors.pipelines.pipeline_stable_diffusion_xl_chatglm_256_inpainting import StableDiffusionXLInpaintPipeline
|
84 |
# from kolors.models.modeling_chatglm import ChatGLMModel
|
|
|
615 |
run_task_time = now_time
|
616 |
return run_task_time, time_cost_str
|
617 |
|
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(
|
647 |
+
prompt=inpaint_prompt,
|
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,
|
655 |
+
api_name="/infer"
|
656 |
+
)
|
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()
|
664 |
+
background = Image.new("RGB", im.size, (255, 255, 255))
|
665 |
+
background.paste(im, mask=im.split()[3])
|
666 |
+
return im
|
667 |
+
except Exception as e:
|
668 |
+
logger.info(f'load_kolors_inpainting_fail_={str(e)}')
|
669 |
+
return None
|
670 |
|
671 |
def run_anything_task(input_image, text_prompt, task_type, inpaint_prompt, box_threshold, text_threshold,
|
672 |
iou_threshold, inpaint_mode, mask_source_radio, remove_mode, remove_mask_extend, num_relation, kosmos_input, cleaner_size_limit=1080):
|
|
|
673 |
text_prompt = getTextTrans(text_prompt, source='zh', target='en')
|
674 |
inpaint_prompt = getTextTrans(inpaint_prompt, source='zh', target='en')
|
675 |
|
|
|
851 |
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
|
852 |
|
853 |
# image_inpainting = sd_model(prompt=inpaint_prompt, image=image_source_for_inpaint, mask_image=image_mask_for_inpaint).images[0]
|
854 |
+
image_inpainting = load_kolors_inpainting(inpaint_prompt, input_image, image_mask_for_inpaint)
|
855 |
+
if image_inpainting is None:
|
856 |
+
logger.info(f'load_kolors_inpainting_failed_')
|
857 |
+
return None, None, None, None, None, None, None
|
858 |
else:
|
859 |
# remove from mask
|
860 |
if mask_source_radio == mask_source_segment:
|
|
|
1064 |
DESCRIPTION += f'Kosmos-2 from [Kosmos-2](https://github.com/microsoft/unilm/tree/master/kosmos-2). <br>'
|
1065 |
if ram_enable:
|
1066 |
DESCRIPTION += f'RAM from [RelateAnything](https://github.com/Luodian/RelateAnything). <br>'
|
1067 |
+
if inpainting_enable:
|
1068 |
+
DESCRIPTION += f'Inpainting from [Kolors-Inpainting](https://huggingface.co/spaces/Kwai-Kolors/Kolors-Inpainting). <br>'
|
1069 |
DESCRIPTION += f'Thanks for their excellent work.'
|
1070 |
DESCRIPTION += f'<p>For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings. \
|
1071 |
<a href="https://huggingface.co/spaces/yizhangliu/Grounded-Segment-Anything?duplicate=true"><img style="display: inline; margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space" /></a></p>'
|