~kris/dots

srice

ref: 65bd807cfdd6651a480f8fc70ed41ef82abaf64b srice/bin/wisdom -rwxr-xr-x 9.5 KiB
65bd807c — Kris Yotam mksh: skip stale history log on first prompt render 2 months ago
                                                                                
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
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env bash
#===============================================================================
#
#   WISDOM - AI Services Management Script
#
#===============================================================================
#
#   DESCRIPTION:
#       This script manages the AI backend services used by the "wisdom" system.
#       It provides commands to start, stop, restart, and check the status of:
#
#       1. OLLAMA SERVICE (Port 11434)
#          - Local LLM inference server
#          - Runs models like deepseek-r1:14b, qwen2.5:14b
#          - Managed via systemd (ollama.service)
#          - API endpoint: http://localhost:11434
#
#       2. FASTER-WHISPER SERVER (Port 8000)
#          - Speech-to-text transcription server
#          - Uses OpenAI Whisper models with CTranslate2 optimization
#          - Runs as a uvicorn ASGI server
#          - Location: /root/faster-whisper-server
#          - API endpoint: http://localhost:8000
#
#   USAGE:
#       wisdom start    | -l   - Start/launch all AI services
#       wisdom stop     | -c   - Stop/close all AI services (frees RAM)
#       wisdom restart  | -r   - Restart all AI services
#       wisdom status   | -s   - Show status of all services
#       wisdom help     | -h   - Show this help message
#
#   REQUIREMENTS:
#       - sudo access (for whisper server management)
#       - systemd (for ollama service)
#       - /root/faster-whisper-server directory with venv
#
#   PORTS USED:
#       - 11434: Ollama API (localhost only)
#       - 8000:  Faster-Whisper API
#
#   MEMORY USAGE:
#       - Ollama: ~200MB idle, up to 8GB+ when running large models
#       - Whisper: ~500MB-2GB depending on model loaded
#
#   AUTHOR: Generated for krisyotam's wisdom system
#   DATE:   2026-01-19
#
#===============================================================================

set -e  # Exit on error

#-------------------------------------------------------------------------------
# CONFIGURATION
#-------------------------------------------------------------------------------

# Ollama settings
OLLAMA_SERVICE="ollama.service"
OLLAMA_PORT=11434
OLLAMA_HOST="127.0.0.1"

# Faster-Whisper settings
WHISPER_DIR="/root/faster-whisper-server"
WHISPER_VENV="$WHISPER_DIR/.venv"
WHISPER_PORT=8000

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

#-------------------------------------------------------------------------------
# HELPER FUNCTIONS
#-------------------------------------------------------------------------------

# Print colored status messages
print_status() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[OK]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Check if a service is running on a specific port
check_port() {
    local port=$1
    ss -tlnp 2>/dev/null | grep -q ":$port " && return 0 || return 1
}

# Get PID of process on port
get_pid_on_port() {
    local port=$1
    ss -tlnp 2>/dev/null | grep ":$port " | grep -oP 'pid=\K\d+' | head -1
}

#-------------------------------------------------------------------------------
# SERVICE MANAGEMENT FUNCTIONS
#-------------------------------------------------------------------------------

#
# START OLLAMA
# Uses systemd to start the ollama service
#
start_ollama() {
    print_status "Starting Ollama service..."

    if systemctl is-active --quiet "$OLLAMA_SERVICE"; then
        print_warning "Ollama is already running"
    else
        sudo systemctl start "$OLLAMA_SERVICE"
        sleep 2

        if systemctl is-active --quiet "$OLLAMA_SERVICE"; then
            print_success "Ollama started successfully on port $OLLAMA_PORT"
        else
            print_error "Failed to start Ollama"
            return 1
        fi
    fi
}

#
# STOP OLLAMA
# Uses systemd to stop the ollama service
#
stop_ollama() {
    print_status "Stopping Ollama service..."

    if ! systemctl is-active --quiet "$OLLAMA_SERVICE"; then
        print_warning "Ollama is not running"
    else
        sudo systemctl stop "$OLLAMA_SERVICE"
        print_success "Ollama stopped"
    fi
}

#
# START FASTER-WHISPER SERVER
# Starts the uvicorn server in the background
# Requires sudo since it runs from /root
#
start_whisper() {
    print_status "Starting Faster-Whisper server..."

    # Check if already running
    if check_port $WHISPER_PORT; then
        print_warning "Whisper server is already running on port $WHISPER_PORT"
        return 0
    fi

    # Check if the directory exists
    if [ ! -d "$WHISPER_DIR" ]; then
        print_error "Whisper server directory not found: $WHISPER_DIR"
        return 1
    fi

    # Start the server using sudo
    # The server runs in the background with nohup
    sudo bash -c "
        cd $WHISPER_DIR && \
        source $WHISPER_VENV/bin/activate && \
        nohup uv run uvicorn --factory faster_whisper_server.main:create_app \
            --host 0.0.0.0 \
            --port $WHISPER_PORT \
            > /var/log/whisper-server.log 2>&1 &
    "

    # Wait for server to start
    sleep 3

    if check_port $WHISPER_PORT; then
        print_success "Whisper server started on port $WHISPER_PORT"
    else
        print_error "Failed to start Whisper server"
        print_status "Check logs: sudo cat /var/log/whisper-server.log"
        return 1
    fi
}

#
# STOP FASTER-WHISPER SERVER
# Finds and kills the uvicorn process
#
stop_whisper() {
    print_status "Stopping Faster-Whisper server..."

    # Find processes related to faster-whisper-server
    local pids=$(pgrep -f "faster_whisper_server" 2>/dev/null || true)

    if [ -z "$pids" ]; then
        print_warning "Whisper server is not running"
        return 0
    fi

    # Kill the processes
    for pid in $pids; do
        sudo kill "$pid" 2>/dev/null || true
    done

    sleep 1

    # Force kill if still running
    pids=$(pgrep -f "faster_whisper_server" 2>/dev/null || true)
    if [ -n "$pids" ]; then
        for pid in $pids; do
            sudo kill -9 "$pid" 2>/dev/null || true
        done
    fi

    print_success "Whisper server stopped"
}

#
# SHOW STATUS OF ALL SERVICES
#
show_status() {
    echo ""
    echo "========================================"
    echo "       WISDOM AI SERVICES STATUS        "
    echo "========================================"
    echo ""

    # Ollama status
    echo -e "${BLUE}OLLAMA SERVICE${NC}"
    echo "  Port: $OLLAMA_PORT"
    if systemctl is-active --quiet "$OLLAMA_SERVICE"; then
        echo -e "  Status: ${GREEN}RUNNING${NC}"
        # Show memory usage
        local ollama_mem=$(systemctl show "$OLLAMA_SERVICE" --property=MemoryCurrent 2>/dev/null | cut -d= -f2)
        if [ -n "$ollama_mem" ] && [ "$ollama_mem" != "[not set]" ]; then
            echo "  Memory: $(numfmt --to=iec $ollama_mem 2>/dev/null || echo $ollama_mem)"
        fi
        # Show loaded models
        echo "  Models:"
        curl -s "http://$OLLAMA_HOST:$OLLAMA_PORT/api/tags" 2>/dev/null | \
            grep -oP '"name":"[^"]+' | cut -d'"' -f4 | while read model; do
                echo "    - $model"
            done
    else
        echo -e "  Status: ${RED}STOPPED${NC}"
    fi
    echo ""

    # Whisper status
    echo -e "${BLUE}FASTER-WHISPER SERVER${NC}"
    echo "  Port: $WHISPER_PORT"
    if check_port $WHISPER_PORT; then
        echo -e "  Status: ${GREEN}RUNNING${NC}"
        local pid=$(get_pid_on_port $WHISPER_PORT)
        if [ -n "$pid" ]; then
            local mem=$(ps -o rss= -p $pid 2>/dev/null | awk '{print $1*1024}')
            echo "  Memory: $(numfmt --to=iec $mem 2>/dev/null || echo "unknown")"
        fi
    else
        echo -e "  Status: ${RED}STOPPED${NC}"
    fi
    echo ""
    echo "========================================"
}

#
# SHOW HELP MESSAGE
#
show_help() {
    echo ""
    echo "WISDOM - AI Services Management"
    echo ""
    echo "Usage: wisdom <command>"
    echo ""
    echo "Commands:"
    echo "  start,   -l    Start/launch all AI services (Ollama + Whisper)"
    echo "  stop,    -c    Stop/close all AI services (frees RAM)"
    echo "  restart, -r    Restart all AI services"
    echo "  status,  -s    Show status of all services"
    echo "  help,    -h    Show this help message"
    echo ""
    echo "Services managed:"
    echo "  - Ollama (LLM inference) - Port 11434"
    echo "  - Faster-Whisper (Speech-to-text) - Port 8000"
    echo ""
}

#-------------------------------------------------------------------------------
# MAIN COMMAND HANDLER
#-------------------------------------------------------------------------------

case "${1:-help}" in
    start|-l|--launch)
        echo ""
        print_status "Starting WISDOM AI services..."
        echo ""
        start_ollama
        start_whisper
        echo ""
        print_success "All services started!"
        show_status
        ;;

    stop|-c|--close)
        echo ""
        print_status "Stopping WISDOM AI services..."
        echo ""
        stop_whisper
        stop_ollama
        echo ""
        print_success "All services stopped. RAM freed."
        ;;

    restart|-r|--restart)
        echo ""
        print_status "Restarting WISDOM AI services..."
        echo ""
        stop_whisper
        stop_ollama
        sleep 2
        start_ollama
        start_whisper
        echo ""
        print_success "All services restarted!"
        show_status
        ;;

    status|-s|--status)
        show_status
        ;;

    help|--help|-h)
        show_help
        ;;

    *)
        print_error "Unknown command: $1"
        show_help
        exit 1
        ;;
esac