File size: 1,675 Bytes
2d4a49c
 
 
 
 
 
 
 
 
 
 
eddb2e2
 
 
 
2d4a49c
 
 
 
 
 
 
 
 
 
eddb2e2
 
 
 
 
 
2d4a49c
 
 
 
 
 
 
50a101c
2d4a49c
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
from typing import Dict, List, Any
from PIL import Image
from io import BytesIO
import torch
import base64
from diffusers import StableDiffusionInstructPix2PixPipeline, EulerAncestralDiscreteScheduler

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

class EndpointHandler():
    def __init__(self, path=""):
        #model_id = "timbrooks/instruct-pix2pix"
        #self.pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, safety_checker=None)
        #self.pipe.to(device)
        #self.pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(self.pipe.scheduler.config)

    def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
        """
       data args:
            inputs (:obj:`string`)
            parameters (:obj:)
      Return:
            A :obj:`string`:. Base64 encoded image string
        """

        model_id = "timbrooks/instruct-pix2pix"
        self.pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, safety_checker=None)
        self.pipe.to(device)
        self.pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(self.pipe.scheduler.config)


        image_data = data.pop("inputs", data)
        # decode base64 image to PIL
        image = Image.open(BytesIO(base64.b64decode(image_data)))

        parameters = data.pop("parameters", data)
        prompt = parameters['prompt']

        images = self.pipe(prompt, image=image, num_inference_steps=10, image_guidance_scale=1).images
        return images[0]