File size: 16,643 Bytes
1606e2d 4b2563c 5998d0e 1606e2d 7df1bb1 a584e78 7df1bb1 a584e78 7df1bb1 5a50ff9 1606e2d 7df1bb1 1606e2d 7df1bb1 1606e2d 7df1bb1 1606e2d 5a50ff9 1606e2d 5998d0e 1606e2d eff043b 1606e2d bbca4ce 1606e2d d1646f5 1606e2d 69fb4e6 7df1bb1 69fb4e6 7df1bb1 69fb4e6 7df1bb1 69fb4e6 7df1bb1 69fb4e6 7df1bb1 69fb4e6 7df1bb1 69fb4e6 7df1bb1 69fb4e6 7df1bb1 69fb4e6 5998d0e 69fb4e6 7df1bb1 69fb4e6 7df1bb1 1dc597e 69fb4e6 7df1bb1 69fb4e6 7df1bb1 69fb4e6 7df1bb1 bbca4ce 7df1bb1 661a282 7df1bb1 69fb4e6 1606e2d |
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
import spaces
import os
import json
import time
import copy
import torch
from diffusers import AutoPipelineForText2Image, StableDiffusionPipeline,DiffusionPipeline, StableDiffusionXLPipeline, AutoencoderKL, AutoencoderTiny, UNet2DConditionModel
from huggingface_hub import hf_hub_download, snapshot_download
from pathlib import Path
from diffusers import EulerAncestralDiscreteScheduler, DPMSolverMultistepScheduler, DPMSolverSDEScheduler
from diffusers.models.attention_processor import AttnProcessor2_0
import os
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import utils
import base64
import json
import jwt
import glob
import traceback
#from onediffx import compile_pipe, save_pipe, load_pipe
HF_TOKEN = os.getenv('HF_TOKEN')
VAR_PUBLIC_KEY = os.getenv('PUBLIC_KEY')
DATASET_ID = 'nsfwalex/checkpoint_n_lora'
class AuthHelper:
def load_public_key_from_file(self):
public_key_bytes = VAR_PUBLIC_KEY.encode('utf-8') # Convert to bytes if it's a string
public_key = serialization.load_pem_public_key(
public_key_bytes,
backend=default_backend()
)
return public_key
def __init__(self):
self.public_key = self.load_public_key_from_file()
# check authkey
# 1. decode with public key
# 2. check timestamp
# 3. check current host, referer, ip it should be the same as values in jwt
def decode_jwt(self, token, algorithms=["RS256"]):
"""
Decode and verify a JWT using a public key.
:param public_key: The public key used for verification.
:param token: The JWT string to decode.
:param algorithms: List of acceptable algorithms (default is ["RS256"]).
:return: The decoded JWT payload if verification is successful.
:raises: Exception if verification fails.
"""
try:
# Decode the JWT
decoded_payload = jwt.decode(
token,
self.public_key,
algorithms=algorithms,
options={"verify_signature": True} # Explicitly enable signature verification
)
return decoded_payload
except Exception as e:
print("Invalid token:", e)
raise
def check_auth(self, session, token):
params = session.get("params") or {}
if params.get("_skip_token_passkey", "") == "nsfwaisio_125687":
return True
sip = session.get("client_ip", "")
shost = session.get("host", "")
sreferer = session.get("refer")
print(sip, shost, sreferer)
jwt_data = self.decode_jwt(token)
tip = jwt_data.get("ip", "")
thost = jwt_data.get("host", "")
treferer = jwt_data.get("referer", "")
print(sip, tip, shost, thost, sreferer, treferer)
if not tip or not thost or not treferer:
raise Exception("invalid token")
if sip == tip and shost == thost and sreferer == treferer:
return True
raise Exception("wrong token")
class InferenceManager:
def __init__(self, config_path="config.json"):
cfg = {}
with open(config_path, "r", encoding="utf-8") as f:
cfg = json.load(f)
self.cfg = cfg
lora_options_path = cfg.get("loras", "")
self.model_version = cfg["model_version"]
self.lora_load_options = self.load_json(lora_options_path) # Load LoRA load options
self.lora_models = self.load_index_file("index.json") # Load index.json
self.preloaded_loras = [] # Array to store preloaded LoRAs with name and weights
self.base_model_pipeline = self.load_base_model() # Load the base model
self.preload_loras() # Preload LoRAs based on options
def load_json(self, filepath):
"""Load JSON file into a dictionary."""
if os.path.exists(filepath):
with open(filepath, "r", encoding="utf-8") as f:
return json.load(f)
return {}
def load_index_file(self, index_file):
"""Download index.json from Hugging Face and return the file path."""
index_path = download_from_hf(index_file)
if index_path:
with open(index_path, "r", encoding="utf-8") as f:
return json.load(f)
return {}
@spaces.GPU(duration=40)
def compile_onediff(self):
self.base_model_pipeline.to("cuda")
pipe = self.base_model_pipeline
# load the compiled pipe
load_pipe(pipe, dir="cached_pipe")
print("Start oneflow compiling...")
start_compile = time.time()
pipe = compile_pipe(pipe)
# run once to trigger compilation
image = pipe(
prompt="street style, detailed, raw photo, woman, face, shot on CineStill 800T",
height=512,
width=512,
num_inference_steps=10,
output_type="pil",
).images
image[0].save(f"test_image.png")
compile_time = time.time() - start_compile
#self.base_model_pipeline.to("cpu")
# save the compiled pipe
save_pipe(pipe, dir="cached_pipe")
self.base_model_pipeline = pipe
print(f"OneDiff compile in {compile_time}s")
def load_base_model(self):
"""Load base model and return the pipeline."""
start = time.time()
cfg = self.cfg
model_version = self.model_version
ckpt_dir = snapshot_download(repo_id=cfg["model_id"], local_files_only=False)
if model_version == "1.5":
vae = AutoencoderKL.from_pretrained(os.path.join(ckpt_dir, "vae"), torch_dtype=torch.bfloat16)
pipe = StableDiffusionPipeline.from_pretrained(ckpt_dir, vae=vae, torch_dtype=torch.bfloat16, use_safetensors=True)
else:
#vae = AutoencoderKL.from_pretrained(os.path.join(ckpt_dir, "vae"), torch_dtype=torch.bfloat16)
vae = AutoencoderTiny.from_pretrained("madebyollin/taesdxl", torch_dtype=torch.bfloat16)
print(ckpt_dir)
pipe = DiffusionPipeline.from_pretrained(
ckpt_dir,
vae=vae,
#unet=unet,
torch_dtype=torch.bfloat16,
use_safetensors=True,
#variant="fp16",
custom_pipeline = "lpw_stable_diffusion_xl",
)
clip_skip = cfg.get("clip_skip", 1)
# Adjust clip skip for XL (assumed not relevant for SD 1.5)
pipe.text_encoder.config.num_hidden_layers -= (clip_skip - 1)
load_time = round(time.time() - start, 2)
print(f"Base model loaded in {load_time}s")
return pipe
def preload_loras(self):
"""Preload all LoRAs marked as 'preload=True' and store for later use."""
for lora_name, lora_info in self.lora_load_options.items():
try:
start = time.time()
# Find the corresponding LoRA in index.json
lora_index_info = next((l for l in self.lora_models['lora'] if l['name'] == lora_name), None)
if not lora_index_info:
raise ValueError(f"LoRA {lora_name} not found in index.json.")
# Check if the LoRA base model matches the current model version
if self.model_version not in lora_info['base_model'] or not lora_info.get('preload', False):
print(f"Skipping {lora_name} as it's not compatible with the current model version.")
continue
# Load LoRA weights from the specified path
weight_path = download_from_hf(lora_index_info['path'], local_dir=None)
if not weight_path:
raise ValueError(f"Failed to download LoRA weights for {lora_name}")
load_time = round(time.time() - start, 2)
print(f"Downloaded {lora_name} in {load_time}s")
self.base_model_pipeline.load_lora_weights(
weight_path,
weight_name=lora_index_info["path"],
adapter_name=lora_name
)
# Store the preloaded LoRA name and weight for merging later
if lora_info.get("preload", False):
self.preloaded_loras.append({
"name": lora_name,
"weight": lora_info.get("weight", 1.0)
})
load_time = round(time.time() - start, 2)
print(f"Preloaded LoRA {lora_name} with weight {lora_info.get('weight', 1.0)} in {load_time}s.")
except Exception as e:
print(f"Lora {lora_name} not loaded, skipping... {e}")
def build_pipeline_with_lora(self, lora_list, sampler=None, new_pipeline=False):
"""Build the pipeline with specific LoRAs, loading any that are not preloaded."""
# Deep copy the base pipeline
start = time.time()
if new_pipeline:
temp_pipeline = copy.deepcopy(self.base_model_pipeline)
else:
temp_pipeline = self.base_model_pipeline
copy_time = round(time.time() - start, 2)
print(f"pipeline copied in {copy_time}s")
# Track LoRAs to be loaded dynamically
dynamic_loras = []
# Check if any LoRAs in lora_list need to be loaded dynamically
for lora_name in lora_list:
if not any(l['name'] == lora_name for l in self.preloaded_loras):
lora_info = next((l for l in self.lora_models['lora'] if l['name'] == lora_name), None)
if lora_info and self.model_version in lora_info["attr"].get("base_model", []):
dynamic_loras.append({
"name": lora_name,
"filename": lora_info["path"],
"scale": 1.0 # Assuming default weight as 1.0 for dynamic LoRAs
})
# Fuse preloaded and dynamic LoRAs
all_loras = [{"name": x["name"], "scale": x["weight"], "preloaded": True} for x in self.preloaded_loras] + dynamic_loras
set_lora_weights(temp_pipeline, all_loras,False)
build_time = round(time.time() - start, 2)
print(f"Pipeline built with LoRAs in {build_time}s.")
if not sampler:
sampler = self.cfg.get("sampler", "DPM2 a")
# Define samplers
samplers = {
"Euler a": EulerAncestralDiscreteScheduler.from_config(temp_pipeline.scheduler.config),
"DPM++ SDE Karras": DPMSolverSDEScheduler.from_config(temp_pipeline.scheduler.config, use_karras_sigmas=True),
"DPM2 a": DPMSolverMultistepScheduler.from_config(temp_pipeline.scheduler.config)
}
# Set the scheduler based on the selected sampler
temp_pipeline.scheduler = samplers[sampler]
# Move the final pipeline to the GPU
temp_pipeline
return temp_pipeline
def release(self, temp_pipeline):
"""Release the deepcopied pipeline to recycle memory."""
del temp_pipeline
torch.cuda.empty_cache()
print("Memory released and cache cleared.")
class ModelManager:
def __init__(self, model_directory):
"""
Initialize the ModelManager by scanning all `.model.json` files in the given directory.
:param model_directory: The directory to scan for model config files (e.g., "/path/to/models").
"""
self.models = {}
self.model_directory = model_directory
self.load_models()
def load_models(self):
"""
Scan the model directory for `.model.json` files and initialize InferenceManager instances for each one.
:param model_directory: Directory to scan for `.model.json` files.
"""
model_files = glob.glob(os.path.join(self.model_directory, "*.model.json"))
if not model_files:
print(f"No model configuration files found in {self.model_directory}")
return
for file_path in model_files:
model_name = self.get_model_name_from_url(file_path).split(".")[0]
print(f"Initializing model: {model_name} from {file_path}")
try:
# Initialize InferenceManager for each model
self.models[model_name] = InferenceManager(config_path=file_path)
except Exception as e:
print(traceback.format_exc())
print(f"Failed to initialize model {model_name} from {file_path}: {e}")
def get_model_name_from_url(self, url):
"""
Extract the model name from the config file path (filename without extension).
:param url: The file path of the configuration file.
:return: The model name (file name without extension).
"""
filename = os.path.basename(url)
model_name, _ = os.path.splitext(filename)
return model_name
def get_model_pipeline(self, model_id, lora_list, sampler=None, new_pipeline=False):
"""
Build the pipeline with specific LoRAs for a model.
:param model_id: The model ID (the model name extracted from the config URL).
:param lora_list: List of LoRAs to be applied to the model pipeline.
:param sampler: The sampler to be used for the pipeline.
:param new_pipeline: Flag to indicate whether to create a new pipeline or reuse the existing one.
:return: The built pipeline with LoRAs applied.
"""
model = self.models.get(model_id)
if not model:
print(f"Model {model_id} not found.")
return None
try:
print(f"Building pipeline with LoRAs for model {model_id}...")
return model.build_pipeline_with_lora(lora_list, sampler, new_pipeline)
except Exception as e:
print(traceback.format_exc())
print(f"Failed to build pipeline for model {model_id}: {e}")
return None
def release_model(self, model_id):
"""
Release resources and clear memory for a specific model.
:param model_id: The model ID (the model name extracted from the config URL).
"""
model = self.models.get(model_id)
if not model:
print(f"Model {model_id} not found.")
return
try:
print(f"Releasing model {model_id}...")
model.release(model.base_model_pipeline)
except Exception as e:
print(f"Failed to release model {model_id}: {e}")
# Hugging Face file download function - returns only file path
def download_from_hf(filename, local_dir=None):
try:
file_path = hf_hub_download(
filename=filename,
repo_id=DATASET_ID,
repo_type="dataset",
revision="main",
local_dir=local_dir,
local_files_only=False, # Attempt to load from cache if available
)
return file_path # Return file path only
except Exception as e:
print(f"Failed to load {filename} from Hugging Face: {str(e)}")
return None
# Function to load and fuse LoRAs
def set_lora_weights(pipe, lorajson: list[dict], fuse=False):
try:
if not lorajson or not isinstance(lorajson, list):
return
a_list = []
w_list = []
for d in lorajson:
if not d or not isinstance(d, dict) or not d["name"] or d["name"] == "None":
continue
k = d["name"]
if not d.get("preloaded", False):
start = time.time()
weight_path = download_from_hf(d['filename'], local_dir=None)
if weight_path:
pipe.load_lora_weights(weight_path, weight_name=d['filename'], adapter_name=k)
load_time = round(time.time() - start, 2)
print(f"LoRA {k} loaded in {load_time}s.")
a_list.append(k)
w_list.append(d["scale"])
if not a_list:
return
start = time.time()
pipe.set_adapters(a_list, adapter_weights=w_list)
if fuse:
pipe.fuse_lora(adapter_names=a_list, lora_scale=1.0)
fuse_time = round(time.time() - start, 2)
print(f"LoRAs fused in {fuse_time}s.")
except Exception as e:
print(f"External LoRA Error: {e}")
raise Exception(f"External LoRA Error: {e}") from e
|