File size: 1,969 Bytes
7088e5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import sys
import time
import subprocess

try:
    import spaces
    HAS_SPACES = True
except Exception:
    # Plain CPU Spaces (and some runtimes) don't ship the ZeroGPU `spaces` package.
    # Without this guard, `import spaces` crashes app.py and the whole proxy never starts.
    spaces = None
    HAS_SPACES = False

print("=======================================================")
print("🚀 Initializing Etrog Bypass Proxy on Hugging Face ZeroGPU Space")
print("=======================================================")

if HAS_SPACES:
    # Decoration itself can raise on non-ZeroGPU runtimes — guard both import AND decoration.
    try:
        @spaces.GPU
        def zero_gpu_dummy():
            return "ZeroGPU Ready"

        zero_gpu_dummy()
    except Exception as e:
        print(f"ZeroGPU init note: {e}")
else:
    print("ℹ️ `spaces` package not available — running without ZeroGPU (fine on CPU Spaces).")

# Ensure Node.js is installed
NODE_VERSION = "v20.18.0"
NODE_DIR = f"node-{NODE_VERSION}-linux-x64"
NODE_TAR = f"{NODE_DIR}.tar.xz"
NODE_URL = f"https://nodejs.org/dist/{NODE_VERSION}/{NODE_TAR}"

if not os.path.exists(NODE_DIR):
    print(f"📦 Downloading standalone Node.js {NODE_VERSION}...")
    subprocess.run(f"curl -fsSL {NODE_URL} | tar -xJ", shell=True, check=True)

node_bin = os.path.abspath(f"{NODE_DIR}/bin/node")
npm_bin = os.path.abspath(f"{NODE_DIR}/bin/npm")

os.environ["PATH"] = os.path.abspath(f"{NODE_DIR}/bin") + os.path.pathsep + os.environ.get("PATH", "")

print("📥 Installing Node.js dependencies...")
subprocess.run([npm_bin, "install", "--omit=dev"], check=True)

# Launch Node server on main HF ingress port 7860
os.environ["PORT"] = "7860"
print("📡 Launching Node server.js on port 7860...")
proc = subprocess.Popen([node_bin, "server.js"], env=os.environ)

# Keep process running without launching Gradio HTTP server
try:
    proc.wait()
except KeyboardInterrupt:
    proc.terminate()