File size: 1,850 Bytes
5181796
 
97936b0
 
 
5181796
97936b0
 
 
5b775cd
97936b0
 
 
5181796
97936b0
511a610
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97936b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
511a610
 
 
97936b0
 
 
 
 
 
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
import os
import subprocess
import threading
import time
import requests

# =====================
# CONFIG
# =====================
PORT = int(os.environ.get("PORT", 7860))
KEEP_ALIVE_URL = "https://srinetwork-abc.hf.space"
INTERVAL = 30 * 60  # 30 minutes
# =====================


def run_cmd(cmd):
    print(f"πŸ“¦ Running: {cmd}")
    subprocess.run(cmd, shell=True, check=False)


def install_packages():
    """Install system dependencies"""
    print("πŸ”§ Installing system packages...")

    run_cmd("apt-get update")

    run_cmd(
        "apt-get install -y "
        "sudo "
        "curl "
        "wget "
        "openjdk-21-jdk"
    )

    # Verify installs
    run_cmd("java -version")
    run_cmd("curl --version")
    run_cmd("wget --version")
    run_cmd("sudo --version")


def keep_alive_loop():
    """Ping HF Space every 30 minutes"""
    while True:
        try:
            r = requests.get(KEEP_ALIVE_URL, timeout=10)
            print(f"πŸ” Keep-alive ping sent | Status: {r.status_code}")
        except Exception as e:
            print(f"⚠️ Keep-alive error: {e}")

        time.sleep(INTERVAL)


def start_jupyterlab():
    cmd = [
        "jupyter",
        "lab",
        "--ip=0.0.0.0",
        f"--port={PORT}",
        "--no-browser",
        "--allow-root",
        "--NotebookApp.token=''",
        "--NotebookApp.password=''",
        "--NotebookApp.allow_origin='*'",
        "--NotebookApp.disable_check_xsrf=True"
    ]

    print("πŸš€ Starting JupyterLab...")
    subprocess.run(" ".join(cmd), shell=True)


# =====================
# MAIN
# =====================
if __name__ == "__main__":
    # Install system packages first
    install_packages()

    # Start keep-alive thread
    t = threading.Thread(target=keep_alive_loop, daemon=True)
    t.start()

    # Start JupyterLab
    start_jupyterlab()