waveshaker commited on
Commit
b827d0e
·
verified ·
1 Parent(s): 7de4592

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -3
app.py CHANGED
@@ -73,6 +73,7 @@ _nodes_ready = False
73
  tasks_status = {}
74
  _last_used_architecture = None
75
  _last_used_unet = None
 
76
 
77
  # ==========================================================================
78
  # ⚙️ Comfy Config
@@ -968,7 +969,7 @@ def build_workflow(
968
 
969
  # 🌟 增加全局变量用于追踪 GPU 域内的架构切换
970
 
971
- @spaces.GPU(duration=30) # 🌟 降低 duration 到 30
972
  def generate(
973
  prompt: str, unet_choice: str, custom_w: int, custom_h: int,
974
  steps: int, cfg: float, seed: int, sampler: str, scheduler: str,
@@ -977,10 +978,24 @@ def generate(
977
  task_id: str = None,
978
  ) -> str:
979
 
980
- global _gpu_last_used_arch, _gpu_last_used_unet
981
 
982
  print(f"[Token探针] task={task_id} | token_present={bool(LocalContext.request.get(None) and LocalContext.request.get(None).headers.get('x-ip-token'))}", flush=True)
983
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
984
  if task_id:
985
  if task_id not in tasks_status:
986
  tasks_status[task_id] = {}
@@ -1217,9 +1232,14 @@ app.add_middleware(
1217
  allow_headers=["*"],
1218
  )
1219
 
 
1220
 
1221
  @app.post("/api/generate")
1222
  async def handle_generate(req: GenerateRequest, background_tasks: BackgroundTasks, request: Request):
 
 
 
 
1223
  if req.char_enable and req.char_lora:
1224
  if not req.char_lora.endswith(".safetensors"):
1225
  req.char_lora = f"{req.char_lora}.safetensors"
 
73
  tasks_status = {}
74
  _last_used_architecture = None
75
  _last_used_unet = None
76
+ # _previous_arch_for_gc = None
77
 
78
  # ==========================================================================
79
  # ⚙️ Comfy Config
 
969
 
970
  # 🌟 增加全局变量用于追踪 GPU 域内的架构切换
971
 
972
+ @spaces.GPU(duration=40) # 🌟 降低 duration 到 40
973
  def generate(
974
  prompt: str, unet_choice: str, custom_w: int, custom_h: int,
975
  steps: int, cfg: float, seed: int, sampler: str, scheduler: str,
 
978
  task_id: str = None,
979
  ) -> str:
980
 
981
+ # global _previous_arch_for_gc
982
 
983
  print(f"[Token探针] task={task_id} | token_present={bool(LocalContext.request.get(None) and LocalContext.request.get(None).headers.get('x-ip-token'))}", flush=True)
984
+
985
+ # 获取当前架构
986
+ # current_arch = UNET_CHOICES.get(unet_choice, {}).get("engine", "unknown")
987
+
988
+ # 🚫 【已注释】原因:经过极限测试,跨架构排队时会导致 ZeroGPU 重新申请显存,拖慢加载。
989
+ # 交给 ComfyUI 原生的 RAM_PRESSURE 机制(lru=8, ram=16.0)平滑过渡效果更好。
990
+ # if _previous_arch_for_gc is not None and _previous_arch_for_gc != current_arch:
991
+ # import gc
992
+ # if torch.cuda.is_available():
993
+ # torch.cuda.empty_cache()
994
+ # gc.collect()
995
+ # print(f"[显存清理] 架构切换 {_previous_arch_for_gc} -> {current_arch},已清理碎片", flush=True)
996
+ #
997
+ # _previous_arch_for_gc = current_arch
998
+
999
  if task_id:
1000
  if task_id not in tasks_status:
1001
  tasks_status[task_id] = {}
 
1232
  allow_headers=["*"],
1233
  )
1234
 
1235
+ from fastapi import HTTPException
1236
 
1237
  @app.post("/api/generate")
1238
  async def handle_generate(req: GenerateRequest, background_tasks: BackgroundTasks, request: Request):
1239
+ # 🌟 兜底拦截:单边最大 1368,防绕过
1240
+ if req.custom_w > 1536 or req.custom_h > 1536:
1241
+ raise HTTPException(status_code=400, detail="单边最大支持 1536 像素,请调整宽高。")
1242
+
1243
  if req.char_enable and req.char_lora:
1244
  if not req.char_lora.endswith(".safetensors"):
1245
  req.char_lora = f"{req.char_lora}.safetensors"