|
| 1 | +import os |
| 2 | +import comfy |
| 3 | +import torch |
| 4 | +import torchvision |
| 5 | +import sys |
| 6 | +import folder_paths |
| 7 | +import numpy |
| 8 | +import pygit2 |
| 9 | +from PIL import Image, ImageOps |
| 10 | + |
| 11 | +this_path = os.path.dirname(__file__) |
| 12 | +repo_dir = os.path.join(this_path,"InfiniteYou") |
| 13 | +requirements_txt = os.path.join(repo_dir,"requirements.txt") |
| 14 | + |
| 15 | +if not os.path.exists(requirements_txt): |
| 16 | + pygit2.clone_repository("https://github.com/bytedance/InfiniteYou", repo_dir, depth=1) |
| 17 | + if not os.path.exists(requirements_txt): |
| 18 | + print("*** Could not get InfiniteYou repository. Please install git.") |
| 19 | + |
| 20 | +sys.path.insert(0, this_path) |
| 21 | +from InfiniteYou.pipelines.pipeline_infu_flux import InfUFluxPipeline |
| 22 | +sys.path.remove(this_path) |
| 23 | + |
| 24 | +class InfiniteYouSampler: |
| 25 | + def __init__(self): |
| 26 | + pass |
| 27 | + |
| 28 | + @classmethod |
| 29 | + def INPUT_TYPES(s): |
| 30 | + return { |
| 31 | + "required": { |
| 32 | + "id_image": ("IMAGE",), |
| 33 | + "base_model": (['auto'] + folder_paths.get_filename_list("diffusion_models"),), |
| 34 | + "seed": ("INT", { |
| 35 | + }), |
| 36 | + "prompt": ("STRING", { |
| 37 | + "multiline": True, |
| 38 | + "default": "a robot", |
| 39 | + "lazy": True |
| 40 | + }), |
| 41 | + "steps": ("INT", { |
| 42 | + "default": 30 |
| 43 | + }), |
| 44 | + }, |
| 45 | + "optional": { |
| 46 | + "guidance": ("FLOAT", { |
| 47 | + "default": 3.5, |
| 48 | + "min": 0.0, |
| 49 | + "step": 0.1, |
| 50 | + "round": 0.01, |
| 51 | + "display": "number", |
| 52 | + "lazy": True |
| 53 | + }), |
| 54 | + "infusenet_conditioning_scale": ("FLOAT", { |
| 55 | + "default": 1.0, |
| 56 | + "min": 0.0, |
| 57 | + "step": 0.1, |
| 58 | + "round": 0.01, |
| 59 | + "display": "number", |
| 60 | + "lazy": True |
| 61 | + }), |
| 62 | + "infusenet_guidance_start": ("FLOAT", { |
| 63 | + "default": 0.0, |
| 64 | + "min": 0.0, |
| 65 | + "step": 0.1, |
| 66 | + "round": 0.01, |
| 67 | + "display": "number", |
| 68 | + "lazy": True |
| 69 | + }), |
| 70 | + "infusenet_guidance_end": ("FLOAT", { |
| 71 | + "default": 1.0, |
| 72 | + "min": 0.0, |
| 73 | + "step": 0.1, |
| 74 | + "round": 0.01, |
| 75 | + "display": "number", |
| 76 | + "lazy": True |
| 77 | + }), |
| 78 | + "model_version": (['aes_stage2', 'sim_stage1'], { |
| 79 | + "default": 'aes_stage2' |
| 80 | + }), |
| 81 | + "control_image": ("IMAGE",), |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + RETURN_TYPES = ("IMAGE",) |
| 86 | + |
| 87 | + FUNCTION = "sampler" |
| 88 | + CATEGORY = "sampling" |
| 89 | + |
| 90 | + def sampler( |
| 91 | + self, |
| 92 | + id_image, |
| 93 | + base_model, # TODO |
| 94 | + seed, |
| 95 | + prompt, |
| 96 | + steps=30, |
| 97 | + guidance=3.5, |
| 98 | + infusenet_conditioning_scale=1.0, |
| 99 | + infusenet_guidance_start=0.0, |
| 100 | + infusenet_guidance_end=1.0, |
| 101 | + model_version='aes_stage2', |
| 102 | + control_image=None, |
| 103 | + ): |
| 104 | + torch.cuda.set_device(comfy.model_management.get_torch_device()) |
| 105 | + |
| 106 | + infu_flux_version = 'v1.0' |
| 107 | + model_dir = 'ByteDance/InfiniteYou' |
| 108 | + if base_model == 'auto': |
| 109 | + base_model_path = 'black-forest-labs/FLUX.1-dev' |
| 110 | + else: |
| 111 | + base_model_path = folder_paths.get_full_path_or_raise("diffusion_models", base_model) |
| 112 | + |
| 113 | + infu_model_path = os.path.join( |
| 114 | + model_dir, f'infu_flux_{infu_flux_version}', |
| 115 | + model_version |
| 116 | + ) |
| 117 | + insightface_root_path = os.path.join( |
| 118 | + model_dir, 'supports', 'insightface' |
| 119 | + ) |
| 120 | + pipe = InfUFluxPipeline( |
| 121 | + base_model_path=base_model_path, |
| 122 | + infu_model_path=infu_model_path, |
| 123 | + insightface_root_path=insightface_root_path, |
| 124 | + infu_flux_version=infu_flux_version, |
| 125 | + model_version=model_version, |
| 126 | + ) |
| 127 | + |
| 128 | + control_image_pil = None |
| 129 | + if control_image is not None: |
| 130 | + control_i = 255. * control_image[0].cpu().numpy() |
| 131 | + control_image_pil = Image.fromarray(numpy.clip(i, 0, 255).astype(numpy.uint8)) |
| 132 | + i = 255. * id_image[0].cpu().numpy() |
| 133 | + img = Image.fromarray(numpy.clip(i, 0, 255).astype(numpy.uint8)) |
| 134 | + image = pipe( |
| 135 | + id_image=img, |
| 136 | + prompt=prompt, |
| 137 | + control_image=control_image_pil, |
| 138 | + seed=seed, |
| 139 | + guidance_scale=guidance, |
| 140 | + num_steps=steps, |
| 141 | + infusenet_conditioning_scale=infusenet_conditioning_scale, |
| 142 | + infusenet_guidance_start=infusenet_guidance_start, |
| 143 | + infusenet_guidance_end=infusenet_guidance_end, |
| 144 | + ) |
| 145 | + |
| 146 | + image = image.convert("RGB") |
| 147 | + image = numpy.array(image).astype(numpy.float32) / 255.0 |
| 148 | + image = torch.from_numpy(image)[None,] |
| 149 | + images = [] |
| 150 | + images.append(image) |
| 151 | + |
| 152 | + if len(images) > 1: |
| 153 | + images = torch.cat(images, dim=0) |
| 154 | + else: |
| 155 | + images = images[0] |
| 156 | + |
| 157 | + return (images,) |
0 commit comments