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()