yizhangliu
commited on
Commit
•
df25cfa
1
Parent(s):
bc4c863
update app.py
Browse files- app.py +68 -30
- requirements.txt +1 -1
app.py
CHANGED
@@ -4,7 +4,7 @@ warnings.filterwarnings('ignore')
|
|
4 |
|
5 |
import subprocess, io, os, sys, time
|
6 |
|
7 |
-
os.system("pip install gradio==4.
|
8 |
|
9 |
import gradio as gr
|
10 |
from loguru import logger
|
@@ -103,6 +103,8 @@ from kosmos_utils import *
|
|
103 |
|
104 |
from util_tencent import getTextTrans
|
105 |
|
|
|
|
|
106 |
config_file = 'GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py'
|
107 |
ckpt_repo_id = "ShilongLiu/GroundingDINO"
|
108 |
ckpt_filenmae = "groundingdino_swint_ogc.pth"
|
@@ -587,7 +589,7 @@ def get_time_cost(run_task_time, time_cost_str):
|
|
587 |
run_task_time = now_time
|
588 |
return run_task_time, time_cost_str
|
589 |
|
590 |
-
def
|
591 |
from gradio_client import Client, handle_file
|
592 |
import tempfile
|
593 |
MAX_IMAGE_SIZE = 1024
|
@@ -602,8 +604,8 @@ def load_kolors_inpainting(inpaint_prompt, input_image, mask_image):
|
|
602 |
return image
|
603 |
|
604 |
try:
|
605 |
-
# logger.info(f'
|
606 |
-
# logger.info(f'
|
607 |
|
608 |
job_image = {}
|
609 |
job_mask_image = None
|
@@ -623,7 +625,7 @@ def load_kolors_inpainting(inpaint_prompt, input_image, mask_image):
|
|
623 |
_, temp_file_path = tempfile.mkstemp(suffix='.png')
|
624 |
img = input_image['background'].convert("RGB").resize((resize_width, resize_height))
|
625 |
img.save(temp_file_path)
|
626 |
-
# logger.info(f'
|
627 |
job_image["background"] = handle_file(temp_file_path)
|
628 |
|
629 |
if mask_image is not None:
|
@@ -634,29 +636,57 @@ def load_kolors_inpainting(inpaint_prompt, input_image, mask_image):
|
|
634 |
# RGB(255,255,255) --> RGB(170,170,170)
|
635 |
img = change_RGB_value(img, 255, 255, 255, 170, 170, 170)
|
636 |
img.save(temp_file_path)
|
637 |
-
# logger.info(f'
|
638 |
job_image["layers"] = [handle_file(temp_file_path)]
|
639 |
|
640 |
-
|
641 |
-
|
642 |
-
|
643 |
-
|
644 |
-
|
645 |
-
|
646 |
-
|
647 |
-
|
648 |
-
|
649 |
-
|
650 |
-
|
651 |
-
|
652 |
-
|
653 |
-
|
654 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
655 |
while not job.done():
|
|
|
|
|
|
|
656 |
time.sleep(0.1)
|
657 |
-
|
658 |
result = job.outputs()
|
659 |
-
logger.info(f'
|
660 |
if len(result) <= 0:
|
661 |
return None
|
662 |
|
@@ -668,7 +698,7 @@ def load_kolors_inpainting(inpaint_prompt, input_image, mask_image):
|
|
668 |
background.paste(im, mask=im.split()[3])
|
669 |
return im
|
670 |
except Exception as e:
|
671 |
-
logger.info(f'
|
672 |
return None
|
673 |
|
674 |
def run_anything_task(input_image, text_prompt, task_type, inpaint_prompt, box_threshold, text_threshold,
|
@@ -682,12 +712,15 @@ def run_anything_task(input_image, text_prompt, task_type, inpaint_prompt, box_t
|
|
682 |
|
683 |
# logger.info(f"input_image==={input_image}")
|
684 |
ori_input_image = input_image
|
|
|
685 |
if 'background' in input_image.keys():
|
686 |
input_image['image'] = input_image['background'].convert("RGB")
|
687 |
if len(input_image['layers']) > 0:
|
688 |
img_arr = np.array(input_image['layers'][0].convert("L"))
|
689 |
img_arr = np.where(img_arr > 0, 1, img_arr)
|
690 |
input_image['mask'] = Image.fromarray(255*img_arr.astype('uint8'))
|
|
|
|
|
691 |
|
692 |
if (task_type == 'Kosmos-2'):
|
693 |
global kosmos_model, kosmos_processor
|
@@ -865,10 +898,10 @@ def run_anything_task(input_image, text_prompt, task_type, inpaint_prompt, box_t
|
|
865 |
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
|
866 |
|
867 |
# image_inpainting = sd_model(prompt=inpaint_prompt, image=image_source_for_inpaint, mask_image=image_mask_for_inpaint).images[0]
|
868 |
-
image_inpainting =
|
869 |
if image_inpainting is None:
|
870 |
-
logger.info(f'
|
871 |
-
time_cost_str = f"
|
872 |
return None, None, time_cost_str, gr.update(visible=(time_cost_str !='')), None, None, None
|
873 |
else:
|
874 |
# remove from mask
|
@@ -979,7 +1012,7 @@ def get_model_device(module):
|
|
979 |
def main_gradio(args):
|
980 |
block = gr.Blocks(
|
981 |
title="SAM and others",
|
982 |
-
theme="shivi/calm_seafoam@>=0.0.1,<1.0.0",
|
983 |
)
|
984 |
with block:
|
985 |
with gr.Row():
|
@@ -999,8 +1032,13 @@ def main_gradio(args):
|
|
999 |
|
1000 |
brush_color = "#00FF00"
|
1001 |
color_mode = "fixed"
|
1002 |
-
input_image = gr.
|
1003 |
-
|
|
|
|
|
|
|
|
|
|
|
1004 |
|
1005 |
task_type = gr.Radio(task_types, value="detection",
|
1006 |
label='Task type', visible=True)
|
|
|
4 |
|
5 |
import subprocess, io, os, sys, time
|
6 |
|
7 |
+
os.system("pip install gradio==4.44.1")
|
8 |
|
9 |
import gradio as gr
|
10 |
from loguru import logger
|
|
|
103 |
|
104 |
from util_tencent import getTextTrans
|
105 |
|
106 |
+
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
107 |
+
|
108 |
config_file = 'GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py'
|
109 |
ckpt_repo_id = "ShilongLiu/GroundingDINO"
|
110 |
ckpt_filenmae = "groundingdino_swint_ogc.pth"
|
|
|
589 |
run_task_time = now_time
|
590 |
return run_task_time, time_cost_str
|
591 |
|
592 |
+
def processs_inpainting(inpaint_prompt, input_image, mask_image, image_input_composite, debug=False):
|
593 |
from gradio_client import Client, handle_file
|
594 |
import tempfile
|
595 |
MAX_IMAGE_SIZE = 1024
|
|
|
604 |
return image
|
605 |
|
606 |
try:
|
607 |
+
# logger.info(f'processs_inpainting_input_image={inpaint_prompt} // {input_image}')
|
608 |
+
# logger.info(f'processs_inpainting_mask_image={mask_image}')
|
609 |
|
610 |
job_image = {}
|
611 |
job_mask_image = None
|
|
|
625 |
_, temp_file_path = tempfile.mkstemp(suffix='.png')
|
626 |
img = input_image['background'].convert("RGB").resize((resize_width, resize_height))
|
627 |
img.save(temp_file_path)
|
628 |
+
# logger.info(f'processs_inpainting_temp_file_background_={temp_file_path}')
|
629 |
job_image["background"] = handle_file(temp_file_path)
|
630 |
|
631 |
if mask_image is not None:
|
|
|
636 |
# RGB(255,255,255) --> RGB(170,170,170)
|
637 |
img = change_RGB_value(img, 255, 255, 255, 170, 170, 170)
|
638 |
img.save(temp_file_path)
|
639 |
+
# logger.info(f'processs_inpainting_temp_file___mask_={temp_file_path}')
|
640 |
job_image["layers"] = [handle_file(temp_file_path)]
|
641 |
|
642 |
+
if image_input_composite:
|
643 |
+
_, temp_file_path = tempfile.mkstemp(suffix='.png')
|
644 |
+
image_input_composite.save(temp_file_path)
|
645 |
+
job_image["composite"] = handle_file(temp_file_path)
|
646 |
+
|
647 |
+
logger.info(f'processs_inpainting_job_image={job_image}')
|
648 |
+
logger.info(f'processs_inpainting_job_mask_image={job_mask_image}')
|
649 |
+
|
650 |
+
if 0==0:
|
651 |
+
client = Client("Kwai-Kolors/Kolors-Inpainting", hf_token=huggingface_token, verbose=True)
|
652 |
+
job = client.submit(
|
653 |
+
prompt=inpaint_prompt,
|
654 |
+
image=job_image,
|
655 |
+
mask_image=job_mask_image,
|
656 |
+
negative_prompt="broken fingers, deformed fingers, deformed hands, stumps, blurriness, low quality",
|
657 |
+
seed=0,
|
658 |
+
randomize_seed=True,
|
659 |
+
guidance_scale=6.0,
|
660 |
+
num_inference_steps=25,
|
661 |
+
api_name="/infer"
|
662 |
+
)
|
663 |
+
elif 0==0:
|
664 |
+
client = Client("ameerazam08/FLUX.1-dev-Inpainting-Model-Beta-GPU")
|
665 |
+
job = client.submit(
|
666 |
+
input_image_editor=job_image,
|
667 |
+
# input_image_editor={"background":handle_file('https://ameerazam08-flux-1-dev-inpainting-model-beta-gpu.hf.space/file=/tmp/gradio/d9270300d40346834ccf596c97d9795fef6af692/background.png'),"layers":[handle_file('https://ameerazam08-flux-1-dev-inpainting-model-beta-gpu.hf.space/file=/tmp/gradio/4245eafd2d9aa8969d94dc22ffff62be95d4df02/layer_0.png')],"composite":handle_file('https://ameerazam08-flux-1-dev-inpainting-model-beta-gpu.hf.space/file=/tmp/gradio/7fe6ddfacbb3b394d19a32b01113a0460e2279ac/composite.png')},
|
668 |
+
prompt=inpaint_prompt,
|
669 |
+
negative_prompt="",
|
670 |
+
controlnet_conditioning_scale=0.9,
|
671 |
+
guidance_scale=3.5,
|
672 |
+
seed=124,
|
673 |
+
num_inference_steps=24,
|
674 |
+
true_guidance_scale=3.5,
|
675 |
+
api_name="/process"
|
676 |
+
)
|
677 |
+
|
678 |
+
debug = True
|
679 |
+
if debug:
|
680 |
+
count = 0
|
681 |
+
logger.info(f'{count}___{job.status()}')
|
682 |
while not job.done():
|
683 |
+
if debug:
|
684 |
+
count += 1
|
685 |
+
logger.info(f'{count}___{job.status()}')
|
686 |
time.sleep(0.1)
|
687 |
+
|
688 |
result = job.outputs()
|
689 |
+
logger.info(f'processs_inpainting_result={result}')
|
690 |
if len(result) <= 0:
|
691 |
return None
|
692 |
|
|
|
698 |
background.paste(im, mask=im.split()[3])
|
699 |
return im
|
700 |
except Exception as e:
|
701 |
+
logger.info(f'processs_inpainting_[Error]:' + str(e))
|
702 |
return None
|
703 |
|
704 |
def run_anything_task(input_image, text_prompt, task_type, inpaint_prompt, box_threshold, text_threshold,
|
|
|
712 |
|
713 |
# logger.info(f"input_image==={input_image}")
|
714 |
ori_input_image = input_image
|
715 |
+
image_input_composite = None
|
716 |
if 'background' in input_image.keys():
|
717 |
input_image['image'] = input_image['background'].convert("RGB")
|
718 |
if len(input_image['layers']) > 0:
|
719 |
img_arr = np.array(input_image['layers'][0].convert("L"))
|
720 |
img_arr = np.where(img_arr > 0, 1, img_arr)
|
721 |
input_image['mask'] = Image.fromarray(255*img_arr.astype('uint8'))
|
722 |
+
if 'composite' in input_image.keys():
|
723 |
+
image_input_composite = input_image['composite']
|
724 |
|
725 |
if (task_type == 'Kosmos-2'):
|
726 |
global kosmos_model, kosmos_processor
|
|
|
898 |
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
|
899 |
|
900 |
# image_inpainting = sd_model(prompt=inpaint_prompt, image=image_source_for_inpaint, mask_image=image_mask_for_inpaint).images[0]
|
901 |
+
image_inpainting = processs_inpainting(inpaint_prompt, input_image, image_mask_for_inpaint, image_input_composite)
|
902 |
if image_inpainting is None:
|
903 |
+
logger.info(f'processs_inpainting_failed_')
|
904 |
+
time_cost_str = f"processs_inpainting_task__failed!"
|
905 |
return None, None, time_cost_str, gr.update(visible=(time_cost_str !='')), None, None, None
|
906 |
else:
|
907 |
# remove from mask
|
|
|
1012 |
def main_gradio(args):
|
1013 |
block = gr.Blocks(
|
1014 |
title="SAM and others",
|
1015 |
+
# theme="shivi/calm_seafoam@>=0.0.1,<1.0.0",
|
1016 |
)
|
1017 |
with block:
|
1018 |
with gr.Row():
|
|
|
1032 |
|
1033 |
brush_color = "#00FF00"
|
1034 |
color_mode = "fixed"
|
1035 |
+
input_image = gr.ImageEditor(sources=["upload", "webcam"],
|
1036 |
+
image_mode='RGB',
|
1037 |
+
elem_id="image_upload",
|
1038 |
+
type='pil',
|
1039 |
+
label="Upload",
|
1040 |
+
layers=False,
|
1041 |
+
brush=gr.Brush(colors=[brush_color], color_mode=color_mode))
|
1042 |
|
1043 |
task_type = gr.Radio(task_types, value="detection",
|
1044 |
label='Task type', visible=True)
|
requirements.txt
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
accelerate
|
2 |
addict
|
3 |
diffusers
|
4 |
-
gradio==3.36.1
|
5 |
huggingface_hub
|
6 |
matplotlib
|
7 |
numpy
|
|
|
1 |
accelerate
|
2 |
addict
|
3 |
diffusers
|
4 |
+
# gradio==3.36.1
|
5 |
huggingface_hub
|
6 |
matplotlib
|
7 |
numpy
|