bizyengine 1.2.56__py3-none-any.whl → 1.2.58__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.
@@ -143,9 +143,10 @@ class Wan_V2_5_I2V_API(BizyAirBaseNode):
143
143
  if "output" not in api_resp or "video_url" not in api_resp["output"]:
144
144
  raise ValueError(f"Invalid response: {api_resp}")
145
145
  video_url = api_resp["output"]["video_url"]
146
+ logging.info(f"video_url: {video_url}")
146
147
  actual_prompt = api_resp["output"].get("actual_prompt", prompt)
147
148
  # 下载视频
148
- video_resp = requests.get(video_url, stream=True, timeout=30)
149
+ video_resp = requests.get(video_url, stream=True, timeout=3600)
149
150
  video_resp.raise_for_status() # 非 2xx 会抛异常
150
151
  return (VideoFromFile(io.BytesIO(video_resp.content)), actual_prompt)
151
152
 
@@ -282,8 +283,9 @@ class Wan_V2_5_T2V_API(BizyAirBaseNode):
282
283
  if "output" not in api_resp or "video_url" not in api_resp["output"]:
283
284
  raise ValueError(f"Invalid response: {api_resp}")
284
285
  video_url = api_resp["output"]["video_url"]
286
+ logging.info(f"video_url: {video_url}")
285
287
  actual_prompt = api_resp["output"].get("actual_prompt", prompt)
286
288
  # 下载视频
287
- video_resp = requests.get(video_url, stream=True, timeout=30)
289
+ video_resp = requests.get(video_url, stream=True, timeout=3600)
288
290
  video_resp.raise_for_status() # 非 2xx 会抛异常
289
291
  return (VideoFromFile(io.BytesIO(video_resp.content)), actual_prompt)
@@ -160,7 +160,7 @@ def send_request(
160
160
  req = urllib.request.Request(
161
161
  url, data=data, headers=headers, method=method, **kwargs
162
162
  )
163
- with urllib.request.urlopen(req, timeout=600) as response:
163
+ with urllib.request.urlopen(req, timeout=3600) as response:
164
164
  response_data = response.read().decode("utf-8")
165
165
 
166
166
  except urllib.error.URLError as e:
bizyengine/misc/utils.py CHANGED
@@ -1,14 +1,21 @@
1
+ import asyncio
1
2
  import base64
3
+ import concurrent.futures
2
4
  import json
5
+ import logging
3
6
  import os
4
7
  import pickle
8
+ import re
9
+ import threading
10
+ import time
5
11
  import urllib.parse
6
12
  import urllib.request
7
13
  import zlib
8
- from typing import List, Tuple, Union
14
+ from typing import Callable, Dict, Generic, List, Optional, Tuple, TypeVar, Union
9
15
 
10
16
  import numpy as np
11
17
 
18
+ from bizyengine.bizy_server.api_client import APIClient
12
19
  from bizyengine.core import pop_api_key_and_prompt_id
13
20
  from bizyengine.core.common import client
14
21
  from bizyengine.core.common.env_var import BIZYAIR_SERVER_ADDRESS
@@ -36,7 +43,7 @@ def send_post_request(api_url, payload, headers):
36
43
  try:
37
44
  data = json.dumps(payload).encode("utf-8")
38
45
  req = urllib.request.Request(api_url, data=data, headers=headers, method="POST")
39
- with urllib.request.urlopen(req, timeout=600) as response:
46
+ with urllib.request.urlopen(req, timeout=3600) as response:
40
47
  response_data = response.read().decode("utf-8")
41
48
  return response_data
42
49
  except urllib.error.URLError as e:
@@ -143,6 +150,18 @@ def get_llm_response(
143
150
  extra_data = pop_api_key_and_prompt_id(kwargs)
144
151
  headers = client.headers(api_key=extra_data["api_key"])
145
152
 
153
+ # 如果model已不可用,选择第一个可用model
154
+ if _MODELS_CACHE.get("llm_models") is None:
155
+ cache_models(extra_data["api_key"])
156
+ llm_models = _MODELS_CACHE.get("llm_models")
157
+ if llm_models is None:
158
+ logging.warning(f"No LLM models available, keeping the original model {model}")
159
+ elif model not in llm_models:
160
+ logging.warning(
161
+ f"Model {model} is not available, using the first available model {llm_models[0]}"
162
+ )
163
+ model = llm_models[0]
164
+
146
165
  payload = {
147
166
  "model": model,
148
167
  "messages": [
@@ -183,6 +202,18 @@ def get_vlm_response(
183
202
  extra_data = pop_api_key_and_prompt_id(kwargs)
184
203
  headers = client.headers(api_key=extra_data["api_key"])
185
204
 
205
+ # 如果model已不可用,选择第一个可用model
206
+ if _MODELS_CACHE.get("vlm_models") is None:
207
+ cache_models(extra_data["api_key"])
208
+ vlm_models = _MODELS_CACHE.get("vlm_models")
209
+ if vlm_models is None:
210
+ logging.warning(f"No VLM models available, keeping the original model {model}")
211
+ elif model not in vlm_models:
212
+ logging.warning(
213
+ f"Model {model} is not available, using the first available model {vlm_models[0]}"
214
+ )
215
+ model = vlm_models[0]
216
+
186
217
  messages = [
187
218
  {
188
219
  "role": "system",
@@ -230,3 +261,149 @@ def get_vlm_response(
230
261
  callback=None,
231
262
  )
232
263
  return response
264
+
265
+
266
+ K = TypeVar("K")
267
+ V = TypeVar("V")
268
+ R = TypeVar("R")
269
+
270
+
271
+ class TTLCache(Generic[K, V]):
272
+ """线程安全 TTL 内存缓存(仅依赖标准库)"""
273
+
274
+ def __init__(self, ttl_sec: float):
275
+ self.ttl = ttl_sec
276
+ self._data: Dict[K, tuple[V, float]] = {}
277
+ self._lock = threading.RLock()
278
+ self._stop_evt = threading.Event()
279
+ # 后台清扫线程
280
+ self._cleaner = threading.Thread(target=self._cleanup, daemon=True)
281
+ self._cleaner.start()
282
+
283
+ # ---------- 公共 API ----------
284
+ def set(self, key: K, value: V) -> None:
285
+ """写入/刷新键值"""
286
+ with self._lock:
287
+ self._data[key] = (value, time.time() + self.ttl)
288
+
289
+ def get(self, key: K) -> Optional[V]:
290
+ """读取键值;不存在或已过期返回 None"""
291
+ with self._lock:
292
+ val, expire = self._data.get(key, (None, 0))
293
+ if val is None or time.time() > expire:
294
+ self._data.pop(key, None)
295
+ return None
296
+ return val
297
+
298
+ def delete(self, key: K) -> None:
299
+ """手动删除"""
300
+ with self._lock:
301
+ self._data.pop(key, None)
302
+
303
+ def stop(self):
304
+ """停止后台线程(程序退出前调用)"""
305
+ self._stop_evt.set()
306
+ self._cleaner.join(timeout=self.ttl + 1)
307
+
308
+ # ---------- 内部 ----------
309
+ def _cleanup(self):
310
+ """周期清扫过期键"""
311
+ while not self._stop_evt.wait(self.ttl / 2):
312
+ with self._lock:
313
+ now = time.time()
314
+ for key, (_, expire) in list(self._data.items()):
315
+ if now > expire:
316
+ self._data.pop(key, None)
317
+
318
+
319
+ class SingleFlight(Generic[R]):
320
+ """Python 版 singleflight.Group(线程安全)"""
321
+
322
+ def __init__(self):
323
+ self._lock = threading.Lock()
324
+ self._call_map: dict[str, SingleFlight._Call[R]] = {}
325
+
326
+ class _Call(Generic[R]):
327
+ __slots__ = ("mu", "done", "result", "err", "waiters")
328
+
329
+ def __init__(self):
330
+ self.mu = threading.Lock()
331
+ self.done = False
332
+ self.result: Optional[R] = None
333
+ self.err: Optional[BaseException] = None
334
+ self.waiters = 0
335
+
336
+ def do(
337
+ self, key: str, fn: Callable[[], R]
338
+ ) -> tuple[R, bool, Optional[BaseException]]:
339
+ """
340
+ 返回值: (result, shared?, exception)
341
+ shared=True 表示本次未真正执行 fn,复用了别人结果
342
+ """
343
+ with self._lock:
344
+ call = self._call_map.get(key)
345
+ if call is None: # 我是第一个
346
+ call = self._Call[R]()
347
+ call.waiters = 1
348
+ self._call_map[key] = call
349
+ first = True
350
+ else: # 已有并发请求
351
+ call.waiters += 1
352
+ first = False
353
+
354
+ if first: # 只有第一个真正执行
355
+ try:
356
+ result = fn()
357
+ with call.mu:
358
+ call.result = result
359
+ call.done = True
360
+ except BaseException as e:
361
+ with call.mu:
362
+ call.err = e
363
+ call.done = True
364
+ raise
365
+ finally: # 把自己从 map 摘掉
366
+ with self._lock:
367
+ if call.waiters == 0:
368
+ self._call_map.pop(key, None)
369
+ else: # 其它人阻塞等待
370
+ with call.mu:
371
+ while not call.done:
372
+ call.mu.wait()
373
+ # 读取结果
374
+ with call.mu:
375
+ if call.err is not None:
376
+ return call.result, True, call.err
377
+ return call.result, True, None
378
+
379
+
380
+ _MODELS_CACHE = TTLCache[str, list[str]](ttl_sec=600)
381
+ _SF = SingleFlight[None]()
382
+
383
+
384
+ def cache_models(request_api_key: str):
385
+ # TODO: 效果待验证,目前节点只会被ComfyUI串行执行,所以不会出现竞争
386
+ _SF.do("_cache_models", lambda: _cache_models(request_api_key))
387
+
388
+
389
+ def _cache_models(request_api_key: str):
390
+ # ① 开一条新线程专门跑协程 - 应该不需要在prompt那层上锁,因为并发只有1
391
+ with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
392
+ api_client = APIClient()
393
+ all_models = pool.submit(
394
+ asyncio.run, api_client.fetch_all_llm_models(request_api_key)
395
+ ).result()
396
+ if len(all_models) == 0:
397
+ return
398
+ llm_models = [
399
+ model
400
+ for model in all_models
401
+ if not (re.search(r"\d+(\.\d+)?v", model.lower()) or "vl" in model.lower())
402
+ ]
403
+ vlm_models = [
404
+ model
405
+ for model in all_models
406
+ if re.search(r"\d+(\.\d+)?v", model.lower()) or "vl" in model.lower()
407
+ ]
408
+ _MODELS_CACHE.set("llm_models", llm_models)
409
+ _MODELS_CACHE.set("vlm_models", vlm_models)
bizyengine/version.txt CHANGED
@@ -1 +1 @@
1
- 1.2.56
1
+ 1.2.58
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: bizyengine
3
- Version: 1.2.56
3
+ Version: 1.2.58
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
@@ -1,5 +1,5 @@
1
1
  bizyengine/__init__.py,sha256=GP9V-JM07fz7uv_qTB43QEA2rKdrVJxi5I7LRnn_3ZQ,914
2
- bizyengine/version.txt,sha256=hVI0lnTLmTtdalpHou3oV96AsS0SmN76nA7QLjtnbz0,7
2
+ bizyengine/version.txt,sha256=GOmifuSbInFqnoj8v1bW8vErzt0ii5bOQn0qEbp5dEg,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=RIyvegX3lzpx_1L1q2XVvu3on0kvYgKiUQ8U3ZtyF68,16823
@@ -40,7 +40,7 @@ bizyengine/bizyair_extras/nodes_trellis.py,sha256=GqSRM8FobuziOIxwyAs3BLztpjVIP4
40
40
  bizyengine/bizyair_extras/nodes_ultimatesdupscale.py,sha256=-_SsLTAWAQDv4uw-4Z7IGP2tXTe73BJ3N5D6RqVVAK4,4133
41
41
  bizyengine/bizyair_extras/nodes_upscale_model.py,sha256=lrzA1BFI2w5aEPCmNPMh07s-WDzG-xTT49uU6WCnlP8,1151
42
42
  bizyengine/bizyair_extras/nodes_utils.py,sha256=whog_tmV-q7JvLEdb03JL3KKsC7wKe3kImzx_jPaQD8,2613
43
- bizyengine/bizyair_extras/nodes_wan_api.py,sha256=lhzkfsLFZPKuBXrJBf0i2t9A2zLB4TxgWIrW8MYJ49g,11088
43
+ bizyengine/bizyair_extras/nodes_wan_api.py,sha256=RMC1xUgvNjI6_jdX4gIyiwMvjWrf2yw9gya9axQYUXo,11188
44
44
  bizyengine/bizyair_extras/nodes_wan_i2v.py,sha256=3XwcxLHmgrihgXDEzcVOjU6VjqnZa3mErINlY014PFA,8435
45
45
  bizyengine/bizyair_extras/nodes_wan_video.py,sha256=aE2JBF0ZT-6BOM0bGu9R4yZ_eMeMnnjCW-YzFe4qg8Q,2804
46
46
  bizyengine/bizyair_extras/route_bizyair_tools.py,sha256=EiP5pS6xoE3tULoNSN2hYZA29vgt7yCErsbRp34gGEg,3898
@@ -75,7 +75,7 @@ bizyengine/core/commands/servers/model_server.py,sha256=47DEQpj8HBSa-_TImW-5JCeu
75
75
  bizyengine/core/commands/servers/prompt_server.py,sha256=e8JhtKRM8nw0kQwe2Ofl-zQtiVqQdbbWRxOqkFmSclM,10873
76
76
  bizyengine/core/common/__init__.py,sha256=GicZw6YeAZk1PsKmFDt9dm1F75zPUlpia9Q_ki5vW1Y,179
77
77
  bizyengine/core/common/caching.py,sha256=hRNsSrfNxgc1zzvBzLVjMY0iMkKqA0TBCr-iYhEpzik,6946
78
- bizyengine/core/common/client.py,sha256=5ZdbmNHNfkNgEvND34wTN9BLHyoe-HcWk_L1G6XmVRw,10700
78
+ bizyengine/core/common/client.py,sha256=5wp_gsyvnSQxETCjUayYe536taOhaVP0zonPvW_asdA,10701
79
79
  bizyengine/core/common/env_var.py,sha256=1EAW3gOXY2bKouCqrGa583vTJRdDasQ1IsFTnzDg7Dk,3450
80
80
  bizyengine/core/common/utils.py,sha256=Jxk4tCURbUhjYBFuHTiQgeUo2w13TYj4upnPTKNNucM,4215
81
81
  bizyengine/core/configs/conf.py,sha256=D_UWG9SSJnK5EhbrfNFryJQ8hUwwdvhOGlq1TielwpI,3830
@@ -94,8 +94,8 @@ bizyengine/misc/nodes_controlnet_union_sdxl.py,sha256=fYyu_XMY7mcX1Ad9x30q1tYB8m
94
94
  bizyengine/misc/route_sam.py,sha256=-bMIR2QalfnszipGxSxvDAHGJa5gPSrjkYPb5baaRg4,1561
95
95
  bizyengine/misc/segment_anything.py,sha256=wNKYwlYPMszfwj23524geFZJjZaG4eye65SGaUnh77I,8941
96
96
  bizyengine/misc/supernode.py,sha256=STN9gaxfTSErH8OiHeZa47d8z-G9S0I7fXuJvHQOBFM,4532
97
- bizyengine/misc/utils.py,sha256=lYRtJxKvwCyAM8eQMvJvzxjLCwYnQGy0mjLsc5p0Dxo,6844
98
- bizyengine-1.2.56.dist-info/METADATA,sha256=5ra9vOgtx_1O1tprDSBt8sQD3uLPvCmVsMG95VpyX28,734
99
- bizyengine-1.2.56.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
100
- bizyengine-1.2.56.dist-info/top_level.txt,sha256=2zapzqxX-we5cRyJkGf9bd5JinRtXp3-_uDI-xCAnc0,11
101
- bizyengine-1.2.56.dist-info/RECORD,,
97
+ bizyengine/misc/utils.py,sha256=nXXTPkj4WBvds4EWjI9c-ydeWwmXl8Vwrdu-4Fh62g8,12914
98
+ bizyengine-1.2.58.dist-info/METADATA,sha256=xjje03R1xq0NXcgJWKZ916QclolZOmQDmoyk4f7TEkw,734
99
+ bizyengine-1.2.58.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
100
+ bizyengine-1.2.58.dist-info/top_level.txt,sha256=2zapzqxX-we5cRyJkGf9bd5JinRtXp3-_uDI-xCAnc0,11
101
+ bizyengine-1.2.58.dist-info/RECORD,,