File size: 2,322 Bytes
370d4c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b854139
370d4c1
 
4e83b84
923adf8
370d4c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65034b6
 
 
 
 
 
 
 
 
 
 
370d4c1
65034b6
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from transformers.tools.base import Tool, get_default_device
from transformers.utils import is_accelerate_available
import torch

from diffusers import StableDiffusionInpaintPipeline


INPAINTING_DESCRIPTION = (
    "This is a tool that inpaints some parts of an image StableDiffusionInpaintPipeline according to a prompt."
    " It takes three inputs: `image`, which should be the original image which will be inpainted,"
    " `mask_image`, which should be used to determine which parts of the original image"
    " (stored in the `image` variable) should be inpainted,"
    " and `prompt`, which should be the prompt to use to guide the inpainting process. It returns the"
    " inpainted image."
)


class InpaintingTool(Tool):
    default_checkpoint = "stabilityai/stable-diffusion-2-inpainting"
    description = INPAINTING_DESCRIPTION
    name = "image_inpainter"
    inputs = ['image', 'image', 'text']
    outputs = ['image']

    def __init__(self, device=None, **hub_kwargs) -> None:
        if not is_accelerate_available():
            raise ImportError("Accelerate should be installed in order to use tools.")

        super().__init__()

        self.device = device
        self.pipeline = None
        self.hub_kwargs = hub_kwargs

    def setup(self):
        if self.device is None:
            self.device = get_default_device()

        self.pipeline = StableDiffusionInpaintPipeline.from_pretrained(self.default_checkpoint)
        self.pipeline.to(self.device)

        if self.device.type == "cuda":
            self.pipeline.to(torch_dtype=torch.float16)

        self.is_initialized = True

    def __call__(self, image, mask_image,  prompt):
        if not self.is_initialized:
            self.setup()
        
        resized_image = image.resize((512, 512))
        mask_image = mask_image.resize((512, 512))
        # negative_prompt = "low quality, bad quality, deformed, low resolution"
        # added_prompt = " , highest quality, highly realistic, very high resolution"

        inpainted_image = self.pipeline(
            # prompt=prompt + added_prompt,
            # negative_prompt=negative_prompt,
            prompt=prompt,
            image=resized_image,
            mask_image=mask_image
        ).images[0]
        return inpainted_image.resize((image.size[0], image.size[1]))