Spaces:
Paused
Paused
| # Ensure we're in the right directory and set up paths | |
| SCRIPT_DIR=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")") | |
| cd "$SCRIPT_DIR" || exit 1 | |
| export DATA_DIR="/app/backend/data" | |
| export PYTHONPATH="/app/backend:${PYTHONPATH}" | |
| # Validate required environment variables | |
| for var in "BACKUP_PASSPHRASE" "HF_TOKEN" "SPACE_ID"; do | |
| if [ -z "${!var}" ]; then | |
| echo "Error: $var is not set. Required for database backup/restore." | |
| exit 1 | |
| fi | |
| done | |
| # Restore database from backup | |
| echo "Restoring database from backup..." | |
| python "$SCRIPT_DIR/scripts/restore.py" | |
| restore_status=$? | |
| if [ $restore_status -ne 0 ]; then | |
| echo "Warning: Database restore failed. Starting with empty database." | |
| fi | |
| # Handle WebUI secret key | |
| KEY_FILE="$SCRIPT_DIR/.webui_secret_key" | |
| PORT="${PORT:-8080}" | |
| HOST="${HOST:-0.0.0.0}" | |
| if test "$WEBUI_SECRET_KEY $WEBUI_JWT_SECRET_KEY" = " "; then | |
| if ! [ -e "$KEY_FILE" ]; then | |
| head -c 12 /dev/random | base64 > "$KEY_FILE" || exit 1 | |
| fi | |
| WEBUI_SECRET_KEY=$(cat "$KEY_FILE") || exit 1 | |
| fi | |
| # Start Ollama if enabled | |
| if [[ "${USE_OLLAMA_DOCKER,,}" == "true" ]]; then | |
| ollama serve & | |
| fi | |
| # Configure CUDA if enabled | |
| if [[ "${USE_CUDA_DOCKER,,}" == "true" ]]; then | |
| export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/lib/python3.11/site-packages/torch/lib:/usr/local/lib/python3.11/site-packages/nvidia/cudnn/lib" | |
| fi | |
| # Handle HuggingFace Space deployment | |
| if [ -n "$SPACE_ID" ]; then | |
| echo "Configuring HuggingFace Space deployment" | |
| if [ -n "$ADMIN_USER_EMAIL" ] && [ -n "$ADMIN_USER_PASSWORD" ]; then | |
| WEBUI_SECRET_KEY="$WEBUI_SECRET_KEY" uvicorn open_webui.main:app --host "$HOST" --port "$PORT" --forwarded-allow-ips '*' & | |
| webui_pid=$! | |
| while ! curl -s http://localhost:8080/health > /dev/null; do | |
| sleep 1 | |
| done | |
| curl \ | |
| -X POST "http://localhost:8080/api/v1/auths/signup" \ | |
| -H "accept: application/json" \ | |
| -H "Content-Type: application/json" \ | |
| -d "{ \"email\": \"${ADMIN_USER_EMAIL}\", \"password\": \"${ADMIN_USER_PASSWORD}\", \"name\": \"Admin\" }" | |
| kill $webui_pid | |
| fi | |
| export WEBUI_URL=${SPACE_HOST} | |
| fi | |
| # Start the main web server | |
| WEBUI_SECRET_KEY="$WEBUI_SECRET_KEY" uvicorn open_webui.main:app \ | |
| --host "$HOST" --port "$PORT" --forwarded-allow-ips '*' & | |
| WEBUI_PID=$! | |
| # Configure and start backup scheduler | |
| BACKUP_INITIAL_WAIT="${BACKUP_INITIAL_WAIT:-100}" | |
| BACKUP_INTERVAL="${BACKUP_INTERVAL:-300}" | |
| ( | |
| echo "Starting backup scheduler (Initial wait: ${BACKUP_INITIAL_WAIT}s, Interval: ${BACKUP_INTERVAL}s)" | |
| sleep "$BACKUP_INITIAL_WAIT" | |
| while true; do | |
| echo "Running scheduled backup" | |
| BACKUP_THRESHOLD_MINUTES="${BACKUP_THRESHOLD_MINUTES:-120}" \ | |
| python "$SCRIPT_DIR/scripts/backup.py" | |
| if [ $? -ne 0 ]; then | |
| echo "Warning: Backup failed" | |
| fi | |
| sleep "$BACKUP_INTERVAL" | |
| done | |
| ) & | |
| # Wait for the main process | |
| wait $WEBUI_PID | |