File size: 3,197 Bytes
825f021
 
2f48b4a
 
825f021
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f48b4a
 
 
 
 
 
825f021
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b177c43
 
825f021
 
 
 
2f48b4a
825f021
 
 
 
 
 
 
 
 
 
 
 
 
 
2f48b4a
825f021
 
 
 
 
 
 
 
 
 
2f48b4a
825f021
 
 
 
 
2f48b4a
825f021
 
 
 
2f48b4a
825f021
 
 
b177c43
2f48b4a
 
 
 
 
 
 
 
825f021
2f48b4a
 
 
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
#!/bin/bash

set -e  # Exit on error

# Default values
PORT=7860
ENV_FILE=".env"
CONTAINER_NAME="llm-inference-server"
BUILD_ONLY=false
PLATFORM=""

# Function to display usage
usage() {
    echo "Usage: $0 [-p <port>] [-e <env_file>] [-n <container_name>] [-b] [-h]"
    echo "  -p : Port to expose (default: 7860)"
    echo "  -e : Environment file path (default: .env)"
    echo "  -n : Container name (default: llm-inference-server)"
    echo "  -b : Build only (don't run container)"
    echo "  -h : Display this help message"
    exit 1
}

# Function to handle errors
handle_error() {
    echo "Error: $1"
    exit 1
}

# Parse command line arguments
while getopts "p:e:n:bh" opt; do
    case $opt in
        p) PORT=$OPTARG ;;
        e) ENV_FILE=$OPTARG ;;
        n) CONTAINER_NAME=$OPTARG ;;
        b) BUILD_ONLY=true ;;
        h) usage ;;
        ?) usage ;;
    esac
done

# Detect platform and set appropriate options
if [[ "$(uname -s)" == "Darwin" ]]; then
    echo "Detected MacOS platform"
    PLATFORM="linux/arm64"
    if [[ "$(uname -m)" == "x86_64" ]]; then
        PLATFORM="linux/amd64"
    fi
    PLATFORM_ARG="--platform=${PLATFORM}"
else
    echo "Detected Linux platform"
    # Check if podman is available (common in RHEL environments)
    if command -v podman &> /dev/null; then
        echo "Podman detected, using podman instead of docker"
        function docker { podman "${@}"; }
    fi
    PLATFORM_ARG=""
fi

# Check if docker is installed
if ! command -v docker &> /dev/null; then
    handle_error "Docker is not installed"
fi

# Check if .env file exists
if [ ! -f "$ENV_FILE" ]; then
    echo "Warning: $ENV_FILE file not found"
    echo "Creating sample $ENV_FILE file..."
    cat > "$ENV_FILE" << EOL
InfAPITokenWrite=your_huggingface_token_here
EOL
    echo "Please edit $ENV_FILE with your actual configuration"
fi

# Build the Docker image
echo "Building Docker image..."
docker build $PLATFORM_ARG -t $CONTAINER_NAME . || handle_error "Failed to build Docker image"

# Exit if build only
if [ "$BUILD_ONLY" = true ]; then
    echo "Build completed. Exiting as requested."
    exit 0
fi

# Check if container is already running
if docker ps -q -f name=$CONTAINER_NAME | grep -q .; then
    echo "Stopping existing container..."
    docker stop $CONTAINER_NAME || handle_error "Failed to stop existing container"
fi

# Remove existing container
if docker ps -aq -f name=$CONTAINER_NAME | grep -q .; then
    echo "Removing existing container..."
    docker rm $CONTAINER_NAME || handle_error "Failed to remove existing container"
fi

# Run the container
echo "Starting container on port $PORT..."
if ! docker run -d \
    --name $CONTAINER_NAME \
    --env-file $ENV_FILE \
    -p $PORT:7860 \
    -v $HOME/.cache/huggingface:/app/.cache/huggingface \
    $CONTAINER_NAME; then
    handle_error "Failed to start container"
fi

# Verify container is running
if ! docker ps | grep -q $CONTAINER_NAME; then
    handle_error "Container failed to start. Check logs with: docker logs $CONTAINER_NAME"
fi

echo "Container started successfully!"
echo "API should be available at http://localhost:$PORT"
echo "To view logs, run: docker logs -f $CONTAINER_NAME"