File size: 6,035 Bytes
158f9b4 850b601 158f9b4 a7d091d 158f9b4 6a8a0fa 1ad6f34 43e69fc 6a8a0fa 1ad6f34 158f9b4 8d5cd20 158f9b4 808dc28 158f9b4 317bb70 8d5cd20 158f9b4 317bb70 8d5cd20 158f9b4 1ad6f34 158f9b4 808dc28 158f9b4 1ad6f34 158f9b4 a7d091d 43e69fc 158f9b4 43e69fc 158f9b4 0b11b0c 9617251 158f9b4 8d5cd20 158f9b4 a7d091d 158f9b4 a7d091d 158f9b4 8d5cd20 850b601 6a8a0fa 158f9b4 30c21cf 8d5cd20 158f9b4 74a33c4 30c21cf a7d091d 30c21cf 8d5cd20 30c21cf 8d5cd20 30c21cf |
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
import cv2
import torch
import random
import numpy as np
from PIL import Image
from pathlib import Path
import requests
from io import BytesIO
from huggingface_hub import hf_hub_download, snapshot_download
from ip_adapter.ip_adapter import IPAdapterXL
from safetensors.torch import load_file
import os
from diffusers import (
ControlNetModel,
StableDiffusionXLControlNetPipeline,
EulerDiscreteScheduler
)
# global variable
MAX_SEED = np.iinfo(np.int32).max
device = "cuda" if torch.cuda.is_available() else "cpu"
# dtype = torch.float16 if str(device).__contains__("cuda") else torch.float16
# device = torch.device("cpu")
dtype = torch.float16
# initialization
base_model_path = "stabilityai/stable-diffusion-xl-base-1.0"
# base_model_path = "Yamer-AI/SDXL_Unstable_Diffusers"
# image_encoder_path = "sdxl_models/image_encoder"
# ip_ckpt = "sdxl_models/ip-adapter_sdxl.bin"
controlnet_path = "diffusers/controlnet-canny-sdxl-1.0"
class EndpointHandler():
def __init__(self, model_dir):
repo_id = "h94/IP-Adapter"
# repo_path = snapshot_download(repo_id="Yamer-AI/SDXL_Unstable_Diffusers")
# print(f"Repositorio clonado en: {repo_path}")
local_repo_path = snapshot_download(repo_id=repo_id)
self.image_encoder_local_path = os.path.join(local_repo_path, "sdxl_models", "image_encoder")
# self.image_encoder_local_path = os.path.join("sdxl_models", "image_encoder")
self.ip_ckpt = os.path.join("sdxl_models", "ip-adapter_sdxl.safetensors")
self.controlnet = ControlNetModel.from_pretrained(
controlnet_path, use_safetensors=False, torch_dtype=torch.float16
).to(device)
self.pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
base_model_path,
controlnet=self.controlnet,
torch_dtype=torch.float16,
variant="fp16",
add_watermarker=False,
).to(device)
self.pipe.set_progress_bar_config(disable=True)
self.pipe.scheduler = EulerDiscreteScheduler.from_config(
self.pipe.scheduler.config, timestep_spacing="trailing", prediction_type="epsilon"
)
state_dict = load_file(
hf_hub_download(
"ByteDance/SDXL-Lightning", "sdxl_lightning_2step_unet.safetensors"
)
)
self.pipe.unet.load_state_dict(state_dict)
self.pipe.unet.to(device)
self.ip_model = IPAdapterXL(
self.pipe,
self.image_encoder_local_path,
self.ip_ckpt,
device,
target_blocks=["up_blocks.0.attentions.1"],
)
def __call__(self, data):
def create_image(
image_pil,
input_image,
prompt,
n_prompt,
scale,
control_scale,
guidance_scale,
num_inference_steps,
seed,
neg_content_prompt=None,
neg_content_scale=0,
):
# seed = random.randint(0, MAX_SEED) if seed == -1 else seed
print(f"Seed: {seed}")
# # if input_image is not None:
# input_image = resize_img(input_image, max_side=1024)
# cv_input_image = pil_to_cv2(input_image)
# detected_map = cv2.Canny(cv_input_image, 50, 200)
# canny_map = Image.fromarray(cv2.cvtColor(detected_map, cv2.COLOR_BGR2RGB))
# # else:
canny_map = Image.new("RGB", (1024, 1024), color=(255, 255, 255))
control_scale = 0
# if float(control_scale) == 0:
# canny_map = canny_map.resize((1024, 1024))
# if len(neg_content_prompt) > 0 and neg_content_scale != 0:
# images = self.ip_model.generate(
# pil_image=image_pil,
# prompt=prompt,
# negative_prompt=n_prompt,
# scale=scale,
# guidance_scale=guidance_scale,
# num_samples=1,
# num_inference_steps=num_inference_steps,
# seed=seed,
# image=canny_map,
# controlnet_conditioning_scale=float(control_scale),
# neg_content_prompt=neg_content_prompt,
# neg_content_scale=neg_content_scale,
# )
# else:
print("Creating image... (inside create_image function)")
images = self.ip_model.generate(
pil_image=image_pil,
prompt=prompt,
negative_prompt=n_prompt,
scale=scale,
guidance_scale=guidance_scale,
num_samples=1,
num_inference_steps=num_inference_steps,
seed=seed,
image=canny_map,
controlnet_conditioning_scale=float(control_scale),
)
image = images[0]
return image
style_image_url = "https://i.ibb.co/yNjNksz/chrome-xaz-Ic-SNk-SY-1.jpg"
response = requests.get(style_image_url)
style_image_pil = Image.open(BytesIO(response.content))
print("Images loaded...")
source_image =None
prompt = "A Art Deco style artwork of tennis court with tennis balls and rackets, a modern car, (featuring Notre Dame Cathedral:1.5)"
scale =0.3
control_scale =0.0
try:
print("Creating image... (outside try block)")
return create_image(
image_pil=style_image_pil,
input_image=source_image,
prompt=prompt,
n_prompt="",
scale=scale,
control_scale=control_scale,
guidance_scale=0.0,
num_inference_steps=25,
seed=1109176307,
neg_content_prompt="",
neg_content_scale=0,
)
except Exception as e:
return None |