| 53 | |
| 54 | lock = threading.Lock() |
| 55 | class DemoUI(object): |
| 56 | def __init__(self, |
| 57 | infer_dir = "./config/ace_plus_diffusers_infer.yaml", |
| 58 | model_list='./models/model_zoo.yaml' |
| 59 | ): |
| 60 | self.model_yamls = [infer_dir] |
| 61 | self.model_choices = dict() |
| 62 | self.default_model_name = '' |
| 63 | for i in self.model_yamls: |
| 64 | model_cfg = Config(load=True, cfg_file=i) |
| 65 | model_name = model_cfg.NAME |
| 66 | if model_cfg.IS_DEFAULT: self.default_model_name = model_name |
| 67 | self.model_choices[model_name] = model_cfg |
| 68 | print('Models: ', self.model_choices.keys()) |
| 69 | assert len(self.model_choices) > 0 |
| 70 | if self.default_model_name == "": self.default_model_name = list(self.model_choices.keys())[0] |
| 71 | self.model_name = self.default_model_name |
| 72 | pipe_cfg = self.model_choices[self.default_model_name] |
| 73 | infer_name = pipe_cfg.get("INFERENCE_TYPE", "ACE") |
| 74 | self.pipe = inference_dict[infer_name]() |
| 75 | self.pipe.init_from_cfg(pipe_cfg) |
| 76 | |
| 77 | # choose different model |
| 78 | self.task_model_cfg = Config(load=True, cfg_file=model_list) |
| 79 | self.task_model = {} |
| 80 | self.task_model_list = [] |
| 81 | self.edit_type_dict = {"repainting": None} |
| 82 | self.edit_type_list = ["repainting"] |
| 83 | for task_name, task_model in self.task_model_cfg.MODEL.items(): |
| 84 | self.task_model[task_name.lower()] = task_model |
| 85 | self.task_model_list.append(task_name.lower()) |
| 86 | for preprocessor in task_model.get("PREPROCESSOR", []): |
| 87 | if preprocessor["TYPE"] in self.edit_type_dict: |
| 88 | continue |
| 89 | preprocessor["REPAINTING_SCALE"] = task_model.get("REPAINTING_SCALE", 1.0) |
| 90 | self.edit_type_dict[preprocessor["TYPE"]] = preprocessor |
| 91 | self.max_msgs = 20 |
| 92 | # reformat examples |
| 93 | self.all_examples = [ |
| 94 | [ |
| 95 | one_example["task_type"], one_example["edit_type"], one_example["instruction"], |
| 96 | one_example["input_reference_image"], one_example["input_image"], |
| 97 | one_example["input_mask"], one_example["output_h"], |
| 98 | one_example["output_w"], one_example["seed"] |
| 99 | ] |
| 100 | for one_example in all_examples |
| 101 | ] |
| 102 | |
| 103 | def construct_edit_image(self, edit_image, edit_mask): |
| 104 | if edit_image is not None and edit_mask is not None: |
| 105 | edit_image_rgb = pillow_convert(edit_image, "RGB") |
| 106 | edit_image_rgba = pillow_convert(edit_image, "RGBA") |
| 107 | edit_mask = pillow_convert(edit_mask, "L") |
| 108 | |
| 109 | arr1 = np.array(edit_image_rgb) |
| 110 | arr2 = np.array(edit_mask)[:, :, np.newaxis] |
| 111 | result_array = np.concatenate((arr1, arr2), axis=2) |
| 112 | layer = Image.fromarray(result_array) |