bizyengine 1.2.46__py3-none-any.whl → 1.2.47__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,292 +1,21 @@
1
- import base64
2
- import io
3
- import json
4
- import os
5
- import pickle
6
- import zlib
7
1
  from enum import Enum
8
- from functools import singledispatch
9
- from typing import Any, List, Union
10
2
 
11
- import numpy as np
12
- import torch
13
- from PIL import Image
3
+ from bizyairsdk import (
4
+ base64_to_tensor,
5
+ decode_base64_to_image,
6
+ decode_base64_to_np,
7
+ decode_comfy_image,
8
+ decode_data,
9
+ encode_comfy_image,
10
+ encode_data,
11
+ encode_image_to_base64,
12
+ numpy_to_base64,
13
+ )
14
14
 
15
- from .common.env_var import BIZYAIR_DEBUG
16
-
17
- # Marker to identify base64-encoded tensors
18
- TENSOR_MARKER = "TENSOR:"
19
- IMAGE_MARKER = "IMAGE:"
15
+ # from .common.env_var import BIZYAIR_DEBUG
20
16
 
21
17
 
22
18
  class TaskStatus(Enum):
23
19
  PENDING = "pending"
24
20
  PROCESSING = "processing"
25
21
  COMPLETED = "completed"
26
-
27
-
28
- def convert_image_to_rgb(image: Image.Image) -> Image.Image:
29
- if image.mode != "RGB":
30
- return image.convert("RGB")
31
- return image
32
-
33
-
34
- def encode_image_to_base64(
35
- image: Image.Image, format: str = "png", quality: int = 100, lossless=False
36
- ) -> str:
37
- image = convert_image_to_rgb(image)
38
- with io.BytesIO() as output:
39
- image.save(output, format=format, quality=quality, lossless=lossless)
40
- output.seek(0)
41
- img_bytes = output.getvalue()
42
- if BIZYAIR_DEBUG:
43
- print(f"encode_image_to_base64: {format_bytes(len(img_bytes))}")
44
- return base64.b64encode(img_bytes).decode("utf-8")
45
-
46
-
47
- def decode_base64_to_np(img_data: str, format: str = "png") -> np.ndarray:
48
- img_bytes = base64.b64decode(img_data)
49
- if BIZYAIR_DEBUG:
50
- print(f"decode_base64_to_np: {format_bytes(len(img_bytes))}")
51
- with io.BytesIO(img_bytes) as input_buffer:
52
- img = Image.open(input_buffer)
53
- # https://github.com/comfyanonymous/ComfyUI/blob/a178e25912b01abf436eba1cfaab316ba02d272d/nodes.py#L1511
54
- img = img.convert("RGB")
55
- return np.array(img)
56
-
57
-
58
- def decode_base64_to_image(img_data: str) -> Image.Image:
59
- img_bytes = base64.b64decode(img_data)
60
- with io.BytesIO(img_bytes) as input_buffer:
61
- img = Image.open(input_buffer)
62
- if BIZYAIR_DEBUG:
63
- format_info = img.format.upper() if img.format else "Unknown"
64
- print(f"decode image format: {format_info}")
65
- return img
66
-
67
-
68
- def format_bytes(num_bytes: int) -> str:
69
- """
70
- Converts a number of bytes to a human-readable string with units (B, KB, or MB).
71
-
72
- :param num_bytes: The number of bytes to convert.
73
- :return: A string representing the number of bytes in a human-readable format.
74
- """
75
- if num_bytes < 1024:
76
- return f"{num_bytes} B"
77
- elif num_bytes < 1024 * 1024:
78
- return f"{num_bytes / 1024:.2f} KB"
79
- else:
80
- return f"{num_bytes / (1024 * 1024):.2f} MB"
81
-
82
-
83
- def _legacy_encode_comfy_image(image: torch.Tensor, image_format="png") -> str:
84
- input_image = image.cpu().detach().numpy()
85
- i = 255.0 * input_image[0]
86
- input_image = np.clip(i, 0, 255).astype(np.uint8)
87
- base64ed_image = encode_image_to_base64(
88
- Image.fromarray(input_image), format=image_format
89
- )
90
- return base64ed_image
91
-
92
-
93
- def _legacy_decode_comfy_image(
94
- img_data: Union[List, str], image_format="png"
95
- ) -> torch.tensor:
96
- if isinstance(img_data, List):
97
- decoded_imgs = [decode_comfy_image(x, old_version=True) for x in img_data]
98
-
99
- combined_imgs = torch.cat(decoded_imgs, dim=0)
100
- return combined_imgs
101
-
102
- out = decode_base64_to_np(img_data, format=image_format)
103
- out = np.array(out).astype(np.float32) / 255.0
104
- output = torch.from_numpy(out)[None,]
105
- return output
106
-
107
-
108
- def _new_encode_comfy_image(images: torch.Tensor, image_format="WEBP", **kwargs) -> str:
109
- """https://docs.comfy.org/essentials/custom_node_snippets#save-an-image-batch
110
- Encode a batch of images to base64 strings.
111
-
112
- Args:
113
- images (torch.Tensor): A batch of images.
114
- image_format (str, optional): The format of the images. Defaults to "WEBP".
115
-
116
- Returns:
117
- str: A JSON string containing the base64-encoded images.
118
- """
119
- results = {}
120
- for batch_number, image in enumerate(images):
121
- i = 255.0 * image.cpu().numpy()
122
- img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
123
- base64ed_image = encode_image_to_base64(img, format=image_format, **kwargs)
124
- results[batch_number] = base64ed_image
125
-
126
- return json.dumps(results)
127
-
128
-
129
- def _new_decode_comfy_image(img_datas: str, image_format="WEBP") -> torch.tensor:
130
- """
131
- Decode a batch of base64-encoded images.
132
-
133
- Args:
134
- img_datas (str): A JSON string containing the base64-encoded images.
135
- image_format (str, optional): The format of the images. Defaults to "WEBP".
136
-
137
- Returns:
138
- torch.Tensor: A tensor containing the decoded images.
139
- """
140
- img_datas = json.loads(img_datas)
141
-
142
- decoded_imgs = []
143
- for img_data in img_datas.values():
144
- decoded_image = decode_base64_to_np(img_data, format=image_format)
145
- decoded_image = np.array(decoded_image).astype(np.float32) / 255.0
146
- decoded_imgs.append(torch.from_numpy(decoded_image)[None,])
147
-
148
- return torch.cat(decoded_imgs, dim=0)
149
-
150
-
151
- def encode_comfy_image(
152
- image: torch.Tensor, image_format="WEBP", old_version=False, lossless=False
153
- ) -> str:
154
- if old_version:
155
- return _legacy_encode_comfy_image(image, image_format)
156
- return _new_encode_comfy_image(image, image_format, lossless=lossless)
157
-
158
-
159
- def decode_comfy_image(
160
- img_data: Union[List, str], image_format="WEBP", old_version=False
161
- ) -> torch.tensor:
162
- if old_version:
163
- return _legacy_decode_comfy_image(img_data, image_format)
164
- return _new_decode_comfy_image(img_data, image_format)
165
-
166
-
167
- def tensor_to_base64(tensor: torch.Tensor, compress=True) -> str:
168
- tensor_np = tensor.cpu().detach().numpy()
169
-
170
- tensor_bytes = pickle.dumps(tensor_np)
171
- if compress:
172
- tensor_bytes = zlib.compress(tensor_bytes)
173
-
174
- tensor_b64 = base64.b64encode(tensor_bytes).decode("utf-8")
175
- return tensor_b64
176
-
177
-
178
- def base64_to_tensor(tensor_b64: str, compress=True) -> torch.Tensor:
179
- tensor_bytes = base64.b64decode(tensor_b64)
180
-
181
- if compress:
182
- tensor_bytes = zlib.decompress(tensor_bytes)
183
-
184
- tensor_np = pickle.loads(tensor_bytes)
185
-
186
- tensor = torch.from_numpy(tensor_np)
187
- return tensor
188
-
189
-
190
- @singledispatch
191
- def decode_data(input, old_version=False):
192
- raise NotImplementedError(f"Unsupported type: {type(input)}")
193
-
194
-
195
- @decode_data.register(int)
196
- @decode_data.register(float)
197
- @decode_data.register(bool)
198
- def _(input, **kwargs):
199
- return input
200
-
201
-
202
- @decode_data.register(type(None))
203
- def _(input, **kwargs):
204
- return [None]
205
-
206
-
207
- @decode_data.register(dict)
208
- def _(input, **kwargs):
209
- return {k: decode_data(v, **kwargs) for k, v in input.items()}
210
-
211
-
212
- @decode_data.register(list)
213
- def _(input, **kwargs):
214
- return [decode_data(x, **kwargs) for x in input]
215
-
216
-
217
- @decode_data.register(str)
218
- def _(input: str, **kwargs):
219
- if input.startswith(TENSOR_MARKER):
220
- tensor_b64 = input[len(TENSOR_MARKER) :]
221
- return base64_to_tensor(tensor_b64)
222
- elif input.startswith(IMAGE_MARKER):
223
- tensor_b64 = input[len(IMAGE_MARKER) :]
224
- old_version = kwargs.get("old_version", False)
225
- return decode_comfy_image(tensor_b64, old_version=old_version)
226
- return input
227
-
228
-
229
- @singledispatch
230
- def encode_data(output, disable_image_marker=False, old_version=False):
231
- raise NotImplementedError(f"Unsupported type: {type(output)}")
232
-
233
-
234
- @encode_data.register(dict)
235
- def _(output, **kwargs):
236
- return {k: encode_data(v, **kwargs) for k, v in output.items()}
237
-
238
-
239
- @encode_data.register(list)
240
- def _(output, **kwargs):
241
- return [encode_data(x, **kwargs) for x in output]
242
-
243
-
244
- def is_image_tensor(tensor) -> bool:
245
- """https://docs.comfy.org/essentials/custom_node_datatypes#image
246
-
247
- Check if the given tensor is in the format of an IMAGE (shape [B, H, W, C] where C=3).
248
-
249
- `Args`:
250
- tensor (torch.Tensor): The tensor to check.
251
-
252
- `Returns`:
253
- bool: True if the tensor is in the IMAGE format, False otherwise.
254
- """
255
- try:
256
- if not isinstance(tensor, torch.Tensor):
257
- return False
258
-
259
- if len(tensor.shape) != 4:
260
- return False
261
-
262
- B, H, W, C = tensor.shape
263
- if C != 3:
264
- return False
265
-
266
- return True
267
- except:
268
- return False
269
-
270
-
271
- @encode_data.register(torch.Tensor)
272
- def _(output, **kwargs):
273
- if is_image_tensor(output) and not kwargs.get("disable_image_marker", False):
274
- old_version = kwargs.get("old_version", False)
275
- lossless = kwargs.get("lossless", True)
276
- return IMAGE_MARKER + encode_comfy_image(
277
- output, image_format="WEBP", old_version=old_version, lossless=lossless
278
- )
279
- return TENSOR_MARKER + tensor_to_base64(output)
280
-
281
-
282
- @encode_data.register(int)
283
- @encode_data.register(float)
284
- @encode_data.register(bool)
285
- @encode_data.register(type(None))
286
- def _(output, **kwargs):
287
- return output
288
-
289
-
290
- @encode_data.register(str)
291
- def _(output, **kwargs):
292
- return output
bizyengine/version.txt CHANGED
@@ -1 +1 @@
1
- 1.2.46
1
+ 1.2.47
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: bizyengine
3
- Version: 1.2.46
3
+ Version: 1.2.47
4
4
  Summary: [a/BizyAir](https://github.com/siliconflow/BizyAir) Comfy Nodes that can run in any environment.
5
5
  Author-email: SiliconFlow <yaochi@siliconflow.cn>
6
6
  Project-URL: Repository, https://github.com/siliconflow/BizyAir
@@ -13,6 +13,7 @@ Requires-Dist: requests
13
13
  Requires-Dist: inputimeout
14
14
  Requires-Dist: openai>=1.77.0
15
15
  Requires-Dist: pycryptodome
16
+ Requires-Dist: bizyairsdk>=0.0.4
16
17
 
17
18
  ## BizyEngine
18
19
 
@@ -1,5 +1,5 @@
1
1
  bizyengine/__init__.py,sha256=GP9V-JM07fz7uv_qTB43QEA2rKdrVJxi5I7LRnn_3ZQ,914
2
- bizyengine/version.txt,sha256=32_H8a3cQVQgF4b_XGm5nAAN2BMgmYP95gQbYLjXLok,7
2
+ bizyengine/version.txt,sha256=fUYp_X7SLV0bJdJTQ07vt0yEf0BiLHjFOMJ46TrHTFg,7
3
3
  bizyengine/bizy_server/__init__.py,sha256=SP9oSblnPo4KQyh7yOGD26YCskFAcQHAZy04nQBNRIw,200
4
4
  bizyengine/bizy_server/api_client.py,sha256=Z7G5IjaEqSJkF6nLLw2R3bpgBAOi5ClQiUbel6NMXmE,43932
5
5
  bizyengine/bizy_server/errno.py,sha256=1UiFmE2U7r7hCHgsw4-p_YL0VCmTJc9NyYDEbhkanaY,16336
@@ -47,7 +47,7 @@ bizyengine/bizyair_extras/nodes_kolors_mz/__init__.py,sha256=HsCCCphW8q0SrWEiFlZ
47
47
  bizyengine/bizyair_extras/oauth_callback/main.py,sha256=KQOZWor3kyNx8xvUNHYNMoHfCF9g_ht13_iPk4K_5YM,3633
48
48
  bizyengine/core/__init__.py,sha256=EV9ZtTwOHC0S_eNvCu-tltIydfxfMsH59LbgVX4e_1c,359
49
49
  bizyengine/core/data_types.py,sha256=2f7QqqZvhKmXw3kZV1AvXuPTda34b4wXQE9tyO8nUSM,1595
50
- bizyengine/core/image_utils.py,sha256=8pJncmVPCUm-_S8Tu7gvESVmSDNp7Ok3M4RXNy_2rRA,8630
50
+ bizyengine/core/image_utils.py,sha256=vJt42FcEDD8-fQumuogZM1XXwgYseSQ79_9Qzu9YTQk,409
51
51
  bizyengine/core/nodes_base.py,sha256=h2f_FWTWj6lpdKjwHRuOmoRifqwkV59ovM2ZxPK4h9c,9019
52
52
  bizyengine/core/nodes_io.py,sha256=VhwRwYkGp0g3Mh0hx1OSwNZbV06NEV13w2aODSiAm5M,2832
53
53
  bizyengine/core/commands/__init__.py,sha256=82yRdMT23RTiZPkFW_G3fVa-fj3-TUAXnj6cnGA3xRA,22
@@ -79,7 +79,7 @@ bizyengine/misc/route_sam.py,sha256=-bMIR2QalfnszipGxSxvDAHGJa5gPSrjkYPb5baaRg4,
79
79
  bizyengine/misc/segment_anything.py,sha256=wNKYwlYPMszfwj23524geFZJjZaG4eye65SGaUnh77I,8941
80
80
  bizyengine/misc/supernode.py,sha256=STN9gaxfTSErH8OiHeZa47d8z-G9S0I7fXuJvHQOBFM,4532
81
81
  bizyengine/misc/utils.py,sha256=CKduySGSMNGlJMImHyZmN-giABY5VUaB88f6Kq-HAV0,6831
82
- bizyengine-1.2.46.dist-info/METADATA,sha256=KThw17QsRFjkDMbYYLkW42PsSbx3mik21HTzG0ZKclE,675
83
- bizyengine-1.2.46.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
84
- bizyengine-1.2.46.dist-info/top_level.txt,sha256=2zapzqxX-we5cRyJkGf9bd5JinRtXp3-_uDI-xCAnc0,11
85
- bizyengine-1.2.46.dist-info/RECORD,,
82
+ bizyengine-1.2.47.dist-info/METADATA,sha256=HRtl5TsLsyT7zKeQZlSPNu_3EqdGZSHcE0XOYklNPlw,708
83
+ bizyengine-1.2.47.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
84
+ bizyengine-1.2.47.dist-info/top_level.txt,sha256=2zapzqxX-we5cRyJkGf9bd5JinRtXp3-_uDI-xCAnc0,11
85
+ bizyengine-1.2.47.dist-info/RECORD,,