Spaces:
Running on Zero
Running on Zero
File size: 5,018 Bytes
e594124 ab25545 24c6880 ab25545 e594124 ab25545 e594124 99fc2d1 a364ccb 55066b7 1ff92db e594124 1ff92db 703dbd9 1ff92db e594124 1ff92db 55066b7 cbc2937 55066b7 99fc2d1 55066b7 99fc2d1 55066b7 99fc2d1 55066b7 99fc2d1 55066b7 99fc2d1 55066b7 99fc2d1 55066b7 1ff92db 55066b7 1ff92db | 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | # ZeroGPU compatibility handler:
# Hugging Face Spaces requires 'import spaces' at the top of the file
# and at least one module-level function decorated with @spaces.GPU.
# CRITICAL: Do NOT invoke this function!
# The presence of the decorator satisfies the ZeroGPU scanner during initialization.
# Invoking it requests a GPU slice and burns the daily quota (default 60s/call).
# Keeping it uncalled consumes 0s of GPU quota permanently.
try:
# pyrefly: ignore [missing-import]
import spaces
@spaces.GPU(duration=1)
def dummy_zero_gpu():
return True
except Exception:
pass
import os
import sys
import json
import subprocess
import urllib.request
import tarfile
import threading
import time
def get_latest_go_version():
try:
req = urllib.request.Request(
"https://go.dev/dl/?mode=json",
headers={"User-Agent": "Mozilla/5.0"}
)
with urllib.request.urlopen(req, timeout=10) as resp:
data = json.loads(resp.read().decode())
if data and len(data) > 0:
ver = data[0]["version"] # e.g. "go1.27.1"
return ver.lstrip("go")
except Exception as e:
print(f"[HF Space] Gagal mendeteksi versi Go otomatis: {e}", flush=True)
return "1.26.0"
def start_keep_alive():
"""Background daemon to ping the public URL periodically so the Space stays awake."""
def pinger():
# Tunggu 30 detik agar server Go selesai booting dan listening
time.sleep(30)
space_host = os.environ.get("SPACE_HOST")
space_id = os.environ.get("SPACE_ID")
if space_host:
target_url = f"https://{space_host}/health"
elif space_id:
slug = space_id.replace("/", "-").lower()
target_url = f"https://{slug}.hf.space/health"
else:
target_url = "https://ilhamdev-wa-full-pfp.hf.space/health"
print(f"[HF Keep-Alive] Background pinger aktif. Target: {target_url}", flush=True)
headers = {"User-Agent": "HF-Space-SelfKeepAlive/1.0"}
hf_token = os.environ.get("HF_TOKEN")
if hf_token:
headers["Authorization"] = f"Bearer {hf_token}"
while True:
try:
# Interval ping setiap 5 menit agar Space selalu terjaga aktif
time.sleep(5 * 60)
req = urllib.request.Request(
target_url,
headers=headers
)
with urllib.request.urlopen(req, timeout=30) as resp:
print(f"[HF Keep-Alive] Self-ping sukses (Status: {resp.status})", flush=True)
except Exception as e:
print(f"[HF Keep-Alive] Ping notice: {e}", flush=True)
t = threading.Thread(target=pinger, daemon=True)
t.start()
def setup_and_run():
# Panggil dummy_zero_gpu dengan durasi minimal 1s untuk registrasi handshake ZeroGPU supervisor
try:
if "spaces" in sys.modules:
dummy_zero_gpu()
except Exception as e:
print(f"[HF Space] ZeroGPU handshake notice: {e}", flush=True)
server_bin = "./server"
# 1. Kompilasi binary Go jika belum ada
if not os.path.exists(server_bin):
go_ver = get_latest_go_version()
go_tar_name = f"go{go_ver}.linux-amd64.tar.gz"
go_url = f"https://go.dev/dl/{go_tar_name}"
print(f"[HF Space] Mengunduh Go v{go_ver} ({go_url})...", flush=True)
tar_path = "/tmp/go.tar.gz"
urllib.request.urlretrieve(go_url, tar_path)
print("[HF Space] Mengekstrak Go toolchain ke /tmp/go...", flush=True)
with tarfile.open(tar_path, "r:gz") as tar:
tar.extractall(path="/tmp")
go_bin = "/tmp/go/bin/go"
print(f"[HF Space] Mengompilasi server Go dengan Go v{go_ver}...", flush=True)
env = os.environ.copy()
env["CGO_ENABLED"] = "0"
env["GOROOT"] = "/tmp/go"
env["GOPATH"] = "/tmp/gopath"
env["PATH"] = f"/tmp/go/bin:{env.get('PATH', '')}"
build_cmd = [go_bin, "build", "-ldflags=-w -s", "-o", "server", "."]
res = subprocess.run(build_cmd, env=env)
if res.returncode != 0:
print("[HF Space] ERROR: Kompilasi Go gagal!", flush=True)
sys.exit(res.returncode)
print("[HF Space] Kompilasi berhasil!", flush=True)
# Bersihkan file tar installer
if os.path.exists(tar_path):
os.remove(tar_path)
# 2. Aktifkan background keep-alive pinger
start_keep_alive()
# 3. Jalankan server Go di port 7860
os.chmod(server_bin, 0o755)
env = os.environ.copy()
env["PORT"] = os.environ.get("PORT", "7860")
print(f"[HF Space] Menjalankan server Go pada port {env['PORT']}...", flush=True)
subprocess.run([server_bin], env=env)
if __name__ == "__main__":
setup_and_run()
|