start.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env bash
  2. SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
  3. cd "$SCRIPT_DIR" || exit
  4. KEY_FILE=.webui_secret_key
  5. PORT="${PORT:-8080}"
  6. HOST="${HOST:-0.0.0.0}"
  7. if test "$WEBUI_SECRET_KEY $WEBUI_JWT_SECRET_KEY" = " "; then
  8. echo "Loading WEBUI_SECRET_KEY from file, not provided as an environment variable."
  9. if ! [ -e "$KEY_FILE" ]; then
  10. echo "Generating WEBUI_SECRET_KEY"
  11. # Generate a random value to use as a WEBUI_SECRET_KEY in case the user didn't provide one.
  12. echo $(head -c 12 /dev/random | base64) > "$KEY_FILE"
  13. fi
  14. echo "Loading WEBUI_SECRET_KEY from $KEY_FILE"
  15. WEBUI_SECRET_KEY=$(cat "$KEY_FILE")
  16. fi
  17. if [ "$USE_OLLAMA_DOCKER" = "true" ]; then
  18. echo "USE_OLLAMA is set to true, starting ollama serve."
  19. ollama serve &
  20. fi
  21. if [ "$USE_CUDA_DOCKER" = "true" ]; then
  22. echo "CUDA is enabled, appending LD_LIBRARY_PATH to include torch/cudnn & cublas libraries."
  23. 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"
  24. fi
  25. # Check if SPACE_ID is set, if so, configure for space
  26. if [ -n "$SPACE_ID" ]; then
  27. echo "Configuring for HuggingFace Space deployment"
  28. # Copy litellm_config.yaml with specified ownership
  29. echo "Copying litellm_config.yaml to the desired location with specified ownership..."
  30. cp -f ./space/litellm_config.yaml ./data/litellm/config.yaml
  31. if [ -n "$ADMIN_USER_EMAIL" ] && [ -n "$ADMIN_USER_PASSWORD" ]; then
  32. echo "Admin user configured, creating"
  33. WEBUI_SECRET_KEY="$WEBUI_SECRET_KEY" uvicorn main:app --host "$HOST" --port "$PORT" --forwarded-allow-ips '*' &
  34. webui_pid=$!
  35. echo "Waiting for webui to start..."
  36. while ! curl -s http://localhost:8080/health > /dev/null; do
  37. sleep 1
  38. done
  39. echo "Creating admin user..."
  40. curl \
  41. -X POST "http://localhost:8080/api/v1/auths/signup" \
  42. -H "accept: application/json" \
  43. -H "Content-Type: application/json" \
  44. -d "{ \"email\": \"${ADMIN_USER_EMAIL}\", \"password\": \"${ADMIN_USER_PASSWORD}\", \"name\": \"Admin\" }"
  45. echo "Shutting down webui..."
  46. kill $webui_pid
  47. fi
  48. export WEBUI_URL=${SPACE_HOST}
  49. fi
  50. WEBUI_SECRET_KEY="$WEBUI_SECRET_KEY" exec uvicorn main:app --host "$HOST" --port "$PORT" --forwarded-allow-ips '*'