| 51 | |
| 52 | lock = threading.Lock() |
| 53 | class DemoUI(object): |
| 54 | def __init__(self, |
| 55 | infer_dir = "./config/ace_plus_fft.yaml" |
| 56 | ): |
| 57 | self.model_yamls = [infer_dir] |
| 58 | self.model_choices = dict() |
| 59 | self.default_model_name = '' |
| 60 | self.edit_type_dict = {} |
| 61 | self.edit_type_list = [] |
| 62 | self.default_type_list = [] |
| 63 | for i in self.model_yamls: |
| 64 | model_cfg = Config(load=True, cfg_file=i) |
| 65 | model_name = model_cfg.VERSION |
| 66 | if model_cfg.IS_DEFAULT: self.default_model_name = model_name |
| 67 | self.model_choices[model_name] = model_cfg |
| 68 | for preprocessor in model_cfg.get("PREPROCESSOR", []): |
| 69 | if preprocessor["TYPE"] in self.edit_type_dict: |
| 70 | continue |
| 71 | self.edit_type_dict[preprocessor["TYPE"]] = preprocessor |
| 72 | self.default_type_list.append(preprocessor["TYPE"]) |
| 73 | print('Models: ', self.model_choices.keys()) |
| 74 | assert len(self.model_choices) > 0 |
| 75 | if self.default_model_name == "": self.default_model_name = list(self.model_choices.keys())[0] |
| 76 | self.model_name = self.default_model_name |
| 77 | pipe_cfg = self.model_choices[self.default_model_name] |
| 78 | self.pipe = INFERENCES.build(pipe_cfg) |
| 79 | # reformat examples |
| 80 | self.all_examples = [ |
| 81 | [ |
| 82 | one_example["edit_type"], one_example["instruction"], |
| 83 | one_example["input_reference_image"], one_example["input_image"], |
| 84 | one_example["input_mask"], one_example["output_h"], |
| 85 | one_example["output_w"], one_example["seed"] |
| 86 | ] |
| 87 | for one_example in fft_examples |
| 88 | ] |
| 89 | |
| 90 | def construct_edit_image(self, edit_image, edit_mask): |
| 91 | if edit_image is not None and edit_mask is not None: |
| 92 | edit_image_rgb = pillow_convert(edit_image, "RGB") |
| 93 | edit_image_rgba = pillow_convert(edit_image, "RGBA") |
| 94 | edit_mask = pillow_convert(edit_mask, "L") |
| 95 | |
| 96 | arr1 = np.array(edit_image_rgb) |
| 97 | arr2 = np.array(edit_mask)[:, :, np.newaxis] |
| 98 | result_array = np.concatenate((arr1, arr2), axis=2) |
| 99 | layer = Image.fromarray(result_array) |
| 100 | |
| 101 | ret_data = { |
| 102 | "background": edit_image_rgba, |
| 103 | "composite": edit_image_rgba, |
| 104 | "layers": [layer] |
| 105 | } |
| 106 | return ret_data |
| 107 | else: |
| 108 | return None |
| 109 | |
| 110 | def create_ui(self): |