ChristophSchuhmann commited on
Commit
309eb57
·
verified ·
1 Parent(s): 55d543d

Upload start-backend.sh with huggingface_hub

Browse files
Files changed (1) hide show
  1. start-backend.sh +249 -0
start-backend.sh ADDED
@@ -0,0 +1,249 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ # ============================================================================
3
+ # LAION-Tunes Backend Startup Script
4
+ # ============================================================================
5
+ # Starts the FastAPI search server with EmbeddingGemma 300M loaded directly
6
+ # in Python (CPU), plus a cloudflared tunnel for public HTTPS access.
7
+ #
8
+ # Usage:
9
+ # ./start-backend.sh # default: port 7860
10
+ # ./start-backend.sh --port 7861 # custom FastAPI port
11
+ # ./start-backend.sh --skip-download # skip LFS data download
12
+ #
13
+ # Prerequisites:
14
+ # - Python 3.10+ with pip
15
+ # - cloudflared binary at ../cloudflared
16
+ # - ~20 GB disk for search indices
17
+ # - ~20 GB RAM for loaded indices + embedder
18
+ # ============================================================================
19
+
20
+ set -euo pipefail
21
+
22
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
23
+ cd "$SCRIPT_DIR"
24
+
25
+ # ── Configuration ────────────────────────────────────────────────────────────
26
+ FASTAPI_PORT="${FASTAPI_PORT:-7860}"
27
+ VENV_DIR="$SCRIPT_DIR/.venv"
28
+ CLOUDFLARED="$SCRIPT_DIR/../cloudflared"
29
+ LOG_DIR="$SCRIPT_DIR/logs"
30
+ SKIP_DOWNLOAD=false
31
+ NO_WHISPER=false # Whisper encoder works on CPU too
32
+
33
+ # Parse args
34
+ while [[ $# -gt 0 ]]; do
35
+ case "$1" in
36
+ --port) FASTAPI_PORT="$2"; shift 2 ;;
37
+ --skip-download) SKIP_DOWNLOAD=true; shift ;;
38
+ --with-whisper) NO_WHISPER=false; shift ;;
39
+ *) echo "Unknown arg: $1"; exit 1 ;;
40
+ esac
41
+ done
42
+
43
+ mkdir -p "$LOG_DIR"
44
+
45
+ echo "============================================================"
46
+ echo " LAION-Tunes Backend Startup"
47
+ echo "============================================================"
48
+ echo " FastAPI port: $FASTAPI_PORT"
49
+ echo " Venv: $VENV_DIR"
50
+ echo " Skip download: $SKIP_DOWNLOAD"
51
+ echo " No whisper: $NO_WHISPER"
52
+ echo "============================================================"
53
+
54
+ # ── Step 1: Download LFS data (search indices) if needed ─────────────────────
55
+ if [ "$SKIP_DOWNLOAD" = false ]; then
56
+ echo ""
57
+ echo "[1/4] Checking search index data..."
58
+ mkdir -p "$SCRIPT_DIR/search_index"
59
+
60
+ # Check if metadata.db exists and is a real file (not LFS pointer)
61
+ if [ -f "$SCRIPT_DIR/search_index/metadata.db" ] && \
62
+ [ "$(stat -c%s "$SCRIPT_DIR/search_index/metadata.db" 2>/dev/null || echo 0)" -gt 1000 ]; then
63
+ echo " Search indices already downloaded."
64
+ else
65
+ echo " Downloading search indices from HuggingFace (this may take a while ~16 GB)..."
66
+
67
+ if [ -d "$SCRIPT_DIR/repo-source/.git" ]; then
68
+ echo " Using git-lfs pull in cloned repo..."
69
+ cd "$SCRIPT_DIR/repo-source"
70
+ git lfs pull --include="search_index/*" 2>&1 | tail -5
71
+ # Copy real files over
72
+ cp -f search_index/* "$SCRIPT_DIR/search_index/" 2>/dev/null || true
73
+ cd "$SCRIPT_DIR"
74
+ elif command -v huggingface-cli &>/dev/null; then
75
+ echo " Using huggingface-cli..."
76
+ huggingface-cli download laion/laion-tunes \
77
+ --repo-type dataset \
78
+ --include "search_index/*" \
79
+ --local-dir "$SCRIPT_DIR" \
80
+ 2>&1 | tail -5
81
+ else
82
+ echo " ERROR: No method to download LFS data."
83
+ echo " Either clone the repo first:"
84
+ echo " GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/datasets/laion/laion-tunes repo-source"
85
+ echo " Or install huggingface-cli:"
86
+ echo " pip install huggingface-hub"
87
+ exit 1
88
+ fi
89
+ echo " Download complete."
90
+ fi
91
+ else
92
+ echo ""
93
+ echo "[1/4] Skipping data download (--skip-download)"
94
+ fi
95
+
96
+ # ── Step 2: Set up Python virtual environment ────────────────────────────────
97
+ echo ""
98
+ echo "[2/4] Setting up Python environment..."
99
+
100
+ if [ ! -d "$VENV_DIR" ]; then
101
+ echo " Creating virtual environment..."
102
+ python3 -m venv "$VENV_DIR"
103
+ fi
104
+
105
+ source "$VENV_DIR/bin/activate"
106
+
107
+ # Install dependencies
108
+ echo " Installing Python dependencies..."
109
+ pip install --quiet --upgrade pip
110
+
111
+ # Set HF token for gated model access (google/embeddinggemma-300m is gated)
112
+ if [ -z "${HF_TOKEN:-}" ]; then
113
+ if [ -f "$HOME/.cache/huggingface/token" ]; then
114
+ export HF_TOKEN="$(cat "$HOME/.cache/huggingface/token")"
115
+ echo " HF token loaded from ~/.cache/huggingface/token"
116
+ elif [ -f "$HOME/.huggingface/token" ]; then
117
+ export HF_TOKEN="$(cat "$HOME/.huggingface/token")"
118
+ echo " HF token loaded from ~/.huggingface/token"
119
+ else
120
+ echo " WARNING: No HF_TOKEN set. google/embeddinggemma-300m is a gated model."
121
+ echo " Set HF_TOKEN env var or run: huggingface-cli login"
122
+ fi
123
+ fi
124
+
125
+ # Install CPU-only PyTorch first (smaller, no CUDA)
126
+ pip install --quiet torch --index-url https://download.pytorch.org/whl/cpu
127
+
128
+ # Install everything else
129
+ pip install --quiet \
130
+ fastapi \
131
+ 'uvicorn[standard]' \
132
+ faiss-cpu \
133
+ numpy \
134
+ pandas \
135
+ scipy \
136
+ tqdm \
137
+ requests \
138
+ pydantic \
139
+ python-multipart \
140
+ sentence-transformers \
141
+ transformers \
142
+ optimum[onnxruntime] \
143
+ onnxruntime \
144
+ librosa \
145
+ soundfile
146
+
147
+ echo " Python environment ready."
148
+
149
+ # ── Step 3: Start FastAPI server (with direct Python EmbeddingGemma) ─────────
150
+ echo ""
151
+ echo "[3/4] Starting FastAPI search server on port $FASTAPI_PORT..."
152
+ echo " EmbeddingGemma 300M will be loaded directly in Python (CPU)."
153
+ echo " First query may take ~500ms for embedding; subsequent ones ~430ms."
154
+
155
+ WHISPER_FLAG=""
156
+ if [ "$NO_WHISPER" = true ]; then
157
+ WHISPER_FLAG="--no-whisper"
158
+ fi
159
+
160
+ # Kill any existing server on this port
161
+ fuser -k "${FASTAPI_PORT}/tcp" 2>/dev/null || true
162
+ sleep 1
163
+
164
+ # Run WITHOUT --tei-url so server.py loads EmbeddingGemma via SentenceTransformer
165
+ # HF_TOKEN is inherited from the environment for gated model access
166
+ nohup env HF_TOKEN="${HF_TOKEN:-}" "$VENV_DIR/bin/python" server.py \
167
+ --port "$FASTAPI_PORT" \
168
+ --host 0.0.0.0 \
169
+ --gpu 0 \
170
+ $WHISPER_FLAG \
171
+ > "$LOG_DIR/fastapi.log" 2>&1 &
172
+
173
+ FASTAPI_PID=$!
174
+ echo " FastAPI PID: $FASTAPI_PID"
175
+ echo "$FASTAPI_PID" > "$LOG_DIR/fastapi.pid"
176
+
177
+ # Wait for FastAPI to be ready
178
+ echo " Waiting for FastAPI to start (loading indices ~20 GB RAM + downloading EmbeddingGemma)..."
179
+ FASTAPI_READY=false
180
+ for i in $(seq 1 600); do
181
+ if curl -sf "http://localhost:${FASTAPI_PORT}/api/stats" >/dev/null 2>&1; then
182
+ FASTAPI_READY=true
183
+ echo " FastAPI ready after ${i}s"
184
+ break
185
+ fi
186
+ # Check if process is still alive
187
+ if ! kill -0 "$FASTAPI_PID" 2>/dev/null; then
188
+ echo " ERROR: FastAPI process died. Check logs:"
189
+ tail -50 "$LOG_DIR/fastapi.log"
190
+ exit 1
191
+ fi
192
+ # Show progress every 30s
193
+ if (( i % 30 == 0 )); then
194
+ echo " Still loading... (${i}s elapsed)"
195
+ tail -1 "$LOG_DIR/fastapi.log" 2>/dev/null | sed 's/^/ /'
196
+ fi
197
+ sleep 1
198
+ done
199
+
200
+ if [ "$FASTAPI_READY" = false ]; then
201
+ echo " WARNING: FastAPI not ready after 600s. Check: tail -f $LOG_DIR/fastapi.log"
202
+ fi
203
+
204
+ # ── Step 4: Start cloudflared tunnel for public HTTPS ────────────────────────
205
+ echo ""
206
+ echo "[4/4] Starting cloudflared tunnel for public HTTPS..."
207
+
208
+ # Kill existing tunnel if running
209
+ if [ -f "$LOG_DIR/cloudflared-backend.pid" ]; then
210
+ kill "$(cat "$LOG_DIR/cloudflared-backend.pid")" 2>/dev/null || true
211
+ fi
212
+
213
+ nohup "$CLOUDFLARED" tunnel --url "http://localhost:${FASTAPI_PORT}" \
214
+ > "$LOG_DIR/cloudflared-backend.log" 2>&1 &
215
+
216
+ CF_PID=$!
217
+ echo "$CF_PID" > "$LOG_DIR/cloudflared-backend.pid"
218
+
219
+ # Wait for tunnel URL
220
+ sleep 5
221
+ TUNNEL_URL=""
222
+ for i in $(seq 1 30); do
223
+ TUNNEL_URL=$(grep -oP 'https://[a-z0-9-]+\.trycloudflare\.com' "$LOG_DIR/cloudflared-backend.log" 2>/dev/null | head -1)
224
+ if [ -n "$TUNNEL_URL" ]; then
225
+ break
226
+ fi
227
+ sleep 1
228
+ done
229
+
230
+ echo ""
231
+ echo "============================================================"
232
+ echo " LAION-Tunes Backend is running!"
233
+ echo "============================================================"
234
+ echo ""
235
+ echo " Local: http://localhost:${FASTAPI_PORT}"
236
+ if [ -n "$TUNNEL_URL" ]; then
237
+ echo " Public: $TUNNEL_URL"
238
+ else
239
+ echo " Public: (tunnel URL pending, check: grep trycloudflare $LOG_DIR/cloudflared-backend.log)"
240
+ fi
241
+ echo ""
242
+ echo " Logs:"
243
+ echo " FastAPI: tail -f $LOG_DIR/fastapi.log"
244
+ echo " Cloudflared: tail -f $LOG_DIR/cloudflared-backend.log"
245
+ echo ""
246
+ echo " Stop everything:"
247
+ echo " kill $FASTAPI_PID $CF_PID"
248
+ echo " # or: ./stop-all.sh"
249
+ echo "============================================================"