Update handler.py
Browse files- handler.py +54 -54
handler.py
CHANGED
|
@@ -1,74 +1,74 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
|
|
|
| 3 |
from diffusers import StableDiffusionXLControlNetPipeline, ControlNetModel
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
CONTROLNET_MODEL = "diffusers/controlnet-depth-sdxl-1.0" # ✅ RECOMMENDED WORKING MODEL
|
| 7 |
|
| 8 |
class EndpointHandler:
|
| 9 |
-
def __init__(self,
|
| 10 |
-
print("🔧 Initializing Juggernaut + ControlNet")
|
| 11 |
-
|
| 12 |
-
token = os.environ.get("HF_TOKEN")
|
| 13 |
-
if not token:
|
| 14 |
-
raise RuntimeError("❌ Missing HF_TOKEN")
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
torch.backends.cuda.enable_mem_efficient_sdp(False)
|
| 19 |
-
torch.backends.cuda.enable_math_sdp(True)
|
| 20 |
-
|
| 21 |
-
print("📥 Loading ControlNet …")
|
| 22 |
self.controlnet = ControlNetModel.from_pretrained(
|
| 23 |
-
|
| 24 |
torch_dtype=torch.float16,
|
| 25 |
use_safetensors=True,
|
| 26 |
-
token=
|
| 27 |
)
|
| 28 |
|
| 29 |
-
|
|
|
|
| 30 |
self.pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
| 31 |
-
|
| 32 |
controlnet=self.controlnet,
|
| 33 |
torch_dtype=torch.float16,
|
| 34 |
use_safetensors=True,
|
| 35 |
-
token=
|
| 36 |
).to("cuda")
|
| 37 |
|
| 38 |
-
|
| 39 |
-
self.pipe.
|
| 40 |
-
self.pipe.unet.to(memory_format=torch.channels_last)
|
| 41 |
|
| 42 |
-
print("
|
| 43 |
|
| 44 |
def __call__(self, data):
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
prompt=prompt,
|
| 62 |
-
negative_prompt=
|
| 63 |
-
image=
|
| 64 |
-
num_inference_steps=
|
| 65 |
-
guidance_scale=
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import io
|
| 3 |
+
import torch
|
| 4 |
from diffusers import StableDiffusionXLControlNetPipeline, ControlNetModel
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
|
|
|
| 9 |
|
| 10 |
class EndpointHandler:
|
| 11 |
+
def __init__(self, path=""):
|
| 12 |
+
print("🔧 Initializing Juggernaut + ControlNet…")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Load ControlNet
|
| 15 |
+
print("📥 Loading ControlNet…")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
self.controlnet = ControlNetModel.from_pretrained(
|
| 17 |
+
"lllyasviel/controlnet-depth-sdxl-1.0",
|
| 18 |
torch_dtype=torch.float16,
|
| 19 |
use_safetensors=True,
|
| 20 |
+
token=HF_TOKEN
|
| 21 |
)
|
| 22 |
|
| 23 |
+
# Load your big base model repo
|
| 24 |
+
print("📥 Loading Base Model (juggernaut-sfw)…")
|
| 25 |
self.pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
| 26 |
+
"Gjm1234/juggernaut-sfw",
|
| 27 |
controlnet=self.controlnet,
|
| 28 |
torch_dtype=torch.float16,
|
| 29 |
use_safetensors=True,
|
| 30 |
+
token=HF_TOKEN
|
| 31 |
).to("cuda")
|
| 32 |
|
| 33 |
+
# prevent OOM
|
| 34 |
+
self.pipe.enable_model_cpu_offload()
|
|
|
|
| 35 |
|
| 36 |
+
print("🚀 Pipeline Loaded Successfully!")
|
| 37 |
|
| 38 |
def __call__(self, data):
|
| 39 |
+
try:
|
| 40 |
+
prompt = data.get("prompt", "")
|
| 41 |
+
negative_prompt = data.get("negative_prompt", "")
|
| 42 |
+
num_images = data.get("num_images", 10)
|
| 43 |
+
|
| 44 |
+
# Handle optional image
|
| 45 |
+
image_data = data.get("image", None)
|
| 46 |
+
|
| 47 |
+
if image_data:
|
| 48 |
+
# Base64 → PIL
|
| 49 |
+
image_bytes = base64.b64decode(image_data)
|
| 50 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 51 |
+
else:
|
| 52 |
+
# ControlNet requires something — generate depth = blank
|
| 53 |
+
image = Image.new("RGB", (1024, 1024), "white")
|
| 54 |
+
|
| 55 |
+
# Run generation
|
| 56 |
+
output = self.pipe(
|
| 57 |
prompt=prompt,
|
| 58 |
+
negative_prompt=negative_prompt,
|
| 59 |
+
image=image,
|
| 60 |
+
num_inference_steps=28,
|
| 61 |
+
guidance_scale=6.5,
|
| 62 |
+
num_images_per_prompt=num_images,
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
images = []
|
| 66 |
+
for img in output.images:
|
| 67 |
+
buf = io.BytesIO()
|
| 68 |
+
img.save(buf, format="PNG")
|
| 69 |
+
images.append(base64.b64encode(buf.getvalue()).decode("utf-8"))
|
| 70 |
+
|
| 71 |
+
return {"images": images}
|
| 72 |
+
|
| 73 |
+
except Exception as e:
|
| 74 |
+
return {"error": str(e)}
|