bizyengine 1.2.56__py3-none-any.whl → 1.2.57__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.
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
@@ -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.57
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: bizyengine
3
- Version: 1.2.56
3
+ Version: 1.2.57
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=txvxJYLvzIKQ52dHqmS8wu-ETqexjhrTKOe0yuriLUE,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
@@ -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=hLGARJ2fkckGBm6_noC38hGT4b6OXUA4dx68eCdTbzc,12913
98
+ bizyengine-1.2.57.dist-info/METADATA,sha256=lgy5VTeQLkdhZzQc8xxX7-uJtTicw0XqR9gZQuYTvGM,734
99
+ bizyengine-1.2.57.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
100
+ bizyengine-1.2.57.dist-info/top_level.txt,sha256=2zapzqxX-we5cRyJkGf9bd5JinRtXp3-_uDI-xCAnc0,11
101
+ bizyengine-1.2.57.dist-info/RECORD,,