#!/bin/bash # NullAI Phi-4 14B v2 - Complete One-Command Setup Script # Downloads model, installs backend, frontend, and starts the application set -e echo "🌳 NullAI Phi-4 14B v2 - Complete Setup" echo "========================================" echo "" # Color codes RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # Configuration INSTALL_DIR="${HOME}/nullai" MODEL_REPO="kofdai/nullai-phi-4-14b-v2" REPO_URL="https://huggingface.co/${MODEL_REPO}" print_info() { echo -e "${GREEN}✓ $1${NC}"; } print_warning() { echo -e "${YELLOW}⚠ $1${NC}"; } print_error() { echo -e "${RED}✗ $1${NC}"; exit 1; } print_step() { echo -e "${BLUE}▶ $1${NC}"; } # ============================================ # STEP 1: System Requirements Check # ============================================ print_step "Step 1/8: Checking system requirements..." # Check RAM if [[ "$OSTYPE" == "darwin"* ]]; then TOTAL_RAM=$(sysctl -n hw.memsize | awk '{print int($1/1024/1024/1024)}') print_info "macOS detected with ${TOTAL_RAM}GB RAM" if [ "$TOTAL_RAM" -lt 32 ]; then print_warning "32GB+ RAM recommended. You have ${TOTAL_RAM}GB" fi elif [[ "$OSTYPE" == "linux-gnu"* ]]; then TOTAL_RAM=$(free -g | awk '/^Mem:/{print $2}') print_info "Linux detected with ${TOTAL_RAM}GB RAM" if [ "$TOTAL_RAM" -lt 32 ]; then print_warning "32GB+ RAM recommended. You have ${TOTAL_RAM}GB" fi fi # Check disk space (need ~35GB) AVAILABLE_SPACE=$(df -h "$HOME" | awk 'NR==2 {print $4}' | sed 's/G.*//') if [ "${AVAILABLE_SPACE%.*}" -lt 40 ]; then print_error "Need 40GB+ free space. Available: ${AVAILABLE_SPACE}GB" fi print_info "Disk space: ${AVAILABLE_SPACE}GB available" # Check Python 3.9+ if ! command -v python3 &> /dev/null; then print_error "Python 3.9+ required. Please install Python first." fi PYTHON_VERSION=$(python3 --version | awk '{print $2}' | cut -d. -f1,2) print_info "Python ${PYTHON_VERSION} found" # Check Node.js if ! command -v node &> /dev/null; then print_warning "Node.js not found. Frontend will not be installed." print_warning "Install Node.js 18+ to use the web interface." INSTALL_FRONTEND=false else NODE_VERSION=$(node --version) print_info "Node.js ${NODE_VERSION} found" INSTALL_FRONTEND=true fi # Check/Install git-lfs if ! command -v git-lfs &> /dev/null; then print_warning "Installing git-lfs..." if [[ "$OSTYPE" == "darwin"* ]]; then brew install git-lfs || print_error "Failed to install git-lfs" elif [[ "$OSTYPE" == "linux-gnu"* ]]; then if command -v apt-get &> /dev/null; then sudo apt-get update && sudo apt-get install -y git-lfs else print_error "Please install git-lfs manually" fi fi fi git lfs install print_info "git-lfs configured" echo "" # ============================================ # STEP 2: Download Repository # ============================================ print_step "Step 2/8: Downloading NullAI repository..." mkdir -p "$INSTALL_DIR" cd "$INSTALL_DIR" if [ -d "nullai-phi-4-14b-v2" ]; then print_info "Repository exists. Updating..." cd nullai-phi-4-14b-v2 git pull else print_info "Cloning repository (27GB model included)..." print_warning "This may take 10-30 minutes depending on your connection" git clone "$REPO_URL" nullai-phi-4-14b-v2 cd nullai-phi-4-14b-v2 fi print_info "Repository ready" echo "" # ============================================ # STEP 3: Setup Environment Variables # ============================================ print_step "Step 3/8: Configuring environment..." if [ ! -f .env ]; then cp .env.example .env # Generate random JWT secret JWT_SECRET=$(openssl rand -hex 32 2>/dev/null || head -c 32 /dev/urandom | base64) if [[ "$OSTYPE" == "darwin"* ]]; then sed -i '' "s/your-secret-key-change-this-in-production/$JWT_SECRET/" .env sed -i '' "s|PHI4_MODEL_PATH=./phi-4-f16.gguf|PHI4_MODEL_PATH=$INSTALL_DIR/nullai-phi-4-14b-v2/phi-4-f16.gguf|" .env else sed -i "s/your-secret-key-change-this-in-production/$JWT_SECRET/" .env sed -i "s|PHI4_MODEL_PATH=./phi-4-f16.gguf|PHI4_MODEL_PATH=$INSTALL_DIR/nullai-phi-4-14b-v2/phi-4-f16.gguf|" .env fi print_info "Environment configured with secure JWT secret" else print_info "Using existing .env file" fi echo "" # ============================================ # STEP 4: Backend Setup # ============================================ print_step "Step 4/8: Setting up Python backend..." cd backend # Create virtual environment if [ ! -d "venv" ]; then python3 -m venv venv print_info "Virtual environment created" fi # Activate virtual environment source venv/bin/activate # Install dependencies print_info "Installing Python dependencies..." pip install --upgrade pip -q pip install -r requirements.txt -q # Install llama-cpp-python with platform optimization print_info "Installing llama-cpp-python with platform optimization..." if [[ "$OSTYPE" == "darwin"* ]]; then CMAKE_ARGS="-DLLAMA_METAL=on" pip install --upgrade --force-reinstall llama-cpp-python --no-cache-dir -q print_info "Installed with Metal GPU acceleration (Apple Silicon)" elif command -v nvidia-smi &> /dev/null; then CMAKE_ARGS="-DLLAMA_CUBLAS=on" pip install --upgrade --force-reinstall llama-cpp-python --no-cache-dir -q print_info "Installed with CUDA GPU acceleration" else pip install --upgrade llama-cpp-python -q print_warning "CPU-only installation. Consider GPU for better performance." fi print_info "Backend dependencies installed" echo "" # ============================================ # STEP 5: Database Initialization # ============================================ print_step "Step 5/8: Initializing database..." if [ -f "create_db.py" ]; then python3 create_db.py print_info "Database initialized" else print_warning "No create_db.py found. You may need to initialize DB manually." fi cd .. echo "" # ============================================ # STEP 6: Frontend Setup # ============================================ if [ "$INSTALL_FRONTEND" = true ]; then print_step "Step 6/8: Setting up frontend..." cd frontend print_info "Installing npm dependencies..." npm install --silent # Update API endpoint in config if [ -f "src/services/api.ts" ]; then print_info "Frontend configured" fi cd .. echo "" else print_step "Step 6/8: Skipping frontend (Node.js not available)" echo "" fi # ============================================ # STEP 7: Create Startup Scripts # ============================================ print_step "Step 7/8: Creating startup scripts..." # Backend startup script cat > start_backend.sh << 'EOF' #!/bin/bash cd ~/nullai/nullai-phi-4-14b-v2/backend source venv/bin/activate export PYTHONPATH="${PYTHONPATH}:$(pwd)/.." echo "🚀 Starting NullAI Backend API Server..." echo "📡 API: http://localhost:8000" echo "📚 Docs: http://localhost:8000/docs" echo "" uvicorn app.main:app --host 0.0.0.0 --port 8000 EOF chmod +x start_backend.sh # Frontend startup script (if applicable) if [ "$INSTALL_FRONTEND" = true ]; then cat > start_frontend.sh << 'EOF' #!/bin/bash cd ~/nullai/nullai-phi-4-14b-v2/frontend echo "🎨 Starting NullAI Frontend..." echo "🌐 Web UI: http://localhost:5173" echo "" npm run dev EOF chmod +x start_frontend.sh fi # Combined startup script if [ "$INSTALL_FRONTEND" = true ]; then cat > start_nullai.sh << 'EOF' #!/bin/bash echo "🌳 Starting NullAI Complete System..." echo "======================================" echo "" # Start backend in background cd ~/nullai/nullai-phi-4-14b-v2 ./start_backend.sh > backend.log 2>&1 & BACKEND_PID=$! echo "✓ Backend starting (PID: $BACKEND_PID)" # Wait for backend to be ready echo "⏳ Waiting for backend to be ready..." for i in {1..30}; do if curl -s http://localhost:8000/health > /dev/null 2>&1; then echo "✓ Backend ready!" break fi sleep 1 done # Start frontend echo "" echo "✓ Starting frontend..." ./start_frontend.sh EOF else cat > start_nullai.sh << 'EOF' #!/bin/bash cd ~/nullai/nullai-phi-4-14b-v2 ./start_backend.sh EOF fi chmod +x start_nullai.sh print_info "Startup scripts created" echo "" # ============================================ # STEP 8: Completion Summary # ============================================ print_step "Step 8/8: Installation complete! 🎉" echo "" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo -e "${GREEN}✅ NullAI Successfully Installed!${NC}" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "" echo "📁 Installation: $INSTALL_DIR/nullai-phi-4-14b-v2" echo "🧠 Model: phi-4-f16.gguf (27GB)" echo "💾 Database: nullai.db" echo "" echo "🚀 Quick Start:" echo "" echo " Start everything:" echo " cd $INSTALL_DIR/nullai-phi-4-14b-v2" echo " ./start_nullai.sh" echo "" echo " Or start components separately:" echo " ./start_backend.sh # API server (port 8000)" if [ "$INSTALL_FRONTEND" = true ]; then echo " ./start_frontend.sh # Web UI (port 5173)" fi echo "" echo "📡 API Endpoints:" echo " - API Server: http://localhost:8000" echo " - API Docs: http://localhost:8000/docs" echo " - Health Check: http://localhost:8000/health" if [ "$INSTALL_FRONTEND" = true ]; then echo "" echo "🌐 Web Interface:" echo " - Frontend: http://localhost:5173" fi echo "" echo "⚙️ Configuration:" echo " - Edit .env for custom settings" echo " - Default user: admin / (check docs)" echo "" echo "📚 Documentation:" echo " - README.md for full documentation" echo " - API docs at /docs endpoint" echo "" echo "💡 Next Steps:" echo " 1. Start the system: ./start_nullai.sh" echo " 2. Open http://localhost:8000/docs" echo " 3. Create your first knowledge tile" echo " 4. Explore the spatial memory system" echo "" echo "🌳 Happy knowledge organizing!" echo ""