Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@
|
|
| 2 |
"""Comfy Version: c1eece62cbf1805d4f64286d7f5592e6a215e6ba"""
|
| 3 |
import base64
|
| 4 |
import asyncio
|
|
|
|
| 5 |
import glob
|
| 6 |
import json
|
| 7 |
import os
|
|
@@ -435,19 +436,23 @@ def _ensure_comfy() -> None:
|
|
| 435 |
print(f"📦 Cloning ComfyUI repo...")
|
| 436 |
_run(["git", "clone", "https://github.com/comfyanonymous/ComfyUI.git", str(COMFY)])
|
| 437 |
|
| 438 |
-
#
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 442 |
|
| 443 |
-
|
| 444 |
-
try:
|
| 445 |
-
current_commit = subprocess.check_output(
|
| 446 |
-
["git", "rev-parse", "HEAD"], cwd=str(COMFY), text=True
|
| 447 |
-
).strip()
|
| 448 |
-
print(f"✅ ComfyUI 当前版本 Commit: {current_commit}")
|
| 449 |
-
except Exception as e:
|
| 450 |
-
print(f"⚠️ 无法获取 ComfyUI Commit 信息: {e}")
|
| 451 |
|
| 452 |
_install_filtered_requirements(COMFY / "requirements.txt")
|
| 453 |
|
|
@@ -2341,7 +2346,16 @@ async def get_sampler_config():
|
|
| 2341 |
|
| 2342 |
@app.get("/api/health")
|
| 2343 |
async def health_check():
|
| 2344 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2345 |
|
| 2346 |
@app.post("/api/generate")
|
| 2347 |
async def handle_generate(req: GenerateRequest, background_tasks: BackgroundTasks, request: Request):
|
|
@@ -2643,7 +2657,16 @@ from model_config import MODEL_CONFIG, SYNC_FROM_GUI_SPACE
|
|
| 2643 |
UI_SPACE_URL = MODEL_CONFIG.get("ui_space_url", "").rstrip("/")
|
| 2644 |
FRONTEND_CACHE_DIR = ROOT / "frontend_cache" # 🌟 指向新文件夹
|
| 2645 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2646 |
def sync_ui_assets():
|
|
|
|
| 2647 |
if not SYNC_FROM_GUI_SPACE:
|
| 2648 |
print("📦 [Frontend Cache] SYNC_FROM_GUI_SPACE = False,跳过同步,使用本地缓存。")
|
| 2649 |
return
|
|
@@ -2656,6 +2679,8 @@ def sync_ui_assets():
|
|
| 2656 |
files_to_sync = ["index.html", "style.css", "app.js"]
|
| 2657 |
|
| 2658 |
print(f"🔄 正在从 {UI_SPACE_URL} 同步前端文件到 /frontend_cache...")
|
|
|
|
|
|
|
| 2659 |
for f in files_to_sync:
|
| 2660 |
try:
|
| 2661 |
res = requests.get(f"{UI_SPACE_URL}/{f.lstrip('/')}?t={int(time.time())}", timeout=10)
|
|
@@ -2663,10 +2688,38 @@ def sync_ui_assets():
|
|
| 2663 |
(FRONTEND_CACHE_DIR / f).write_text(res.text, encoding="utf-8")
|
| 2664 |
print(f"✅ 已更新: {f}")
|
| 2665 |
else:
|
|
|
|
| 2666 |
print(f"⚠️ 同步 {f} 失败,状态码: {res.status_code}")
|
| 2667 |
except Exception as e:
|
|
|
|
| 2668 |
print(f"⚠️ 同步 {f} 网络异常: {e}")
|
| 2669 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2670 |
# 1. 启动时执行同步
|
| 2671 |
sync_ui_assets()
|
| 2672 |
|
|
@@ -2701,8 +2754,20 @@ if __name__ == "__main__":
|
|
| 2701 |
print("🚀 Triggering ZeroGPU scan on 7861...")
|
| 2702 |
threading.Thread(target=trigger_gpu_scan, daemon=True).start()
|
| 2703 |
|
| 2704 |
-
# 3.
|
| 2705 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2706 |
|
| 2707 |
# 4. 最后启动主 API 服务
|
| 2708 |
print("🌐 Starting main API Gateway on 7860...")
|
|
|
|
| 2 |
"""Comfy Version: c1eece62cbf1805d4f64286d7f5592e6a215e6ba"""
|
| 3 |
import base64
|
| 4 |
import asyncio
|
| 5 |
+
import hashlib
|
| 6 |
import glob
|
| 7 |
import json
|
| 8 |
import os
|
|
|
|
| 436 |
print(f"📦 Cloning ComfyUI repo...")
|
| 437 |
_run(["git", "clone", "https://github.com/comfyanonymous/ComfyUI.git", str(COMFY)])
|
| 438 |
|
| 439 |
+
# 已经停在目标 commit → 跳过 fetch/checkout(每次重启省一次网络往返 + 磁盘 churn)
|
| 440 |
+
def _head() -> str:
|
| 441 |
+
try:
|
| 442 |
+
return subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=str(COMFY), text=True).strip()
|
| 443 |
+
except Exception:
|
| 444 |
+
return ""
|
| 445 |
+
|
| 446 |
+
current_commit = _head()
|
| 447 |
+
if current_commit != COMFY_COMMIT:
|
| 448 |
+
print(f"🔄 Fetching and checking out ComfyUI commit: {COMFY_COMMIT[:8]}...")
|
| 449 |
+
_run(["git", "fetch", "origin"], cwd=COMFY)
|
| 450 |
+
_run(["git", "checkout", COMFY_COMMIT], cwd=COMFY)
|
| 451 |
+
current_commit = _head()
|
| 452 |
+
else:
|
| 453 |
+
print(f"✅ ComfyUI 已在目标 commit,跳过 fetch/checkout")
|
| 454 |
|
| 455 |
+
print(f"✅ ComfyUI 当前版本 Commit: {current_commit or 'unknown'}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 456 |
|
| 457 |
_install_filtered_requirements(COMFY / "requirements.txt")
|
| 458 |
|
|
|
|
| 2346 |
|
| 2347 |
@app.get("/api/health")
|
| 2348 |
async def health_check():
|
| 2349 |
+
return {
|
| 2350 |
+
"status": "ok",
|
| 2351 |
+
"frontend": {
|
| 2352 |
+
"sync_ok": FRONTEND_SYNC_OK,
|
| 2353 |
+
"error": FRONTEND_SYNC_ERR,
|
| 2354 |
+
# 有缓存但本次没同步成功 = 页面能开,但你刚 push 的改动不在里面
|
| 2355 |
+
"stale": (not FRONTEND_SYNC_OK) and bool(FRONTEND_MANIFEST),
|
| 2356 |
+
"files": FRONTEND_MANIFEST,
|
| 2357 |
+
},
|
| 2358 |
+
}
|
| 2359 |
|
| 2360 |
@app.post("/api/generate")
|
| 2361 |
async def handle_generate(req: GenerateRequest, background_tasks: BackgroundTasks, request: Request):
|
|
|
|
| 2657 |
UI_SPACE_URL = MODEL_CONFIG.get("ui_space_url", "").rstrip("/")
|
| 2658 |
FRONTEND_CACHE_DIR = ROOT / "frontend_cache" # 🌟 指向新文件夹
|
| 2659 |
|
| 2660 |
+
# ---- 前端指纹:让"这一版到底拉到新前端没有"变成机器可回答 ----
|
| 2661 |
+
# 动机:同步失败时旧缓存仍可服务(页面正常打开),
|
| 2662 |
+
# 肉眼无法区分"没生效"和"没拉到",这是排查黑洞。
|
| 2663 |
+
FRONTEND_MANIFEST = {} # {"index.html": {"md5": "a1b2c3d4", "size": n, "mtime": t}, ...}
|
| 2664 |
+
FRONTEND_SYNC_OK = False # 本次启动三个文件是否全部拉取成功
|
| 2665 |
+
FRONTEND_SYNC_ERR = "" # 失败摘要(供 /api/health 展示)
|
| 2666 |
+
|
| 2667 |
+
|
| 2668 |
def sync_ui_assets():
|
| 2669 |
+
global FRONTEND_MANIFEST, FRONTEND_SYNC_OK, FRONTEND_SYNC_ERR
|
| 2670 |
if not SYNC_FROM_GUI_SPACE:
|
| 2671 |
print("📦 [Frontend Cache] SYNC_FROM_GUI_SPACE = False,跳过同步,使用本地缓存。")
|
| 2672 |
return
|
|
|
|
| 2679 |
files_to_sync = ["index.html", "style.css", "app.js"]
|
| 2680 |
|
| 2681 |
print(f"🔄 正在从 {UI_SPACE_URL} 同步前端文件到 /frontend_cache...")
|
| 2682 |
+
ok = True
|
| 2683 |
+
errs = []
|
| 2684 |
for f in files_to_sync:
|
| 2685 |
try:
|
| 2686 |
res = requests.get(f"{UI_SPACE_URL}/{f.lstrip('/')}?t={int(time.time())}", timeout=10)
|
|
|
|
| 2688 |
(FRONTEND_CACHE_DIR / f).write_text(res.text, encoding="utf-8")
|
| 2689 |
print(f"✅ 已更新: {f}")
|
| 2690 |
else:
|
| 2691 |
+
ok = False; errs.append(f"{f}=HTTP{res.status_code}")
|
| 2692 |
print(f"⚠️ 同步 {f} 失败,状态码: {res.status_code}")
|
| 2693 |
except Exception as e:
|
| 2694 |
+
ok = False; errs.append(f"{f}={type(e).__name__}")
|
| 2695 |
print(f"⚠️ 同步 {f} 网络异常: {e}")
|
| 2696 |
|
| 2697 |
+
FRONTEND_SYNC_OK = ok
|
| 2698 |
+
FRONTEND_SYNC_ERR = ";".join(errs)
|
| 2699 |
+
|
| 2700 |
+
# 指纹:内容 md5(前8位) + size + mtime —— 三者任一变化都能被看出来
|
| 2701 |
+
man = {}
|
| 2702 |
+
for f in files_to_sync:
|
| 2703 |
+
p = FRONTEND_CACHE_DIR / f
|
| 2704 |
+
if not p.exists():
|
| 2705 |
+
continue
|
| 2706 |
+
data = p.read_bytes()
|
| 2707 |
+
st = p.stat()
|
| 2708 |
+
man[f] = {
|
| 2709 |
+
"md5": hashlib.md5(data).hexdigest()[:8],
|
| 2710 |
+
"size": st.st_size,
|
| 2711 |
+
"mtime": int(st.st_mtime),
|
| 2712 |
+
}
|
| 2713 |
+
FRONTEND_MANIFEST = man
|
| 2714 |
+
|
| 2715 |
+
fingerprint = " | ".join(f"{k}={v['md5']}" for k, v in man.items())
|
| 2716 |
+
if ok:
|
| 2717 |
+
print(f"🧩 [Frontend] 指纹: {fingerprint}")
|
| 2718 |
+
else:
|
| 2719 |
+
# 🚨 大字告警:这次没拉到,正在用旧缓存 → 你改的东西不会生效
|
| 2720 |
+
print(f"🚨 [Frontend] 同步不完整,正在使用旧缓存!({FRONTEND_SYNC_ERR})")
|
| 2721 |
+
print(f"🚨 [Frontend] 实际生效的指纹: {fingerprint}")
|
| 2722 |
+
|
| 2723 |
# 1. 启动时执行同步
|
| 2724 |
sync_ui_assets()
|
| 2725 |
|
|
|
|
| 2754 |
print("🚀 Triggering ZeroGPU scan on 7861...")
|
| 2755 |
threading.Thread(target=trigger_gpu_scan, daemon=True).start()
|
| 2756 |
|
| 2757 |
+
# 3. 等 ZeroGPU 探针(7861)真正 listen —— 端口探测比固定 sleep 更快也更稳
|
| 2758 |
+
def _wait_port(port: int, timeout: float = 12.0) -> bool:
|
| 2759 |
+
t0 = time.time()
|
| 2760 |
+
while time.time() - t0 < timeout:
|
| 2761 |
+
try:
|
| 2762 |
+
with socket.create_connection(("127.0.0.1", port), timeout=0.3):
|
| 2763 |
+
print(f"✅ 端口 {port} 已就绪 ({round(time.time() - t0, 2)}s)")
|
| 2764 |
+
return True
|
| 2765 |
+
except OSError:
|
| 2766 |
+
time.sleep(0.15)
|
| 2767 |
+
print(f"⚠️ 端口 {port} 等待超时,继续启动主服务")
|
| 2768 |
+
return False
|
| 2769 |
+
|
| 2770 |
+
_wait_port(7861)
|
| 2771 |
|
| 2772 |
# 4. 最后启动主 API 服务
|
| 2773 |
print("🌐 Starting main API Gateway on 7860...")
|