Spaces:
Sleeping
Sleeping
File size: 4,458 Bytes
6124680 6bf7731 6124680 e46ab0f 6bf7731 6124680 6bf7731 e46ab0f 6124680 6bf7731 e46ab0f 38b5e46 729deb0 e46ab0f 6bf7731 729deb0 6124680 6bf7731 6124680 6bf7731 6124680 729deb0 6bf7731 6124680 729deb0 6124680 729deb0 6124680 729deb0 6124680 e46ab0f 729deb0 e46ab0f 6bf7731 729deb0 e46ab0f 729deb0 e46ab0f 729deb0 e46ab0f 729deb0 e46ab0f 6bf7731 6124680 729deb0 6bf7731 | 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | #!/bin/bash
echo "===== Startup $(date) ====="
umask 077
pip3 install --no-cache-dir -q huggingface_hub==0.23.0 --break-system-packages 2>/dev/null
if ! command -v node &> /dev/null; then
echo "[Install] Node.js..."
curl -fsSL https://deb.nodesource.com/setup_20.x | bash - 2>/dev/null
apt-get install -y nodejs 2>/dev/null
fi
echo "[Install] Claude CLI..."
npm install -g @anthropic-ai/claude-code 2>/dev/null
echo "[Install] Codex CLI..."
npm install -g @openai/codex 2>/dev/null
mkdir -p /persistent/.secrets /persistent/sessions
chmod 700 /persistent/.secrets
# Restore from dataset
if [ -n "$HF_TOKEN" ] && [ -n "$HF_USERNAME" ]; then
echo "[Storage] Restoring from dataset..."
python3 -c "
import os
from huggingface_hub import snapshot_download
token = os.environ.get('HF_TOKEN')
username = os.environ.get('HF_USERNAME')
repo_id = f'{username}/vps-storage'
try:
snapshot_download(repo_id=repo_id, repo_type='dataset', local_dir='/persistent', token=token, ignore_patterns=['.gitattributes','README.md','.git/*'])
print('[Storage] Restored!')
except: print('[Storage] Fresh start')
" 2>/dev/null
fi
# Reinstall packages
if [ -s /persistent/installed_packages.txt ]; then
echo "[Packages] Reinstalling tracked packages..."
while IFS= read -r pkg; do
[ -z "$pkg" ] && continue
if [[ "$pkg" == apt:* ]]; then
apt_pkg="${pkg#apt:}"
apt-get install -y -qq "$apt_pkg" 2>/dev/null
else
pip3 install -q "$pkg" --break-system-packages 2>/dev/null
fi
done < /persistent/installed_packages.txt
echo "[Packages] Done!"
fi
# Auto-save every 3 minutes
python3 -c "
import os, time, threading
def auto_save():
while True:
time.sleep(180)
token = os.environ.get('HF_TOKEN')
username = os.environ.get('HF_USERNAME')
if token and username:
try:
from huggingface_hub import HfApi
api = HfApi(token=token)
api.upload_folder(
folder_path='/persistent',
repo_id=f'{username}/vps-storage',
repo_type='dataset',
token=token,
commit_message='auto-save',
ignore_patterns=['.secrets/', '*.key', '*.pem', '.env', '.git/*']
)
except: pass
threading.Thread(target=auto_save, daemon=True).start()
" &
# Package tracker
python3 -c "
import os, time, json, threading
def watch_pkgs():
seen = set()
if os.path.exists('/persistent/installed_packages.txt'):
with open('/persistent/installed_packages.txt') as f:
seen = {l.strip() for l in f if l.strip()}
while True:
time.sleep(15)
try:
result = os.popen('pip3 list --format=json 2>/dev/null').read()
if result:
pkgs = json.loads(result)
for pkg in pkgs:
name = pkg.get('name','')
if name and name not in seen:
with open('/persistent/installed_packages.txt', 'a') as f:
f.write(name + '\n')
seen.add(name)
except: pass
threading.Thread(target=watch_pkgs, daemon=True).start()
" &
# Keep-alive ping
python3 -c "
import os, time, random, threading, requests
def ping():
while True:
try:
host = os.environ.get('SPACE_HOST','')
if host:
requests.get(f'https://{host}/', headers={'User-Agent': 'Mozilla/5.0'}, timeout=10)
except: pass
time.sleep(random.randint(600, 1800))
threading.Thread(target=ping, daemon=True).start()
" &
# Bashrc for persistent env
cat > /root/.bashrc << 'EOF'
export HISTFILE=/persistent/.bash_history
export HISTSIZE=10000
export HISTFILESIZE=20000
export HISTCONTROL=ignoredups:erasedups
shopt -s histappend
alias pip='pip3'
alias install='track_pkg'
track_pkg() {
pip3 install "$@"
pkg_name=$(echo "$@" | awk '{print $1}' | sed 's/[^a-zA-Z0-9_-]//g')
if [ -n "$pkg_name" ] && ! grep -q "^${pkg_name}$" /persistent/installed_packages.txt 2>/dev/null; then
echo "$pkg_name" >> /persistent/installed_packages.txt
echo "[Tracking] Added: $pkg_name"
fi
}
EOF
cat > /root/.profile << 'EOF'
if [ -f /root/.bashrc ]; then . /root/.bashrc; fi
EOF
echo "[Startup] Ready!"
exec uvicorn app:app --host 0.0.0.0 --port 7860 --ws websockets --proxy-headers --forwarded-allow-ips="*"
|