X
File size: 3,416 Bytes
acc05d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash

# model_setup.sh - Script to setup and manage Ollama models

set -e

# Function to log with timestamp

log() {
echo “[$(date ‘+%Y-%m-%d %H:%M:%S’)] MODEL_SETUP: $1”
}

# Function to check if Ollama is running

check_ollama() {
curl -s http://localhost:11434/api/tags > /dev/null 2>&1
}

# Function to pull a model with retry logic

pull_model() {
local model_name=$1
local max_retries=${2:-3}
local retry_count=0

```
log "Attempting to pull model: $model_name"

while [ $retry_count -lt $max_retries ]; do
    if ollama pull "$model_name"; then
        log "Successfully pulled model: $model_name"
        return 0
    else
        retry_count=$((retry_count + 1))
        log "Failed to pull $model_name (attempt $retry_count/$max_retries)"
        if [ $retry_count -lt $max_retries ]; then
            log "Retrying in 10 seconds..."
            sleep 10
        fi
    fi
done

log "ERROR: Failed to pull model $model_name after $max_retries attempts"
return 1
```

}

# Function to list available models

list_models() {
log “Available models:”
ollama list 2>/dev/null || log “No models installed yet”
}

# Wait for Ollama to be ready

log “Waiting for Ollama service…”
timeout=60
elapsed=0
while ! check_ollama && [ $elapsed -lt $timeout ]; do
sleep 2
elapsed=$((elapsed + 2))
done

if ! check_ollama; then
log “ERROR: Ollama service is not ready after ${timeout}s”
exit 1
fi

log “Ollama service is ready!”

# Define models to install (in order of preference)

MODELS_TO_INSTALL=(
“llama3.1:8b”      # General purpose, good balance
“codellama:7b”     # Coding specialized
“mistral:7b”       # Fast and efficient
“qwen2.5:7b”       # Alternative general model
)

# Alternative lightweight models if the above fail

LIGHTWEIGHT_MODELS=(
“llama3.1:7b”
“mistral:7b-instruct”
“codellama:7b-instruct”
“phi3:3.8b”
)

log “Starting model installation process…”

# Try to install at least one model from the primary list

model_installed=false
for model in${MODELS_TO_INSTALL[@]}”; do
log “Trying to install: $modelif pull_model “$model” 2; then
model_installed=true
log “✅ Successfully installed: $modelbreak
else
log “❌ Failed to install: $modelfi
done

# If no primary model was installed, try lightweight alternatives

if [ “$model_installed” = false ]; then
log “Primary models failed, trying lightweight alternatives…”
for model in${LIGHTWEIGHT_MODELS[@]}”; do
log “Trying lightweight model: $modelif pull_model “$model” 1; then
model_installed=true
log “✅ Successfully installed lightweight model: $modelbreak
else
log “❌ Failed to install lightweight model: $modelfi
done
fi

# Final check

if [ “$model_installed” = false ]; then
log “⚠️ WARNING: No models were successfully installed!”
log “The application will start but won’t be able to generate responses until a model is manually installed.”
else
log “✅ Model setup completed successfully!”
fi

# List all installed models

log “Final model inventory:”
list_models

# Create a status file for the main app to check

if [ “$model_installed” = true ]; then
echo “success” > /app/model_setup_status
log “Created success status file”
else
echo “failed” > /app/model_setup_status
log “Created failed status file”
fi

log “Model setup script completed”