Update handler.py
Browse files- handler.py +38 -3
handler.py
CHANGED
|
@@ -3,7 +3,7 @@ from diffusers import StableDiffusionInstructPix2PixPipeline, EulerAncestralDisc
|
|
| 3 |
import base64
|
| 4 |
from io import BytesIO
|
| 5 |
from PIL import Image
|
| 6 |
-
import
|
| 7 |
|
| 8 |
class EndpointHandler():
|
| 9 |
def __init__(self, path=""):
|
|
@@ -16,16 +16,51 @@ class EndpointHandler():
|
|
| 16 |
|
| 17 |
info=data['inputs']
|
| 18 |
image=info.pop("image",data)
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
image=base64.b64decode(image)
|
| 21 |
raw_images = Image.open(BytesIO(image)).convert('RGB')
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
img=images[0]
|
| 25 |
img.save("./1.png")
|
| 26 |
with open('./1.png','rb') as img_file:
|
| 27 |
encoded_string = base64.b64encode(img_file.read()).decode('utf-8')
|
| 28 |
return {'image':encoded_string}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
|
|
|
|
| 3 |
import base64
|
| 4 |
from io import BytesIO
|
| 5 |
from PIL import Image
|
| 6 |
+
from webcolors import hex_to_name, CSS3_HEX_TO_NAMES
|
| 7 |
|
| 8 |
class EndpointHandler():
|
| 9 |
def __init__(self, path=""):
|
|
|
|
| 16 |
|
| 17 |
info=data['inputs']
|
| 18 |
image=info.pop("image",data)
|
| 19 |
+
|
| 20 |
+
# color code:
|
| 21 |
+
color_code=info.pop("color_code",data)
|
| 22 |
+
|
| 23 |
+
# prompt
|
| 24 |
+
text_prompt=info.pop("text",data)
|
| 25 |
+
|
| 26 |
+
# image
|
| 27 |
image=base64.b64decode(image)
|
| 28 |
raw_images = Image.open(BytesIO(image)).convert('RGB')
|
| 29 |
+
raw_images = raw_images.convert("RGB")
|
| 30 |
+
raw_images = raw_images.resize((512, 512))
|
| 31 |
|
| 32 |
+
result_prompt, negative_prompt = self.build_prompt(text_prompt, color_code)
|
| 33 |
+
|
| 34 |
+
images = self.pipe(result_prompt, negative_prompt = negative_prompt, image=raw_images, num_inference_steps=25, image_guidance_scale=1).images
|
| 35 |
img=images[0]
|
| 36 |
img.save("./1.png")
|
| 37 |
with open('./1.png','rb') as img_file:
|
| 38 |
encoded_string = base64.b64encode(img_file.read()).decode('utf-8')
|
| 39 |
return {'image':encoded_string}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def build_prompt(self, text_prompt, color_code):
|
| 43 |
+
color_name = self.hex_to_name(color_code)
|
| 44 |
+
q_prompt = "No blur, high quality, no distortion over objects, text remains clear and undistorted. Do not modify or alter the logo."
|
| 45 |
+
base_prompts = text_prompt + q_prompt
|
| 46 |
+
coloring_prompt = f" with a {color_name} color applied to only the designated area or key element in the picture by avoiding it becoming the dominant color of the image, leaving the text, logo, and shadows untouched."
|
| 47 |
+
result_prompt = f"{base_prompts}{coloring_prompt}"
|
| 48 |
+
negative_prompt = 'blue shadows, worst quality, low quality, low res, blurry, watermark, banner, extra digits, cropped, jpeg artifacts, signature, username, error, sketch ,duplicate, ugly, monochrome, horror, geometry, mutation, disgusting'
|
| 49 |
+
|
| 50 |
+
return result_prompt, negative_prompt
|
| 51 |
+
|
| 52 |
+
def hex_to_name(self, hex_color):
|
| 53 |
+
rgb_tuple = tuple(int(hex_color[i:i+2], 16) for i in (1, 3, 5))
|
| 54 |
+
names = []
|
| 55 |
+
rgb_values = []
|
| 56 |
+
|
| 57 |
+
for color_hex, color_name in CSS3_HEX_TO_NAMES.items():
|
| 58 |
+
names.append(color_name)
|
| 59 |
+
rgb_values.append(hex_to_rgb(color_hex))
|
| 60 |
+
|
| 61 |
+
kdt_db = KDTree(rgb_values)
|
| 62 |
+
distance, index = kdt_db.query(rgb_tuple)
|
| 63 |
+
return names[index]
|
| 64 |
|
| 65 |
|
| 66 |
|