Files
bdi_podman_serverconf/containers/whisper/build-container.sh
T
2026-07-04 14:41:42 +02:00

201 baris
5.6 KiB
Bash
Executable File

#!/bin/bash
# Build whisper.cpp with Vulkan support and create container image
# This script compiles whisper.cpp locally and creates a Podman image
# Usage: ./build-container.sh [--no-cache]
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TMP_BUILD_DIR="${SCRIPT_DIR}/.tmp"
BUILD_DIR="${TMP_BUILD_DIR}/whisper"
BIN_DIR="${TMP_BUILD_DIR}/bin-vulkan"
MODELS_DIR="${TMP_BUILD_DIR}/models"
HOME_DIR="${HOME}"
NO_CACHE=""
EXIT_CODE=0
# Cleanup function - always runs on exit
cleanup() {
local exit_code=$?
if [ $exit_code -ne 0 ]; then
echo
echo "⚠ Build failed (exit code: $exit_code). Cleaning up temporary files..."
fi
rm -rf "$TMP_BUILD_DIR" "${SCRIPT_DIR}/bin-vulkan" "${SCRIPT_DIR}/models" 2>/dev/null || true
if [ $exit_code -eq 0 ]; then
echo "✓ Cleanup complete"
fi
return $exit_code
}
# Set trap to cleanup on exit (success or failure)
trap cleanup EXIT
# Parse arguments
if [[ "$1" == "--no-cache" ]]; then
NO_CACHE="--no-cache"
echo "Build mode: NO CACHE (clean rebuild)"
fi
echo "=== Whisper.cpp Vulkan Build ==="
echo "Temporary build directory: $TMP_BUILD_DIR"
echo "Output directory: $BIN_DIR"
echo "Home directory: $HOME_DIR"
if [ -n "$NO_CACHE" ]; then
echo "Cache mode: DISABLED"
fi
echo
# Create temporary build directory
mkdir -p "$TMP_BUILD_DIR"
echo
# Install dependencies
echo "[1/6] Installing dependencies..."
REQUIRED_PACKAGES="build-essential cmake git libvulkan-dev vulkan-tools glslc spirv-headers spirv-tools python3"
MISSING_PACKAGES=""
for pkg in $REQUIRED_PACKAGES; do
if ! dpkg -l | grep -q "^ii $pkg"; then
MISSING_PACKAGES="$MISSING_PACKAGES $pkg"
fi
done
if [ -n "$MISSING_PACKAGES" ]; then
echo " Installing missing packages:$MISSING_PACKAGES"
sudo apt-get update
sudo apt-get install -y $MISSING_PACKAGES
echo "✓ Dependencies installed"
else
echo "✓ All dependencies already installed"
fi
# Verify dependencies
echo " Verifying dependencies..."
for cmd in git cmake make gcc g++; do
if ! command -v $cmd &> /dev/null; then
echo "ERROR: $cmd is still not available after installation."
exit 1
fi
done
# Check Vulkan
if ! pkg-config --exists vulkan; then
echo "ERROR: Vulkan development files not found."
exit 1
fi
if ! command -v glslc &> /dev/null; then
echo "ERROR: glslc (GLSL compiler) not found. Install with: sudo apt install glslc"
exit 1
fi
echo "✓ All dependencies verified"
echo
# Clone or update whisper.cpp
echo "[2/6] Cloning/updating whisper.cpp repository..."
if [ -d "$BUILD_DIR" ]; then
echo " Updating existing repository..."
cd "$BUILD_DIR"
git fetch origin
git checkout master
git pull origin master
else
echo " Cloning whisper.cpp..."
git clone https://github.com/ggml-org/whisper.cpp.git "$BUILD_DIR"
cd "$BUILD_DIR"
fi
echo "✓ Repository ready"
echo
# Build with Vulkan support
echo "[3/6] Building with Vulkan support (this may take a few minutes)..."
mkdir -p build
cd build
# Clean if --no-cache is specified
if [ -n "$NO_CACHE" ]; then
echo " Cleaning previous build..."
rm -rf * .cmake
fi
cmake .. -DGGML_VULKAN=1 -DWHISPER_COMMON_FFMPEG=yes -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF
cmake --build . -j $(nproc) --config Release
echo "✓ Build complete"
echo
# Prepare binary directory
echo "[4/6] Preparing binary directory..."
mkdir -p "$BIN_DIR"
cd "$BUILD_DIR"
# Copy binaries
echo " Copying binaries..."
cp build/bin/whisper-cli "$BIN_DIR/"
cp build/bin/whisper-server "$BIN_DIR/"
cp build/bin/whisper-bench "$BIN_DIR/"
cp build/bin/whisper-quantize "$BIN_DIR/"
chmod +x "$BIN_DIR"/*
# Copy model download script
mkdir -p "$MODELS_DIR"
cp models/download-ggml-model.sh "$MODELS_DIR/"
chmod +x "$MODELS_DIR/download-ggml-model.sh"
# Download default model if not present
if [ ! -f "$MODELS_DIR/ggml-base.en.bin" ]; then
echo " Downloading base.en model (~140MB)..."
cd "$MODELS_DIR"
./download-ggml-model.sh base.en -o . >/dev/null 2>&1 || {
echo " ⚠ Model download failed, you can download it manually later"
echo " Command: $MODELS_DIR/download-ggml-model.sh base.en -o $MODELS_DIR"
}
else
echo " Model already present: ggml-base.en.bin"
fi
echo "✓ Binaries ready"
echo
# Build Podman image
echo "[5/6] Building Podman image..."
# Copy Containerfile and entrypoint to .tmp for build
echo " Preparing build context..."
cp "${SCRIPT_DIR}/whisper-vulkan.Containerfile" "${TMP_BUILD_DIR}/"
cp "${SCRIPT_DIR}/entrypoint.sh" "${TMP_BUILD_DIR}/"
# Build from within .tmp - everything stays isolated
cd "$TMP_BUILD_DIR"
podman build $NO_CACHE -t whisper:vulkan-amd64 -f whisper-vulkan.Containerfile .
echo "✓ Podman image built"
echo
# Save image to home directory
echo "[6/6] Saving image to $HOME_DIR..."
IMAGE_FILE="$HOME_DIR/whisper-vulkan-amd64.tar"
podman save -o "$IMAGE_FILE" localhost/whisper:vulkan-amd64
IMAGE_SIZE=$(du -h "$IMAGE_FILE" | cut -f1)
echo "✓ Image saved: $IMAGE_FILE ($IMAGE_SIZE)"
echo
echo "=== BUILD COMPLETE ==="
echo
echo "✓ Container image created: whisper:vulkan-amd64"
echo "✓ Image saved: $IMAGE_FILE"
echo
echo "Next steps:"
echo " 1. Load the image locally:"
echo " podman load -i $IMAGE_FILE"
echo
echo " 2. Install as systemd service:"
echo " podman container runlabel install -n whisper whisper.container localhost/whisper:vulkan-amd64"
echo " systemctl --user daemon-reload"
echo " systemctl --user enable whisper"
echo " systemctl --user start whisper"
echo
echo " 3. Or run directly:"
echo " podman run --device /dev/dri/renderD128 -p 8080:8080 -v /path/to/models:/app/models whisper:vulkan-amd64"
echo