Spaces:
Paused
Paused
Download app.py from srinetwork/srinet1: direct link, hf CLI and curl.
- Browser
- Download file 2.39 kB
-
https://huggingface.co/spaces/srinetwork/srinet1/resolve/main/app.py
- Command line
-
hf download hf://spaces/srinetwork/srinet1/app.py
-
curl -L -o app.py https://huggingface.co/spaces/srinetwork/srinet1/resolve/main/app.py
2.39 kB
| import os | |
| import subprocess | |
| import threading | |
| import time | |
| import requests | |
| import secrets | |
| import string | |
| from jupyter_server.auth import passwd | |
| # ==================== | |
| # CONFIG | |
| # ===================== | |
| PORT = int(os.environ.get("PORT", 7860)) | |
| KEEP_ALIVE_URL = "https://srinetwork-srinet1.hf.space" | |
| INTERVAL = 30 * 60 | |
| # π HF Variable / Secret (PLAIN PASSWORD) | |
| ENV_PASSWORD = os.environ.get("JUPYTER_PASSWORD") | |
| # ===================== | |
| def run_cmd(cmd): | |
| print(f"π¦ {cmd}") | |
| subprocess.run(cmd, shell=True, check=False) | |
| def install_packages(): | |
| print("π§ Installing system packages...") | |
| run_cmd("apt-get update") | |
| run_cmd("apt-get install -y sudo curl wget openjdk-21-jdk") | |
| run_cmd("java -version") | |
| def keep_alive_loop(): | |
| while True: | |
| try: | |
| r = requests.get(KEEP_ALIVE_URL, timeout=10) | |
| print(f"π Keep-alive OK | {r.status_code}") | |
| except Exception as e: | |
| print(f"β οΈ Keep-alive error: {e}") | |
| time.sleep(INTERVAL) | |
| def generate_password(length=14): | |
| chars = string.ascii_letters + string.digits | |
| return ''.join(secrets.choice(chars) for _ in range(length)) | |
| def start_jupyterlab(): | |
| # π Decide password source | |
| if ENV_PASSWORD and ENV_PASSWORD.strip(): | |
| plain_password = ENV_PASSWORD.strip() | |
| source = "HF VARIABLE" | |
| else: | |
| plain_password = generate_password() | |
| source = "RANDOM (fallback)" | |
| password_hash = passwd(plain_password) | |
| print("\n" + "=" * 60) | |
| print("π JUPYTERLAB LOGIN INFO") | |
| print(f"π Password Source : {source}") | |
| print(f"π PASSWORD : {plain_password}") | |
| print("β οΈ Save this password (restart = may change)") | |
| print("=" * 60 + "\n") | |
| cmd = [ | |
| "jupyter", | |
| "lab", | |
| "--ip=0.0.0.0", | |
| f"--port={PORT}", | |
| "--no-browser", | |
| "--allow-root", | |
| # β Correct auth flags | |
| "--ServerApp.token=''", | |
| f"--ServerApp.password='{password_hash}'", | |
| "--ServerApp.disable_check_xsrf=True", | |
| "--ServerApp.allow_origin='*'" | |
| ] | |
| print("π Starting JupyterLab...") | |
| subprocess.run(" ".join(cmd), shell=True) | |
| # ===================== | |
| # MAIN | |
| # ===================== | |
| if __name__ == "__main__": | |
| install_packages() | |
| threading.Thread( | |
| target=keep_alive_loop, | |
| daemon=True | |
| ).start() | |
| start_jupyterlab() |