-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrun_ollama_workbench.sh
More file actions
487 lines (411 loc) · 14.8 KB
/
run_ollama_workbench.sh
File metadata and controls
487 lines (411 loc) · 14.8 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#!/bin/bash
# Text formatting
BOLD='\033[1m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Set directory paths
LOCAL_DIR="$(dirname "$0")"
VENV_DIR="$LOCAL_DIR/venv"
LOG_DIR="$LOCAL_DIR/logs"
LOG_FILE="$LOG_DIR/setup.log"
# Create necessary directories
mkdir -p "$LOG_DIR"
# Function to log messages
log_message() {
local level=$1
local message=$2
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "[$timestamp] [$level] $message" >> "$LOG_FILE"
case $level in
"INFO") echo -e "${BLUE}$message${NC}" ;;
"SUCCESS") echo -e "${GREEN}$message${NC}" ;;
"WARNING") echo -e "${YELLOW}$message${NC}" ;;
"ERROR") echo -e "${RED}$message${NC}" ;;
esac
}
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to check if a Python package is installed
is_package_installed() {
$VENV_DIR/bin/pip show "$1" >/dev/null 2>&1
}
# Function to display the header
show_header() {
clear
echo -e "${BOLD}╔════════════════════════════════════════╗${NC}"
echo -e "${BOLD}║ Ollama Workbench Manager ║${NC}"
echo -e "${BOLD}╚════════════════════════════════════════╝${NC}"
echo
}
# Function to check system requirements
check_system_requirements() {
log_message "INFO" "Checking system requirements..."
# Check Python version
if command_exists python3; then
local python_version=$(python3 -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')
local major_version=$(echo "$python_version" | cut -d. -f1)
local minor_version=$(echo "$python_version" | cut -d. -f2)
if [ "$major_version" -ge 3 ] && [ "$minor_version" -ge 8 ]; then
log_message "SUCCESS" "Python $python_version found"
else
log_message "ERROR" "Python version must be 3.8 or higher (found $python_version)"
return 1
fi
else
log_message "ERROR" "Python 3 not found"
return 1
fi
# Check disk space (convert to integer before comparison)
local free_space=$(df -k "$HOME" | awk 'NR==2 {print int($4/1024/1024)}')
if [ "$free_space" -lt 10 ]; then
log_message "WARNING" "Low disk space: ${free_space}GB available. Recommend at least 10GB"
fi
# Check memory (convert to GB and compare as integer)
local total_mem=$(sysctl -n hw.memsize)
local total_mem_gb=$((total_mem/1024/1024/1024))
if [ "$total_mem_gb" -lt 8 ]; then
log_message "WARNING" "Low memory: ${total_mem_gb}GB RAM. Recommend at least 8GB"
fi
return 0
}
# Function to setup system dependencies
setup_system_deps() {
log_message "INFO" "Setting up system dependencies..."
# Check if Homebrew is installed
if ! command_exists brew; then
log_message "INFO" "Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
# Function to install formula if not installed
install_formula() {
local formula=$1
if ! brew list --formula | grep -q "^${formula}\$"; then
log_message "INFO" "Installing ${formula}..."
brew install --formula "$formula"
else
log_message "INFO" "${formula} is already installed"
fi
}
# Install/Update required system packages
log_message "INFO" "Installing/Updating system packages..."
install_formula "gcc"
install_formula "gfortran"
install_formula "meson"
install_formula "pkg-config"
install_formula "cmake"
install_formula "openblas"
# Link OpenBLAS
brew link --force openblas >/dev/null 2>&1
# Install git if not present
if ! command_exists git; then
log_message "INFO" "Installing git..."
install_formula "git"
fi
}
# Function to setup Python environment
setup_python_env() {
log_message "INFO" "Setting up Python environment..."
# Create virtual environment if it doesn't exist
if [ ! -d "$VENV_DIR" ]; then
log_message "INFO" "Creating virtual environment..."
python3 -m venv "$VENV_DIR"
fi
# Activate virtual environment
source "$VENV_DIR/bin/activate"
# Upgrade pip and essential tools
log_message "INFO" "Upgrading pip and essential tools..."
$VENV_DIR/bin/pip install --upgrade pip wheel setuptools build
}
# Function to install/update dependencies
install_dependencies() {
log_message "INFO" "Installing Python dependencies..."
# Install required packages from requirements.txt
$VENV_DIR/bin/pip install -r "$LOCAL_DIR/requirements.txt"
# Install Flask explicitly
if ! is_package_installed "Flask"; then
log_message "INFO" "Installing Flask..."
$VENV_DIR/bin/pip install Flask
fi
# Install Ollama explicitly
if ! is_package_installed "ollama"; then
log_message "INFO" "Installing Ollama..."
$VENV_DIR/bin/pip install ollama
fi
# Install psutil explicitly
if ! is_package_installed "psutil"; then
log_message "INFO" "Installing psutil..."
$VENV_DIR/bin/pip install psutil
fi
# Install openai explicitly
if ! is_package_installed "openai"; then
log_message "INFO" "Installing openai..."
$VENV_DIR/bin/pip install openai
fi
# Install groq explicitly
if ! is_package_installed "groq"; then
log_message "INFO" "Installing groq..."
$VENV_DIR/bin/pip install groq
fi
# Install sentence_transformers explicitly
if ! is_package_installed "sentence_transformers"; then
log_message "INFO" "Installing sentence_transformers..."
$VENV_DIR/bin/pip install sentence_transformers
fi
# Install mistralai explicitly
if ! is_package_installed "mistralai"; then
log_message "INFO" "Installing mistralai..."
$VENV_DIR/bin/pip install mistralai
fi
# Install tiktoken explicitly
if ! is_package_installed "tiktoken"; then
log_message "INFO" "Installing tiktoken..."
$VENV_DIR/bin/pip install tiktoken
fi
# Install PyPDF2 explicitly
if ! is_package_installed "PyPDF2"; then
log_message "INFO" "Installing PyPDF2..."
$VENV_DIR/bin/pip install PyPDF2
fi
# Install gtts explicitly
if ! is_package_installed "gtts"; then
log_message "INFO" "Installing gtts..."
$VENV_DIR/bin/pip install gtts
fi
# Install pygame explicitly
if ! is_package_installed "pygame"; then
log_message "INFO" "Installing pygame..."
$VENV_DIR/bin/pip install pygame
fi
# Install autogen explicitly
if ! is_package_installed "autogen"; then
log_message "INFO" "Installing autogen..."
$VENV_DIR/bin/pip install autogen
fi
# Install pdfkit explicitly
if ! is_package_installed "pdfkit"; then
log_message "INFO" "Installing pdfkit..."
$VENV_DIR/bin/pip install pdfkit
fi
# Install selenium explicitly
if ! is_package_installed "selenium"; then
log_message "INFO" "Installing selenium..."
$VENV_DIR/bin/pip install selenium
fi
# Install webdriver-manager explicitly
if ! is_package_installed "webdriver-manager"; then
log_message "INFO" "Installing webdriver-manager..."
$VENV_DIR/bin/pip install webdriver-manager
fi
# Install fake-useragent explicitly
if ! is_package_installed "fake-useragent"; then
log_message "INFO" "Installing fake-useragent..."
$VENV_DIR/bin/pip install fake-useragent
fi
# Install humanize explicitly
if ! is_package_installed "humanize"; then
log_message "INFO" "Installing humanize..."
$VENV_DIR/bin/pip install humanize
fi
# Install fpdf explicitly
if ! is_package_installed "fpdf"; then
log_message "INFO" "Installing fpdf..."
$VENV_DIR/bin/pip install fpdf
fi
# Install radon explicitly
if ! is_package_installed "radon"; then
log_message "INFO" "Installing radon..."
$VENV_DIR/bin/pip install radon
fi
# Install flake8 explicitly
if ! is_package_installed "flake8"; then
log_message "INFO" "Installing flake8..."
$VENV_DIR/bin/pip install flake8
fi
# Install langchain-community explicitly
if ! is_package_installed "langchain-community"; then
log_message "INFO" "Installing langchain-community..."
$VENV_DIR/bin/pip install langchain-community
fi
# Install duckduckgo-search explicitly
if ! is_package_installed "duckduckgo-search"; then
log_message "INFO" "Installing duckduckgo-search..."
$VENV_DIR/bin/pip install duckduckgo-search
fi
# Install googleapiclient explicitly
if ! is_package_installed "googleapiclient"; then
log_message "INFO" "Installing googleapiclient..."
$VENV_DIR/bin/pip install google-api-python-client
fi
# Install reportlab explicitly
if ! is_package_installed "reportlab"; then
log_message "INFO" "Installing reportlab..."
$VENV_DIR/bin/pip install reportlab
fi
# Install pytest explicitly
if ! is_package_installed "pytest"; then
log_message "INFO" "Installing pytest..."
$VENV_DIR/bin/pip install pytest
fi
# Install streamlit-javascript explicitly
if ! is_package_installed "streamlit-javascript"; then
log_message "INFO" "Installing streamlit-javascript..."
$VENV_DIR/bin/pip install streamlit-javascript
fi
# Install streamlit explicitly
if ! is_package_installed "streamlit"; then
log_message "INFO" "Installing streamlit..."
$VENV_DIR/bin/pip install streamlit
fi
log_message "SUCCESS" "Package installation complete"
}
# Function to check and install Ollama
setup_ollama() {
log_message "INFO" "Checking Ollama installation..."
if ! command_exists ollama; then
log_message "INFO" "Installing Ollama..."
curl -fsSL https://ollama.ai/install.sh | sh
else
log_message "SUCCESS" "Ollama is already installed"
fi
# Function to check if Ollama is running
is_ollama_running() {
lsof -i :11434 >/dev/null 2>&1
}
# Check if Ollama is running before starting it
if ! is_ollama_running; then
log_message "INFO" "Starting Ollama service..."
# Command to start the Ollama service
# (Add the actual command to start the service here)
else
log_message "INFO" "Ollama is already running"
fi
}
# Function to run health checks
run_health_checks() {
log_message "INFO" "Running health checks..."
# Check if critical services are running
if ! is_ollama_running; then
log_message "ERROR" "Ollama service is not running"
return 1
fi
# Check if critical packages are installed
local missing_packages=()
while IFS= read -r line || [ -n "$line" ]; do
# Skip comments and empty lines
[[ "$line" =~ ^#.*$ ]] && continue
[[ -z "$line" ]] && continue
# Extract package name (remove version specifier)
local package_name=$(echo "$line" | cut -d'>' -f1 | cut -d'=' -f1 | tr -d ' ')
if [ -n "$package_name" ] && ! is_package_installed "$package_name"; then
missing_packages+=("$package_name")
fi
done < "$LOCAL_DIR/requirements.txt"
if [ ${#missing_packages[@]} -ne 0 ]; then
log_message "ERROR" "Missing packages: ${missing_packages[*]}"
return 1
fi
log_message "SUCCESS" "All health checks passed"
return 0
}
# Function to run the application
run_app() {
log_message "INFO" "Launching Ollama Workbench..."
cd "$LOCAL_DIR"
# Run health checks before starting
if ! run_health_checks; then
log_message "ERROR" "Health checks failed. Please run the installation/update option first."
return 1
fi
$VENV_DIR/bin/streamlit run "$LOCAL_DIR/main.py"
}
# Function to clean installation
clean_install() {
log_message "INFO" "Cleaning installation..."
# Remove virtual environment
if [ -d "$VENV_DIR" ]; then
rm -rf "$VENV_DIR"
fi
# Remove logs
if [ -d "$LOG_DIR" ]; then
rm -rf "$LOG_DIR"
fi
log_message "SUCCESS" "Clean-up complete"
}
# Main menu function
show_menu() {
show_header
echo -e "1) ${GREEN}Install/Update${NC} - Fresh install or update existing installation"
echo -e "2) ${GREEN}Run Application${NC} - Start Ollama Workbench"
echo -e "3) ${GREEN}Health Check${NC} - Run system and dependency checks"
echo -e "4) ${GREEN}Clean Install${NC} - Remove and reinstall everything"
echo -e "5) ${GREEN}Exit${NC}"
echo -e ""
read -p "Please select an option (1-5): " choice
case $choice in
1)
show_header
log_message "INFO" "Starting installation/update process..."
if check_system_requirements; then
setup_system_deps
setup_python_env
install_dependencies
setup_ollama
python "$LOCAL_DIR/setup.py"
log_message "SUCCESS" "Installation/Update complete!"
read -p "Press Enter to return to main menu..."
show_menu
else
log_message "ERROR" "System requirements not met"
fi
;;
2)
show_header
log_message "INFO" "Starting Ollama Workbench..."
cd "$LOCAL_DIR"
$VENV_DIR/bin/streamlit run "$LOCAL_DIR/main.py"
;;
3)
show_header
if check_system_requirements && run_health_checks; then
log_message "SUCCESS" "All checks passed!"
else
log_message "WARNING" "Some checks failed. Please check the logs."
fi
read -p "Press Enter to return to main menu..."
show_menu
;;
4)
show_header
read -p "This will remove all existing installations. Are you sure? (y/N): " confirm
if [[ $confirm == [yY] ]]; then
clean_install
show_menu
else
show_menu
fi
;;
5)
log_message "INFO" "Exiting..."
exit 0
;;
*)
log_message "ERROR" "Invalid option"
sleep 2
show_menu
;;
esac
}
# Start the script
log_message "INFO" "Starting Ollama Workbench Manager..."
# Check if Python is installed
if ! command_exists python3; then
log_message "ERROR" "Python 3 is not installed. Please install Python and try again."
exit 1
fi
# Start the menu
show_menu