Spaces:
Sleeping
Sleeping
Daemon: drop RELAY_SECRET; auth via HF token only
Browse filesFiles changed (2)
5 unmodified lines67891011121356 unmodified lines70717273747576237 unmodified lines31431531631731831932012 unmodified lines33333433533633733833920 unmodified lines36036136236336436536624 unmodified lines39139239339439539639739839940040140240361 unmodified lines4654664674684694704714724734744754764774785 unmodified linesRun it (foreground, or under nohup / tmux / systemd):export RELAY_SPACE="your-username/nii-relay" # the Space repo idexport RELAY_SECRET="<same secret as the Space>"export HF_TOKEN="hf_..." # read token; needed if Space is privateexport CLIENT_ID="$(hostname)" # how it shows up in the UI (optional)python client.py56 unmodified linesSPACE = os.environ.get("RELAY_SPACE", "")SECRET = os.environ.get("RELAY_SECRET", "")HF_TOKEN = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN")CLIENT_ID = os.environ.get("CLIENT_ID") or _default_client_id()POLL_INTERVAL = float(os.environ.get("POLL_INTERVAL", "5"))237 unmodified linestry:log(f"connecting to Space {SPACE} β¦")c = _make_client()c.predict(CLIENT_ID, SECRET, _meta(), api_name="/register")CLIENT = clog(f"registered as '{CLIENT_ID}'")return c12 unmodified linesdef _safe_result(cid: str, code: int, output: str, session: str):try:_post(cid, CLIENT_ID, SECRET, code, output, session, api_name="/result")except Exception as e:log(f"result post failed for {session}: {type(e).__name__}: {e}")20 unmodified linesdest = _expand(cmd.get("dest") or cmd.get("filename") or "downloaded.bin")log(f"[file] put β {dest}")try:ret = _post(cmd["file_id"], SECRET, api_name="/fetch_file")src = _result_path(ret)if not src or not os.path.isfile(src):raise FileNotFoundError("relay returned no file")24 unmodified linesraise ValueError(f"{size} bytes exceeds the {MAX_FILE_BYTES}-byte limit")except Exception as e:try:_post(cid, CLIENT_ID, SECRET, None, f"{e}", api_name="/upload_result")except Exception as e2:log(f"error report failed: {type(e2).__name__}: {e2}")log(f"[file] get β error: {e}")returntry:_post(cid, CLIENT_ID, SECRET, handle_file(remote), "", api_name="/upload_result")log(f"[file] get β sent {size} bytes")except Exception as e:log(f"[file] get upload failed: {type(e).__name__}: {e}")61 unmodified linesdef main():if not SPACE:sys.exit("set RELAY_SPACE to the Space repo id, e.g. your-username/nii-relay")if not SECRET:log("WARNING: RELAY_SECRET is empty β the relay will reject this daemon")log(f"daemon starting β client_id='{CLIENT_ID}', poll every {POLL_INTERVAL}s")threading.Thread(target=_shell_reaper, daemon=True).start()connect()while True:try:res = CLIENT.predict(CLIENT_ID, SECRET, api_name="/poll")for cmd in (res or {}).get("commands", []):get_worker(cmd.get("session") or "main").submit(cmd)except KeyboardInterrupt:5 unmodified lines678910111256 unmodified lines697071727374237 unmodified lines31231331431531631731812 unmodified lines33133233333433533633720 unmodified lines35835936036136236336424 unmodified lines38939039139239339439539639739839940040161 unmodified lines4634644654664674684694704714724734745 unmodified linesRun it (foreground, or under nohup / tmux / systemd):export RELAY_SPACE="your-username/nii-relay" # the Space repo idexport HF_TOKEN="hf_..." # token with access to the private Space (this is the auth)export CLIENT_ID="$(hostname)" # how it shows up in the UI (optional)python client.py56 unmodified linesSPACE = os.environ.get("RELAY_SPACE", "")HF_TOKEN = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN")CLIENT_ID = os.environ.get("CLIENT_ID") or _default_client_id()POLL_INTERVAL = float(os.environ.get("POLL_INTERVAL", "5"))237 unmodified linestry:log(f"connecting to Space {SPACE} β¦")c = _make_client()c.predict(CLIENT_ID, _meta(), api_name="/register")CLIENT = clog(f"registered as '{CLIENT_ID}'")return c12 unmodified linesdef _safe_result(cid: str, code: int, output: str, session: str):try:_post(cid, CLIENT_ID, code, output, session, api_name="/result")except Exception as e:log(f"result post failed for {session}: {type(e).__name__}: {e}")20 unmodified linesdest = _expand(cmd.get("dest") or cmd.get("filename") or "downloaded.bin")log(f"[file] put β {dest}")try:ret = _post(cmd["file_id"], api_name="/fetch_file")src = _result_path(ret)if not src or not os.path.isfile(src):raise FileNotFoundError("relay returned no file")24 unmodified linesraise ValueError(f"{size} bytes exceeds the {MAX_FILE_BYTES}-byte limit")except Exception as e:try:_post(cid, CLIENT_ID, None, f"{e}", api_name="/upload_result")except Exception as e2:log(f"error report failed: {type(e2).__name__}: {e2}")log(f"[file] get β error: {e}")returntry:_post(cid, CLIENT_ID, handle_file(remote), "", api_name="/upload_result")log(f"[file] get β sent {size} bytes")except Exception as e:log(f"[file] get upload failed: {type(e).__name__}: {e}")61 unmodified linesdef main():if not SPACE:sys.exit("set RELAY_SPACE to the Space repo id, e.g. your-username/nii-relay")log(f"daemon starting β client_id='{CLIENT_ID}', poll every {POLL_INTERVAL}s")threading.Thread(target=_shell_reaper, daemon=True).start()connect()while True:try:res = CLIENT.predict(CLIENT_ID, api_name="/poll")for cmd in (res or {}).get("commands", []):get_worker(cmd.get("session") or "main").submit(cmd)except KeyboardInterrupt:
daemon/__pycache__/client.cpython-311.pyc
daemon/client.py+7-11