Upload folder using huggingface_hub
Browse files- .gitattributes +1 -3
- config.json +37 -0
- diffusion_pytorch_model.safetensors +3 -0
- handler.py +81 -0
- requirements.txt +3 -0
.gitattributes
CHANGED
@@ -2,7 +2,6 @@
|
|
2 |
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
*.h5 filter=lfs diff=lfs merge=lfs -text
|
@@ -22,10 +21,8 @@
|
|
22 |
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
*.wasm filter=lfs diff=lfs merge=lfs -text
|
@@ -33,3 +30,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
2 |
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
|
|
5 |
*.ftz filter=lfs diff=lfs merge=lfs -text
|
6 |
*.gz filter=lfs diff=lfs merge=lfs -text
|
7 |
*.h5 filter=lfs diff=lfs merge=lfs -text
|
|
|
21 |
*.pt filter=lfs diff=lfs merge=lfs -text
|
22 |
*.pth filter=lfs diff=lfs merge=lfs -text
|
23 |
*.rar filter=lfs diff=lfs merge=lfs -text
|
|
|
24 |
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
25 |
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
|
|
26 |
*.tflite filter=lfs diff=lfs merge=lfs -text
|
27 |
*.tgz filter=lfs diff=lfs merge=lfs -text
|
28 |
*.wasm filter=lfs diff=lfs merge=lfs -text
|
|
|
30 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
31 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
32 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
33 |
+
diffusion_pytorch_model.safetensors filter=lfs diff=lfs merge=lfs -text
|
config.json
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_class_name": "AutoencoderKL",
|
3 |
+
"_diffusers_version": "0.30.0.dev0",
|
4 |
+
"act_fn": "silu",
|
5 |
+
"block_out_channels": [
|
6 |
+
128,
|
7 |
+
256,
|
8 |
+
512,
|
9 |
+
512
|
10 |
+
],
|
11 |
+
"down_block_types": [
|
12 |
+
"DownEncoderBlock2D",
|
13 |
+
"DownEncoderBlock2D",
|
14 |
+
"DownEncoderBlock2D",
|
15 |
+
"DownEncoderBlock2D"
|
16 |
+
],
|
17 |
+
"force_upcast": true,
|
18 |
+
"in_channels": 3,
|
19 |
+
"latent_channels": 16,
|
20 |
+
"latents_mean": null,
|
21 |
+
"latents_std": null,
|
22 |
+
"layers_per_block": 2,
|
23 |
+
"mid_block_add_attention": true,
|
24 |
+
"norm_num_groups": 32,
|
25 |
+
"out_channels": 3,
|
26 |
+
"sample_size": 1024,
|
27 |
+
"scaling_factor": 0.3611,
|
28 |
+
"shift_factor": 0.1159,
|
29 |
+
"up_block_types": [
|
30 |
+
"UpDecoderBlock2D",
|
31 |
+
"UpDecoderBlock2D",
|
32 |
+
"UpDecoderBlock2D",
|
33 |
+
"UpDecoderBlock2D"
|
34 |
+
],
|
35 |
+
"use_post_quant_conv": false,
|
36 |
+
"use_quant_conv": false
|
37 |
+
}
|
diffusion_pytorch_model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f5b59a26851551b67ae1fe58d32e76486e1e812def4696a4bea97f16604d40a3
|
3 |
+
size 167666902
|
handler.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, List, Any
|
2 |
+
import torch
|
3 |
+
from base64 import b64decode
|
4 |
+
from diffusers import AutoencoderKL
|
5 |
+
from diffusers.image_processor import VaeImageProcessor
|
6 |
+
|
7 |
+
|
8 |
+
class EndpointHandler:
|
9 |
+
def __init__(self, path=""):
|
10 |
+
self.device = "cuda"
|
11 |
+
self.dtype = torch.bfloat16
|
12 |
+
self.vae = (
|
13 |
+
AutoencoderKL.from_pretrained(path, torch_dtype=self.dtype)
|
14 |
+
.to(self.device, self.dtype)
|
15 |
+
.eval()
|
16 |
+
)
|
17 |
+
|
18 |
+
self.vae_scale_factor = 2 ** (len(self.vae.config.block_out_channels) - 1)
|
19 |
+
self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor)
|
20 |
+
|
21 |
+
@staticmethod
|
22 |
+
def _unpack_latents(latents, height, width, vae_scale_factor):
|
23 |
+
batch_size, num_patches, channels = latents.shape
|
24 |
+
|
25 |
+
# VAE applies 8x compression on images but we must also account for packing which requires
|
26 |
+
# latent height and width to be divisible by 2.
|
27 |
+
height = 2 * (int(height) // (vae_scale_factor * 2))
|
28 |
+
width = 2 * (int(width) // (vae_scale_factor * 2))
|
29 |
+
|
30 |
+
latents = latents.view(batch_size, height // 2, width // 2, channels // 4, 2, 2)
|
31 |
+
latents = latents.permute(0, 3, 1, 4, 2, 5)
|
32 |
+
|
33 |
+
latents = latents.reshape(batch_size, channels // (2 * 2), height, width)
|
34 |
+
|
35 |
+
return latents
|
36 |
+
|
37 |
+
@torch.no_grad()
|
38 |
+
def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
|
39 |
+
"""
|
40 |
+
Args:
|
41 |
+
data (:obj:):
|
42 |
+
includes the input data and the parameters for the inference.
|
43 |
+
"""
|
44 |
+
tensor = data["inputs"]
|
45 |
+
tensor = b64decode(tensor.encode("utf-8"))
|
46 |
+
parameters = data.get("parameters", {})
|
47 |
+
if "shape" not in parameters:
|
48 |
+
raise ValueError("Expected `shape` in parameters.")
|
49 |
+
if "dtype" not in parameters:
|
50 |
+
raise ValueError("Expected `dtype` in parameters.")
|
51 |
+
if "height" not in parameters:
|
52 |
+
raise ValueError("Expected `height` in parameters.")
|
53 |
+
if "width" not in parameters:
|
54 |
+
raise ValueError("Expected `width` in parameters.")
|
55 |
+
|
56 |
+
DTYPE_MAP = {
|
57 |
+
"float16": torch.float16,
|
58 |
+
"float32": torch.float32,
|
59 |
+
"bfloat16": torch.bfloat16,
|
60 |
+
}
|
61 |
+
|
62 |
+
shape = parameters.get("shape")
|
63 |
+
dtype = DTYPE_MAP.get(parameters.get("dtype"))
|
64 |
+
height = parameters.get("height")
|
65 |
+
width = parameters.get("width")
|
66 |
+
|
67 |
+
tensor = torch.frombuffer(bytearray(tensor), dtype=dtype).reshape(shape)
|
68 |
+
|
69 |
+
tensor = tensor.to(self.device, self.dtype)
|
70 |
+
|
71 |
+
tensor = self._unpack_latents(tensor, height, width, self.vae_scale_factor)
|
72 |
+
tensor = (
|
73 |
+
tensor / self.vae.config.scaling_factor
|
74 |
+
) + self.vae.config.shift_factor
|
75 |
+
|
76 |
+
with torch.no_grad():
|
77 |
+
image = self.vae.decode(tensor, return_dict=False)[0]
|
78 |
+
|
79 |
+
image = self.image_processor.postprocess(image, output_type="pil")
|
80 |
+
|
81 |
+
return image[0]
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
huggingface_hub
|
2 |
+
diffusers
|
3 |
+
|