Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -81,14 +81,15 @@ def fetch_models():
|
|
| 81 |
print("✅ Model downloads done.")
|
| 82 |
|
| 83 |
# -----------------------------
|
| 84 |
-
# Check if port is
|
| 85 |
# -----------------------------
|
| 86 |
-
def wait_for_port(port, host="127.0.0.1", timeout=
|
| 87 |
"""Wait until the WebUI port is open."""
|
| 88 |
start = time.time()
|
| 89 |
while time.time() - start < timeout:
|
| 90 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
| 91 |
try:
|
|
|
|
| 92 |
s.connect((host, port))
|
| 93 |
return True
|
| 94 |
except:
|
|
@@ -96,24 +97,22 @@ def wait_for_port(port, host="127.0.0.1", timeout=120):
|
|
| 96 |
return False
|
| 97 |
|
| 98 |
# -----------------------------
|
| 99 |
-
# Start WebUI
|
| 100 |
# -----------------------------
|
| 101 |
def start_webui():
|
| 102 |
ensure_folders()
|
| 103 |
fetch_models()
|
| 104 |
|
| 105 |
port = int(os.environ.get("PORT", 7860))
|
| 106 |
-
cmd = ["python", LAUNCH_PY] + shlex.split(WEBUI_ARGS) + [f"--port
|
| 107 |
-
print("▶️ Launching WebUI
|
| 108 |
|
| 109 |
-
#
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
except Exception as e:
|
| 116 |
-
print(f"❌ Exception: {e}")
|
| 117 |
|
| 118 |
threading.Thread(target=start_webui, daemon=True).start()
|
| 119 |
print("🧵 WebUI background thread started.")
|
|
@@ -123,10 +122,10 @@ print("🧵 WebUI background thread started.")
|
|
| 123 |
# -----------------------------
|
| 124 |
def show_status():
|
| 125 |
port = int(os.environ.get("PORT", 7860))
|
| 126 |
-
|
|
|
|
| 127 |
lines = ["✅ HF Space running (Option B launcher)."]
|
| 128 |
-
lines.append(f"WebUI URL
|
| 129 |
-
# Check models
|
| 130 |
for key, info in DOWNLOADS.items():
|
| 131 |
present = "yes" if os.path.exists(info["dest"]) else "no"
|
| 132 |
lines.append(f"{key}: {present}")
|
|
|
|
| 81 |
print("✅ Model downloads done.")
|
| 82 |
|
| 83 |
# -----------------------------
|
| 84 |
+
# Check if port is open
|
| 85 |
# -----------------------------
|
| 86 |
+
def wait_for_port(port, host="127.0.0.1", timeout=180):
|
| 87 |
"""Wait until the WebUI port is open."""
|
| 88 |
start = time.time()
|
| 89 |
while time.time() - start < timeout:
|
| 90 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
| 91 |
try:
|
| 92 |
+
s.settimeout(1)
|
| 93 |
s.connect((host, port))
|
| 94 |
return True
|
| 95 |
except:
|
|
|
|
| 97 |
return False
|
| 98 |
|
| 99 |
# -----------------------------
|
| 100 |
+
# Start WebUI in background
|
| 101 |
# -----------------------------
|
| 102 |
def start_webui():
|
| 103 |
ensure_folders()
|
| 104 |
fetch_models()
|
| 105 |
|
| 106 |
port = int(os.environ.get("PORT", 7860))
|
| 107 |
+
cmd = ["python", LAUNCH_PY] + shlex.split(WEBUI_ARGS) + [f"--port={port}"]
|
| 108 |
+
print("▶️ Launching WebUI (logs suppressed)...")
|
| 109 |
|
| 110 |
+
# Redirect stdout/stderr to avoid spamming HF logs
|
| 111 |
+
with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
|
| 112 |
+
if wait_for_port(port, timeout=180):
|
| 113 |
+
print(f"✅ WebUI is ready on port {port}")
|
| 114 |
+
else:
|
| 115 |
+
print("⚠️ WebUI did not start within timeout.")
|
|
|
|
|
|
|
| 116 |
|
| 117 |
threading.Thread(target=start_webui, daemon=True).start()
|
| 118 |
print("🧵 WebUI background thread started.")
|
|
|
|
| 122 |
# -----------------------------
|
| 123 |
def show_status():
|
| 124 |
port = int(os.environ.get("PORT", 7860))
|
| 125 |
+
space_domain = os.environ.get("SPACE_DOMAIN", "your-space-name.hf.space")
|
| 126 |
+
url = f"https://{space_domain}/?__theme=light" # HF public URL
|
| 127 |
lines = ["✅ HF Space running (Option B launcher)."]
|
| 128 |
+
lines.append(f"WebUI URL (open in browser when ready): {url}")
|
|
|
|
| 129 |
for key, info in DOWNLOADS.items():
|
| 130 |
present = "yes" if os.path.exists(info["dest"]) else "no"
|
| 131 |
lines.append(f"{key}: {present}")
|