TeamGenKI/Inference-API
0
1#!/bin/bash2 3set -e # Exit on error4 5# Default values6CONFIG_FILE="main/resources/local_config.yaml"7ENV_FILE=".env"8CONTAINER_NAME="llm-inference-server"9BUILD_ONLY=false10PLATFORM=""11 12# Function to display usage13usage() {14 echo "Usage: $0 [-c <config_file>] [-e <env_file>] [-n <container_name>] [-b] [-h]"15 echo " -c : Config file path (default: resources/local_config.yaml)"16 echo " -e : Environment file path (default: .env)"17 echo " -n : Container name (default: llm-inference-server)"18 echo " -b : Build only (don't run container)"19 echo " -h : Display this help message"20 exit 121}22 23# Function to handle errors24handle_error() {25 echo "Error: $1"26 exit 127}28 29# Function to read yaml using grep and sed30parse_yaml() {31 local file=$132 # Get port - look for port under server section and extract the number33 PORT=$(grep -A5 "^server:" "$file" | grep "port:" | sed 's/[^0-9]*//g')34 # Get host - look for host under server section and extract the value in quotes35 HOST=$(grep -A5 "^server:" "$file" | grep "host:" | sed 's/.*"//;s/".*//')36 echo "$PORT $HOST"37}38 39# Parse command line arguments40while getopts "c:e:n:bh" opt; do41 case $opt in42 c) CONFIG_FILE=$OPTARG ;;43 e) ENV_FILE=$OPTARG ;;44 n) CONTAINER_NAME=$OPTARG ;;45 b) BUILD_ONLY=true ;;46 h) usage ;;47 ?) usage ;;48 esac49done50 51# Check if config file exists52if [ ! -f "$CONFIG_FILE" ]; then53 handle_error "Config file not found: $CONFIG_FILE"54fi55 56# Read port and host from config57read PORT HOST < <(parse_yaml "$CONFIG_FILE")58echo "Using configuration - Port: $PORT, Host: $HOST"59 60# Detect platform and set appropriate options61if [[ "$(uname -s)" == "Darwin" ]]; then62 echo "Detected MacOS platform"63 PLATFORM="linux/arm64"64 if [[ "$(uname -m)" == "x86_64" ]]; then65 PLATFORM="linux/amd64"66 fi67 PLATFORM_ARG="--platform=${PLATFORM}"68else69 echo "Detected Linux platform"70 # Check if podman is available (common in RHEL environments)71 if command -v podman &> /dev/null; then72 echo "Podman detected, using podman instead of docker"73 function docker { podman "${@}"; }74 fi75 PLATFORM_ARG=""76fi77 78# Check if docker is installed79if ! command -v docker &> /dev/null; then80 handle_error "Docker is not installed"81fi82 83# Check if .env file exists84if [ ! -f "$ENV_FILE" ]; then85 echo "Warning: $ENV_FILE file not found"86 echo "Creating sample $ENV_FILE file..."87 cat > "$ENV_FILE" << EOL88InfAPITokenWrite=your_huggingface_token_here89EOL90 echo "Please edit $ENV_FILE with your actual configuration"91fi92 93# Build the Docker image94echo "Building Docker image..."95docker build $PLATFORM_ARG -t $CONTAINER_NAME . || handle_error "Failed to build Docker image"96 97# Exit if build only98if [ "$BUILD_ONLY" = true ]; then99 echo "Build completed. Exiting as requested."100 exit 0101fi102 103# Check if container is already running104if docker ps -q -f name=$CONTAINER_NAME | grep -q .; then105 echo "Stopping existing container..."106 docker stop $CONTAINER_NAME || handle_error "Failed to stop existing container"107fi108 109# Remove existing container110if docker ps -aq -f name=$CONTAINER_NAME | grep -q .; then111 echo "Removing existing container..."112 docker rm $CONTAINER_NAME || handle_error "Failed to remove existing container"113fi114 115# Run the container in interactive mode116echo "Starting container on port $PORT..."117echo "Press Ctrl+C to stop the server"118docker run --rm -it \119 --name $CONTAINER_NAME \120 --env-file $ENV_FILE \121 -p $PORT:$PORT \122 -v $HOME/.cache/huggingface:/app/.cache/huggingface \123 -v "$(pwd)/$CONFIG_FILE:/app/$CONFIG_FILE" \124 $CONTAINER_NAME125 126echo "Container stopped"