File size: 10,143 Bytes
075a2b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/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 ""