Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# app.py - HF Space launcher (Option B, HF tree aware, clean logs)
|
| 2 |
import os
|
| 3 |
import subprocess
|
| 4 |
import threading
|
|
@@ -53,12 +53,10 @@ def ensure_folders():
|
|
| 53 |
def download_file(url, dest, retries=3, backoff=5):
|
| 54 |
"""Download a file if missing, with retries."""
|
| 55 |
if os.path.exists(dest):
|
| 56 |
-
print(f"✅ Already exists: {dest}")
|
| 57 |
return True
|
| 58 |
os.makedirs(os.path.dirname(dest), exist_ok=True)
|
| 59 |
for attempt in range(1, retries + 1):
|
| 60 |
try:
|
| 61 |
-
print(f"⬇️ Download attempt {attempt}: {url}")
|
| 62 |
with requests.get(url, stream=True, timeout=60) as r:
|
| 63 |
r.raise_for_status()
|
| 64 |
with open(dest + ".part", "wb") as f:
|
|
@@ -66,25 +64,20 @@ def download_file(url, dest, retries=3, backoff=5):
|
|
| 66 |
if chunk:
|
| 67 |
f.write(chunk)
|
| 68 |
os.replace(dest + ".part", dest)
|
| 69 |
-
print(f"✅ Downloaded: {dest}")
|
| 70 |
return True
|
| 71 |
-
except Exception
|
| 72 |
-
print(f"⚠️ Attempt {attempt} failed: {e}")
|
| 73 |
time.sleep(backoff * attempt)
|
| 74 |
-
print(f"❌ Failed to download: {dest}")
|
| 75 |
return False
|
| 76 |
|
| 77 |
def fetch_models():
|
| 78 |
-
|
| 79 |
for key, info in DOWNLOADS.items():
|
| 80 |
download_file(info["url"], info["dest"])
|
| 81 |
-
print("✅ Model downloads done.")
|
| 82 |
|
| 83 |
# -----------------------------
|
| 84 |
-
#
|
| 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:
|
|
@@ -102,12 +95,10 @@ def wait_for_port(port, host="127.0.0.1", timeout=180):
|
|
| 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
|
| 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}")
|
|
@@ -126,6 +117,7 @@ def show_status():
|
|
| 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}")
|
|
@@ -141,5 +133,5 @@ with gr.Blocks() as demo:
|
|
| 141 |
demo.launch(
|
| 142 |
server_name="0.0.0.0",
|
| 143 |
server_port=int(os.environ.get("PORT", 7860)),
|
| 144 |
-
ssr_mode=False
|
| 145 |
)
|
|
|
|
| 1 |
+
# app.py - HF Space launcher (Option B, HF tree aware, clean logs + Gradio UI)
|
| 2 |
import os
|
| 3 |
import subprocess
|
| 4 |
import threading
|
|
|
|
| 53 |
def download_file(url, dest, retries=3, backoff=5):
|
| 54 |
"""Download a file if missing, with retries."""
|
| 55 |
if os.path.exists(dest):
|
|
|
|
| 56 |
return True
|
| 57 |
os.makedirs(os.path.dirname(dest), exist_ok=True)
|
| 58 |
for attempt in range(1, retries + 1):
|
| 59 |
try:
|
|
|
|
| 60 |
with requests.get(url, stream=True, timeout=60) as r:
|
| 61 |
r.raise_for_status()
|
| 62 |
with open(dest + ".part", "wb") as f:
|
|
|
|
| 64 |
if chunk:
|
| 65 |
f.write(chunk)
|
| 66 |
os.replace(dest + ".part", dest)
|
|
|
|
| 67 |
return True
|
| 68 |
+
except Exception:
|
|
|
|
| 69 |
time.sleep(backoff * attempt)
|
|
|
|
| 70 |
return False
|
| 71 |
|
| 72 |
def fetch_models():
|
| 73 |
+
"""Download runtime models if missing."""
|
| 74 |
for key, info in DOWNLOADS.items():
|
| 75 |
download_file(info["url"], info["dest"])
|
|
|
|
| 76 |
|
| 77 |
# -----------------------------
|
| 78 |
+
# Wait for WebUI port
|
| 79 |
# -----------------------------
|
| 80 |
def wait_for_port(port, host="127.0.0.1", timeout=180):
|
|
|
|
| 81 |
start = time.time()
|
| 82 |
while time.time() - start < timeout:
|
| 83 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
|
|
| 95 |
def start_webui():
|
| 96 |
ensure_folders()
|
| 97 |
fetch_models()
|
|
|
|
| 98 |
port = int(os.environ.get("PORT", 7860))
|
| 99 |
cmd = ["python", LAUNCH_PY] + shlex.split(WEBUI_ARGS) + [f"--port={port}"]
|
|
|
|
| 100 |
|
| 101 |
+
# Redirect output to avoid HF spam
|
| 102 |
with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
|
| 103 |
if wait_for_port(port, timeout=180):
|
| 104 |
print(f"✅ WebUI is ready on port {port}")
|
|
|
|
| 117 |
url = f"https://{space_domain}/?__theme=light" # HF public URL
|
| 118 |
lines = ["✅ HF Space running (Option B launcher)."]
|
| 119 |
lines.append(f"WebUI URL (open in browser when ready): {url}")
|
| 120 |
+
# Model status
|
| 121 |
for key, info in DOWNLOADS.items():
|
| 122 |
present = "yes" if os.path.exists(info["dest"]) else "no"
|
| 123 |
lines.append(f"{key}: {present}")
|
|
|
|
| 133 |
demo.launch(
|
| 134 |
server_name="0.0.0.0",
|
| 135 |
server_port=int(os.environ.get("PORT", 7860)),
|
| 136 |
+
ssr_mode=False # prevent Node server port issues
|
| 137 |
)
|