#!/bin/bash
# Start Qwen3-VL server

MODEL_PATH="/Users/accusys/models/Qwen3VL-8B-Instruct-Q8_0.gguf"
MMPROJ_PATH="/Users/accusys/models/mmproj-Qwen3VL-8B-Instruct-F16.gguf"
LOG_FILE="/Users/accusys/momentry_core/logs/qwen3vl_8086.log"
PID_FILE="/tmp/qwen3vl.pid"

# Kill existing process if running
if [ -f "$PID_FILE" ]; then
    OLD_PID=$(cat "$PID_FILE")
    if ps -p "$OLD_PID" > /dev/null 2>&1; then
        kill "$OLD_PID"
        sleep 2
    fi
    rm "$PID_FILE"
fi

# Start server
nohup /opt/homebrew/bin/llama-server \
    --model "$MODEL_PATH" \
    --mmproj "$MMPROJ_PATH" \
    --host 127.0.0.1 \
    --port 8086 \
    --ctx-size 8192 \
    --n-gpu-layers 99 \
    --threads 8 \
    --batch-size 512 \
    --media-path /Users/accusys/momentry/output_dev \
    > "$LOG_FILE" 2>&1 &

echo $! > "$PID_FILE"
echo "Qwen3-VL started with PID $(cat $PID_FILE)"
echo "Log file: $LOG_FILE"
echo "Health check: http://localhost:8086/health"