kipeles commited on
Commit
3c5edb9
·
1 Parent(s): fe77730

wasm support added

Browse files
Files changed (2) hide show
  1. Dockerfile +13 -18
  2. entrypoint.sh +13 -18
Dockerfile CHANGED
@@ -3,7 +3,7 @@ FROM ubuntu:24.04
3
  # Disable interactive prompts during package installation
4
  ENV DEBIAN_FRONTEND=noninteractive
5
 
6
- # 1. Install dependencies (including python3, pip, and venv)
7
  RUN apt-get update && apt-get install -y --no-install-recommends \
8
  curl \
9
  jq \
@@ -18,20 +18,17 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
18
  python3-venv \
19
  && rm -rf /var/lib/apt/lists/*
20
 
21
- # 2. Delete the pre-created 'ubuntu' user (UID 1000) and create your custom 'user' (UID 1000)
22
  RUN userdel -r ubuntu && useradd -m -u 1000 user
23
 
24
  WORKDIR /app
25
 
26
- # 3. Create a Python Virtual Environment and install packages
27
- # This avoids PEP 668 "externally-managed-environment" errors on Ubuntu 24.04
28
  RUN python3 -m venv /app/venv
29
  ENV PATH="/app/venv/bin:$PATH"
 
30
 
31
- # Install FastAPI and Uvicorn
32
- RUN pip install --no-cache-dir fastapi uvicorn requests pydantic
33
-
34
- # 4. Download & Extract ApexKit Binary
35
  RUN echo "Downloading latest public release archive..." && \
36
  LATEST_URL=$(curl -s https://api.github.com/repos/deniskipeles/apexkit/releases/latest | \
37
  jq -r '.assets[] | select(.name | contains("linux-gnu.tar.gz")) | .browser_download_url') && \
@@ -42,25 +39,23 @@ RUN echo "Downloading latest public release archive..." && \
42
  chmod +x /usr/local/bin/apexkit && \
43
  rm apexkit.tar.gz
44
 
45
- # 5. Copy your FastAPI application code
46
- # Make sure to create a 'main.py' file in the same directory as your Dockerfile
 
 
 
 
47
  COPY --chown=user:user main.py /app/main.py
48
 
49
- # 6. Setup directories
50
- # /app/storage is the fast ephemeral NVMe
51
- # /data/storage is the mounted persistent storage provided by HF
52
  RUN mkdir -p /app/storage /data/storage && \
53
  chown -R user:user /app /data
54
 
55
- # 7. Copy and prepare the entrypoint script
56
  COPY --chown=user:user entrypoint.sh /app/entrypoint.sh
57
  RUN chmod +x /app/entrypoint.sh
58
 
59
- # 8. Switch to the non-root user
60
  USER user
61
-
62
- # Hugging Face exposes port 7860 by default
63
  ENV PORT=7860
64
 
65
- # 9. Start via the sync manager script
66
  ENTRYPOINT ["/app/entrypoint.sh"]
 
3
  # Disable interactive prompts during package installation
4
  ENV DEBIAN_FRONTEND=noninteractive
5
 
6
+ # 1. Install dependencies
7
  RUN apt-get update && apt-get install -y --no-install-recommends \
8
  curl \
9
  jq \
 
18
  python3-venv \
19
  && rm -rf /var/lib/apt/lists/*
20
 
21
+ # 2. Setup non-root user (UID 1000)
22
  RUN userdel -r ubuntu && useradd -m -u 1000 user
23
 
24
  WORKDIR /app
25
 
26
+ # 3. Setup Python Virtual Environment
 
27
  RUN python3 -m venv /app/venv
28
  ENV PATH="/app/venv/bin:$PATH"
29
+ RUN pip install --no-cache-dir fastapi uvicorn requests pydantic
30
 
31
+ # 4. Download & Install ApexKit Binary
 
 
 
32
  RUN echo "Downloading latest public release archive..." && \
33
  LATEST_URL=$(curl -s https://api.github.com/repos/deniskipeles/apexkit/releases/latest | \
34
  jq -r '.assets[] | select(.name | contains("linux-gnu.tar.gz")) | .browser_download_url') && \
 
39
  chmod +x /usr/local/bin/apexkit && \
40
  rm apexkit.tar.gz
41
 
42
+ # 5. Bake WASM Binaries directly into NVMe Cache (/app/.cache/wasm)
43
+ RUN mkdir -p /app/.cache/wasm && \
44
+ curl -fL -o /app/.cache/wasm/ffmpeg.wasm "https://kipeles-apexkit.hf.space/api/v1/storage/file/333dc27f-4698-45ad-96cc-57e801d1fa22.wasm" && \
45
+ curl -fL -o /app/.cache/wasm/ffprobe.wasm "https://kipeles-apexkit.hf.space/api/v1/storage/file/f3760ac4-f8d9-4a8b-9993-6fe00c256fc6.wasm"
46
+
47
+ # 6. Copy FastAPI Application
48
  COPY --chown=user:user main.py /app/main.py
49
 
50
+ # 7. Setup storage directories
 
 
51
  RUN mkdir -p /app/storage /data/storage && \
52
  chown -R user:user /app /data
53
 
54
+ # 8. Copy and prepare entrypoint script
55
  COPY --chown=user:user entrypoint.sh /app/entrypoint.sh
56
  RUN chmod +x /app/entrypoint.sh
57
 
 
58
  USER user
 
 
59
  ENV PORT=7860
60
 
 
61
  ENTRYPOINT ["/app/entrypoint.sh"]
entrypoint.sh CHANGED
@@ -4,18 +4,22 @@
4
  APP_STORAGE="/app/storage"
5
  PERSISTENT_STORAGE="/data/storage"
6
 
7
- # Set the mounted file storage environment variable for ApexKit
8
  export APEXKIT_MOUNTED_FILE_STORAGE="/data/"
9
 
 
 
 
10
  echo "[Init] Setting up Hugging Face Storage Sync..."
11
- echo "[Init] Set APEXKIT_MOUNTED_FILE_STORAGE to ${APEXKIT_MOUNTED_FILE_STORAGE}"
 
12
 
13
  # 1. Restore from persistent storage on boot (EXCLUDING uploads)
14
  if [ -d "$PERSISTENT_STORAGE" ] && [ "$(ls -A $PERSISTENT_STORAGE 2>/dev/null)" ]; then
15
  echo "[Sync] Restoring data from persistent storage ($PERSISTENT_STORAGE) to NVMe ($APP_STORAGE)..."
16
  mkdir -p "$APP_STORAGE"
17
 
18
- # Exclude uploads so we don't pull large media files down to the ephemeral disk
19
  rsync -a --exclude="**/uploads/" "$PERSISTENT_STORAGE/" "$APP_STORAGE/"
20
  echo "[Sync] Restore complete."
21
  else
@@ -24,42 +28,37 @@ else
24
  mkdir -p "$PERSISTENT_STORAGE"
25
  fi
26
 
27
- # Inject Replica ID if provided via Env Vars (Survives HF Space Restarts)
28
  if [ -n "$APEX_REPLICA_ID" ]; then
29
  mkdir -p "$APP_STORAGE/system"
30
  echo "$APEX_REPLICA_ID" > "$APP_STORAGE/system/.replica_id"
31
  echo "[Init] Injected static Replica ID: $APEX_REPLICA_ID"
32
  fi
33
 
34
- # 2. Background Sync Loop (Every 20 minutes = 1200 seconds)
35
  sync_data() {
36
  while true; do
37
  sleep 1200
38
  echo "[Sync] Backing up NVMe data to persistent storage..."
39
- # --exclude protects the persistent uploads folder from being deleted by --delete
40
  rsync -a --delete --exclude="**/uploads/" "$APP_STORAGE/" "$PERSISTENT_STORAGE/"
41
  echo "[Sync] Backup complete."
42
  done
43
  }
44
 
45
- # Start sync loop in the background
46
  sync_data &
47
  SYNC_PID=$!
48
 
49
- # 3. Graceful Shutdown Handler (Catches Space restarts/pauses)
50
  cleanup() {
51
  echo "[Shutdown] Caught stop signal. Stopping application..."
52
 
53
- # Terminate background services
54
  kill -TERM "$APP_PID" 2>/dev/null
55
  kill -TERM "$FASTAPI_PID" 2>/dev/null
56
 
57
- # Wait for processes to exit
58
  wait "$APP_PID" 2>/dev/null
59
  wait "$FASTAPI_PID" 2>/dev/null
60
 
61
  echo "[Shutdown] Performing final data sync to persistent storage..."
62
- # --exclude protects the persistent uploads folder from being deleted by --delete
63
  rsync -a --delete --exclude="**/uploads/" "$APP_STORAGE/" "$PERSISTENT_STORAGE/"
64
  echo "[Shutdown] Final sync complete. Safely exiting."
65
 
@@ -67,25 +66,21 @@ cleanup() {
67
  exit 0
68
  }
69
 
70
- # Trap termination signals to trigger the cleanup function
71
  trap cleanup SIGINT SIGTERM
72
 
73
- # 4. Start the FastAPI background server on port 8000
74
- # Run on localhost (127.0.0.1) if only ApexKit needs to communicate with it internally,
75
- # or 0.0.0.0 if other containers or processes on the host network need access.
76
  echo "[App] Starting FastAPI on port 8000..."
77
  uvicorn main:app --host 127.0.0.1 --port 8000 --workers 1 &
78
  FASTAPI_PID=$!
79
 
80
- # 5. Start the main ApexKit application on HF exposed port
81
  echo "[App] Starting ApexKit on port ${PORT:-7860}..."
82
  apexkit --port ${PORT:-7860} &
83
  APP_PID=$!
84
 
85
- # Wait for the main application to exit
86
  wait "$APP_PID"
87
 
88
- # If the app crashes or exits naturally, do a final sync before the container dies
89
  echo "[App] Application exited. Syncing data before container stops..."
90
  kill -TERM "$FASTAPI_PID" 2>/dev/null
91
  rsync -a --delete --exclude="**/uploads/" "$APP_STORAGE/" "$PERSISTENT_STORAGE/"
 
4
  APP_STORAGE="/app/storage"
5
  PERSISTENT_STORAGE="/data/storage"
6
 
7
+ # ApexKit persistent file/database storage
8
  export APEXKIT_MOUNTED_FILE_STORAGE="/data/"
9
 
10
+ # Force WASM engine to strictly use the fast local NVMe cache in /app
11
+ export APEXKIT_WASM_CACHE_DIR="/app/.cache/wasm"
12
+
13
  echo "[Init] Setting up Hugging Face Storage Sync..."
14
+ echo "[Init] Persistent File Storage: ${APEXKIT_MOUNTED_FILE_STORAGE}"
15
+ echo "[Init] Fast NVMe WASM Cache: ${APEXKIT_WASM_CACHE_DIR}"
16
 
17
  # 1. Restore from persistent storage on boot (EXCLUDING uploads)
18
  if [ -d "$PERSISTENT_STORAGE" ] && [ "$(ls -A $PERSISTENT_STORAGE 2>/dev/null)" ]; then
19
  echo "[Sync] Restoring data from persistent storage ($PERSISTENT_STORAGE) to NVMe ($APP_STORAGE)..."
20
  mkdir -p "$APP_STORAGE"
21
 
22
+ # Exclude uploads so large media files remain directly on persistent disk
23
  rsync -a --exclude="**/uploads/" "$PERSISTENT_STORAGE/" "$APP_STORAGE/"
24
  echo "[Sync] Restore complete."
25
  else
 
28
  mkdir -p "$PERSISTENT_STORAGE"
29
  fi
30
 
31
+ # Inject Replica ID if provided via Env Vars
32
  if [ -n "$APEX_REPLICA_ID" ]; then
33
  mkdir -p "$APP_STORAGE/system"
34
  echo "$APEX_REPLICA_ID" > "$APP_STORAGE/system/.replica_id"
35
  echo "[Init] Injected static Replica ID: $APEX_REPLICA_ID"
36
  fi
37
 
38
+ # 2. Background Sync Loop (Every 20 minutes)
39
  sync_data() {
40
  while true; do
41
  sleep 1200
42
  echo "[Sync] Backing up NVMe data to persistent storage..."
 
43
  rsync -a --delete --exclude="**/uploads/" "$APP_STORAGE/" "$PERSISTENT_STORAGE/"
44
  echo "[Sync] Backup complete."
45
  done
46
  }
47
 
 
48
  sync_data &
49
  SYNC_PID=$!
50
 
51
+ # 3. Graceful Shutdown Handler
52
  cleanup() {
53
  echo "[Shutdown] Caught stop signal. Stopping application..."
54
 
 
55
  kill -TERM "$APP_PID" 2>/dev/null
56
  kill -TERM "$FASTAPI_PID" 2>/dev/null
57
 
 
58
  wait "$APP_PID" 2>/dev/null
59
  wait "$FASTAPI_PID" 2>/dev/null
60
 
61
  echo "[Shutdown] Performing final data sync to persistent storage..."
 
62
  rsync -a --delete --exclude="**/uploads/" "$APP_STORAGE/" "$PERSISTENT_STORAGE/"
63
  echo "[Shutdown] Final sync complete. Safely exiting."
64
 
 
66
  exit 0
67
  }
68
 
 
69
  trap cleanup SIGINT SIGTERM
70
 
71
+ # 4. Start FastAPI background server on port 8000
 
 
72
  echo "[App] Starting FastAPI on port 8000..."
73
  uvicorn main:app --host 127.0.0.1 --port 8000 --workers 1 &
74
  FASTAPI_PID=$!
75
 
76
+ # 5. Start ApexKit on the exposed port
77
  echo "[App] Starting ApexKit on port ${PORT:-7860}..."
78
  apexkit --port ${PORT:-7860} &
79
  APP_PID=$!
80
 
81
+ # Wait for main process
82
  wait "$APP_PID"
83
 
 
84
  echo "[App] Application exited. Syncing data before container stops..."
85
  kill -TERM "$FASTAPI_PID" 2>/dev/null
86
  rsync -a --delete --exclude="**/uploads/" "$APP_STORAGE/" "$PERSISTENT_STORAGE/"