updated.
Browse files- handler.py +24 -27
handler.py
CHANGED
|
@@ -5,42 +5,34 @@ from PIL import Image
|
|
| 5 |
from webcolors import CSS3_HEX_TO_NAMES, hex_to_rgb
|
| 6 |
from scipy.spatial import KDTree
|
| 7 |
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
class EndpointHandler:
|
| 10 |
|
| 11 |
def __init__(self, path=""):
|
| 12 |
model_id = "timbrooks/instruct-pix2pix"
|
| 13 |
self.model = StableDiffusionInstructPix2PixPipeline.from_pretrained(path, torch_dtype=torch.float16, safety_checker=None).to("cuda")
|
| 14 |
self.model.scheduler = EulerAncestralDiscreteScheduler.from_config(self.model.scheduler.config)
|
| 15 |
|
| 16 |
-
def __call__(self, data
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
input_image = process_image_base64(data['inputs']['base64_image'])
|
| 24 |
-
|
| 25 |
-
text_prompt = data['inputs']['text_prompt']
|
| 26 |
-
color_code = data['inputs']['color_code']
|
| 27 |
-
|
| 28 |
-
guidance_scale = data['inputs'].get('guidance_scale', 7.5)
|
| 29 |
-
image_guidance_scale = data['inputs'].get('image_guidance_scale', 1.5)
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
result_prompt,
|
| 36 |
-
image=input_image,
|
| 37 |
-
num_inference_steps=50,
|
| 38 |
-
guidance_scale=guidance_scale,
|
| 39 |
-
image_guidance_scale=image_guidance_scale,
|
| 40 |
-
).images
|
| 41 |
-
|
| 42 |
-
return images
|
| 43 |
-
|
| 44 |
def process_image_base64(self, base64_image_data):
|
| 45 |
# Decode base64 data to bytes
|
| 46 |
image_bytes = base64.b64decode(base64_image_data)
|
|
@@ -69,3 +61,8 @@ class EndpointHandler:
|
|
| 69 |
kdt_db = KDTree(rgb_values)
|
| 70 |
distance, index = kdt_db.query(rgb_tuple)
|
| 71 |
return names[index]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from webcolors import CSS3_HEX_TO_NAMES, hex_to_rgb
|
| 6 |
from scipy.spatial import KDTree
|
| 7 |
import io
|
| 8 |
+
import base64
|
| 9 |
+
from io import BytesIO
|
| 10 |
+
from PIL import Image
|
| 11 |
+
import json
|
| 12 |
|
| 13 |
+
class EndpointHandler():
|
| 14 |
|
| 15 |
def __init__(self, path=""):
|
| 16 |
model_id = "timbrooks/instruct-pix2pix"
|
| 17 |
self.model = StableDiffusionInstructPix2PixPipeline.from_pretrained(path, torch_dtype=torch.float16, safety_checker=None).to("cuda")
|
| 18 |
self.model.scheduler = EulerAncestralDiscreteScheduler.from_config(self.model.scheduler.config)
|
| 19 |
|
| 20 |
+
def __call__(self, data):
|
| 21 |
+
|
| 22 |
+
info=data['inputs']
|
| 23 |
+
image=info.pop("image",data)
|
| 24 |
+
prompt=info.pop("text",data)
|
| 25 |
+
image=base64.b64decode(image)
|
| 26 |
+
raw_images = Image.open(BytesIO(image)).convert('RGB')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
images = self.pipe(prompt, image=raw_images, num_inference_steps=25, image_guidance_scale=1).images
|
| 29 |
+
img=images[0]
|
| 30 |
+
img.save("./1.png")
|
| 31 |
+
with open('./1.png','rb') as img_file:
|
| 32 |
+
encoded_string = base64.b64encode(img_file.read()).decode('utf-8')
|
| 33 |
|
| 34 |
+
return {'image':encoded_string}
|
| 35 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
def process_image_base64(self, base64_image_data):
|
| 37 |
# Decode base64 data to bytes
|
| 38 |
image_bytes = base64.b64decode(base64_image_data)
|
|
|
|
| 61 |
kdt_db = KDTree(rgb_values)
|
| 62 |
distance, index = kdt_db.query(rgb_tuple)
|
| 63 |
return names[index]
|
| 64 |
+
"""
|
| 65 |
+
if __name__=="__main__":
|
| 66 |
+
my_handler = EndpointHandler(path='.')
|
| 67 |
+
|
| 68 |
+
|