X / model-setup.sh
likhonsheikh's picture
Create model-setup.sh
acc05d0 verified
#!/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: $model
if pull_model “$model” 2; then
model_installed=true
log “✅ Successfully installed: $model
break
else
log “❌ Failed to install: $model
fi
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: $model
if pull_model “$model” 1; then
model_installed=true
log “✅ Successfully installed lightweight model: $model
break
else
log “❌ Failed to install lightweight model: $model
fi
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”