cacodex commited on
Commit
7bfdb1c
·
verified ·
1 Parent(s): 75cdf0c

Upload 6 files

Browse files
Files changed (6) hide show
  1. .dockerignore +8 -0
  2. Dockerfile +57 -0
  3. README.md +53 -5
  4. jupyter_server_config.py +17 -0
  5. requirements.txt +1 -7
  6. start-services.sh +61 -0
.dockerignore ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ .git
2
+ .gitignore
3
+ __pycache__/
4
+ .pytest_cache/
5
+ .mypy_cache/
6
+ .venv/
7
+ venv/
8
+ node_modules/
Dockerfile ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ ARG BESZEL_VERSION=0.18.7
4
+
5
+ ENV DEBIAN_FRONTEND=noninteractive \
6
+ PYTHONDONTWRITEBYTECODE=1 \
7
+ PYTHONUNBUFFERED=1
8
+
9
+ RUN useradd -m -u 1000 user && \
10
+ apt-get update && \
11
+ apt-get install -y --no-install-recommends \
12
+ bash \
13
+ ca-certificates \
14
+ curl \
15
+ git \
16
+ git-lfs \
17
+ tini && \
18
+ rm -rf /var/lib/apt/lists/* && \
19
+ mkdir -p /data/workspace /data/beszel-agent && \
20
+ chown -R user:user /data /home/user
21
+
22
+ USER user
23
+
24
+ ENV HOME=/home/user \
25
+ PATH=/home/user/.local/bin:$PATH \
26
+ HF_HOME=/data/.huggingface \
27
+ JUPYTER_ROOT_DIR=/data/workspace \
28
+ BESZEL_DATA_DIR=/data/beszel-agent \
29
+ BESZEL_LISTEN=45876 \
30
+ DISABLE_SSH=true \
31
+ DOCKER_HOST="" \
32
+ SKIP_SYSTEMD=true \
33
+ SKIP_GPU=true
34
+
35
+ WORKDIR $HOME/app
36
+
37
+ COPY --chown=user requirements.txt ./requirements.txt
38
+
39
+ RUN python -m pip install --user --no-cache-dir --upgrade pip && \
40
+ python -m pip install --user --no-cache-dir -r requirements.txt
41
+
42
+ RUN mkdir -p "$HOME/.local/bin" && \
43
+ curl -fsSL \
44
+ "https://github.com/henrygd/beszel/releases/download/v${BESZEL_VERSION}/beszel-agent_Linux_amd64.tar.gz" | \
45
+ tar -xz -C /tmp beszel-agent && \
46
+ install -m 0755 /tmp/beszel-agent "$HOME/.local/bin/beszel-agent" && \
47
+ rm -f /tmp/beszel-agent
48
+
49
+ COPY --chown=user jupyter_server_config.py ./jupyter_server_config.py
50
+ COPY --chown=user start-services.sh ./start-services.sh
51
+
52
+ RUN chmod +x ./start-services.sh
53
+
54
+ EXPOSE 7860
55
+
56
+ ENTRYPOINT ["/usr/bin/tini", "--"]
57
+ CMD ["./start-services.sh"]
README.md CHANGED
@@ -1,10 +1,58 @@
1
  ---
2
- title: JupyterLab
3
- emoji: 💻
4
- colorFrom: red
5
- colorTo: blue
6
  sdk: docker
 
7
  pinned: false
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: JupyterLab with Beszel Agent
3
+ colorFrom: blue
4
+ colorTo: green
 
5
  sdk: docker
6
+ app_port: 7860
7
  pinned: false
8
  ---
9
 
10
+ # JupyterLab with Beszel Agent
11
+
12
+ This Hugging Face Docker Space runs JupyterLab on port `7860` and starts a
13
+ `beszel-agent` process in the same container.
14
+
15
+ ## Required Space secrets
16
+
17
+ Add these values in `Settings -> Variables and secrets -> Secrets`:
18
+
19
+ - `JUPYTER_TOKEN`
20
+ - `BESZEL_KEY`
21
+ - `BESZEL_TOKEN`
22
+ - `BESZEL_HUB_URL`
23
+
24
+ Optional:
25
+
26
+ - `BESZEL_SYSTEM_NAME`
27
+ - `BESZEL_LISTEN`
28
+ - `JUPYTER_ROOT_DIR`
29
+ - `BESZEL_DATA_DIR`
30
+ - `SKIP_GPU`
31
+
32
+ ## Important notes
33
+
34
+ - Hugging Face Docker Spaces expose a single public port. JupyterLab is served
35
+ on `7860`.
36
+ - `beszel-agent` uses an outgoing WebSocket connection to the hub, so there is
37
+ no need for `--network host`.
38
+ - Docker monitoring is intentionally disabled because Docker Spaces do not
39
+ expose `/var/run/docker.sock`.
40
+ - Notebook files are stored in `/data/workspace`.
41
+ - Beszel agent state is stored in `/data/beszel-agent`.
42
+ - If `BESZEL_SYSTEM_NAME` is not set, the startup script falls back to the
43
+ built-in `SPACE_ID` runtime variable when available.
44
+ - Without persistent storage, both directories are ephemeral and may be lost on
45
+ restart.
46
+ - For long-running monitoring, use paid hardware or a custom sleep policy. Free
47
+ CPU Spaces can go to sleep after inactivity.
48
+
49
+ ## What this Space can monitor
50
+
51
+ - JupyterLab availability
52
+ - CPU, memory, disk, and network usage of the Space container
53
+ - Beszel agent online/offline status
54
+
55
+ ## What this Space cannot monitor
56
+
57
+ - Per-container Docker metrics inside the Space
58
+ - Host-level metrics outside the Hugging Face Space runtime
jupyter_server_config.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ c.ServerApp.ip = "0.0.0.0"
4
+ c.ServerApp.port = 7860
5
+ c.ServerApp.open_browser = False
6
+ c.ServerApp.root_dir = os.environ.get("JUPYTER_ROOT_DIR", "/data/workspace")
7
+ c.ServerApp.token = os.environ.get("JUPYTER_TOKEN", "huggingface")
8
+ c.ServerApp.disable_check_xsrf = True
9
+ c.ServerApp.tornado_settings = {
10
+ "headers": {
11
+ "Content-Security-Policy": "frame-ancestors *",
12
+ }
13
+ }
14
+ c.ServerApp.cookie_options = {
15
+ "SameSite": "None",
16
+ "Secure": True,
17
+ }
requirements.txt CHANGED
@@ -1,7 +1 @@
1
- jupyterlab
2
- notebook
3
- jupyter-server
4
- ipykernel
5
- numpy
6
- pandas
7
- matplotlib
 
1
+ jupyterlab>=4.2,<5
 
 
 
 
 
 
start-services.sh ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+
3
+ set -Eeuo pipefail
4
+
5
+ export JUPYTER_TOKEN="${JUPYTER_TOKEN:-huggingface}"
6
+ export JUPYTER_ROOT_DIR="${JUPYTER_ROOT_DIR:-/data/workspace}"
7
+ export BESZEL_DATA_DIR="${BESZEL_DATA_DIR:-/data/beszel-agent}"
8
+ export BESZEL_LISTEN="${BESZEL_LISTEN:-45876}"
9
+ export BESZEL_SYSTEM_NAME="${BESZEL_SYSTEM_NAME:-${SPACE_ID:-hf-space-jupyterlab}}"
10
+ export DISABLE_SSH="${DISABLE_SSH:-true}"
11
+ export DOCKER_HOST="${DOCKER_HOST:-}"
12
+ export SKIP_SYSTEMD="${SKIP_SYSTEMD:-true}"
13
+ export SKIP_GPU="${SKIP_GPU:-true}"
14
+
15
+ mkdir -p \
16
+ "$JUPYTER_ROOT_DIR" \
17
+ "$BESZEL_DATA_DIR" \
18
+ "$HOME/.local/share/jupyter/runtime"
19
+
20
+ if [[ -z "${BESZEL_KEY:-}" || -z "${BESZEL_TOKEN:-}" || -z "${BESZEL_HUB_URL:-}" ]]; then
21
+ echo "Missing required Space secrets: BESZEL_KEY, BESZEL_TOKEN, BESZEL_HUB_URL" >&2
22
+ exit 1
23
+ fi
24
+
25
+ cleanup() {
26
+ local exit_code=$?
27
+
28
+ if [[ -n "${JUPYTER_PID:-}" ]]; then
29
+ kill "$JUPYTER_PID" 2>/dev/null || true
30
+ fi
31
+
32
+ if [[ -n "${BESZEL_PID:-}" ]]; then
33
+ kill "$BESZEL_PID" 2>/dev/null || true
34
+ fi
35
+
36
+ wait 2>/dev/null || true
37
+ exit "$exit_code"
38
+ }
39
+
40
+ trap cleanup EXIT INT TERM
41
+
42
+ echo "Starting beszel-agent..."
43
+ KEY="$BESZEL_KEY" \
44
+ TOKEN="$BESZEL_TOKEN" \
45
+ HUB_URL="$BESZEL_HUB_URL" \
46
+ LISTEN="$BESZEL_LISTEN" \
47
+ DATA_DIR="$BESZEL_DATA_DIR" \
48
+ SYSTEM_NAME="$BESZEL_SYSTEM_NAME" \
49
+ DISABLE_SSH="$DISABLE_SSH" \
50
+ DOCKER_HOST="$DOCKER_HOST" \
51
+ SKIP_SYSTEMD="$SKIP_SYSTEMD" \
52
+ SKIP_GPU="$SKIP_GPU" \
53
+ beszel-agent &
54
+ BESZEL_PID=$!
55
+
56
+ echo "Starting JupyterLab on 0.0.0.0:7860..."
57
+ jupyter lab --config="$HOME/app/jupyter_server_config.py" &
58
+ JUPYTER_PID=$!
59
+
60
+ wait -n "$BESZEL_PID" "$JUPYTER_PID"
61
+ echo "A managed process exited, stopping the container." >&2