Update handler.py
Browse files- handler.py +23 -19
handler.py
CHANGED
|
@@ -2,26 +2,32 @@ import torch
|
|
| 2 |
import base64
|
| 3 |
import io
|
| 4 |
from PIL import Image
|
| 5 |
-
from diffusers import StableDiffusionXLImg2ImgPipeline
|
| 6 |
import os
|
| 7 |
|
| 8 |
class EndpointHandler():
|
| 9 |
def __init__(self, path=""):
|
| 10 |
-
|
| 11 |
-
print("Loading ARX Pipeline...")
|
| 12 |
|
| 13 |
-
# 1. Point directly to the model you uploaded to the repo
|
| 14 |
model_path = os.path.join(path, "biglust.safetensors")
|
| 15 |
|
| 16 |
-
#
|
| 17 |
self.pipe = StableDiffusionXLImg2ImgPipeline.from_single_file(
|
| 18 |
model_path,
|
| 19 |
torch_dtype=torch.float16,
|
| 20 |
use_safetensors=True,
|
| 21 |
safety_checker=None
|
| 22 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
# 3. Load IP-Adapter
|
| 25 |
self.pipe.load_ip_adapter(
|
| 26 |
"h94/IP-Adapter",
|
| 27 |
subfolder="sdxl_models",
|
|
@@ -29,7 +35,7 @@ class EndpointHandler():
|
|
| 29 |
)
|
| 30 |
|
| 31 |
self.pipe.to("cuda")
|
| 32 |
-
print("ARX
|
| 33 |
|
| 34 |
def decode_base64_image(self, image_string):
|
| 35 |
if "," in image_string:
|
|
@@ -43,31 +49,29 @@ class EndpointHandler():
|
|
| 43 |
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 44 |
|
| 45 |
def __call__(self, data):
|
| 46 |
-
"""
|
| 47 |
-
data param format: {"inputs": { "prompt": "...", "init_image": "..." }}
|
| 48 |
-
"""
|
| 49 |
-
# HF wraps payloads in an "inputs" key automatically
|
| 50 |
inputs = data.pop("inputs", data)
|
| 51 |
|
| 52 |
-
prompt = inputs.get("prompt", "masterpiece, best quality")
|
| 53 |
-
negative_prompt = inputs.get("negative_prompt", "lowres, bad anatomy, worst quality, ugly")
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
|
| 59 |
init_image_b64 = inputs.get("init_image")
|
| 60 |
ip_adapter_image_b64 = inputs.get("ip_adapter_image")
|
| 61 |
|
| 62 |
if not init_image_b64 or not ip_adapter_image_b64:
|
| 63 |
-
return {"error": "
|
| 64 |
|
| 65 |
init_image = self.decode_base64_image(init_image_b64).resize((1024, 1024))
|
| 66 |
ip_image = self.decode_base64_image(ip_adapter_image_b64).resize((1024, 1024))
|
| 67 |
|
| 68 |
self.pipe.set_ip_adapter_scale(ip_adapter_scale)
|
| 69 |
|
| 70 |
-
# Generate
|
| 71 |
result = self.pipe(
|
| 72 |
prompt=prompt,
|
| 73 |
negative_prompt=negative_prompt,
|
|
|
|
| 2 |
import base64
|
| 3 |
import io
|
| 4 |
from PIL import Image
|
| 5 |
+
from diffusers import StableDiffusionXLImg2ImgPipeline, DPMSolverMultistepScheduler
|
| 6 |
import os
|
| 7 |
|
| 8 |
class EndpointHandler():
|
| 9 |
def __init__(self, path=""):
|
| 10 |
+
print("Loading ARX Elite Pipeline...")
|
|
|
|
| 11 |
|
|
|
|
| 12 |
model_path = os.path.join(path, "biglust.safetensors")
|
| 13 |
|
| 14 |
+
# 1. Load the core pipeline
|
| 15 |
self.pipe = StableDiffusionXLImg2ImgPipeline.from_single_file(
|
| 16 |
model_path,
|
| 17 |
torch_dtype=torch.float16,
|
| 18 |
use_safetensors=True,
|
| 19 |
safety_checker=None
|
| 20 |
)
|
| 21 |
+
|
| 22 |
+
# 2. THE SECRET SAUCE: Upgrade the Scheduler to DPM++ 2M Karras
|
| 23 |
+
# This makes the images much sharper and helps maintain facial features
|
| 24 |
+
self.pipe.scheduler = DPMSolverMultistepScheduler.from_config(
|
| 25 |
+
self.pipe.scheduler.config,
|
| 26 |
+
use_karras_sigmas=True,
|
| 27 |
+
algorithm_type="sde-dpmsolver++"
|
| 28 |
+
)
|
| 29 |
|
| 30 |
+
# 3. Load IP-Adapter
|
| 31 |
self.pipe.load_ip_adapter(
|
| 32 |
"h94/IP-Adapter",
|
| 33 |
subfolder="sdxl_models",
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
self.pipe.to("cuda")
|
| 38 |
+
print("ARX Elite Ready.")
|
| 39 |
|
| 40 |
def decode_base64_image(self, image_string):
|
| 41 |
if "," in image_string:
|
|
|
|
| 49 |
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 50 |
|
| 51 |
def __call__(self, data):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
inputs = data.pop("inputs", data)
|
| 53 |
|
| 54 |
+
prompt = inputs.get("prompt", "masterpiece, best quality, highly detailed")
|
| 55 |
+
negative_prompt = inputs.get("negative_prompt", "blurry, lowres, bad anatomy, worst quality, ugly, deformed eyes")
|
| 56 |
+
|
| 57 |
+
# We increase steps slightly to 30 for better quality with this scheduler
|
| 58 |
+
strength = float(inputs.get("strength", 0.55))
|
| 59 |
+
guidance_scale = float(inputs.get("guidance_scale", 7.5))
|
| 60 |
+
num_inference_steps = int(inputs.get("steps", 30))
|
| 61 |
+
ip_adapter_scale = float(inputs.get("ip_adapter_scale", 0.75))
|
| 62 |
|
| 63 |
init_image_b64 = inputs.get("init_image")
|
| 64 |
ip_adapter_image_b64 = inputs.get("ip_adapter_image")
|
| 65 |
|
| 66 |
if not init_image_b64 or not ip_adapter_image_b64:
|
| 67 |
+
return {"error": "Missing image inputs."}
|
| 68 |
|
| 69 |
init_image = self.decode_base64_image(init_image_b64).resize((1024, 1024))
|
| 70 |
ip_image = self.decode_base64_image(ip_adapter_image_b64).resize((1024, 1024))
|
| 71 |
|
| 72 |
self.pipe.set_ip_adapter_scale(ip_adapter_scale)
|
| 73 |
|
| 74 |
+
# Generate with the new high-precision scheduler
|
| 75 |
result = self.pipe(
|
| 76 |
prompt=prompt,
|
| 77 |
negative_prompt=negative_prompt,
|