Upload folder using huggingface_hub
Browse files- deploy.bat +1 -0
- handler.py +27 -20
deploy.bat
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
F:\Projects\UAI\UAIBrainServer\Code\Env\Scripts\huggingface-cli.exe upload API_SDXLLightning . .
|
handler.py
CHANGED
@@ -4,6 +4,10 @@ import sys
|
|
4 |
rootDir = os.path.abspath(os.path.dirname(__file__))
|
5 |
sys.path.append(rootDir)
|
6 |
from imageRequest import ImageRequest
|
|
|
|
|
|
|
|
|
7 |
|
8 |
class EndpointHandler:
|
9 |
def __init__(self, path=""):
|
@@ -12,7 +16,27 @@ class EndpointHandler:
|
|
12 |
# self.model= load_model(path)
|
13 |
self.pipe = None
|
14 |
self.modelName = ""
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
18 |
"""
|
@@ -48,26 +72,9 @@ class EndpointHandler:
|
|
48 |
Run SDXL Lightning pipeline
|
49 |
"""
|
50 |
import torch
|
51 |
-
|
52 |
-
from huggingface_hub import hf_hub_download
|
53 |
-
from safetensors.torch import load_file
|
54 |
-
|
55 |
-
base = "stabilityai/stable-diffusion-xl-base-1.0"
|
56 |
-
repo = "ByteDance/SDXL-Lightning"
|
57 |
-
ckpt = "sdxl_lightning_4step_unet.safetensors" # Use the correct ckpt for your step setting!
|
58 |
-
if request.model == "default":
|
59 |
-
request.model = base
|
60 |
-
else:
|
61 |
-
base = request.model
|
62 |
-
if self.pipe is None or self.modelName != request.model:
|
63 |
-
# Load model.
|
64 |
-
unet = UNet2DConditionModel.from_config(base, subfolder="unet").to("cuda", torch.float16)
|
65 |
-
unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device="cuda"))
|
66 |
-
pipe = StableDiffusionXLPipeline.from_pretrained(base, unet=unet, torch_dtype=torch.float16, variant="fp16").to("cuda")
|
67 |
|
68 |
-
|
69 |
-
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
|
70 |
-
self.pipe = pipe
|
71 |
|
72 |
# Ensure using the same inference steps as the loaded model and CFG set to 0.
|
73 |
images = pipe(request.prompt, negative_prompt = request.negative_prompt, num_inference_steps=request.steps, guidance_scale=0).images
|
|
|
4 |
rootDir = os.path.abspath(os.path.dirname(__file__))
|
5 |
sys.path.append(rootDir)
|
6 |
from imageRequest import ImageRequest
|
7 |
+
from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel, EulerDiscreteScheduler
|
8 |
+
from huggingface_hub import hf_hub_download
|
9 |
+
from safetensors.torch import load_file
|
10 |
+
import torch
|
11 |
|
12 |
class EndpointHandler:
|
13 |
def __init__(self, path=""):
|
|
|
16 |
# self.model= load_model(path)
|
17 |
self.pipe = None
|
18 |
self.modelName = ""
|
19 |
+
baseReq = ImageRequest()
|
20 |
+
baseReq.model = "SG161222/RealVisXL_V4.0"
|
21 |
+
self.LoadModel(baseReq)
|
22 |
+
|
23 |
+
def LoadModel(self, request):
|
24 |
+
base = "stabilityai/stable-diffusion-xl-base-1.0"
|
25 |
+
repo = "ByteDance/SDXL-Lightning"
|
26 |
+
ckpt = "sdxl_lightning_8step_unet.safetensors" # Use the correct ckpt for your step setting!
|
27 |
+
if request.model == "default":
|
28 |
+
request.model = base
|
29 |
+
else:
|
30 |
+
base = request.model
|
31 |
+
if self.pipe is None or self.modelName != request.model:
|
32 |
+
# Load model.
|
33 |
+
unet = UNet2DConditionModel.from_config(base, subfolder="unet").to("cuda", torch.float16)
|
34 |
+
unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device="cuda"))
|
35 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(base, unet=unet, torch_dtype=torch.float16, variant="fp16").to("cuda")
|
36 |
+
|
37 |
+
# Ensure sampler uses "trailing" timesteps.
|
38 |
+
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
|
39 |
+
self.pipe = pipe
|
40 |
|
41 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
42 |
"""
|
|
|
72 |
Run SDXL Lightning pipeline
|
73 |
"""
|
74 |
import torch
|
75 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
+
|
|
|
|
|
78 |
|
79 |
# Ensure using the same inference steps as the loaded model and CFG set to 0.
|
80 |
images = pipe(request.prompt, negative_prompt = request.negative_prompt, num_inference_steps=request.steps, guidance_scale=0).images
|