Upload scripts/start_space.sh with huggingface_hub
Browse files- scripts/start_space.sh +71 -0
scripts/start_space.sh
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env bash
|
| 2 |
+
set -euo pipefail
|
| 3 |
+
|
| 4 |
+
MODEL_ID="${LLAMA_CPP_MODEL:-CohereLabs/tiny-aya-global-GGUF}"
|
| 5 |
+
MODEL_FILE="${LLAMA_CPP_FILE:-*Q4_K_M.gguf}"
|
| 6 |
+
LLAMA_PORT="${LLAMA_CPP_PORT:-8080}"
|
| 7 |
+
APP_PORT="${PORT:-${GRADIO_SERVER_PORT:-7860}}"
|
| 8 |
+
|
| 9 |
+
export GRADIO_SERVER_NAME="${GRADIO_SERVER_NAME:-0.0.0.0}"
|
| 10 |
+
export GRADIO_SERVER_PORT="$APP_PORT"
|
| 11 |
+
export LLAMA_CPP_PORT="$LLAMA_PORT"
|
| 12 |
+
|
| 13 |
+
# ββ Install llama.cpp βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 14 |
+
echo "Installing llama.cpp..."
|
| 15 |
+
pip install llama-cpp-python[server] 2>/dev/null || true
|
| 16 |
+
|
| 17 |
+
# Try to get llama-server binary
|
| 18 |
+
if ! command -v llama-server &>/dev/null; then
|
| 19 |
+
echo "llama-server binary not found, trying to install..."
|
| 20 |
+
pip install llama-cpp-python 2>/dev/null || true
|
| 21 |
+
# Fallback: download pre-built binary
|
| 22 |
+
curl -sL https://github.com/ggml-org/llama.cpp/releases/latest/download/llama-server-linux-x64 \
|
| 23 |
+
-o /usr/local/bin/llama-server 2>/dev/null || true
|
| 24 |
+
chmod +x /usr/local/bin/llama-server 2>/dev/null || true
|
| 25 |
+
fi
|
| 26 |
+
|
| 27 |
+
# ββ Start llama.cpp server ββββββββββββββββββββββββββββββββββββββββββ
|
| 28 |
+
echo "Starting llama.cpp server for ${MODEL_ID} (file: ${MODEL_FILE}) on port ${LLAMA_PORT}"
|
| 29 |
+
|
| 30 |
+
llama-server \
|
| 31 |
+
-hf "${MODEL_ID}:${MODEL_FILE}" \
|
| 32 |
+
--port "$LLAMA_PORT" \
|
| 33 |
+
-ngl 99 \
|
| 34 |
+
--ctx-size 4096 \
|
| 35 |
+
--no-warmup \
|
| 36 |
+
&
|
| 37 |
+
|
| 38 |
+
LLAMA_PID=$!
|
| 39 |
+
|
| 40 |
+
# ββ Cleanup on exit βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 41 |
+
cleanup() {
|
| 42 |
+
echo "Shutting down..."
|
| 43 |
+
kill "$LLAMA_PID" 2>/dev/null || true
|
| 44 |
+
}
|
| 45 |
+
trap cleanup EXIT
|
| 46 |
+
|
| 47 |
+
# ββ Wait for llama.cpp to be ready ββββββββββββββββββββββββββββββββββ
|
| 48 |
+
echo "Waiting for llama.cpp server to become ready..."
|
| 49 |
+
for attempt in $(seq 1 120); do
|
| 50 |
+
if curl -sf "http://localhost:${LLAMA_PORT}/health" >/dev/null 2>&1; then
|
| 51 |
+
echo "llama.cpp server is ready (attempt ${attempt})."
|
| 52 |
+
break
|
| 53 |
+
fi
|
| 54 |
+
|
| 55 |
+
if ! kill -0 "$LLAMA_PID" 2>/dev/null; then
|
| 56 |
+
echo "llama.cpp server exited prematurely."
|
| 57 |
+
wait "$LLAMA_PID" 2>/dev/null || true
|
| 58 |
+
break
|
| 59 |
+
fi
|
| 60 |
+
|
| 61 |
+
if [ "$attempt" -eq 120 ]; then
|
| 62 |
+
echo "Timed out waiting for llama.cpp server. Starting app anyway (fallback mode)."
|
| 63 |
+
break
|
| 64 |
+
fi
|
| 65 |
+
|
| 66 |
+
sleep 2
|
| 67 |
+
done
|
| 68 |
+
|
| 69 |
+
# ββ Start Gradio app ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 70 |
+
echo "Starting Gradio app on ${GRADIO_SERVER_NAME}:${APP_PORT}"
|
| 71 |
+
exec /opt/neighbourhood-guide/bin/python /app/app.py
|