File size: 2,899 Bytes
7482820
 
 
 
4da2f34
 
7482820
 
e2f927d
7482820
4da2f34
 
 
 
 
7482820
 
 
4da2f34
 
7482820
 
 
 
4da2f34
 
 
 
 
 
 
 
7482820
 
 
4da2f34
7482820
 
 
4da2f34
 
 
 
 
7482820
 
 
 
 
 
4da2f34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7482820
 
 
 
4da2f34
 
 
 
 
7482820
 
 
 
 
 
 
 
 
 
 
4da2f34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env bash
set -euo pipefail

APP_ROOT="/app"
RUNTIME_ROOT="${HF_RUNTIME_ROOT:-/tmp/codex-console}"
PERSIST_ROOT=""

effective_host="${APP_HOST:-${WEBUI_HOST:-0.0.0.0}}"
effective_port="${PORT:-${APP_PORT:-${WEBUI_PORT:-7860}}}"
effective_password="${APP_ACCESS_PASSWORD:-${WEBUI_ACCESS_PASSWORD:-${SPACE_PASSWORD:-}}}"
configured_database_url="${APP_DATABASE_URL:-${DATABASE_URL:-}}"

if [[ -z "${configured_database_url}" && -d "/data" ]]; then
  PERSIST_ROOT="/data"
fi

if [[ -n "${APP_DATA_DIR:-}" ]]; then
  data_dir="${APP_DATA_DIR}"
elif [[ -n "${PERSIST_ROOT}" ]]; then
  data_dir="${RUNTIME_ROOT}/data"
else
  data_dir="${APP_ROOT}/data"
fi

if [[ -n "${APP_LOGS_DIR:-}" ]]; then
  logs_dir="${APP_LOGS_DIR}"
elif [[ -n "${PERSIST_ROOT}" ]]; then
  logs_dir="${RUNTIME_ROOT}/logs"
else
  logs_dir="${data_dir}/logs"
fi

export APP_HOST="${effective_host}"
export APP_PORT="${effective_port}"
export APP_DATA_DIR="${data_dir}"
export APP_LOGS_DIR="${logs_dir}"

mkdir -p "${APP_DATA_DIR}" "${APP_LOGS_DIR}"

local_db_path="${APP_DATA_DIR}/database.db"
backup_pid=""

if [[ -z "${configured_database_url}" ]]; then
  export APP_DATABASE_URL="sqlite:///${local_db_path}"
fi

if [[ -n "${effective_password}" ]]; then
  export APP_ACCESS_PASSWORD="${effective_password}"
fi

if [[ -n "${PERSIST_ROOT}" ]]; then
  persist_db_path="${PERSIST_ROOT}/database.db"
  backup_interval="${HF_BACKUP_INTERVAL_SECONDS:-60}"

  python /app/deploy/huggingface/sqlite_backup.py restore \
    --source "${persist_db_path}" \
    --destination "${local_db_path}" || true

  backup_loop() {
    while true; do
      sleep "${backup_interval}"
      python /app/deploy/huggingface/sqlite_backup.py backup \
        --source "${local_db_path}" \
        --destination "${persist_db_path}" || true
    done
  }

  backup_loop &
  backup_pid="$!"
fi

echo "[hf-space] starting codex-console"
echo "[hf-space] listen: ${APP_HOST}:${APP_PORT}"
echo "[hf-space] data dir: ${APP_DATA_DIR}"
echo "[hf-space] logs dir: ${APP_LOGS_DIR}"
if [[ -n "${PERSIST_ROOT}" ]]; then
  echo "[hf-space] persistence: sqlite runtime at ${local_db_path}, bucket snapshot at ${persist_db_path}"
else
  echo "[hf-space] persistence: direct filesystem"
fi
if [[ -n "${APP_ACCESS_PASSWORD:-}" ]]; then
  echo "[hf-space] access password: configured"
else
  echo "[hf-space] access password: not set"
fi

cmd=(python webui.py --host "${APP_HOST}" --port "${APP_PORT}")
if [[ -n "${APP_ACCESS_PASSWORD:-}" ]]; then
  cmd+=(--access-password "${APP_ACCESS_PASSWORD}")
fi

"${cmd[@]}" &
app_pid="$!"
set +e
wait "${app_pid}"
app_status="$?"
set -e

if [[ -n "${backup_pid}" ]]; then
  python /app/deploy/huggingface/sqlite_backup.py backup \
    --source "${local_db_path}" \
    --destination "${persist_db_path}" || true
  kill "${backup_pid}" 2>/dev/null || true
  wait "${backup_pid}" 2>/dev/null || true
fi

exit "${app_status}"